dirut_ut.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "dirut.h"
  2. #include "tempdir.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/generic/string.h>
  5. #include <util/memory/tempbuf.h>
  6. #include <util/stream/file.h>
  7. Y_UNIT_TEST_SUITE(TDirutTest) {
  8. Y_UNIT_TEST(TestRealPath) {
  9. UNIT_ASSERT(IsDir(RealPath(".")));
  10. }
  11. Y_UNIT_TEST(TestRealLocation) {
  12. UNIT_ASSERT(IsDir(RealLocation(".")));
  13. TTempDir tempDir;
  14. TString base = RealPath(tempDir());
  15. UNIT_ASSERT(!base.empty());
  16. if (base.back() == GetDirectorySeparator()) {
  17. base.pop_back();
  18. }
  19. TString path;
  20. TString pathNotNorm;
  21. path = base + GetDirectorySeparatorS() + "no_such_file";
  22. UNIT_ASSERT(NFs::Exists(GetDirName(path)));
  23. UNIT_ASSERT(!NFs::Exists(path));
  24. path = RealLocation(path);
  25. UNIT_ASSERT(NFs::Exists(GetDirName(path)));
  26. UNIT_ASSERT(!NFs::Exists(path));
  27. UNIT_ASSERT_EQUAL(GetDirName(path), base);
  28. pathNotNorm = base + GetDirectorySeparatorS() + "some_dir" + GetDirectorySeparatorS() + ".." + GetDirectorySeparatorS() + "no_such_file";
  29. MakeDirIfNotExist((base + GetDirectorySeparatorS() + "some_dir").data());
  30. pathNotNorm = RealLocation(pathNotNorm);
  31. UNIT_ASSERT(NFs::Exists(GetDirName(pathNotNorm)));
  32. UNIT_ASSERT(!NFs::Exists(pathNotNorm));
  33. UNIT_ASSERT_EQUAL(GetDirName(pathNotNorm), base);
  34. UNIT_ASSERT_EQUAL(path, pathNotNorm);
  35. path = base + GetDirectorySeparatorS() + "file";
  36. {
  37. TFixedBufferFileOutput file(path);
  38. }
  39. UNIT_ASSERT(NFs::Exists(GetDirName(path)));
  40. UNIT_ASSERT(NFs::Exists(path));
  41. UNIT_ASSERT(NFs::Exists(path));
  42. path = RealLocation(path);
  43. UNIT_ASSERT(NFs::Exists(GetDirName(path)));
  44. UNIT_ASSERT(NFs::Exists(path));
  45. UNIT_ASSERT_EQUAL(GetDirName(path), base);
  46. }
  47. void DoTest(const char* p, const char* base, const char* canon) {
  48. TString path(p);
  49. UNIT_ASSERT(resolvepath(path, base));
  50. UNIT_ASSERT(path == canon);
  51. }
  52. Y_UNIT_TEST(TestResolvePath) {
  53. #ifdef _win_
  54. DoTest("bar", "c:\\foo\\baz", "c:\\foo\\baz\\bar");
  55. DoTest("c:\\foo\\bar", "c:\\bar\\baz", "c:\\foo\\bar");
  56. #else
  57. DoTest("bar", "/foo/baz", "/foo/bar");
  58. DoTest("/foo/bar", "/bar/baz", "/foo/bar");
  59. #ifdef NDEBUG
  60. DoTest("bar", "./baz", "./bar");
  61. #if 0 // should we support, for consistency, single-label dirs
  62. DoTest("bar", "baz", "bar");
  63. #endif
  64. #endif
  65. #endif
  66. }
  67. Y_UNIT_TEST(TestResolvePathRelative) {
  68. TTempDir tempDir;
  69. TTempBuf tempBuf;
  70. TString base = RealPath(tempDir());
  71. if (base.back() == GetDirectorySeparator()) {
  72. base.pop_back();
  73. }
  74. // File
  75. TString path = base + GetDirectorySeparatorS() + "file";
  76. {
  77. TFixedBufferFileOutput file(path);
  78. }
  79. ResolvePath("file", base.data(), tempBuf.Data(), false);
  80. UNIT_ASSERT_EQUAL(tempBuf.Data(), path);
  81. // Dir
  82. path = base + GetDirectorySeparatorS() + "dir";
  83. MakeDirIfNotExist(path.data());
  84. ResolvePath("dir", base.data(), tempBuf.Data(), true);
  85. UNIT_ASSERT_EQUAL(tempBuf.Data(), path + GetDirectorySeparatorS());
  86. // Absent file in existent dir
  87. path = base + GetDirectorySeparatorS() + "nofile";
  88. ResolvePath("nofile", base.data(), tempBuf.Data(), false);
  89. UNIT_ASSERT_EQUAL(tempBuf.Data(), path);
  90. }
  91. Y_UNIT_TEST(TestGetDirName) {
  92. UNIT_ASSERT_VALUES_EQUAL(".", GetDirName("parambambam"));
  93. }
  94. Y_UNIT_TEST(TestStripFileComponent) {
  95. static const TString tmpDir = "tmp_dir_for_tests";
  96. static const TString tmpSubDir = tmpDir + GetDirectorySeparatorS() + "subdir";
  97. static const TString tmpFile = tmpDir + GetDirectorySeparatorS() + "file";
  98. // creating tmp dir and subdirs
  99. MakeDirIfNotExist(tmpDir.data());
  100. MakeDirIfNotExist(tmpSubDir.data());
  101. {
  102. TFixedBufferFileOutput file(tmpFile);
  103. }
  104. UNIT_ASSERT_EQUAL(StripFileComponent(tmpDir), tmpDir + GetDirectorySeparatorS());
  105. UNIT_ASSERT_EQUAL(StripFileComponent(tmpSubDir), tmpSubDir + GetDirectorySeparatorS());
  106. UNIT_ASSERT_EQUAL(StripFileComponent(tmpFile), tmpDir + GetDirectorySeparatorS());
  107. RemoveDirWithContents(tmpDir);
  108. }
  109. }