testDirectory.cxx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. res += !testdir.Load(testdirpath);
  53. // Increment res failure if the directory appears empty
  54. res += testdir.GetNumberOfFiles() == 0;
  55. // Increment res failures if the path has changed from
  56. // what was provided.
  57. res += testdirpath != testdir.GetPath();
  58. SystemTools::RemoveADirectory(topdir);
  59. } else {
  60. std::cerr << "Failed to create directory with long path: "
  61. << extendedtestdirpath << std::endl;
  62. res += 1;
  63. }
  64. return res;
  65. }
  66. int testDirectory(int, char* [])
  67. {
  68. return _doLongPathTest();
  69. }