Procházet zdrojové kódy

get_test_property(): Add DIRECTORY option

Kyle Edwards před 2 roky
rodič
revize
2ccb897d86

+ 12 - 1
Help/command/get_test_property.rst

@@ -5,7 +5,7 @@ Get a property of the test.
 
 .. code-block:: cmake
 
-  get_test_property(test property VAR)
+  get_test_property(test property [DIRECTORY <dir>] VAR)
 
 Get a property from the test.  The value of the property is stored in
 the variable ``VAR``.  If the test property is not found, the behavior
@@ -19,6 +19,17 @@ an empty string.
 For a list of standard properties you can type
 :option:`cmake --help-property-list`.
 
+.. versionadded:: 3.28
+  Directory scope can be overridden with the following sub-option:
+
+  ``DIRECTORY <dir>``
+    The test property will be read from the ``<dir>`` directory's
+    scope.  CMake must already know about that source directory, either by
+    having added it through a call to :command:`add_subdirectory` or ``<dir>``
+    being the top level source directory.  Relative paths are treated as
+    relative to the current source directory. ``<dir>`` may reference a binary
+    directory.
+
 See Also
 ^^^^^^^^
 

+ 3 - 0
Help/release/dev/test-properties-directory.rst

@@ -10,3 +10,6 @@ test-properties-directory
 * The ``TEST`` mode of the :command:`get_property` command gained a
   ``DIRECTORY`` sub-option, which allows you to get properties on tests in
   other directories.
+* The :command:`get_test_property` command gained a ``DIRECTORY``
+  sub-option, which allows you to get properties on tests in other
+  directories.

+ 24 - 3
Source/cmGetTestPropertyCommand.cxx

@@ -4,21 +4,42 @@
 
 #include "cmExecutionStatus.h"
 #include "cmMakefile.h"
+#include "cmSetPropertyCommand.h"
 #include "cmTest.h"
 #include "cmValue.h"
 
 bool cmGetTestPropertyCommand(std::vector<std::string> const& args,
                               cmExecutionStatus& status)
 {
-  if (args.size() < 3) {
+  std::vector<std::string>::size_type args_size = args.size();
+  if (args_size != 3 && args_size != 5) {
     status.SetError("called with incorrect number of arguments");
     return false;
   }
 
+  std::string test_directory;
+  bool test_directory_option_enabled = false;
+
+  int var_arg_index = 2;
+  if (args[2] == "DIRECTORY" && args_size == 5) {
+    var_arg_index = 4;
+    test_directory_option_enabled = true;
+    test_directory = args[3];
+  }
+
+  cmMakefile* test_directory_makefile = &status.GetMakefile();
+  bool file_scopes_handled =
+    SetPropertyCommand::HandleAndValidateTestDirectoryScopes(
+      status, test_directory_option_enabled, test_directory,
+      test_directory_makefile);
+  if (!file_scopes_handled) {
+    return false;
+  }
+
   std::string const& testName = args[0];
-  std::string const& var = args[2];
+  std::string const& var = args[var_arg_index];
   cmMakefile& mf = status.GetMakefile();
-  cmTest* test = mf.GetTest(testName);
+  cmTest* test = test_directory_makefile->GetTest(testName);
   if (test) {
     cmValue prop;
     if (!args[1].empty()) {

+ 8 - 2
Tests/RunCMake/get_property/test_properties-stderr.txt

@@ -1,6 +1,12 @@
-^get_test_property: --><--
+^get_test_property: -->value<--
+get_property: -->value<--
+get_test_property: --><--
 get_property: --><--
 get_test_property: -->value<--
 get_property: -->value<--
 get_test_property: -->NOTFOUND<--
-get_property: --><--$
+get_property: --><--
+get_test_property: -->anotherValue<--
+get_property: -->anotherValue<--
+get_test_property: -->anotherValue<--
+get_property: -->anotherValue<--$

+ 13 - 6
Tests/RunCMake/get_property/test_properties.cmake

@@ -1,7 +1,11 @@
-function (check_test_property test prop)
-  get_test_property("${test}" "${prop}" gtp_val)
+function (check_test_property test prop dir)
+  set(dir_args)
+  if(dir)
+    set(dir_args DIRECTORY ${dir})
+  endif()
+  get_test_property("${test}" "${prop}" ${dir_args} gtp_val)
   get_property(gp_val
-    TEST "${test}"
+    TEST "${test}" ${dir_args}
     PROPERTY "${prop}")
 
   message("get_test_property: -->${gtp_val}<--")
@@ -11,7 +15,10 @@ endfunction ()
 include(CTest)
 add_test(NAME test COMMAND "${CMAKE_COMMAND}" --help)
 set_tests_properties(test PROPERTIES empty "" custom value)
+add_subdirectory(test_properties)
 
-check_test_property(test empty)
-check_test_property(test custom)
-check_test_property(test noexist)
+check_test_property(test empty "")
+check_test_property(test custom "")
+check_test_property(test noexist "")
+check_test_property(test custom test_properties)
+check_test_property(test custom ${CMAKE_BINARY_DIR}/test_properties)

+ 4 - 0
Tests/RunCMake/get_property/test_properties/CMakeLists.txt

@@ -0,0 +1,4 @@
+add_test(NAME test COMMAND "${CMAKE_COMMAND}" --help)
+set_tests_properties(test PROPERTIES empty "" custom anotherValue)
+
+check_test_property(test custom ..)