Skip to content

Commit b45d396

Browse files
authored
Refactor the way to set labels to tests (#59)
Signed-off-by: jparisu <javierparis@eprosima.com>
1 parent f6f46d4 commit b45d396

11 files changed

Lines changed: 115 additions & 21 deletions

File tree

cmake_utils/cmake/test/gtest.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ macro(check_gmock)
7575
endif()
7676
endmacro()
7777

78+
# TODO remove when possible in favor of test_utils.cmake::add_test_label
7879
macro(add_xfail_label LIST_FILE)
7980
if(EXISTS ${LIST_FILE})
8081
file(STRINGS ${LIST_FILE} TEST_LIST)
@@ -98,4 +99,3 @@ macro(add_xtsan_label LIST_FILE)
9899
endforeach()
99100
endif()
100101
endmacro()
101-
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# In order to centralize all the labels for all the tests, these macros has been implemented
16+
# A variable ${PROJECT_NAME}_TEST_LABELS is set with all labels accepted (labels could be repeated)
17+
# A variable ${PROJECT_NAME}_LABEL_TEST_${N} is set for each N and contains the name of the file
18+
# where to read the tests that must have such label set in previous variable.
19+
# The value N in ${PROJECT_NAME}_TEST_LABELS is the label to set in tests listed in file
20+
# ${PROJECT_NAME}_LABEL_TEST_${N}
21+
22+
macro(set_test_label_file FILE_NAME LABEL)
23+
24+
message(STATUS "Set ${LABEL} label to tests in ${FILE_NAME} <${${PROJECT_NAME}_TEST_LABELS}>")
25+
26+
# First check if file exists
27+
if(NOT EXISTS ${FILE_NAME})
28+
message(WARNING "File ${FILE_NAME} does not exist, impossible to set label ${LABEL}")
29+
return()
30+
else()
31+
# Read file
32+
file(STRINGS ${FILE_NAME} TEST_LIST)
33+
endif()
34+
35+
list(LENGTH INTERNAL_TEST_LABELS LABELS_SIZE)
36+
37+
message(STATUS "set_test_label_file <${INTERNAL_TEST_LABELS}> <${LABELS_SIZE}>")
38+
39+
# Append the new label to the list
40+
list(APPEND INTERNAL_TEST_LABELS "${LABEL}")
41+
set(${PROJECT_NAME}_TEST_LABELS "${INTERNAL_TEST_LABELS}")
42+
set(${PROJECT_NAME}_TEST_LABELS "${INTERNAL_TEST_LABELS}" PARENT_SCOPE)
43+
44+
# Store the file content in a variable with the label index in its name
45+
# NOTE: for some reason this must be set twice, once with parent scope and one without
46+
set(${PROJECT_NAME}_LABEL_TEST_${LABELS_SIZE} "${TEST_LIST}")
47+
set(${PROJECT_NAME}_LABEL_TEST_${LABELS_SIZE} "${TEST_LIST}" PARENT_SCOPE)
48+
49+
endmacro()
50+
51+
macro(check_and_add_tests_label TEST_NAME TEST_LABEL_LIST LABEL)
52+
53+
if(${TEST_NAME} IN_LIST "${TEST_LABEL_LIST}")
54+
message(STATUS "Setting label ${LABEL} to test ${TEST_NAME}")
55+
set_property(TEST ${TEST_NAME} PROPERTY LABELS ${LABEL})
56+
endif()
57+
58+
endmacro()
59+
60+
macro(set_test_labels TEST_NAME)
61+
62+
set (CURRENT_INDEX 0)
63+
foreach(TEST_LABEL ${${PROJECT_NAME}_TEST_LABELS})
64+
65+
check_and_add_tests_label(
66+
"${TEST_NAME}"
67+
${PROJECT_NAME}_LABEL_TEST_${CURRENT_INDEX}
68+
"${TEST_LABEL}"
69+
)
70+
71+
MATH(EXPR CURRENT_INDEX "${CURRENT_INDEX} + 1")
72+
73+
endforeach()
74+
75+
endmacro()

cmake_utils/cmake/test/test_target.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,21 @@ function(add_test_executable TEST_EXECUTABLE_NAME TEST_SOURCES TEST_NAME TEST_LI
6868
if(TEST_FRIENDLY_PATH)
6969
set_tests_properties(${TEST_NAME}.${test_name} PROPERTIES ENVIRONMENT "PATH=${TEST_FRIENDLY_PATH}")
7070
endif(TEST_FRIENDLY_PATH)
71+
72+
# Add labels to tests
73+
set_test_labels(${TEST_NAME}.${test_name})
74+
7175
endforeach()
7276
else()
77+
7378
# If no tests are provided, create a single test
7479
message(STATUS "Creating general test ${TEST_NAME}.")
7580
add_test(NAME ${TEST_NAME}
7681
COMMAND ${TEST_EXECUTABLE_NAME})
82+
83+
# Add labels to tests
84+
set_test_labels(${TEST_NAME})
85+
7786
endif( TEST_LIST )
7887

7988
target_compile_definitions(${TEST_EXECUTABLE_NAME}
@@ -87,6 +96,7 @@ function(add_test_executable TEST_EXECUTABLE_NAME TEST_SOURCES TEST_NAME TEST_LI
8796
COPYONLY)
8897
endforeach()
8998

99+
90100
endfunction(add_test_executable)
91101

92102
# Create an executable for a unittest

cpp_utils/test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Include utils for this specific test
16-
include_directories("TestUtils")
15+
# Add subdirectory with labels for tests
16+
add_subdirectory(labels)
1717

1818
# Add subdirectory with tests
1919
add_subdirectory(unittest)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Set list of tests that can fail
16+
set_test_label_file(${CMAKE_CURRENT_SOURCE_DIR}/XFAIL.list "xfail")
17+
# Set list of tests that can fail using TSAN
18+
set_test_label_file(${CMAKE_CURRENT_SOURCE_DIR}/XTSAN.list "xtsan")
19+
20+
# Set list of tests that can fail in windows
21+
if (WIN32)
22+
set_test_label_file(${CMAKE_CURRENT_SOURCE_DIR}/WINDOWS_TEST_XFAIL.list "xfail")
23+
endif()
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SignalEventHandlerTest.receive_signal_trivial
2-
SignalEventHandlerTest.receive_signal
3-
SignalEventHandlerTest.receive_n_signals
41
SignalEventHandlerTest.erase_callback_while_other_handling
2+
SignalEventHandlerTest.receive_n_signals
3+
SignalEventHandlerTest.receive_signal
4+
SignalEventHandlerTest.receive_signal_trivial

cpp_utils/test/labels/XFAIL.list

Whitespace-only changes.

cpp_utils/test/unittest/event/signal/WINDOWS_TEST_XFAIL.list renamed to cpp_utils/test/labels/XTSAN.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
SignalEventHandlerTest.erase_callback_while_other_handling
22
SignalEventHandlerTest.receive_n_signals
33
SignalEventHandlerTest.receive_signal
4+
SignalEventHandlerTest.receive_signal_trivial

cpp_utils/test/unittest/event/signal/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,3 @@ add_unittest_executable(
4040
"${TEST_LIST}"
4141
"${TEST_EXTRA_LIBRARIES}"
4242
)
43-
44-
add_windows_xfail_label(${CMAKE_CURRENT_SOURCE_DIR}/WINDOWS_TEST_XFAIL.list)
45-
46-
# TSAN captures and handles raised signals, thus these tests cannot succeed
47-
add_xtsan_label(${CMAKE_CURRENT_SOURCE_DIR}/TEST_XTSAN.list)

cpp_utils/test/unittest/math/random/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,3 @@ add_unittest_executable(
8383
"${TEST_EXTRA_LIBRARIES}"
8484
"${TEST_NEEDED_SOURCES}"
8585
)
86-
87-
# These tests check random behaviours, thys some of them could eventually fail
88-
# Set flaky tests as xfail
89-
add_xfail_label(${CMAKE_CURRENT_SOURCE_DIR}/TEST_XFAIL.list)

0 commit comments

Comments
 (0)