Skip to content

Commit

Permalink
add tests of the CMake package and pkgconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
phlptp committed Aug 15, 2023
1 parent 6da4ff5 commit 80356ae
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@ jobs:

- name: Build
run: meson compile -C build-meson

install-test:
name: install tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: true
#- name: Add pkgconfig
# run: sudo apt-get update && sudo apt-get install -y pkgconfig
- name: Configure
run: cmake -S . -B build -DCLI11_INSTALL_PACKAGE_TESTS=ON
- name: Build
run: cmake --build build -j2
- name: install
run: cmake --install build
- name: Run tests
run: ctest --output-on-failure
working-directory: build

cmake-config-ubuntu-2004:
name: CMake config check (Ubuntu 20.04)
Expand Down
95 changes: 95 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,98 @@ if(CMAKE_BUILD_TYPE STREQUAL Coverage)
${CLI11_TESTS}
${CLI11_MULTIONLY_TESTS})
endif()


if(CLI11_INSTALL_PACKAGE_TESTS)
set(CLI11_PACKAGE_SEARCH_LOC)
else()
set(CLI11_PACKAGE_SEARCH_LOC "-DCLI11_DIR=${PROJECT_BINARY_DIR}")
endif()

# tests of the cmake package and pkg-config package
if (CLI11_INSTALL_PACKAGE_TESTS)
if(NOT MSVC)
set(package_test_command --test-command "${CMAKE_CTEST_COMMAND}")
else() # don't try to run the tests on MSVC since that would require copying the dll's and doing
# some other setup that isn't that important to run on all OS
set(package_test_command)
endif()

if(CMAKE_BUILD_TYPE)
set(CLI11_PACKAGE_TEST_BUILD_TYPE ${CMAKE_BUILD_TYPE})
else()
set(CLI11_PACKAGE_TEST_BUILD_TYPE Release)
endif()

file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests")

if(MSVC AND ${CMAKE_VERSION} VERSION_GREATER 3.12.9)
# Tests for other CMake projects including and using CLI11 using find_package
add_test(
NAME find-package-testsA
COMMAND
${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" -A "${CMAKE_GENERATOR_PLATFORM}"
${CLI11_PACKAGE_SEARCH_LOC}
"-DCLI11_VERSION=${CLI11_VERSION}" "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
"${CMAKE_CURRENT_SOURCE_DIR}/find_package_tests"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests"
)
else()
add_test(
NAME find-package-testsA
COMMAND
${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" ${CLI11_PACKAGE_SEARCH_LOC}
"-DCLI11_VERSION=${CLI11_VERSION}" "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
"${CMAKE_CURRENT_SOURCE_DIR}/find_package_tests"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests"
)
endif()

add_test(NAME find-package-testsB
COMMAND ${CMAKE_COMMAND} --build "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests" --config
${CLI11_PACKAGE_TEST_BUILD_TYPE}
)

add_test(NAME find-package-testsC COMMAND ${CMAKE_CTEST_COMMAND} -C
${CLI11_PACKAGE_TEST_BUILD_TYPE}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/find_package_tests"
)
set_property(TEST find-package-testsA PROPERTY LABELS Packaging PackagingFast)
set_property(TEST find-package-testsB PROPERTY LABELS Packaging PackagingFast)
set_property(TEST find-package-testsB PROPERTY DEPENDS find-package-testsA)
set_property(TEST find-package-testsC PROPERTY LABELS Packaging PackagingFast)
set_property(TEST find-package-testsC PROPERTY DEPENDS find-package-testsB)

cmake_dependent_advanced_option(
CLI11_ENABLE_SUBPROJECT_TESTS
"Enable tests of using CLI11 as a subproject, potentially a slow tests" OFF
"CLI11_BUILD_TESTS" OFF
)

if(NOT MSVC)
# Tests for other CMake projects using the package_config files
add_test(
package-config-tests
${CMAKE_CTEST_COMMAND}
-C
${CLI11_PACKAGE_TEST_BUILD_TYPE}
--build-and-test
"${CMAKE_CURRENT_SOURCE_DIR}/package_config_tests"
"${CMAKE_CURRENT_BINARY_DIR}/package_config_tests"
--build-generator
"${CMAKE_GENERATOR}"
--build-generator-platform
"${CMAKE_GENERATOR_PLATFORM}"
--build-options
"-DCLI11_VERSION=${CLI11_VERSION}"
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}"
${package_test_command}
)
set_property(TEST package-config-tests PROPERTY LABELS Packaging)
endif()
endif()
16 changes: 16 additions & 0 deletions tests/find_package_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

cmake_minimum_required(VERSION 3.10...3.26)

project(CLI11-find-package-test)

include(CTest)

# Test the CLI11 CMake package config
find_package(CLI11 2.0 REQUIRED)

# Test the target
add_executable(package-test ../../examples/positional_validation.cpp)
target_link_libraries(package-test CLI11::CLI11)

add_test(NAME package-test1 COMMAND package-test one)
set_property(TEST package-test1 PROPERTY PASS_REGULAR_EXPRESSION "File 1 = one")
19 changes: 19 additions & 0 deletions tests/package_config_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 3.10...3.26)

project(CLI11-package-config-test)

include(CTest)

# Test the CLI11 CMake package config
find_package(CLI11 2.0 REQUIRED)

find_package(PkgConfig)

pkg_check_modules(CLI11 REQUIRED IMPORTED_TARGET cli11)

# Test the target
add_executable(package-config-test ../../examples/positional_validation.cpp)
target_link_libraries(package-config-test PkgConfig::CLI11)

add_test(NAME package-config-test1 COMMAND package-config-test one)
set_property(TEST package-config-test1 PROPERTY PASS_REGULAR_EXPRESSION "File 1 = one")

0 comments on commit 80356ae

Please sign in to comment.