GoogleTest: Add test for spaces in WORKING_DIRECTORY

This commit is contained in:
Craig Scott
2025-03-28 18:05:40 +11:00
parent 01cb522276
commit b435d16202
4 changed files with 101 additions and 0 deletions

View File

@@ -401,3 +401,30 @@ if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
message(STATUS "Testing PRE_TEST discovery multi configuration...")
run_GoogleTest_discovery_multi_config()
endif()
block(SCOPE_FOR VARIABLES)
# Use a single build tree for a few tests without cleaning.
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/WorkDirWithSpaces-build)
set(RunCMake_TEST_NO_CLEAN 1)
if(NOT RunCMake_GENERATOR_IS_MULTI_CONFIG)
set(RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Debug)
endif()
file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
run_cmake(WorkDirWithSpaces)
run_cmake_command(WorkDirWithSpaces-build
${CMAKE_COMMAND}
--build .
--config Debug
--target test_workdir
)
run_cmake_command(WorkDirWithSpaces-test
${CMAKE_CTEST_COMMAND}
-C Debug
--no-label-summary
--output-on-failure
)
endblock()

View File

@@ -0,0 +1,5 @@
Start 1: WorkDirWithSpaces\.test1_discovered
.* Start 2: WorkDirWithSpaces\.test2_discovered
.* Start 3: WorkDirWithSpaces\.test1
.* Start 4: WorkDirWithSpaces\.test2
.*0 tests failed out of 4

View File

@@ -0,0 +1,29 @@
enable_language(CXX)
include(GoogleTest)
enable_testing()
include(xcode_sign_adhoc.cmake)
add_executable(test_workdir test_workdir.cpp)
xcode_sign_adhoc(test_workdir)
set(workdir "${CMAKE_CURRENT_BINARY_DIR}/with spaces")
file(WRITE ${workdir}/test_list_output.txt [=[
WorkDirWithSpaces.
test1
test2
]=])
file(WRITE ${workdir}/test_output.txt [=[
Some output text for the test.
]=])
gtest_add_tests(
TARGET test_workdir
WORKING_DIRECTORY "${workdir}"
)
gtest_discover_tests(
test_workdir
TEST_SUFFIX _discovered
WORKING_DIRECTORY "${workdir}"
)

View File

@@ -0,0 +1,40 @@
#include <fstream>
#include <iostream>
#include <string>
/* Having this as a comment lets gtest_add_tests() recognize the tests we fake
here without requiring googletest
TEST_F( WorkDirWithSpaces, test1 )
{
}
TEST_F( WorkDirWithSpaces, test2 )
{
}
*/
int main(int argc, char** argv)
{
// Fake gtest behavior so we don't actually require gtest.
// Both listing tests and test execution try to read a file from
// the current directory. If we are not handling spaces in the
// working directory correctly, the files we expect won't exist.
if (argc > 1 && std::string(argv[1]) == "--gtest_list_tests") {
std::ifstream inFile("test_list_output.txt");
if (!inFile) {
std::cout << "ERROR: Failed to open test_list_output.txt" << std::endl;
return 1;
}
std::cout << inFile.rdbuf();
return 0;
}
std::ifstream inFile("test_output.txt");
if (!inFile) {
std::cout << "ERROR: Failed to open test_output.txt" << std::endl;
return 1;
}
std::cout << "Test output read from file to follow:" << std::endl;
std::cout << inFile.rdbuf();
return 0;
}