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