From 64839e393318ca213dea38183aec25f78e32be27 Mon Sep 17 00:00:00 2001 From: "Christopher M. Punches" Date: Sun, 16 Aug 2026 00:29:35 -0400 Subject: [PATCH] Define the tests in tests/CMakeLists.txt The fixture targets, the test binary, and the six test cases live beside the sources they build, leaving the top-level file with enable_testing, the ctest scratch-area write, and the add_subdirectory call. The paths both files need are set once above that call, so the child's output directories and the parent's clean rule refer to the same variables rather than repeating the paths. --- CMakeLists.txt | 79 ++++++++------------------------------------ tests/CMakeLists.txt | 66 ++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 66 deletions(-) create mode 100644 tests/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index a680459..c51fa3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,79 +78,26 @@ set_target_properties(info PROPERTIES add_dependencies(dpm info) # --------------------------------------------------------------------- -# Test fixture modules: one known-good stub, two deliberately broken +# Tests +# +# The test targets and cases are defined in tests/CMakeLists.txt. The +# paths they build into are named here, above that directory, so each is +# written once and the clean rule below refers to the same values. # --------------------------------------------------------------------- enable_testing() +set(DPM_TEST_DIR ${CMAKE_BINARY_DIR}/tests) +set(DPM_TEST_FIXTURE_DIR ${DPM_TEST_DIR}/fixtures) +set(DPM_TEST_RESULTS_DIR ${CMAKE_BINARY_DIR}/test-results) + # CTest's scratch area is named by its BuildDirectory configuration option. # Writing it into DartConfiguration.tcl puts it where ctest reads it on every # invocation — the test target, a bare ctest, and the IDE's test discovery # alike — rather than only the ones the build drives. file(WRITE ${CMAKE_BINARY_DIR}/DartConfiguration.tcl - "BuildDirectory: ${CMAKE_BINARY_DIR}/test-results\n") + "BuildDirectory: ${DPM_TEST_RESULTS_DIR}\n") -set(FIXTURE_MODULE_DIR ${CMAKE_BINARY_DIR}/tests/fixtures) - -foreach(fixture good missing_symbols bad_version) - add_library(fixture_${fixture} MODULE tests/fixtures/src/${fixture}.cpp) - set_target_properties(fixture_${fixture} PROPERTIES - PREFIX "" - SUFFIX ".so" - OUTPUT_NAME ${fixture} - LIBRARY_OUTPUT_DIRECTORY ${FIXTURE_MODULE_DIR} - ) -endforeach() - -# --------------------------------------------------------------------- -# API test binary — drives libdpm-core through its public C API -# --------------------------------------------------------------------- -add_executable(test_core tests/test_core.cpp) - -target_link_libraries(test_core PRIVATE dpm-core) - -target_compile_definitions(test_core PRIVATE - TEST_FIXTURE_MODULES="${FIXTURE_MODULE_DIR}" - TEST_FIXTURE_CONF="${CMAKE_CURRENT_SOURCE_DIR}/tests/fixtures/conf" - DPM_CORE_VERSION_EXPECTED="${PROJECT_VERSION}" -) - -set_target_properties(test_core PROPERTIES - RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tests - BUILD_RPATH "$ORIGIN/../lib" -) - -foreach(fixture good missing_symbols bad_version) - add_dependencies(test_core fixture_${fixture}) -endforeach() - -add_test(NAME core_api COMMAND test_core) - -# --------------------------------------------------------------------- -# CLI end-to-end tests (the dpm binary is the cheapest full-stack client) -# --------------------------------------------------------------------- -add_test(NAME cli_help COMMAND dpm --help) -set_tests_properties(cli_help PROPERTIES PASS_REGULAR_EXPRESSION "Usage: dpm") - -add_test(NAME cli_list - COMMAND dpm -c ${CMAKE_CURRENT_SOURCE_DIR}/tests/fixtures/conf - -m ${CMAKE_BINARY_DIR}/modules --list-modules) -set_tests_properties(cli_list PROPERTIES PASS_REGULAR_EXPRESSION "info") - -add_test(NAME cli_info_version - COMMAND dpm -c ${CMAKE_CURRENT_SOURCE_DIR}/tests/fixtures/conf - -m ${CMAKE_BINARY_DIR}/modules -L INFO info version) -set_tests_properties(cli_info_version PROPERTIES - PASS_REGULAR_EXPRESSION "libdpm-core Version: ${PROJECT_VERSION}") - -add_test(NAME cli_module_not_found - COMMAND dpm -c ${CMAKE_CURRENT_SOURCE_DIR}/tests/fixtures/conf - -m ${CMAKE_BINARY_DIR}/modules nonexistent) -set_tests_properties(cli_module_not_found PROPERTIES WILL_FAIL TRUE) - -add_test(NAME cli_rejects_invalid_module - COMMAND dpm -c ${CMAKE_CURRENT_SOURCE_DIR}/tests/fixtures/conf - -m ${FIXTURE_MODULE_DIR} missing_symbols) -set_tests_properties(cli_rejects_invalid_module PROPERTIES WILL_FAIL TRUE) +add_subdirectory(tests) # --------------------------------------------------------------------- # Code reference (Doxygen) — optional 'docs' target @@ -261,12 +208,12 @@ endif() # the project is reloaded. Removing the build system is what configuring a # fresh directory is for. set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES - ${CMAKE_BINARY_DIR}/test-results + ${DPM_TEST_RESULTS_DIR} ${CMAKE_BINARY_DIR}/Testing ${CMAKE_BINARY_DIR}/bin ${CMAKE_BINARY_DIR}/lib ${CMAKE_BINARY_DIR}/modules - ${CMAKE_BINARY_DIR}/tests + ${DPM_TEST_DIR} ${CMAKE_BINARY_DIR}/docs ) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..dc0d48d --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,66 @@ +# --------------------------------------------------------------------- +# Test fixture modules: one known-good stub, two deliberately broken +# +# The output paths come from the top-level file, which defines them +# before adding this directory so that each is written in one place. +# --------------------------------------------------------------------- +foreach(fixture good missing_symbols bad_version) + add_library(fixture_${fixture} MODULE fixtures/src/${fixture}.cpp) + set_target_properties(fixture_${fixture} PROPERTIES + PREFIX "" + SUFFIX ".so" + OUTPUT_NAME ${fixture} + LIBRARY_OUTPUT_DIRECTORY ${DPM_TEST_FIXTURE_DIR} + ) +endforeach() + +# --------------------------------------------------------------------- +# API test binary — drives libdpm-core.so through its public C API +# --------------------------------------------------------------------- +add_executable(test_core test_core.cpp) + +target_link_libraries(test_core PRIVATE dpm-core) + +target_compile_definitions(test_core PRIVATE + TEST_FIXTURE_MODULES="${DPM_TEST_FIXTURE_DIR}" + TEST_FIXTURE_CONF="${CMAKE_CURRENT_SOURCE_DIR}/fixtures/conf" + DPM_CORE_VERSION_EXPECTED="${PROJECT_VERSION}" +) + +set_target_properties(test_core PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${DPM_TEST_DIR} + BUILD_RPATH "$ORIGIN/../lib" +) + +foreach(fixture good missing_symbols bad_version) + add_dependencies(test_core fixture_${fixture}) +endforeach() + +add_test(NAME core_api COMMAND test_core) + +# --------------------------------------------------------------------- +# CLI end-to-end tests (the dpm binary is the cheapest full-stack client) +# --------------------------------------------------------------------- +add_test(NAME cli_help COMMAND dpm --help) +set_tests_properties(cli_help PROPERTIES PASS_REGULAR_EXPRESSION "Usage: dpm") + +add_test(NAME cli_list + COMMAND dpm -c ${CMAKE_CURRENT_SOURCE_DIR}/fixtures/conf + -m ${CMAKE_BINARY_DIR}/modules --list-modules) +set_tests_properties(cli_list PROPERTIES PASS_REGULAR_EXPRESSION "info") + +add_test(NAME cli_info_version + COMMAND dpm -c ${CMAKE_CURRENT_SOURCE_DIR}/fixtures/conf + -m ${CMAKE_BINARY_DIR}/modules -L INFO info version) +set_tests_properties(cli_info_version PROPERTIES + PASS_REGULAR_EXPRESSION "libdpm-core Version: ${PROJECT_VERSION}") + +add_test(NAME cli_module_not_found + COMMAND dpm -c ${CMAKE_CURRENT_SOURCE_DIR}/fixtures/conf + -m ${CMAKE_BINARY_DIR}/modules nonexistent) +set_tests_properties(cli_module_not_found PROPERTIES WILL_FAIL TRUE) + +add_test(NAME cli_rejects_invalid_module + COMMAND dpm -c ${CMAKE_CURRENT_SOURCE_DIR}/fixtures/conf + -m ${DPM_TEST_FIXTURE_DIR} missing_symbols) +set_tests_properties(cli_rejects_invalid_module PROPERTIES WILL_FAIL TRUE)