gtest_discover_tests() doesn't re-run discovery if command arguments change
When using gtest_discover_tests()
with DISCOVERY_MODE
set to PRE_TEST
(or equivalently, with CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE
set to PRE_TEST
), the logic written to the ..._include.cmake
file attempts to work out whether it needs to re-run discovery based on timestamps of the test executable and the generated file that lists the tests. However, changing the arguments passed to gtest_discover_tests()
can also change the discovered set of tests without requiring the test executable to be rebuilt. Because the ..._include.cmake
file is written with file(GENERATE)
, its timestamp will only be updated when its contents change. We should therefore also re-run test discovery if the ..._include.cmake
file is newer than its generated ..._tests.cmake
file.
There is an analogous problem with Edit: Turns out there is already a dependency on the generated DISCOVERY_MODE
set to POST_BUILD
, but in that case, the discovery only runs if the test executable is relinked. If the gtest_discover_tests()
command arguments change but nothing makes the test executable out of date, no relinking occurs and therefore the POST_BUILD
rule doesn't run. One would have to force relinking if the command arguments change, which isn't ideal. While it would be possible to set up a dependency to enforce this, that might not be desirable if linking is expensive. It may potentially be better to just document this as a limitation of the POST_BUILD
discovery mode instead and recommend switching to PRE_TEST
anyway (which we can fix without any down side)...._tests.cmake
file, at least with Ninja.
The new TEST_FILTER
option added in CMake 3.22 is very likely to be changed while the user experiments with getting the correct filter for their needs. That increases the likelihood of users now hitting this bug.