From bd38749fd43c1d550f7c45e96a97cddb90c29437 Mon Sep 17 00:00:00 2001 From: Zack Galbreath Date: Wed, 16 Jun 2021 11:38:45 -0400 Subject: ctest: allow test output to add labels Parse test output for .... If found, add this value to the list of labels associated with this test. --- Help/command/ctest_test.rst | 15 ++++++++++++ Help/release/dev/ctest-runtime-labels.rst | 7 ++++++ Source/CTest/cmCTestRunTest.cxx | 17 +++++++++++++ Source/CTest/cmCTestTestHandler.cxx | 2 ++ Source/CTest/cmCTestTestHandler.h | 1 + Tests/RunCMake/ctest_test/RunCMakeTest.cmake | 13 ++++++++++ .../ctest_test/TestExtraLabels-check.cmake | 28 ++++++++++++++++++++++ 7 files changed, 83 insertions(+) create mode 100644 Help/release/dev/ctest-runtime-labels.rst create mode 100644 Tests/RunCMake/ctest_test/TestExtraLabels-check.cmake diff --git a/Help/command/ctest_test.rst b/Help/command/ctest_test.rst index 2153c90..03ec1a4 100644 --- a/Help/command/ctest_test.rst +++ b/Help/command/ctest_test.rst @@ -270,3 +270,18 @@ The following example demonstrates how to specify a custom value for the std::cout << "My Custom Details Value" << std::endl; + +Additional Labels +""""""""""""""""" + +The following example demonstrates how to add additional labels to a test +at runtime. + +.. code-block:: c++ + + std::cout << + "Custom Label 1\n" << + "Custom Label 2" << std::endl; + +Use the :prop_test:`LABELS` test property instead for labels that can be +determined at configure time. diff --git a/Help/release/dev/ctest-runtime-labels.rst b/Help/release/dev/ctest-runtime-labels.rst new file mode 100644 index 0000000..7ce0b64 --- /dev/null +++ b/Help/release/dev/ctest-runtime-labels.rst @@ -0,0 +1,7 @@ +ctest-runtime-labels +-------------------- + +* :manual:`ctest(1)` learned to recognize labels attached to a test at run time. + Previously it was only possible to attach labels to tests at configure time + by using the :prop_test:`LABELS` test property. + See :ref:`Additional Test Measurements` for more information. diff --git a/Source/CTest/cmCTestRunTest.cxx b/Source/CTest/cmCTestRunTest.cxx index a892113..50072c5 100644 --- a/Source/CTest/cmCTestRunTest.cxx +++ b/Source/CTest/cmCTestRunTest.cxx @@ -2,6 +2,7 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmCTestRunTest.h" +#include #include #include // IWYU pragma: keep #include @@ -44,7 +45,9 @@ void cmCTestRunTest::CheckOutput(std::string const& line) // Check for special CTest XML tags in this line of output. // If any are found, this line is excluded from ProcessOutput. if (!line.empty() && line.find("TestHandler->CustomCompletionStatusRegex.find(line)) { + ctest_tag_found = true; this->TestResult.CustomCompletionStatus = this->TestHandler->CustomCompletionStatusRegex.match(1); cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, @@ -52,6 +55,20 @@ void cmCTestRunTest::CheckOutput(std::string const& line) << "Test Details changed to '" << this->TestResult.CustomCompletionStatus << "'" << std::endl); + } else if (this->TestHandler->CustomLabelRegex.find(line)) { + ctest_tag_found = true; + auto label = this->TestHandler->CustomLabelRegex.match(1); + auto& labels = this->TestProperties->Labels; + if (std::find(labels.begin(), labels.end(), label) == labels.end()) { + labels.push_back(label); + std::sort(labels.begin(), labels.end()); + cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, + this->GetIndex() + << ": " + << "Test Label added: '" << label << "'" << std::endl); + } + } + if (ctest_tag_found) { return; } } diff --git a/Source/CTest/cmCTestTestHandler.cxx b/Source/CTest/cmCTestTestHandler.cxx index 730ec0f..aeaf696 100644 --- a/Source/CTest/cmCTestTestHandler.cxx +++ b/Source/CTest/cmCTestTestHandler.cxx @@ -312,6 +312,8 @@ cmCTestTestHandler::cmCTestTestHandler() // regex to detect ... this->CustomCompletionStatusRegex.compile( "(.*)"); + // regex to detect ... + this->CustomLabelRegex.compile("(.*)"); } void cmCTestTestHandler::Initialize() diff --git a/Source/CTest/cmCTestTestHandler.h b/Source/CTest/cmCTestTestHandler.h index bd51738..cc19984 100644 --- a/Source/CTest/cmCTestTestHandler.h +++ b/Source/CTest/cmCTestTestHandler.h @@ -360,6 +360,7 @@ private: size_t TotalNumberOfTests; cmsys::RegularExpression DartStuff; cmsys::RegularExpression CustomCompletionStatusRegex; + cmsys::RegularExpression CustomLabelRegex; std::ostream* LogFile; diff --git a/Tests/RunCMake/ctest_test/RunCMakeTest.cmake b/Tests/RunCMake/ctest_test/RunCMakeTest.cmake index f07a12b..31bc075 100644 --- a/Tests/RunCMake/ctest_test/RunCMakeTest.cmake +++ b/Tests/RunCMake/ctest_test/RunCMakeTest.cmake @@ -181,3 +181,16 @@ add_test( run_ctest(TestCompletionStatus) endfunction() run_completion_status() + +# Verify that test output can add additional labels +function(run_extra_labels) + set(CASE_CMAKELISTS_SUFFIX_CODE [[ +add_test( + NAME custom_labels + COMMAND ${CMAKE_COMMAND} -E + echo before\nlabel2\nlabel1\nlabel3\nlabel2\nafter) +set_tests_properties(custom_labels PROPERTIES LABELS "label1") + ]]) + run_ctest(TestExtraLabels) +endfunction() +run_extra_labels() diff --git a/Tests/RunCMake/ctest_test/TestExtraLabels-check.cmake b/Tests/RunCMake/ctest_test/TestExtraLabels-check.cmake new file mode 100644 index 0000000..beb39de --- /dev/null +++ b/Tests/RunCMake/ctest_test/TestExtraLabels-check.cmake @@ -0,0 +1,28 @@ +file(READ "${RunCMake_TEST_BINARY_DIR}/Testing/TAG" _tag) +string(REGEX REPLACE "^([^\n]*)\n.*$" "\\1" _date "${_tag}") +file(READ "${RunCMake_TEST_BINARY_DIR}/Testing/${_date}/Test.xml" _test_contents) + +# Check labels. +STRING(REGEX MATCHALL [[]] matches "${_test_contents}") +list(LENGTH matches n_matches) +if(NOT n_matches EQUAL 1) + string(APPEND RunCMake_TEST_FAILED "expected 1 match for label1, found ${n_matches}") +endif() +STRING(REGEX MATCHALL [[]] matches "${_test_contents}") +list(LENGTH matches n_matches) +if(NOT n_matches EQUAL 1) + string(APPEND RunCMake_TEST_FAILED "expected 1 match for label2, found ${n_matches}") +endif() +STRING(REGEX MATCHALL [[]] matches "${_test_contents}") +list(LENGTH matches n_matches) +if(NOT n_matches EQUAL 1) + string(APPEND RunCMake_TEST_FAILED "expected 1 match for label3, found ${n_matches}") +endif() + +# Check test output. +if(NOT _test_contents MATCHES "before") + string(APPEND RunCMake_TEST_FAILED "Could not find expected string 'before' in Test.xml") +endif() +if(NOT _test_contents MATCHES "after") + string(APPEND RunCMake_TEST_FAILED "Could not find expected string 'after' in Test.xml") +endif() -- cgit v0.12