testDirectory.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. #include "kwsysPrivate.h"
  4. #include KWSYS_HEADER(Directory.hxx)
  5. #include KWSYS_HEADER(Encoding.hxx)
  6. #include KWSYS_HEADER(SystemTools.hxx)
  7. // Work-around CMake dependency scanning limitation. This must
  8. // duplicate the above list of headers.
  9. #if 0
  10. # include "Directory.hxx.in"
  11. # include "Encoding.hxx.in"
  12. # include "SystemTools.hxx.in"
  13. #endif
  14. #include <fstream>
  15. #include <iostream>
  16. #include <sstream>
  17. #include <testSystemTools.h>
  18. int _doLongPathTest()
  19. {
  20. using namespace kwsys;
  21. static const int LONG_PATH_THRESHOLD = 512;
  22. int res = 0;
  23. std::string topdir(TEST_SYSTEMTOOLS_BINARY_DIR "/directory_testing/");
  24. std::stringstream testpathstrm;
  25. std::string testdirpath;
  26. std::string extendedtestdirpath;
  27. testpathstrm << topdir;
  28. size_t pathlen = testpathstrm.str().length();
  29. testpathstrm.seekp(0, std::ios_base::end);
  30. while (pathlen < LONG_PATH_THRESHOLD) {
  31. testpathstrm << "0123456789/";
  32. pathlen = testpathstrm.str().length();
  33. }
  34. testdirpath = testpathstrm.str();
  35. #ifdef _WIN32
  36. extendedtestdirpath =
  37. Encoding::ToNarrow(SystemTools::ConvertToWindowsExtendedPath(testdirpath));
  38. #else
  39. extendedtestdirpath = testdirpath;
  40. #endif
  41. if (SystemTools::MakeDirectory(extendedtestdirpath)) {
  42. std::ofstream testfile1(
  43. (extendedtestdirpath + "longfilepathtest1.txt").c_str());
  44. std::ofstream testfile2(
  45. (extendedtestdirpath + "longfilepathtest2.txt").c_str());
  46. testfile1 << "foo";
  47. testfile2 << "bar";
  48. testfile1.close();
  49. testfile2.close();
  50. Directory testdir;
  51. // Set res to failure if the directory doesn't load
  52. std::string errorMessage = "";
  53. res += !testdir.Load(testdirpath, &errorMessage);
  54. if (errorMessage != "") {
  55. std::cerr << "Failed to list directory: " << errorMessage << std::endl;
  56. }
  57. // Increment res failure if the directory appears empty
  58. res += testdir.GetNumberOfFiles() == 0;
  59. // Increment res failures if the path has changed from
  60. // what was provided.
  61. res += testdirpath != testdir.GetPath();
  62. SystemTools::RemoveADirectory(topdir);
  63. } else {
  64. std::cerr << "Failed to create directory with long path: "
  65. << extendedtestdirpath << std::endl;
  66. res += 1;
  67. }
  68. return res;
  69. }
  70. int _nonExistentDirectoryTest()
  71. {
  72. using namespace kwsys;
  73. int res = 0;
  74. std::string testdirpath(TEST_SYSTEMTOOLS_BINARY_DIR
  75. "/directory_testing/doesnt_exist/");
  76. std::string errorMessage;
  77. Directory testdir;
  78. errorMessage = "foo";
  79. // Increment res failure if directory lists
  80. res += testdir.Load(testdirpath, &errorMessage) ? 1 : 0;
  81. #if !defined(_WIN32) || defined(__CYGWIN__)
  82. // Increment res failure if errorMessage is unmodified
  83. res += (errorMessage == "foo");
  84. #endif
  85. errorMessage = "foo";
  86. // Increment res failure if directory has files
  87. res += (testdir.GetNumberOfFilesInDirectory(testdirpath, &errorMessage) > 0);
  88. #if !defined(_WIN32) || defined(__CYGWIN__)
  89. // Increment res failure if errorMessage is unmodified
  90. res += (errorMessage == "foo");
  91. #endif
  92. return res;
  93. }
  94. int _copyDirectoryTest()
  95. {
  96. using namespace kwsys;
  97. const std::string source(TEST_SYSTEMTOOLS_BINARY_DIR
  98. "/directory_testing/copyDirectoryTestSrc");
  99. if (SystemTools::PathExists(source)) {
  100. std::cerr << source << " shouldn't exist before test" << std::endl;
  101. return 1;
  102. }
  103. const std::string destination(TEST_SYSTEMTOOLS_BINARY_DIR
  104. "/directory_testing/copyDirectoryTestDst");
  105. if (SystemTools::PathExists(destination)) {
  106. std::cerr << destination << " shouldn't exist before test" << std::endl;
  107. return 2;
  108. }
  109. const Status copysuccess = SystemTools::CopyADirectory(source, destination);
  110. const bool destinationexists = SystemTools::PathExists(destination);
  111. if (copysuccess.IsSuccess()) {
  112. std::cerr << "CopyADirectory should have returned false" << std::endl;
  113. SystemTools::RemoveADirectory(destination);
  114. return 3;
  115. }
  116. if (destinationexists) {
  117. std::cerr << "CopyADirectory returned false, but destination directory"
  118. << " has been created" << std::endl;
  119. SystemTools::RemoveADirectory(destination);
  120. return 4;
  121. }
  122. return 0;
  123. }
  124. int testDirectory(int, char* [])
  125. {
  126. return _doLongPathTest() + _nonExistentDirectoryTest() +
  127. _copyDirectoryTest();
  128. }