Commit 3c8f73b5 authored by Brad King's avatar Brad King Committed by Kitware Robot

Merge topic 'MoveFileIfDiff'

f73d1b4f SystemTools: factor out "dest is directory" case from CopyFileIfDifferent()
1ed70242 tests: verify SystemTools::CopyFileIfDifferent()
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !147
parents e7be56ac f73d1b4f
......@@ -2169,24 +2169,24 @@ std::string SystemTools::ConvertToWindowsOutputPath(const std::string& path)
return ret;
}
/**
* Append the filename from the path source to the directory name dir.
*/
static std::string FileInDir(const std::string& source, const std::string& dir)
{
std::string new_destination = dir;
SystemTools::ConvertToUnixSlashes(new_destination);
return new_destination + '/' + SystemTools::GetFilenameName(source);
}
bool SystemTools::CopyFileIfDifferent(const std::string& source,
const std::string& destination)
{
// special check for a destination that is a directory
// FilesDiffer does not handle file to directory compare
if (SystemTools::FileIsDirectory(destination)) {
std::string new_destination = destination;
SystemTools::ConvertToUnixSlashes(new_destination);
new_destination += '/';
std::string source_name = source;
new_destination += SystemTools::GetFilenameName(source_name);
if (SystemTools::FilesDiffer(source, new_destination)) {
return SystemTools::CopyFileAlways(source, destination);
} else {
// the files are the same so the copy is done return
// true
return true;
}
const std::string new_destination = FileInDir(source, destination);
return SystemTools::CopyFileIfDifferent(source, new_destination);
}
// source and destination are files so do a copy if they
// are different
......
......@@ -999,30 +999,45 @@ static bool writeFile(const char* fileName, const char* data)
return true;
}
static std::string readFile(const char* fileName)
{
kwsys::ifstream in(fileName, std::ios::binary);
std::stringstream sstr;
sstr << in.rdbuf();
std::string data = sstr.str();
if (!in) {
std::cerr << "Failed to read file: " << fileName << std::endl;
return std::string();
}
return data;
}
struct
{
const char* a;
const char* b;
bool differ;
} diff_test_cases[] = { { "one", "one", false },
{ "one", "two", true },
{ "", "", false },
{ "\n", "\r\n", false },
{ "one\n", "one\n", false },
{ "one\r\n", "one\n", false },
{ "one\n", "one", false },
{ "one\ntwo", "one\ntwo", false },
{ "one\ntwo", "one\r\ntwo", false } };
static bool CheckTextFilesDiffer()
{
struct
{
const char* a;
const char* b;
bool differ;
} test_cases[] = { { "one", "one", false },
{ "one", "two", true },
{ "", "", false },
{ "\n", "\r\n", false },
{ "one\n", "one\n", false },
{ "one\r\n", "one\n", false },
{ "one\n", "one", false },
{ "one\ntwo", "one\ntwo", false },
{ "one\ntwo", "one\r\ntwo", false } };
const int num_test_cases = sizeof(test_cases) / sizeof(test_cases[0]);
const int num_test_cases =
sizeof(diff_test_cases) / sizeof(diff_test_cases[0]);
for (int i = 0; i < num_test_cases; ++i) {
if (!writeFile("file_a", test_cases[i].a) ||
!writeFile("file_b", test_cases[i].b)) {
if (!writeFile("file_a", diff_test_cases[i].a) ||
!writeFile("file_b", diff_test_cases[i].b)) {
return false;
}
if (kwsys::SystemTools::TextFilesDiffer("file_a", "file_b") !=
test_cases[i].differ) {
diff_test_cases[i].differ) {
std::cerr << "Incorrect TextFilesDiffer result for test case " << i + 1
<< "." << std::endl;
return false;
......@@ -1032,6 +1047,36 @@ static bool CheckTextFilesDiffer()
return true;
}
static bool CheckCopyFileIfDifferent()
{
bool ret = true;
const int num_test_cases =
sizeof(diff_test_cases) / sizeof(diff_test_cases[0]);
for (int i = 0; i < num_test_cases; ++i) {
if (!writeFile("file_a", diff_test_cases[i].a) ||
!writeFile("file_b", diff_test_cases[i].b)) {
return false;
}
const char* cptarget =
i < 4 ? TEST_SYSTEMTOOLS_BINARY_DIR "/file_b" : "file_b";
if (!kwsys::SystemTools::CopyFileIfDifferent("file_a", cptarget)) {
std::cerr << "CopyFileIfDifferent() returned false for test case "
<< i + 1 << "." << std::endl;
ret = false;
continue;
}
std::string bdata = readFile("file_b");
if (diff_test_cases[i].a != bdata) {
std::cerr << "Incorrect CopyFileIfDifferent file contents in test case "
<< i + 1 << "." << std::endl;
ret = false;
continue;
}
}
return ret;
}
int testSystemTools(int, char* [])
{
bool res = true;
......@@ -1077,5 +1122,7 @@ int testSystemTools(int, char* [])
res &= CheckTextFilesDiffer();
res &= CheckCopyFileIfDifferent();
return res ? 0 : 1;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment