From 49e9f817deddeb5587090b3362ef0801fcbc29cc Mon Sep 17 00:00:00 2001 From: Wim Colgate Date: Fri, 14 Jun 2024 19:26:43 +0000 Subject: [PATCH] ctest: Display test labels in failure summary In a complex test environment, there may be a desire to add a test property label to indicate context. Here is one such example: set_property(TEST mygoofytest PROPERTY LABELS might-fail-on-config-change) Upon looking at the failed test summary, one can quickly decide whether the failure might be expected or not. 198 - mygoofytest (Failed) might-fail-on-config-change --- Source/CTest/cmCTestTestHandler.cxx | 16 +++++++++++++--- .../CTestCommandLine/FailureLabels-result.txt | 1 + .../CTestCommandLine/FailureLabels-stderr.txt | 1 + .../CTestCommandLine/FailureLabels-stdout.txt | 4 ++++ .../RunCMake/CTestCommandLine/RunCMakeTest.cmake | 16 ++++++++++++++++ 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 Tests/RunCMake/CTestCommandLine/FailureLabels-result.txt create mode 100644 Tests/RunCMake/CTestCommandLine/FailureLabels-stderr.txt create mode 100644 Tests/RunCMake/CTestCommandLine/FailureLabels-stdout.txt diff --git a/Source/CTest/cmCTestTestHandler.cxx b/Source/CTest/cmCTestTestHandler.cxx index 2018b73527..c7875cd99e 100644 --- a/Source/CTest/cmCTestTestHandler.cxx +++ b/Source/CTest/cmCTestTestHandler.cxx @@ -702,13 +702,23 @@ void cmCTestTestHandler::LogFailedTests(const std::vector& failed, if (this->GetTestStatus(ft) == "Not Run") { testColor = cmCTest::Color::YELLOW; } + std::string ft_name_and_status = + cmStrCat(ft.Name, " (", this->GetTestStatus(ft), ")"); + std::string labels; + const cmCTestTestProperties& p = *ft.Properties; + if (!p.Labels.empty()) { + static size_t const maxLen = 50; + size_t const ns = ft_name_and_status.size() >= maxLen + ? 1 + : maxLen - ft_name_and_status.size(); + labels = cmStrCat(std::string(ns, ' '), cmJoin(p.Labels, " ")); + } cmCTestLog( this->CTest, HANDLER_OUTPUT, "\t" << this->CTest->GetColorCode(testColor) << std::setw(3) - << ft.TestCount << " - " << ft.Name << " (" - << this->GetTestStatus(ft) << ")" + << ft.TestCount << " - " << ft_name_and_status << this->CTest->GetColorCode(cmCTest::Color::CLEAR_COLOR) - << std::endl); + << labels << std::endl); } } } diff --git a/Tests/RunCMake/CTestCommandLine/FailureLabels-result.txt b/Tests/RunCMake/CTestCommandLine/FailureLabels-result.txt new file mode 100644 index 0000000000..45a4fb75db --- /dev/null +++ b/Tests/RunCMake/CTestCommandLine/FailureLabels-result.txt @@ -0,0 +1 @@ +8 diff --git a/Tests/RunCMake/CTestCommandLine/FailureLabels-stderr.txt b/Tests/RunCMake/CTestCommandLine/FailureLabels-stderr.txt new file mode 100644 index 0000000000..ba4235defb --- /dev/null +++ b/Tests/RunCMake/CTestCommandLine/FailureLabels-stderr.txt @@ -0,0 +1 @@ +Errors while running CTest diff --git a/Tests/RunCMake/CTestCommandLine/FailureLabels-stdout.txt b/Tests/RunCMake/CTestCommandLine/FailureLabels-stdout.txt new file mode 100644 index 0000000000..3fa9fb8c98 --- /dev/null +++ b/Tests/RunCMake/CTestCommandLine/FailureLabels-stdout.txt @@ -0,0 +1,4 @@ +The following tests FAILED: +[ ]+1 - ShortName \(Failed\) Label1 Label2 +[ ]+2 - LongerName \(Failed\) Label3 +[ ]+3 - Long_Test_Name_That_Is_Over_Fifty_Characters_In_Length \(Failed\) Label4 diff --git a/Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake b/Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake index 190c6c1bb9..c1c26edd40 100644 --- a/Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake +++ b/Tests/RunCMake/CTestCommandLine/RunCMakeTest.cmake @@ -7,6 +7,22 @@ set(ENV{no_proxy} "$ENV{no_proxy},badhostname.invalid") set(RunCMake_TEST_TIMEOUT 60) +block() + set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/FailureLabels) + set(RunCMake_TEST_NO_CLEAN 1) + file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}") + file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}") + file(WRITE "${RunCMake_TEST_BINARY_DIR}/CTestTestfile.cmake" " +add_test(ShortName \"${CMAKE_COMMAND}\" -E false) +set_tests_properties(ShortName PROPERTIES LABELS \"Label1;Label2\") +add_test(LongerName \"${CMAKE_COMMAND}\" -E false) +set_tests_properties(LongerName PROPERTIES LABELS \"Label3\") +add_test(Long_Test_Name_That_Is_Over_Fifty_Characters_In_Length \"${CMAKE_COMMAND}\" -E false) +set_tests_properties(Long_Test_Name_That_Is_Over_Fifty_Characters_In_Length PROPERTIES LABELS \"Label4\") +") + run_cmake_command(FailureLabels ${CMAKE_CTEST_COMMAND}) +endblock() + run_cmake_command(repeat-opt-bad1 ${CMAKE_CTEST_COMMAND} --repeat until-pass )