aboutsummaryrefslogtreecommitdiffstats
path: root/util/folder/dirut_ut.cpp
blob: 9afcf0362133de7c8b5262212d164eeb2df0569b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "dirut.h"
#include "tempdir.h"

#include <library/cpp/testing/unittest/registar.h>

#include <util/generic/string.h> 
#include <util/memory/tempbuf.h>
#include <util/stream/file.h>
#include <util/system/platform.h>

Y_UNIT_TEST_SUITE(TDirutTest) {
    Y_UNIT_TEST(TestRealPath) {
        UNIT_ASSERT(IsDir(RealPath(".")));
    }

    Y_UNIT_TEST(TestRealLocation) {
        UNIT_ASSERT(IsDir(RealLocation(".")));

        TTempDir tempDir;
        TString base = RealPath(tempDir());
        UNIT_ASSERT(!base.empty());

        if (base.back() == GetDirectorySeparator()) {
            base.pop_back();
        }

        TString path;
        TString pathNotNorm;

        path = base + GetDirectorySeparatorS() + "no_such_file";
        UNIT_ASSERT(NFs::Exists(GetDirName(path)));
        UNIT_ASSERT(!NFs::Exists(path));
        path = RealLocation(path);
        UNIT_ASSERT(NFs::Exists(GetDirName(path)));
        UNIT_ASSERT(!NFs::Exists(path));
        UNIT_ASSERT_EQUAL(GetDirName(path), base);

        pathNotNorm = base + GetDirectorySeparatorS() + "some_dir" + GetDirectorySeparatorS() + ".." + GetDirectorySeparatorS() + "no_such_file";
        MakeDirIfNotExist((base + GetDirectorySeparatorS() + "some_dir").data());
        pathNotNorm = RealLocation(pathNotNorm);
        UNIT_ASSERT(NFs::Exists(GetDirName(pathNotNorm)));
        UNIT_ASSERT(!NFs::Exists(pathNotNorm));
        UNIT_ASSERT_EQUAL(GetDirName(pathNotNorm), base);

        UNIT_ASSERT_EQUAL(path, pathNotNorm);

        path = base + GetDirectorySeparatorS() + "file";
        {
            TFixedBufferFileOutput file(path);
        }
        UNIT_ASSERT(NFs::Exists(GetDirName(path)));
        UNIT_ASSERT(NFs::Exists(path));
        UNIT_ASSERT(NFs::Exists(path));
        path = RealLocation(path);
        UNIT_ASSERT(NFs::Exists(GetDirName(path)));
        UNIT_ASSERT(NFs::Exists(path));
        UNIT_ASSERT_EQUAL(GetDirName(path), base);
    }

    void DoTest(const char* p, const char* base, const char* canon) {
        TString path(p);
        UNIT_ASSERT(resolvepath(path, base));
        UNIT_ASSERT(path == canon);
    }

    Y_UNIT_TEST(TestResolvePath) {
#ifdef _win_
        DoTest("bar", "c:\\foo\\baz", "c:\\foo\\baz\\bar");
        DoTest("c:\\foo\\bar", "c:\\bar\\baz", "c:\\foo\\bar");
#else
        DoTest("bar", "/foo/baz", "/foo/bar");
        DoTest("/foo/bar", "/bar/baz", "/foo/bar");

    #ifdef NDEBUG
        DoTest("bar", "./baz", "./bar");
        #if 0 // should we support, for consistency, single-label dirs
        DoTest("bar", "baz", "bar");
        #endif
    #endif
#endif
    }

    Y_UNIT_TEST(TestResolvePathRelative) {
        TTempDir tempDir;
        TTempBuf tempBuf;
        TString base = RealPath(tempDir());
        if (base.back() == GetDirectorySeparator()) {
            base.pop_back();
        }

        // File
        TString path = base + GetDirectorySeparatorS() + "file";
        {
            TFixedBufferFileOutput file(path);
        }
        ResolvePath("file", base.data(), tempBuf.Data(), false);
        UNIT_ASSERT_EQUAL(tempBuf.Data(), path);

        // Dir
        path = base + GetDirectorySeparatorS() + "dir";
        MakeDirIfNotExist(path.data());
        ResolvePath("dir", base.data(), tempBuf.Data(), true);
        UNIT_ASSERT_EQUAL(tempBuf.Data(), path + GetDirectorySeparatorS());

        // Absent file in existent dir
        path = base + GetDirectorySeparatorS() + "nofile";
        ResolvePath("nofile", base.data(), tempBuf.Data(), false);
        UNIT_ASSERT_EQUAL(tempBuf.Data(), path);
    }

    Y_UNIT_TEST(TestGetDirName) {
        UNIT_ASSERT_VALUES_EQUAL(".", GetDirName("parambambam"));
    }

    Y_UNIT_TEST(TestStripFileComponent) {
        static const TString tmpDir = "tmp_dir_for_tests";
        static const TString tmpSubDir = tmpDir + GetDirectorySeparatorS() + "subdir";
        static const TString tmpFile = tmpDir + GetDirectorySeparatorS() + "file";

        // creating tmp dir and subdirs
        MakeDirIfNotExist(tmpDir.data());
        MakeDirIfNotExist(tmpSubDir.data());
        {
            TFixedBufferFileOutput file(tmpFile);
        }

        UNIT_ASSERT_EQUAL(StripFileComponent(tmpDir), tmpDir + GetDirectorySeparatorS());
        UNIT_ASSERT_EQUAL(StripFileComponent(tmpSubDir), tmpSubDir + GetDirectorySeparatorS());
        UNIT_ASSERT_EQUAL(StripFileComponent(tmpFile), tmpDir + GetDirectorySeparatorS());

        RemoveDirWithContents(tmpDir);
    }
};