summaryrefslogtreecommitdiffstats
path: root/Source/cmCTest.cxx
diff options
context:
space:
mode:
authorZack Galbreath <zack.galbreath@kitware.com>2021-06-04 15:22:34 (GMT)
committerZack Galbreath <zack.galbreath@kitware.com>2021-06-08 13:27:19 (GMT)
commitcbcb92d1cb4230426761fb7d08f9135546c98881 (patch)
tree18fa4c3f5e3b53700aa2f9cf18bd9ad34116b6bb /Source/cmCTest.cxx
parentacb25d50d9d37e93cafcbbc4401e1b45029b6461 (diff)
downloadCMake-cbcb92d1cb4230426761fb7d08f9135546c98881.zip
CMake-cbcb92d1cb4230426761fb7d08f9135546c98881.tar.gz
CMake-cbcb92d1cb4230426761fb7d08f9135546c98881.tar.bz2
ctest: add support for attaching files to tests at run time
Allow tests to specify files to upload at runtime. Previously this was only possible to specify at configure time with the ATTACHED_FILES test properties. This commit also fixes a bug in how our test data tarballs were generated by CTest. Previously, if you tried to attach a file outside of the binary directory, CTest would generate a tar file with a relative path, and tar would not allow you to extract it. We resolve this problem by creating tar files with a flat directory structure instead. Fixes: #22284
Diffstat (limited to 'Source/cmCTest.cxx')
-rw-r--r--Source/cmCTest.cxx40
1 files changed, 32 insertions, 8 deletions
diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx
index 7534e37..7c469c8 100644
--- a/Source/cmCTest.cxx
+++ b/Source/cmCTest.cxx
@@ -1612,8 +1612,33 @@ int cmCTest::GenerateDoneFile()
return 0;
}
+bool cmCTest::TryToChangeDirectory(std::string const& dir)
+{
+ cmCTestLog(this, OUTPUT,
+ "Internal ctest changing into directory: " << dir << std::endl);
+ cmsys::Status status = cmSystemTools::ChangeDirectory(dir);
+ if (!status) {
+ auto msg = "Failed to change working directory to \"" + dir +
+ "\" : " + status.GetString() + "\n";
+ cmCTestLog(this, ERROR_MESSAGE, msg);
+ return false;
+ }
+ return true;
+}
+
std::string cmCTest::Base64GzipEncodeFile(std::string const& file)
{
+ const std::string currDir = cmSystemTools::GetCurrentWorkingDirectory();
+ std::string parentDir = cmSystemTools::GetParentDirectory(file);
+
+ // Temporarily change to the file's directory so the tar gets created
+ // with a flat directory structure.
+ if (currDir != parentDir) {
+ if (!this->TryToChangeDirectory(parentDir)) {
+ return "";
+ }
+ }
+
std::string tarFile = file + "_temp.tar.gz";
std::vector<std::string> files;
files.push_back(file);
@@ -1628,6 +1653,12 @@ std::string cmCTest::Base64GzipEncodeFile(std::string const& file)
}
std::string base64 = this->Base64EncodeFile(tarFile);
cmSystemTools::RemoveFile(tarFile);
+
+ // Change back to the directory we started in.
+ if (currDir != parentDir) {
+ cmSystemTools::ChangeDirectory(currDir);
+ }
+
return base64;
}
@@ -2853,14 +2884,7 @@ int cmCTest::ExecuteTests()
}
if (currDir != workDir) {
- cmCTestLog(this, OUTPUT,
- "Internal ctest changing into directory: " << workDir
- << std::endl);
- cmsys::Status status = cmSystemTools::ChangeDirectory(workDir);
- if (!status) {
- auto msg = "Failed to change working directory to \"" + workDir +
- "\" : " + status.GetString() + "\n";
- cmCTestLog(this, ERROR_MESSAGE, msg);
+ if (!this->TryToChangeDirectory(workDir)) {
return 1;
}
}