Merge topic 'ctest-no-tests-action-env-var'

0661de58d8 ctest(1): Add CTEST_NO_TESTS_ACTION env var

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !8044
diff --git a/Help/envvar/CTEST_NO_TESTS_ACTION.rst b/Help/envvar/CTEST_NO_TESTS_ACTION.rst
new file mode 100644
index 0000000..2bc86dc
--- /dev/null
+++ b/Help/envvar/CTEST_NO_TESTS_ACTION.rst
@@ -0,0 +1,14 @@
+CTEST_NO_TESTS_ACTION
+---------------------
+
+.. versionadded:: 3.26
+
+.. include:: ENV_VAR.txt
+
+Environment variable that controls how :manual:`ctest <ctest(1)>` handles
+cases when there are no tests to run. Possible values are: ``error``,
+``ignore``, empty or unset.
+
+The :option:`--no-tests=\<action\> <ctest --no-tests>` option to
+:manual:`ctest <ctest(1)>` overrides this environment variable if both
+are given.
diff --git a/Help/manual/cmake-env-variables.7.rst b/Help/manual/cmake-env-variables.7.rst
index 50fcf75..4c29b80 100644
--- a/Help/manual/cmake-env-variables.7.rst
+++ b/Help/manual/cmake-env-variables.7.rst
@@ -92,6 +92,7 @@
 
    /envvar/CMAKE_CONFIG_TYPE
    /envvar/CTEST_INTERACTIVE_DEBUG_MODE
+   /envvar/CTEST_NO_TESTS_ACTION
    /envvar/CTEST_OUTPUT_ON_FAILURE
    /envvar/CTEST_PARALLEL_LEVEL
    /envvar/CTEST_PROGRESS_OUTPUT
diff --git a/Help/manual/ctest.1.rst b/Help/manual/ctest.1.rst
index 30a9eae..5e82faa 100644
--- a/Help/manual/ctest.1.rst
+++ b/Help/manual/ctest.1.rst
@@ -435,6 +435,11 @@
  unifies the behavior of CTest by either returning an error code if no tests
  were found or by ignoring it.
 
+ .. versionadded:: 3.26
+
+ This option can also be set by setting the :envvar:`CTEST_NO_TESTS_ACTION`
+ environment variable.
+
 View Help
 =========
 
diff --git a/Help/release/dev/ctest-no-tests-action-env-var.rst b/Help/release/dev/ctest-no-tests-action-env-var.rst
new file mode 100644
index 0000000..8679977
--- /dev/null
+++ b/Help/release/dev/ctest-no-tests-action-env-var.rst
@@ -0,0 +1,7 @@
+ctest-no-tests-action-env-var
+-----------------------------
+
+* The :envvar:`CTEST_NO_TESTS_ACTION` environment variable was added to
+  provide a default value for the
+  :option:`--no-tests=\<action\> <ctest --no-tests>` command line
+  argument of :manual:`ctest(1)`.
diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx
index b00fa73..72cd8cd 100644
--- a/Source/cmCTest.cxx
+++ b/Source/cmCTest.cxx
@@ -217,6 +217,7 @@
   std::map<std::string, std::string> Definitions;
 
   cmCTest::NoTests NoTestsMode = cmCTest::NoTests::Legacy;
+  bool NoTestsModeSetInCli = false;
 };
 
 struct tm* cmCTest::GetNightlyTime(std::string const& str, bool tomorrowtag)
@@ -2132,6 +2133,7 @@
     } else {
       this->Impl->NoTestsMode = cmCTest::NoTests::Ignore;
     }
+    this->Impl->NoTestsModeSetInCli = true;
   }
 
   // options that control what tests are run
@@ -2774,6 +2776,24 @@
     }
   }
 
+  // handle CTEST_NO_TESTS_ACTION environment variable
+  if (!this->Impl->NoTestsModeSetInCli) {
+    std::string action;
+    if (cmSystemTools::GetEnv("CTEST_NO_TESTS_ACTION", action) &&
+        !action.empty()) {
+      if (action == "error"_s) {
+        this->Impl->NoTestsMode = cmCTest::NoTests::Error;
+      } else if (action == "ignore"_s) {
+        this->Impl->NoTestsMode = cmCTest::NoTests::Ignore;
+      } else {
+        cmCTestLog(this, ERROR_MESSAGE,
+                   "Unknown value for CTEST_NO_TESTS_ACTION: '" << action
+                                                                << '\'');
+        return 1;
+      }
+    }
+  }
+
   // TestProgressOutput only supported if console supports it and not logging
   // to a file
   this->Impl->TestProgressOutput = this->Impl->TestProgressOutput &&
diff --git a/Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake
index 8c35fe5..bda260a 100644
--- a/Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake
+++ b/Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake
@@ -389,6 +389,14 @@
   run_cmake_command(no-tests_error ${CMAKE_CTEST_COMMAND} --no-tests=error)
   run_cmake_command(no-tests_bad ${CMAKE_CTEST_COMMAND} --no-tests=bad)
   run_cmake_command(no-tests_legacy ${CMAKE_CTEST_COMMAND})
+
+  run_cmake_command(no-tests_env_ignore ${CMAKE_COMMAND} -E env CTEST_NO_TESTS_ACTION=ignore ${CMAKE_CTEST_COMMAND})
+  run_cmake_command(no-tests_env_error ${CMAKE_COMMAND} -E env CTEST_NO_TESTS_ACTION=error ${CMAKE_CTEST_COMMAND})
+  run_cmake_command(no-tests_env_bad ${CMAKE_COMMAND} -E env CTEST_NO_TESTS_ACTION=bad ${CMAKE_CTEST_COMMAND})
+  run_cmake_command(no-tests_env_empty_legacy ${CMAKE_COMMAND} -E env CTEST_NO_TESTS_ACTION= ${CMAKE_CTEST_COMMAND})
+
+  run_cmake_command(no-tests_env_bad_with_cli_error ${CMAKE_COMMAND} -E env CTEST_NO_TESTS_ACTION=bad ${CMAKE_CTEST_COMMAND} --no-tests=error)
+
   file(WRITE "${RunCMake_TEST_BINARY_DIR}/NoTestsScript.cmake" "
     set(CTEST_COMMAND \"${CMAKE_CTEST_COMMAND}\")
     set(CTEST_SOURCE_DIRECTORY \"${RunCMake_SOURCE_DIR}\")
diff --git a/Tests/RunCMake/CTestCommandLine/no-tests_env_bad-result.txt b/Tests/RunCMake/CTestCommandLine/no-tests_env_bad-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/CTestCommandLine/no-tests_env_bad-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/CTestCommandLine/no-tests_env_bad-stderr.txt b/Tests/RunCMake/CTestCommandLine/no-tests_env_bad-stderr.txt
new file mode 100644
index 0000000..97187e6
--- /dev/null
+++ b/Tests/RunCMake/CTestCommandLine/no-tests_env_bad-stderr.txt
@@ -0,0 +1 @@
+^Unknown value for CTEST_NO_TESTS_ACTION: 'bad'$
diff --git a/Tests/RunCMake/CTestCommandLine/no-tests_env_bad_with_cli_error-result.txt b/Tests/RunCMake/CTestCommandLine/no-tests_env_bad_with_cli_error-result.txt
new file mode 100644
index 0000000..45a4fb7
--- /dev/null
+++ b/Tests/RunCMake/CTestCommandLine/no-tests_env_bad_with_cli_error-result.txt
@@ -0,0 +1 @@
+8
diff --git a/Tests/RunCMake/CTestCommandLine/no-tests_env_bad_with_cli_error-stderr.txt b/Tests/RunCMake/CTestCommandLine/no-tests_env_bad_with_cli_error-stderr.txt
new file mode 100644
index 0000000..eafba1c
--- /dev/null
+++ b/Tests/RunCMake/CTestCommandLine/no-tests_env_bad_with_cli_error-stderr.txt
@@ -0,0 +1 @@
+No tests were found!!!
diff --git a/Tests/RunCMake/CTestCommandLine/no-tests_env_empty_legacy-stderr.txt b/Tests/RunCMake/CTestCommandLine/no-tests_env_empty_legacy-stderr.txt
new file mode 100644
index 0000000..a7c4b11
--- /dev/null
+++ b/Tests/RunCMake/CTestCommandLine/no-tests_env_empty_legacy-stderr.txt
@@ -0,0 +1 @@
+^No tests were found!!!$
diff --git a/Tests/RunCMake/CTestCommandLine/no-tests_env_error-result.txt b/Tests/RunCMake/CTestCommandLine/no-tests_env_error-result.txt
new file mode 100644
index 0000000..45a4fb7
--- /dev/null
+++ b/Tests/RunCMake/CTestCommandLine/no-tests_env_error-result.txt
@@ -0,0 +1 @@
+8
diff --git a/Tests/RunCMake/CTestCommandLine/no-tests_env_error-stderr.txt b/Tests/RunCMake/CTestCommandLine/no-tests_env_error-stderr.txt
new file mode 100644
index 0000000..eafba1c
--- /dev/null
+++ b/Tests/RunCMake/CTestCommandLine/no-tests_env_error-stderr.txt
@@ -0,0 +1 @@
+No tests were found!!!
diff --git a/Tests/RunCMake/RunCTest.cmake b/Tests/RunCMake/RunCTest.cmake
index 86f5b3a..d46f6ad 100644
--- a/Tests/RunCMake/RunCTest.cmake
+++ b/Tests/RunCMake/RunCTest.cmake
@@ -3,6 +3,7 @@
 # Isolate our ctest runs from external environment.
 unset(ENV{CTEST_PARALLEL_LEVEL})
 unset(ENV{CTEST_OUTPUT_ON_FAILURE})
+unset(ENV{CTEST_NO_TESTS_ACTION})
 
 function(run_ctest CASE_NAME)
   configure_file(${RunCMake_SOURCE_DIR}/test.cmake.in