Skip to content

Commit

Permalink
Ensure gtest_discover_tests doesn't crash rapids_test_install_relocat…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
robertmaynard committed Oct 1, 2024
1 parent 2413be6 commit a199db3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ execute_process(COMMAND ./${rapids_test_generate_exe_name} OUTPUT_FILE \"\${CTES
if(EXISTS "${_RAPIDS_BUILD_DIR}/CTestTestfile.cmake")
# Support multi-generators by setting the CTest config mode to be equal to the install mode
set(CTEST_CONFIGURATION_TYPE "${CMAKE_INSTALL_CONFIG_NAME}")

# Too support tests added via gtest_discover_tests we
# need to tell GoogleTest we aren't in script mode.
# This stops GoogleTestAddTests from thinking it is being used
# in a POST_BUILD manner and should try and look for an undefined
# executable
include(GoogleTest)
set(CMAKE_SCRIPT_MODE_FILE OFF)

include("${_RAPIDS_BUILD_DIR}/CTestTestfile.cmake")
endif()

Expand Down
3 changes: 2 additions & 1 deletion testing/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ add_cmake_config_test(init-simple.cmake)
set(wrong_component_message "No install component set [wrong_component] can be found")
add_cmake_build_test(install_relocatable-include-in-all.cmake)
add_cmake_build_test(install_relocatable-labels.cmake)
add_cmake_build_test(install_relocatable-complex)
add_cmake_build_test(install_relocatable-no-tests.cmake)
add_cmake_build_test(install_relocatable-simple.cmake)
add_cmake_build_test(install_relocatable-complex)
add_cmake_build_test(install_relocatable-installed-tests-run)
add_cmake_build_test(install_relocatable-installed-tests-run-lazy-msg)
add_cmake_build_test(install_relocatable-with-gtest_discover_tests.cmake)
add_cmake_config_test(install_relocatable-wrong-component.cmake SHOULD_FAIL "${wrong_component_message}")

add_cmake_ctest_test(add-impossible-allocation SHOULD_FAIL "Insufficient resources for test")
Expand Down
79 changes: 79 additions & 0 deletions testing/test/install_relocatable-with-gtest_discover_tests.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#=============================================================================
# Copyright (c) 2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================
include(${rapids-cmake-dir}/cpm/gtest.cmake)
include(${rapids-cmake-dir}/cpm/init.cmake)
include(${rapids-cmake-dir}/test/init.cmake)
include(${rapids-cmake-dir}/test/add.cmake)
include(${rapids-cmake-dir}/test/install_relocatable.cmake)

enable_language(CXX)

rapids_cpm_init()
rapids_cpm_gtest()

enable_testing()
rapids_test_init()

file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/main.cpp"
[=[
#include <gtest/gtest.h>

using Foo = ::testing::Test;

TEST_F(Foo, Bar) { }
TEST_F(Foo, Woo) { }

int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
]=])
add_executable(main ${CMAKE_CURRENT_BINARY_DIR}/main.cpp)

target_link_libraries(main PRIVATE GTest::gtest)


gtest_discover_tests(main DISCOVERY_MODE PRE_TEST)
rapids_test_add(NAME main COMMAND main INSTALL_COMPONENT_SET testing)

rapids_test_install_relocatable(INSTALL_COMPONENT_SET testing
DESTINATION bin/testing)


file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/verify_installed_CTestTestfile.cmake"
"set(installed_test_file \"${CMAKE_CURRENT_BINARY_DIR}/install/bin/testing/CTestTestfile.cmake\")")
file(APPEND "${CMAKE_CURRENT_BINARY_DIR}/verify_installed_CTestTestfile.cmake"
[==[

file(READ "${installed_test_file}" contents)
set(execute_process_match_string [===[execute_process(COMMAND ./generate_ctest_json OUTPUT_FILE "${CTEST_RESOURCE_SPEC_FILE}" COMMAND_ERROR_IS_FATAL ANY)]===])
string(FIND "${contents}" ${execute_process_match_string} is_found)
if(is_found EQUAL -1)
message(FATAL_ERROR "Failed to generate a `execute_process` with escaped CTEST_RESOURCE_SPEC_FILE")
endif()
set(add_test_match_string [===[add_test([=[main]=] "cmake" -Dcommand_to_run=${CMAKE_INSTALL_PREFIX}/bin/testing/main -Dcommand_args= -P=./run_gpu_test.cmake)]===])
string(FIND "${contents}" ${add_test_match_string} is_found)
if(is_found EQUAL -1)
message(FATAL_ERROR "Failed to generate an installed `add_test` for main")
endif()
]==])

add_custom_target(install_testing_component ALL
COMMAND ${CMAKE_COMMAND} --install "${CMAKE_CURRENT_BINARY_DIR}" --component testing --prefix install/
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINARY_DIR}/verify_installed_CTestTestfile.cmake"
)

0 comments on commit a199db3

Please sign in to comment.