From 756c91ab895aa52f650599bb1a3fc131f1f4b5ef Mon Sep 17 00:00:00 2001 From: Carson Radtke Date: Fri, 5 Dec 2025 13:24:15 -0700 Subject: [PATCH] infra: individual test executables (#1212) * infra: individual test executables We used have tests contained in a single executable. This was fine for testing, but it would be more convient to separate tests into indivudal modules so targeted changes could have targeted tests. This change associates each test file with its own executable. We now have 14 tests for GSL each of which testing a different component. * revert -Wno-reserved-identifier * Update tests/span_tests.cpp thanks copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * be sure to include build type in ctest command * [VS] make sure we are building the correct configuration * restore tests/span_tests.cpp * fix build break after merge conflicts * fix build break after merge conflicts #2 * another try at fixing a build break * fix silly typo. build break pt 4 * Use file globbing for test sources instead of manual list (#1227) * Initial plan * Use file globbing for test sources instead of manual list Replace the manually maintained list of test sources with file(GLOB) to automatically discover all .cpp files in the tests directory, excluding no_exception_ensure_tests.cpp which needs special compilation flags. This approach: - Automatically picks up new test files without CMake updates - Still correctly excludes no_exception_ensure_tests.cpp - Maintains the same test build configuration - Works with both C++14 and C++20 Co-authored-by: carsonRadtke <10507970+carsonRadtke@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: carsonRadtke <10507970+carsonRadtke@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> --- .github/workflows/cmake/action.yml | 22 ++++++++++++++------- .github/workflows/compilers.yml | 7 ++++--- tests/CMakeLists.txt | 31 ++++++++++++------------------ 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/.github/workflows/cmake/action.yml b/.github/workflows/cmake/action.yml index b8794da..d2130cd 100644 --- a/.github/workflows/cmake/action.yml +++ b/.github/workflows/cmake/action.yml @@ -3,7 +3,15 @@ inputs: cmake_preset: required: true type: string - extra_cmake_args: + extra_cmake_build_args: + required: false + type: string + default: '' + extra_cmake_configure_args: + required: false + type: string + default: '' + extra_ctest_args: required: false type: string default: '' @@ -12,14 +20,14 @@ runs: using: composite steps: - name: Configure CMake - run: cmake --preset ${{ inputs.cmake_preset }} -DCI_TESTING:BOOL=ON -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -Werror=dev ${{ inputs.extra_cmake_args }} - shell: bash + run: cmake --preset ${{ inputs.cmake_preset }} ${{ inputs.extra_cmake_configure_args }} -DCI_TESTING:BOOL=ON -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -Werror=dev + shell: ${{ env.RUNNER_OS == 'Windows' && 'pwsh' || 'bash' }} - name: Build (with preset) - run: cmake --build --preset ${{ inputs.cmake_preset }} - shell: bash + run: cmake --build --preset ${{ inputs.cmake_preset }} ${{ inputs.extra_cmake_build_args }} + shell: ${{ env.RUNNER_OS == 'Windows' && 'pwsh' || 'bash' }} - name: Test (with preset) - run: ctest --preset ${{ inputs.cmake_preset }} --output-on-failure --no-compress-output - shell: pwsh + run: ctest --preset ${{ inputs.cmake_preset }} ${{ inputs.extra_ctest_args }} --output-on-failure --no-compress-output + shell: ${{ env.RUNNER_OS == 'Windows' && 'pwsh' || 'bash' }} diff --git a/.github/workflows/compilers.yml b/.github/workflows/compilers.yml index e576ce4..3ab6b5d 100644 --- a/.github/workflows/compilers.yml +++ b/.github/workflows/compilers.yml @@ -75,7 +75,7 @@ jobs: uses: ./.github/workflows/cmake with: cmake_preset: clang-${{ matrix.cxx_version }}-${{ matrix.build_type == 'Debug' && 'debug' || 'release' }} - extra_cmake_args: '-DCMAKE_CXX_FLAGS="-isysroot \"$(xcode-select --print-path)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk\""' + extra_cmake_configure_args: '-DCMAKE_CXX_FLAGS="-isysroot \"$(xcode-select --print-path)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk\""' VisualStudio: strategy: @@ -94,6 +94,7 @@ jobs: uses: ./.github/workflows/cmake with: cmake_preset: msvc-${{ matrix.cxx_version }}-${{ matrix.build_type == 'Debug' && 'debug' || 'release' }} - extra_cmake_args: ${{ matrix.extra_args }} - shell: pwsh + extra_cmake_configure_args: ${{ matrix.extra_args }} + extra_cmake_build_args: --config ${{ matrix.build_type }} + extra_ctest_args: -C ${{ matrix.build_type }} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a8fd30a..d583c24 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -199,29 +199,22 @@ target_include_directories(gsl_tests_config SYSTEM INTERFACE googletest/googletest/include ) -add_executable(gsl_tests - algorithm_tests.cpp - assertion_tests.cpp - at_tests.cpp - byte_tests.cpp - constexpr_notnull_tests.cpp - notnull_tests.cpp - owner_tests.cpp - pointers_tests.cpp - span_compatibility_tests.cpp - span_ext_tests.cpp - span_tests.cpp - strict_notnull_tests.cpp - - utils_tests.cpp -) +# Individually build and register each test source (except no_exception_ensure_tests.cpp) +# no_exception_ensure_tests.cpp is built separately with exceptions disabled +file(GLOB GSL_TEST_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") +list(FILTER GSL_TEST_SOURCES EXCLUDE REGEX "no_exception_ensure_tests\\.cpp$") -target_link_libraries(gsl_tests +foreach(src IN LISTS GSL_TEST_SOURCES) + get_filename_component(test_name "${src}" NAME_WE) + add_executable(${test_name} ${src}) + target_link_libraries(${test_name} Microsoft.GSL::GSL gsl_tests_config ${GTestMain_LIBRARIES} -) -add_test(gsl_tests gsl_tests) + ) + add_test(NAME ${test_name} COMMAND ${test_name}) + set_target_properties(${test_name} PROPERTIES FOLDER "tests") +endforeach() # No exception tests