diff options
author | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2021-12-28 16:43:40 +0100 |
---|---|---|
committer | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2021-12-28 17:57:04 +0100 |
commit | c1ad91505523198490504b3363e6b610f60cfe2c (patch) | |
tree | 7c616ce9a8d9f0ce294feb1f1c3db1c83c996860 /utils/cmake/QtTestAddTests.cmake | |
parent | f53f1fbafab6f994a4ed5f63d884d27c42739d53 (diff) | |
download | rockbox-c1ad91505523198490504b3363e6b610f60cfe2c.tar.gz rockbox-c1ad91505523198490504b3363e6b610f60cfe2c.zip |
cmake: Add QtTest test discovery module.
Allow discivery of the QtTest based unit test.
Taken from https://github.com/ocroquette/cmake-qtest-discovery
Change-Id: I79a54568b2dbc216d35a463b6be36c197203c5b5
Diffstat (limited to 'utils/cmake/QtTestAddTests.cmake')
-rwxr-xr-x | utils/cmake/QtTestAddTests.cmake | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/utils/cmake/QtTestAddTests.cmake b/utils/cmake/QtTestAddTests.cmake new file mode 100755 index 0000000000..1a8db46783 --- /dev/null +++ b/utils/cmake/QtTestAddTests.cmake | |||
@@ -0,0 +1,102 @@ | |||
1 | # Copyright 2020 Olivier Croquette <ocroquette@free.fr> | ||
2 | # | ||
3 | # Permission is hereby granted, free of charge, to any person obtaining | ||
4 | # a copy of this software and associated documentation files (the | ||
5 | # "Software"), to deal in the Software without restriction, including | ||
6 | # without limitation the rights to use, copy, modify, merge, publish, | ||
7 | # distribute, sublicense, and/or sell copies of the Software, and to | ||
8 | # permit persons to whom the Software is furnished to do so, subject to | ||
9 | # the following conditions: | ||
10 | # | ||
11 | # The above copyright notice and this permission notice shall be | ||
12 | # included in all copies or substantial portions of the Software. | ||
13 | # | ||
14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT | ||
15 | # WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO | ||
16 | # THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
17 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
18 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
19 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
20 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
21 | # | ||
22 | # This script is run as POST_BUILD step, added by QtTest.cmake. See there | ||
23 | # for more information. | ||
24 | # | ||
25 | # v1.2 | ||
26 | # | ||
27 | # Latest version is available from GitHub: | ||
28 | # https://github.com/ocroquette/cmake-qtest-discovery | ||
29 | |||
30 | get_filename_component(working_dir ${TEST_EXECUTABLE} DIRECTORY) | ||
31 | |||
32 | execute_process( | ||
33 | COMMAND "${TEST_EXECUTABLE}" -datatags | ||
34 | WORKING_DIR ${working_dir} | ||
35 | OUTPUT_VARIABLE output_variable | ||
36 | TIMEOUT ${TEST_DISCOVERY_TIMEOUT} | ||
37 | ERROR_VARIABLE error_variable | ||
38 | RESULT_VARIABLE result_variable | ||
39 | ) | ||
40 | |||
41 | if(NOT "${result_variable}" EQUAL 0) | ||
42 | string(REPLACE "\n" "\n " output "${output}") | ||
43 | message(FATAL_ERROR | ||
44 | "Error running test executable.\n" | ||
45 | " Path: '${TEST_EXECUTABLE}'\n" | ||
46 | " Result: ${result_variable}\n" | ||
47 | " Output:\n" | ||
48 | " ${output_variable}\n" | ||
49 | " Error:\n" | ||
50 | " ${error_variable}\n" | ||
51 | ) | ||
52 | endif() | ||
53 | |||
54 | macro(get_and_escape_parameters) | ||
55 | set(test_class ${CMAKE_MATCH_1}) | ||
56 | set(test_method ${CMAKE_MATCH_2}) | ||
57 | set(test_dataset ${CMAKE_MATCH_3}) | ||
58 | |||
59 | # Test class and method should be safe, but the dataset name might | ||
60 | # contain problematic characters | ||
61 | set(test_dataset_escaped "${test_dataset}") | ||
62 | string(REPLACE "\\" "\\\\" test_dataset_escaped "${test_dataset_escaped}") | ||
63 | string(REPLACE "\"" "\\\"" test_dataset_escaped "${test_dataset_escaped}") | ||
64 | endmacro() | ||
65 | |||
66 | set(ctest_script_content) | ||
67 | string(REPLACE "\n" ";" output_lines "${output_variable}") | ||
68 | foreach(line ${output_lines}) | ||
69 | set(generated_name) | ||
70 | if(line MATCHES "^([^ ]*) ([^ ]*) (.*)$") | ||
71 | # Line contains a data set name, like in: | ||
72 | # test_qttestdemo myParameterizedTest data set name 1 | ||
73 | # test_qttestdemo myParameterizedTest data set name 2 | ||
74 | get_and_escape_parameters() | ||
75 | set(generated_name "${TEST_PREFIX}${test_class}.${test_method}:${test_dataset_escaped}${TEST_SUFFIX}") | ||
76 | string(APPEND ctest_script_content "add_test(\"${generated_name}\" \"${TEST_EXECUTABLE}\" \"${test_method}:${test_dataset_escaped}\")\n") | ||
77 | elseif(line MATCHES "^([^ ]*) ([^ ]*)$") | ||
78 | # Line doesn't contain a data set name, like in: | ||
79 | # test_qttestdemo myFirstTest | ||
80 | # test_qttestdemo mySecondTest | ||
81 | get_and_escape_parameters() | ||
82 | set(generated_name "${TEST_PREFIX}${test_class}.${test_method}${TEST_SUFFIX}") | ||
83 | string(APPEND ctest_script_content "add_test(\"${generated_name}\" \"${TEST_EXECUTABLE}\" \"${test_method}\")\n") | ||
84 | endif() | ||
85 | if(generated_name) | ||
86 | # Make ctest aware of tests skipped with QSKIP() | ||
87 | # SKIP : test_qttestdemo::mySkippedTest() Example of skipped test | ||
88 | string(APPEND ctest_script_content | ||
89 | "set_tests_properties(\"${generated_name}\" PROPERTIES SKIP_REGULAR_EXPRESSION \"SKIP : \")\n" | ||
90 | ) | ||
91 | # Set other properties | ||
92 | string(APPEND ctest_script_content | ||
93 | "set_tests_properties(\"${generated_name}\" PROPERTIES WORKING_DIRECTORY \"${TEST_WORKING_DIR}\")\n" | ||
94 | ) | ||
95 | string(REPLACE ";" " " external_properties "${TEST_PROPERTIES}") | ||
96 | string(APPEND ctest_script_content | ||
97 | "set_tests_properties(\"${generated_name}\" PROPERTIES ${external_properties})\n" | ||
98 | ) | ||
99 | endif() | ||
100 | endforeach() | ||
101 | |||
102 | file(WRITE "${CTEST_FILE}" "${ctest_script_content}") | ||