summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAsit Dhal <dhal.asitk@gmail.com>2020-12-16 16:14:26 (GMT)
committerAsit Dhal <dhal.asitk@gmail.com>2020-12-17 04:01:03 (GMT)
commitdbcf86d24d9e0d762e97e3a4b492739f99816c13 (patch)
tree33514dd1556487d9518f2d122c25d951cd16cedf
parent0f61fac6f028214d64ef6a407c6b98d4503ee2fe (diff)
downloadCMake-dbcf86d24d9e0d762e97e3a4b492739f99816c13.zip
CMake-dbcf86d24d9e0d762e97e3a4b492739f99816c13.tar.gz
CMake-dbcf86d24d9e0d762e97e3a4b492739f99816c13.tar.bz2
Ctest: Support build tree on command line
Fixes: #21268
-rw-r--r--Help/manual/ctest.1.rst3
-rw-r--r--Help/release/dev/ctest-test-dir.rst5
-rw-r--r--Source/cmCTest.cxx35
-rw-r--r--Source/ctest.cxx1
-rw-r--r--Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake17
-rw-r--r--Tests/RunCMake/CTestCommandLine/test-dir-invalid-arg-result.txt1
-rw-r--r--Tests/RunCMake/CTestCommandLine/test-dir-invalid-arg-stderr.txt1
-rw-r--r--Tests/RunCMake/CTestCommandLine/test-dir-non-existing-dir-result.txt1
-rw-r--r--Tests/RunCMake/CTestCommandLine/test-dir-non-existing-dir-stderr.txt1
-rw-r--r--Tests/RunCMake/CTestCommandLine/test-dir-non-existing-dir-stdout.txt1
10 files changed, 64 insertions, 2 deletions
diff --git a/Help/manual/ctest.1.rst b/Help/manual/ctest.1.rst
index 00df24b..16afcec 100644
--- a/Help/manual/ctest.1.rst
+++ b/Help/manual/ctest.1.rst
@@ -324,6 +324,9 @@ Options
``--build-and-test``
See `Build and Test Mode`_.
+``--test-dir <dir>``
+Specify the directory in which to look for tests.
+
``--test-output-size-passed <size>``
Limit the output for passed tests to ``<size>`` bytes.
diff --git a/Help/release/dev/ctest-test-dir.rst b/Help/release/dev/ctest-test-dir.rst
new file mode 100644
index 0000000..ab8d618
--- /dev/null
+++ b/Help/release/dev/ctest-test-dir.rst
@@ -0,0 +1,5 @@
+ctest-test-dir.rst
+------------------
+
+* :manual:`ctest(1)` gained a ``--test-dir`` option to specify the directory
+ in which to look for tests.
diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx
index 8479458..014ce4e 100644
--- a/Source/cmCTest.cxx
+++ b/Source/cmCTest.cxx
@@ -4,6 +4,7 @@
#include <algorithm>
#include <cctype>
+#include <cerrno>
#include <chrono>
#include <cstdio>
#include <cstdlib>
@@ -179,6 +180,7 @@ struct cmCTest::Private
// information for the --build-and-test options
std::string BinaryDir;
+ std::string TestDir;
std::string NotesFiles;
@@ -2059,6 +2061,13 @@ bool cmCTest::HandleCommandLineArguments(size_t& i,
i++;
this->SetNotesFiles(args[i]);
return true;
+ } else if (this->CheckArgument(arg, "--test-dir"_s)) {
+ if (i >= args.size() - 1) {
+ errormsg = "'--test-dir' requires an argument";
+ return false;
+ }
+ i++;
+ this->Impl->TestDir = std::string(args[i]);
}
cm::string_view noTestsPrefix = "--no-tests=";
@@ -2467,8 +2476,26 @@ int cmCTest::ExecuteTests()
handler->SetVerbose(this->Impl->Verbose);
handler->SetSubmitIndex(this->Impl->SubmitIndex);
}
- std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
- if (!this->Initialize(cwd.c_str(), nullptr)) {
+
+ const std::string currDir = cmSystemTools::GetCurrentWorkingDirectory();
+ std::string workDir = currDir;
+ if (!this->Impl->TestDir.empty()) {
+ workDir = cmSystemTools::CollapseFullPath(this->Impl->TestDir);
+ }
+
+ if (currDir != workDir) {
+ cmCTestLog(this, OUTPUT,
+ "Internal ctest changing into directory: " << workDir
+ << std::endl);
+ if (cmSystemTools::ChangeDirectory(workDir) != 0) {
+ auto msg = "Failed to change working directory to \"" + workDir +
+ "\" : " + std::strerror(errno) + "\n";
+ cmCTestLog(this, ERROR_MESSAGE, msg);
+ return 1;
+ }
+ }
+
+ if (!this->Initialize(workDir.c_str(), nullptr)) {
res = 12;
cmCTestLog(this, ERROR_MESSAGE,
"Problem initializing the dashboard." << std::endl);
@@ -2476,6 +2503,10 @@ int cmCTest::ExecuteTests()
res = this->ProcessSteps();
}
this->Finalize();
+
+ if (currDir != workDir) {
+ cmSystemTools::ChangeDirectory(currDir);
+ }
}
if (res != 0) {
cmCTestLog(this, DEBUG,
diff --git a/Source/ctest.cxx b/Source/ctest.cxx
index d0bc061..600df1d 100644
--- a/Source/ctest.cxx
+++ b/Source/ctest.cxx
@@ -111,6 +111,7 @@ static const char* cmDocumentationOptions[][2] = {
{ "--no-subproject-summary",
"Disable timing summary information for "
"subprojects." },
+ { "--test-dir <dir>", "Specify the directory in which to look for tests." },
{ "--build-and-test", "Configure, build and run a test." },
{ "--build-target", "Specify a specific target to build." },
{ "--build-nocmake", "Run the build without running cmake first." },
diff --git a/Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake
index e05ad79..6cf1476 100644
--- a/Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake
+++ b/Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake
@@ -380,3 +380,20 @@ run_MemCheckSan(Leak "simulate_sanitizer=1:report_bugs=1:history_size=5:exitcode
run_MemCheckSan(Memory "simulate_sanitizer=1:report_bugs=1:history_size=5:exitcode=55")
run_MemCheckSan(Thread "report_bugs=1:history_size=5:exitcode=55")
run_MemCheckSan(UndefinedBehavior "simulate_sanitizer=1")
+
+run_cmake_command(test-dir-invalid-arg ${CMAKE_CTEST_COMMAND} --test-dir)
+run_cmake_command(test-dir-non-existing-dir ${CMAKE_CTEST_COMMAND} --test-dir non-existing-dir)
+
+function(run_testDir)
+ set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/testDir)
+ set(RunCMake_TEST_NO_CLEAN 1)
+ file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
+ file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
+ file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}/sub")
+ file(WRITE "${RunCMake_TEST_BINARY_DIR}/sub/CTestTestfile.cmake" "
+ add_test(Test1 \"${CMAKE_COMMAND}\" -E true)
+ add_test(Test2 \"${CMAKE_COMMAND}\" -E true)
+ ")
+ run_cmake_command(testDir ${CMAKE_CTEST_COMMAND} --test-dir "${RunCMake_TEST_BINARY_DIR}/sub")
+endfunction()
+run_testDir()
diff --git a/Tests/RunCMake/CTestCommandLine/test-dir-invalid-arg-result.txt b/Tests/RunCMake/CTestCommandLine/test-dir-invalid-arg-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/CTestCommandLine/test-dir-invalid-arg-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/CTestCommandLine/test-dir-invalid-arg-stderr.txt b/Tests/RunCMake/CTestCommandLine/test-dir-invalid-arg-stderr.txt
new file mode 100644
index 0000000..15908a7
--- /dev/null
+++ b/Tests/RunCMake/CTestCommandLine/test-dir-invalid-arg-stderr.txt
@@ -0,0 +1 @@
+CMake Error: '--test-dir' requires an argument
diff --git a/Tests/RunCMake/CTestCommandLine/test-dir-non-existing-dir-result.txt b/Tests/RunCMake/CTestCommandLine/test-dir-non-existing-dir-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/CTestCommandLine/test-dir-non-existing-dir-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/CTestCommandLine/test-dir-non-existing-dir-stderr.txt b/Tests/RunCMake/CTestCommandLine/test-dir-non-existing-dir-stderr.txt
new file mode 100644
index 0000000..017ccb0
--- /dev/null
+++ b/Tests/RunCMake/CTestCommandLine/test-dir-non-existing-dir-stderr.txt
@@ -0,0 +1 @@
+Failed to change working directory to ".*/non-existing-dir" : No such file or directory
diff --git a/Tests/RunCMake/CTestCommandLine/test-dir-non-existing-dir-stdout.txt b/Tests/RunCMake/CTestCommandLine/test-dir-non-existing-dir-stdout.txt
new file mode 100644
index 0000000..ddcd238
--- /dev/null
+++ b/Tests/RunCMake/CTestCommandLine/test-dir-non-existing-dir-stdout.txt
@@ -0,0 +1 @@
+Internal ctest changing into directory: .*/non-existing-dir