GoogleTest: Avoid POST_BUILD race condition for gtest_discover_tests()

If multiple calls to `gtest_discover_tests()` are made with different
targets and they use the same working directory, they would previously
have tried to use the same `cmake_test_discovery.json` file for collecting
the set of tests during discovery. Incorporate a hash of the target name
into the file name to ensure that no longer occurs.

Fixes: #27319
This commit is contained in:
Craig Scott
2025-10-26 17:51:01 +11:00
parent 5a7394d3ac
commit 6680df042e

View File

@@ -342,7 +342,17 @@ function(gtest_discover_tests_impl)
set(discovery_extra_args "[==[${discovery_extra_args}]==]")
endif()
set(json_file "${arg_TEST_WORKING_DIR}/cmake_test_discovery.json")
# Avoid a potential race condition for the POST_BUILD case when multiple
# calls are made to gtest_discover_tests() for different targets but the same
# working directory. For PRE_TEST, we're always executing serially during the
# ctest setup phase, so there is no race condition there, but POST_BUILD can
# lead to this code path being run in parallel. Use a hash to avoid potential
# problems with very long target names.
string(SHA256 target_hash "${arg_TEST_TARGET}")
string(SUBSTRING "${target_hash}" 0 10 target_hash)
set(json_file
"${arg_TEST_WORKING_DIR}/cmake_test_discovery_${target_hash}.json"
)
# Remove json file to make sure we don't pick up an outdated one
file(REMOVE "${json_file}")