tempfile_ut.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "tempfile.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/folder/dirut.h>
  4. #include <util/generic/yexception.h>
  5. #include <util/stream/file.h>
  6. #include <algorithm>
  7. Y_UNIT_TEST_SUITE(TTempFileHandle) {
  8. Y_UNIT_TEST(Create) {
  9. TString path;
  10. {
  11. TTempFileHandle tmp;
  12. path = tmp.Name();
  13. tmp.Write("hello world\n", 12);
  14. tmp.FlushData();
  15. UNIT_ASSERT_STRINGS_EQUAL(TUnbufferedFileInput(tmp.Name()).ReadAll(), "hello world\n");
  16. }
  17. UNIT_ASSERT(!NFs::Exists(path));
  18. }
  19. Y_UNIT_TEST(InCurrentDir) {
  20. #ifndef _win32_
  21. static const TString TEST_PREFIX = "unique_prefix";
  22. #else
  23. static const TString TEST_PREFIX = "uni";
  24. #endif
  25. TString path;
  26. {
  27. TTempFileHandle tmp = TTempFileHandle::InCurrentDir(TEST_PREFIX);
  28. path = tmp.Name();
  29. UNIT_ASSERT(NFs::Exists(path));
  30. TVector<TString> names;
  31. TFsPath(".").ListNames(names);
  32. bool containsFileWithPrefix = std::any_of(names.begin(), names.end(), [&](const TString& name) {
  33. return name.Contains(TEST_PREFIX);
  34. });
  35. UNIT_ASSERT(containsFileWithPrefix);
  36. }
  37. UNIT_ASSERT(!NFs::Exists(path));
  38. }
  39. Y_UNIT_TEST(UseExtensionWithoutDot) {
  40. TString path;
  41. {
  42. TTempFileHandle tmp = TTempFileHandle::InCurrentDir("hello", "world");
  43. path = tmp.Name();
  44. UNIT_ASSERT(NFs::Exists(path));
  45. #ifndef _win32_
  46. UNIT_ASSERT(path.Contains("hello"));
  47. UNIT_ASSERT(path.EndsWith(".world"));
  48. UNIT_ASSERT(!path.EndsWith("..world"));
  49. #else
  50. UNIT_ASSERT(path.Contains("hel"));
  51. UNIT_ASSERT(path.EndsWith(".tmp"));
  52. #endif
  53. }
  54. UNIT_ASSERT(!NFs::Exists(path));
  55. }
  56. Y_UNIT_TEST(UseExtensionWithDot) {
  57. TString path;
  58. {
  59. TTempFileHandle tmp = TTempFileHandle::InCurrentDir("lorem", ".ipsum");
  60. path = tmp.Name();
  61. UNIT_ASSERT(NFs::Exists(path));
  62. #ifndef _win32_
  63. UNIT_ASSERT(path.Contains("lorem"));
  64. UNIT_ASSERT(path.EndsWith(".ipsum"));
  65. UNIT_ASSERT(!path.EndsWith("..ipsum"));
  66. #else
  67. UNIT_ASSERT(path.Contains("lor"));
  68. UNIT_ASSERT(path.EndsWith(".tmp"));
  69. #endif
  70. }
  71. UNIT_ASSERT(!NFs::Exists(path));
  72. }
  73. Y_UNIT_TEST(SafeDestructor) {
  74. TString path;
  75. {
  76. path = MakeTempName();
  77. UNIT_ASSERT(NFs::Exists(path));
  78. TTempFileHandle tmp(path);
  79. Y_UNUSED(tmp);
  80. UNIT_ASSERT(NFs::Exists(path));
  81. TTempFileHandle anotherTmp(path);
  82. Y_UNUSED(anotherTmp);
  83. UNIT_ASSERT(NFs::Exists(path));
  84. }
  85. UNIT_ASSERT(!NFs::Exists(path));
  86. }
  87. Y_UNIT_TEST(RemovesOpen) {
  88. TString path;
  89. {
  90. TTempFileHandle tmp;
  91. path = tmp.Name();
  92. tmp.Write("hello world\n", 12);
  93. tmp.FlushData();
  94. UNIT_ASSERT(NFs::Exists(path));
  95. UNIT_ASSERT(tmp.IsOpen());
  96. }
  97. UNIT_ASSERT(!NFs::Exists(path));
  98. }
  99. Y_UNIT_TEST(NonExistingDirectory) {
  100. UNIT_ASSERT_EXCEPTION(TTempFileHandle::InDir("nonexsistingdirname"), TSystemError);
  101. }
  102. } // Y_UNIT_TEST_SUITE(TTempFileHandle)
  103. Y_UNIT_TEST_SUITE(MakeTempName) {
  104. Y_UNIT_TEST(Default) {
  105. TString path;
  106. {
  107. TTempFile tmp(MakeTempName());
  108. path = tmp.Name();
  109. UNIT_ASSERT(!path.Contains('\0'));
  110. UNIT_ASSERT(NFs::Exists(path));
  111. UNIT_ASSERT(path.EndsWith(".tmp"));
  112. #ifndef _win32_
  113. UNIT_ASSERT(path.Contains("yandex"));
  114. #else
  115. UNIT_ASSERT(path.Contains("yan"));
  116. #endif
  117. }
  118. UNIT_ASSERT(!NFs::Exists(path));
  119. }
  120. Y_UNIT_TEST(UseNullptr) {
  121. TString path;
  122. {
  123. TTempFile tmp(MakeTempName(nullptr, nullptr, nullptr));
  124. path = tmp.Name();
  125. UNIT_ASSERT(!path.Contains('\0'));
  126. UNIT_ASSERT(NFs::Exists(path));
  127. }
  128. UNIT_ASSERT(!NFs::Exists(path));
  129. }
  130. } // Y_UNIT_TEST_SUITE(MakeTempName)