diff options
author | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/folder/dirut_ut.cpp |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/folder/dirut_ut.cpp')
-rw-r--r-- | util/folder/dirut_ut.cpp | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/util/folder/dirut_ut.cpp b/util/folder/dirut_ut.cpp new file mode 100644 index 00000000000..45ebfc842ca --- /dev/null +++ b/util/folder/dirut_ut.cpp @@ -0,0 +1,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); + } +}; |