Skip to content

Commit

Permalink
Merge pull request #162 from eseiler/infra/test_binary
Browse files Browse the repository at this point in the history
[INFRA] Add NEEDLE_TEST_BINARY_DIR
  • Loading branch information
eseiler authored Nov 5, 2024
2 parents 626dbcb + 449afb8 commit e0c6339
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 34 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci_documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ jobs:
run: |
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release \
-Dneedle_TEST=OFF \
-Dneedle_DOCS=ON \
-DNEEDLE_TEST=OFF \
-DNEEDLE_DOCS=ON
Expand Down
54 changes: 40 additions & 14 deletions .github/workflows/ci_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,59 @@ jobs:
with:
path: needle

- name: Remove ccache
run: apt-get purge --yes ccache

- name: Create source package
run: |
mkdir package && cd package
mkdir create_source_package && cd create_source_package
cmake ../needle -DCMAKE_BUILD_TYPE=Release \
-Dneedle_PACKAGE=ON \
-Dneedle_TEST=OFF
-DNEEDLE_PACKAGE=ON \
-DNEEDLE_TEST=OFF
make package_source
mkdir ../unpacked
tar xf needle-*.tar.xz -C ../unpacked --strip-components=1
mkdir ../source_package
tar xf needle-*.tar.xz -C ../source_package --strip-components=1
- name: Install from source package
run: |
mkdir build_from_source_package install && cd build_from_source_package
unshare -r -n cmake ../source_package -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/install \
-DNEEDLE_TEST=OFF
unshare -r -n make install
- name: Check installed executable
run: ${GITHUB_WORKSPACE}/install/bin/needle --help

# Clang builds with libc++, but GTest is built with libstdc++
- name: Install GTest
if: contains(matrix.compiler, 'gcc')
run: apt-get update && apt-get install --yes libgtest-dev

- name: Build tests
if: contains(matrix.compiler, 'gcc')
run: |
mkdir build && cd build
unshare -r -n cmake ../unpacked -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$(pwd)/install \
-Dneedle_TEST=OFF
make install
unshare -r -n cmake ../source_package -DCMAKE_BUILD_TYPE=Release \
-DCPM_USE_LOCAL_PACKAGES=ON \
-DNEEDLE_TEST_BINARY_DIR=${GITHUB_WORKSPACE}/install/bin \
-DNEEDLE_TEST=ON
unshare -r -n make -k tests
test -n $(find . -type f -executable -name "needle") # needle binary should exist
rm bin/needle
test -z $(find . -type f -executable -name "needle") # needle binary should not exist
- name: Run tests
if: contains(matrix.compiler, 'gcc')
working-directory: build
run: ./install/bin/needle --help
run: |
unshare -r -n ctest -j --output-on-failure --test-dir test
test -z $(find . -type f -executable -name "needle") # needle binary should not exist
- name: Upload source package
if: matrix.compiler == 'gcc-14'
if: contains(matrix.compiler, 'gcc')
uses: actions/upload-artifact@v4
with:
name: source-package
path: package/needle-*.tar.xz*
name: needle-source-package
path: create_source_package/needle-*.tar.xz*
retention-days: 1

12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ include (output_directories)
# It can be used when calling CMake: `cmake .. -Dapp-template_TEST=OFF`.
# It is good practice to allow disabling tests. If another project includes your application,
# it might not want to build your tests.
option (${PROJECT_NAME}_TEST "Enable testing for ${PROJECT_NAME}." ON)
option (${PROJECT_NAME}_DOCS "Enable documentation for ${PROJECT_NAME}." OFF)
option (${PROJECT_NAME}_PACKAGE "Enable packaging for ${PROJECT_NAME}." OFF)
option (NEEDLE_TEST "Enable testing for NEEDLE." ON)
option (NEEDLE_DOCS "Enable documentation for NEEDLE." OFF)
option (NEEDLE_PACKAGE "Enable packaging for NEEDLE." OFF)

if (${PROJECT_NAME}_PACKAGE)
if (NEEDLE_PACKAGE)
set (CPM_SOURCE_CACHE "${CMAKE_CURRENT_BINARY_DIR}/vendor")
set (CPM_USE_LOCAL_PACKAGES OFF)
include (package)
Expand All @@ -48,11 +48,11 @@ CPMGetPackage (robin-hood)
# Add the application. This will include `src/CMakeLists.txt`.
add_subdirectory (src)

if (${PROJECT_NAME}_TEST)
if (NEEDLE_TEST)
# Add the tests. This will include `test/CMakeLists.txt`.
add_subdirectory (test EXCLUDE_FROM_ALL)
endif ()

if (${PROJECT_NAME}_DOCS)
if (NEEDLE_DOCS)
add_subdirectory (doc EXCLUDE_FROM_ALL)
endif ()
14 changes: 13 additions & 1 deletion cmake/test/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@ enable_testing ()
file (MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/output)
add_definitions (-DOUTPUTDIR=\"${CMAKE_CURRENT_BINARY_DIR}/output/\")
add_definitions (-DDATADIR=\"${CMAKE_CURRENT_BINARY_DIR}/data/\")
add_definitions (-DBINDIR=\"${CMAKE_BINARY_DIR}/bin/\")
add_definitions (-DAPPNAME=\"${PROJECT_NAME}\")

if (NEEDLE_TEST_BINARY_DIR)
if (NOT EXISTS "${NEEDLE_TEST_BINARY_DIR}")
message (FATAL_ERROR "The directory \"${NEEDLE_TEST_BINARY_DIR}\" (NEEDLE_TEST_BINARY_DIR) does not exist.")
endif ()
if (NOT EXISTS "${NEEDLE_TEST_BINARY_DIR}/${PROJECT_NAME}")
message (FATAL_ERROR "Executable \"${PROJECT_NAME}\" not found in \"${NEEDLE_TEST_BINARY_DIR}\" (NEEDLE_TEST_BINARY_DIR)."
)
endif ()
else ()
set (NEEDLE_TEST_BINARY_DIR "${CMAKE_BINARY_DIR}/bin")
endif ()
add_definitions (-DBINDIR=\"${NEEDLE_TEST_BINARY_DIR}/\")

# Add the test interface library.
if (NOT TARGET ${PROJECT_NAME}_test)
add_library (${PROJECT_NAME}_test INTERFACE)
Expand Down
13 changes: 2 additions & 11 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,8 @@ set_target_properties ("${PROJECT_NAME}" PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${
include (GNUInstallDirs)
install (TARGETS ${PROJECT_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")

# https://cmake.org/cmake/help/latest/module/CMakeDependentOption.html
# We usually want "-Werror" in the CI, but not when developing locally.
# Virtually all CI providers set the environment variable `CI` to some value.
# If `CI` is defined in the environment, we use `-Werror`, otherwise not.
# Caveat: If `CI` is not set, `-Dneedle_WITH_WERROR=ON` has no effect.
# The other way around works. If `CI` is set, `-Dneedle_WITH_WERROR=OFF` is honored.
# Solutions: * `export CI=1` (any value after `=` works, the variable only needs to be not empty)
# * `-DCMAKE_CXX_FLAGS="-Werror"`
include (CMakeDependentOption)
cmake_dependent_option (${PROJECT_NAME}_WITH_WERROR "Report compiler warnings as errors." ON "DEFINED ENV{CI}" OFF)
option (NEEDLE_WITH_WERROR "Report compiler warnings as errors." ON)

if (${PROJECT_NAME}_WITH_WERROR)
if (NEEDLE_WITH_WERROR)
target_compile_options (${PROJECT_NAME}_lib PUBLIC "-Werror")
endif ()

0 comments on commit e0c6339

Please sign in to comment.