dirut_ut.cpp 4.4 KB

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