Locate the test data independently of the build directory The tests derived the data directory from the location of their executables, so they could only find their data if the build directory was located directly inside the source tree. Pass the absolute data directory to the tests in CMake builds. Change-Id: Ia03f7105df8ddf630c1220d99e484ac2a927941e
diff --git a/internal/ceres/CMakeLists.txt b/internal/ceres/CMakeLists.txt index 6f76924..961e661 100644 --- a/internal/ceres/CMakeLists.txt +++ b/internal/ceres/CMakeLists.txt
@@ -359,7 +359,8 @@ target_include_directories(test_util PUBLIC ${Ceres_SOURCE_DIR}/internal) target_link_libraries (test_util PUBLIC GTest::gmock ceres_static) target_compile_definitions(test_util PUBLIC CERES_HAS_GTEST) - target_compile_definitions(test_util PRIVATE CERES_TEST_SRCDIR_SUFFIX="../../data") + target_compile_definitions(test_util PRIVATE + CERES_TEST_DATA_DIR="${Ceres_SOURCE_DIR}/data") # GoogleTest built as part of Ceres provides the definition only to consumers # of its installed package. Imported targets already provide it. @@ -392,9 +393,6 @@ endif (TARGET SuiteSparse::SPQR) add_test(NAME ${NAME}_test COMMAND ${NAME}_test) - # Override testing::SrcDir to support multi-configuration builds, such as MSVC - # that write binaries into addtional configuration specific subdirectories. - set_tests_properties(${NAME}_test PROPERTIES ENVIRONMENT TEST_SRCDIR=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) endmacro (CERES_TEST) ceres_test(accurate_norm)
diff --git a/internal/ceres/test_util.cc b/internal/ceres/test_util.cc index 1805fcd..5a66104 100644 --- a/internal/ceres/test_util.cc +++ b/internal/ceres/test_util.cc
@@ -139,7 +139,13 @@ } std::string TestFileAbsolutePath(const std::string& filename) { +#ifdef CERES_TEST_DATA_DIR + // The absolute data directory does not depend on the location of the + // build directory or on how the test is run. + return JoinPath(CERES_TEST_DATA_DIR, filename); +#else return JoinPath(::testing::SrcDir() + CERES_TEST_SRCDIR_SUFFIX, filename); +#endif } } // namespace internal