mktemp_ut.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <filesystem>
  3. #include "tempfile.h"
  4. #include <util/folder/dirut.h>
  5. Y_UNIT_TEST_SUITE(MakeTempFileSuite) {
  6. static const char TestDir[] = "Test";
  7. static const char Prefix[] = "PREFIX_____PREFIX";
  8. static const char Extension[] = "txt";
  9. static const unsigned int PathSegmentSizeNormal = 55;
  10. static const unsigned int PathSegmentSizeLong = 255;
  11. Y_UNIT_TEST(TestMakeTempName) {
  12. const TFsPath systemTemp{GetSystemTempDir()};
  13. UNIT_ASSERT(systemTemp.Exists());
  14. for (auto dirNameLength : {PathSegmentSizeNormal, PathSegmentSizeLong}) {
  15. const TFsPath testDir{systemTemp / TestDir};
  16. testDir.MkDir();
  17. UNIT_ASSERT(testDir.Exists());
  18. Y_DEFER {
  19. std::filesystem::remove_all(testDir.c_str());
  20. };
  21. const TString dirName(dirNameLength, 'X');
  22. const TFsPath dirPath = testDir / dirName;
  23. UNIT_ASSERT(std::filesystem::create_directory(dirPath.GetPath().c_str()));
  24. TString tempFilePath;
  25. try {
  26. tempFilePath = MakeTempName(dirPath.c_str(), Prefix, Extension);
  27. } catch (const TSystemError& ex) {
  28. Cerr << "Unexpected exception: " << ex.what() << Endl;
  29. }
  30. UNIT_ASSERT(TFsPath{tempFilePath}.Exists());
  31. }
  32. }
  33. } // Y_UNIT_TEST_SUITE(MakeTempFileSuite)