Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
CMakeLists.txt 5.37 KiB
#
# This file is a part of the LinkAhead Project.
#
# Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
# Copyright (C) 2021-2024 IndiScale GmbH <info@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

# append all the test cases here (file name without the ".cpp" suffix)
set(test_cases
    test_data_type
    test_configuration
    test_connection
    test_entity
    test_file_transmission
    test_info
    test_issues
    test_list_properties
    test_protobuf
    test_transaction
    test_utility
    test_value
    test_clinkahead
    )

IF(BUILD_ACM)
    list(APPEND test_cases
         test_user
         )
ENDIF()

###################################################
### Set up tests using GoogleTest (GTest)
###################################################

# special linting for tests
set(_CMAKE_CXX_CLANG_TIDY_TEST_CHECKS
  "${_CMAKE_CXX_CLANG_TIDY_CHECKS},-cert-err58-cpp,-cppcoreguidelines-avoid-non-const-global-variables,-cppcoreguidelines-owning-memory,-modernize-use-trailing-return-type,-google-readability-avoid-underscore-in-googletest-name,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers,-cppcoreguidelines-avoid-goto,-hicpp-avoid-goto,-readability-function-cognitive-complexity,-cppcoreguidelines-non-private-member-variables-in-classes,-misc-non-private-member-variables-in-classes,-clang-analyzer-cplusplus.Move,-clang-diagnostic-unused-result"
)

# add special cmake functions for gtest
find_package(GTest CONFIG REQUIRED)
include(GoogleTest)

message(DEBUG "CMAKE_INCLUDE_PATH: ${CMAKE_INCLUDE_PATH}")

# loop over all test cases and add them to the test runner
list(LENGTH test_cases len_test_cases)
math(EXPR len_test_cases "${len_test_cases} - 1")
foreach (i RANGE "${len_test_cases}")
    list(GET test_cases ${i} test_case_name)
    add_executable(${test_case_name} ${test_case_name}.cpp)    
    set(liblinkahead_TEST_SRC "${CMAKE_CURRENT_SOURCE_DIR}/${test_case_name}.cpp
        ${liblinkahead_TEST_SRC}")
    target_link_libraries(${test_case_name}
        PRIVATE GTest::gtest_main linkahead clinkahead GTest::gtest caosdb_grpc ${Boost_LIBRARIES})
    target_include_directories(${test_case_name}
      PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/include ${CMAKE_CURRENT_BINARY_DIR})     
     set_target_properties(${test_case_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY  "${CMAKE_BINARY_DIR}")
    if(_LINTING)
        message(STATUS "linting for tests: ${_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}")
        set_target_properties(${test_case_name}
            PROPERTIES
            CXX_CLANG_TIDY "${_CMAKE_CXX_CLANG_TIDY};${_CMAKE_CXX_CLANG_TIDY_TEST_CHECKS}"
            CXX_INCLUDE_WHAT_YOU_USE "${_CMAKE_CXX_INCLUDE_WHAT_YOU_USE}")
    endif()
    gtest_discover_tests(${test_case_name}
        WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
        PROPERTIES
            LABELS "linkahead-cpplib-unit-tests"
        )
endforeach ()

message(STATUS "CMAKE_BINARY_DIR ${CMAKE_BINARY_DIR}")
message(STATUS "CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}")

# copy test data to build dir
set(TEST_DATA_DIR "${CMAKE_CURRENT_SOURCE_DIR}/test_data")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/linkahead_test_utility.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/linkahead_test_utility.h)




###################################################
### Set up test coverage repor (Gcov + Lcov)
###################################################

include(CodeCoverage)

if (LCOV_PATH)
    message(STATUS "Found LCOV: ${LCOV_PATH}")
    target_link_libraries(linkahead gcov)
    target_link_libraries(clinkahead gcov)
    setup_target_for_coverage_lcov(
        NAME unit_test_coverage
        EXECUTABLE ctest -L linkahead-cpplib-unit-tests
        EXCLUDE "${CMAKE_BINARY_DIR}/*"
        DEPENDENCIES linkahead clinkahead ${test_cases}
        LCOV_ARGS --rc lcov_branch_coverage=1 --no-external
        GENHTML_ARGS --rc lcov_branch_coverage=1
        )
    message(STATUS "Adding COMPILE_FLAGS for coverage: ${COVERAGE_COMPILER_FLAGS}")
    set(TARGET_LINKAHEAD_COMPILE_FLAGS "${TARGET_LINKAHEAD_COMPILE_FLAGS} ${COVERAGE_COMPILER_FLAGS}")
    set(TARGET_CLINKAHEAD_COMPILE_FLAGS "${TARGET_CLINKAHEAD_COMPILE_FLAGS} ${COVERAGE_COMPILER_FLAGS}")
    set(TARGET_LINKAHEAD_COMPILE_FLAGS ${TARGET_LINKAHEAD_COMPILE_FLAGS} PARENT_SCOPE)
    set(TARGET_CLINKAHEAD_COMPILE_FLAGS ${TARGET_CLINKAHEAD_COMPILE_FLAGS} PARENT_SCOPE)
    set_target_properties(linkahead PROPERTIES
        COMPILE_FLAGS "${TARGET_LINKAHEAD_COMPILE_FLAGS}")
    set_target_properties(clinkahead PROPERTIES
        COMPILE_FLAGS "${TARGET_CLINKAHEAD_COMPILE_FLAGS}")
else ()
    if (NOT SKIP_CODE_COVERAGE)
        message(WARNING "Could not generate code coverage report. Please install lcov.")
        add_custom_target(unit_test_coverage ctest -L linkahead-cpplib-unit-tests
            DEPENDS linkahead clinkahead ${test_cases}
        )
    endif()
endif ()