-
Notifications
You must be signed in to change notification settings - Fork 502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework CMake test discovery #1673
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4b53437
Remove test output "normalization"
thetic 64d8a0f
Move private CMake out of public module path
thetic 07d2776
Organize toolchains
thetic abff03f
Create new CppUTest CMake module
thetic 8de69b7
Silence discovery output
thetic ed954d8
Rewrite discovery script
thetic 1ed5e67
Support multiple calls to cpputest_discover_tests
thetic 73fa9fb
Account for multi-config generators
thetic File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
set(_CPPUTEST_DISCOVERY_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/_CppUTestDiscovery.cmake | ||
CACHE INTERNAL "CppUTest discovery scripts" | ||
) | ||
|
||
option(CPPUTEST_TESTS_DETAILED "Run discovered tests individually") | ||
option(CPPUTEST_JUNIT_REPORT "Output JUnit test reports") | ||
|
||
function(cpputest_discover_tests target) | ||
set(options JUNIT) | ||
set(oneValueArgs DETAILED) | ||
set(multiValueArgs EXTRA_ARGS) | ||
cmake_parse_arguments( | ||
"" | ||
"${options}" | ||
"${oneValueArgs}" | ||
"${multiValueArgs}" | ||
${ARGN} | ||
) | ||
|
||
# Validate arguments. | ||
if(NOT TARGET ${target}) | ||
message(FATAL_ERROR | ||
"Cannot discover tests for target \"${target}\" " | ||
"which is not built by this project." | ||
) | ||
endif() | ||
|
||
get_property(target_type | ||
TARGET ${target} | ||
PROPERTY TYPE | ||
) | ||
if(NOT target_type STREQUAL "EXECUTABLE") | ||
message(FATAL_ERROR | ||
"Cannot discover tests for target \"${target}\" " | ||
"which is not an executable." | ||
) | ||
endif() | ||
|
||
if(NOT DEFINED _EXTRA_ARGS) | ||
set(_EXTRA_ARGS -v) | ||
endif() | ||
|
||
if(NOT DEFINED _DETAILED) | ||
set(_DETAILED ${CPPUTEST_TESTS_DETAILED}) | ||
endif() | ||
|
||
if(_JUNIT OR CPPUTEST_JUNIT_REPORT) | ||
list(APPEND _EXTRA_ARGS -ojunit) | ||
endif() | ||
|
||
set(CTEST_INCLUDE_FILE "${CMAKE_CURRENT_BINARY_DIR}/${target}_include.cmake") | ||
if(CMAKE_CONFIGURATION_TYPES) | ||
set(CTEST_GENERATED_FILE "${CMAKE_CURRENT_BINARY_DIR}/${target}.$<CONFIG>.cmake") | ||
file(WRITE "${CTEST_INCLUDE_FILE}" | ||
"if(EXISTS \"${CMAKE_CURRENT_BINARY_DIR}/${target}.\${CTEST_CONFIGURATION_TYPE}.cmake\")\n" | ||
" include(\"${CMAKE_CURRENT_BINARY_DIR}/${target}.\${CTEST_CONFIGURATION_TYPE}.cmake\")\n" | ||
"endif()\n" | ||
) | ||
else() | ||
set(CTEST_GENERATED_FILE "${CMAKE_CURRENT_BINARY_DIR}/${target}.cmake") | ||
file(WRITE "${CTEST_INCLUDE_FILE}" | ||
"if(EXISTS \"${CTEST_GENERATED_FILE}\")\n" | ||
" include(\"${CTEST_GENERATED_FILE}\")\n" | ||
"endif()\n" | ||
) | ||
endif() | ||
|
||
add_custom_command( | ||
TARGET ${target} POST_BUILD | ||
BYPRODUCTS "${CTEST_GENERATED_FILE}" | ||
COMMAND | ||
"${CMAKE_COMMAND}" | ||
-D "TESTS_DETAILED:BOOL=${_DETAILED}" | ||
-D "EXECUTABLE=$<TARGET_FILE:${target}>" | ||
-D "EMULATOR=$<TARGET_PROPERTY:${target},CROSSCOMPILING_EMULATOR>" | ||
-D "ARGS=${run_args}" | ||
-D "CTEST_FILE=${CTEST_GENERATED_FILE}" | ||
-P "${_CPPUTEST_DISCOVERY_SCRIPT}" | ||
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" | ||
DEPENDS "${_CPPUTEST_DISCOVERY_SCRIPT}" | ||
VERBATIM | ||
) | ||
|
||
if(${CMAKE_VERSION} VERSION_LESS "3.10") | ||
# We can only set one. | ||
get_property(already_set | ||
DIRECTORY | ||
PROPERTY TEST_INCLUDE_FILE | ||
SET | ||
) | ||
if(${already_set}) | ||
message(FATAL_ERROR | ||
"Cannot discovery multiple tests from the same file" | ||
) | ||
endif() | ||
set_property( | ||
DIRECTORY | ||
PROPERTY TEST_INCLUDE_FILE "${CTEST_INCLUDE_FILE}" | ||
) | ||
else() | ||
set_property( | ||
DIRECTORY APPEND | ||
PROPERTY TEST_INCLUDE_FILES "${CTEST_INCLUDE_FILE}" | ||
) | ||
endif() | ||
|
||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
set(script) | ||
|
||
function(add_command NAME) | ||
set(_args "") | ||
# use ARGV* instead of ARGN, because ARGN splits arrays into multiple arguments | ||
math(EXPR _last_arg ${ARGC}-1) | ||
foreach(_n RANGE 1 ${_last_arg}) | ||
set(_arg "${ARGV${_n}}") | ||
if(_arg MATCHES "[^-./:a-zA-Z0-9_]") | ||
set(_args "${_args} [==[${_arg}]==]") # form a bracket_argument | ||
else() | ||
set(_args "${_args} ${_arg}") | ||
endif() | ||
endforeach() | ||
set(script "${script}${NAME}(${_args})\n" PARENT_SCOPE) | ||
endfunction() | ||
|
||
if(NOT EXISTS "${EXECUTABLE}") | ||
message(FATAL_ERROR | ||
"Executable \"${EXECUTABLE}\" does not exist" | ||
) | ||
endif() | ||
|
||
if(TESTS_DETAILED) | ||
set(discovery_arg "-ln") | ||
set(select_arg "-st") | ||
else() | ||
set(discovery_arg "-lg") | ||
set(select_arg "-sg") | ||
endif() | ||
|
||
execute_process( | ||
COMMAND ${EMULATOR} "${EXECUTABLE}" ${discovery_arg} | ||
OUTPUT_VARIABLE discovered_tests | ||
RESULT_VARIABLE result | ||
ERROR_VARIABLE error | ||
) | ||
if(NOT ${result} EQUAL 0) | ||
message(FATAL_ERROR | ||
"Error executing ${EXECUTABLE}:\n" | ||
"${error}" | ||
) | ||
endif() | ||
separate_arguments(discovered_tests) | ||
foreach(test_name IN LISTS discovered_tests) | ||
add_command( | ||
add_test | ||
"${test_name}" | ||
${EMULATOR} | ||
"${EXECUTABLE}" | ||
${ARGS} | ||
${select_arg} | ||
${test_name} | ||
) | ||
endforeach() | ||
|
||
file(WRITE "${CTEST_FILE}" "${script}") |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose to create a new module rather than refactoring the existing one. Their behavior is not the same. When using
cpputest_buildtime_discover_tests
with a multi-config project, ctest runs tests regardless of whether and which config it is passed. When usingcpputest_discover_tests
with such a project, ctest requires a config to be passed. This is the more "normal" behavior that mimics the nativeadd_test
command.I don't know the workflow for breaking changes, but this should be deleted eventually, ideally prior to the next major release.