Skip to content

Commit

Permalink
cmake BUGFIX normal and static test compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvasko committed May 20, 2024
1 parent dac4bf1 commit ea7bc89
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,10 @@ else()
endif()

# tests
if(ENABLE_TESTS)
enable_testing()
if(ENABLE_TESTS OR BUILD_NETOPEER2_LIB AND NETOPEER2_LIB_TESTS)
if(ENABLE_TEST)
enable_testing()
endif()
add_subdirectory(tests)
endif()

Expand Down
72 changes: 37 additions & 35 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,56 +51,58 @@ if(NP2SRV_URL_CAPAB)
list(APPEND TESTS test_url)
endif()

# build the executables
foreach(test_name IN LISTS TESTS)
add_executable(${test_name} ${TEST_SRC} ${test_name}.c)
target_link_libraries(${test_name} ${CMOCKA_LIBRARIES} ${LIBNETCONF2_LIBRARIES} ${LIBYANG_LIBRARIES} ${SYSREPO_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
set_property(TARGET ${test_name} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endforeach(test_name)

set(TEST_KILL_SERVER_COMMAND ${CMAKE_CURRENT_BINARY_DIR}/scripts/kill_np_server.sh)
set(TEST_CLEAR_STATE_COMMAND rm -rf /dev/shm/_tests_np_*)
if(${CMAKE_VERSION} VERSION_GREATER "3.7")
# tests cleanup fixtures, keep repos with server log files
add_test(NAME tests_kill_server COMMAND ${TEST_KILL_SERVER_COMMAND})
add_test(NAME tests_clear_state COMMAND ${TEST_CLEAR_STATE_COMMAND})
set_tests_properties(tests_kill_server PROPERTIES FIXTURES_CLEANUP tests_cleanup)
set_tests_properties(tests_clear_state PROPERTIES FIXTURES_CLEANUP tests_cleanup DEPENDS tests_kill_server)
endif()
if(ENABLE_TESTS)
# build the executables
foreach(test_name IN LISTS TESTS)
add_executable(${test_name} ${TEST_SRC} ${test_name}.c)
target_link_libraries(${test_name} ${CMOCKA_LIBRARIES} ${LIBNETCONF2_LIBRARIES} ${LIBYANG_LIBRARIES} ${SYSREPO_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
set_property(TARGET ${test_name} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endforeach(test_name)

# add tests with their attributes
foreach(test_name IN LISTS TESTS)
add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}> WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "MALLOC_CHECK_=3")
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "TEST_NAME=${test_name}")
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "SYSREPOCTL_EXECUTABLE=${SYSREPOCTL_EXECUTABLE}")
set(TEST_KILL_SERVER_COMMAND ${CMAKE_CURRENT_BINARY_DIR}/scripts/kill_np_server.sh)
set(TEST_CLEAR_STATE_COMMAND rm -rf /dev/shm/_tests_np_*)
if(${CMAKE_VERSION} VERSION_GREATER "3.7")
set_tests_properties(${test_name} PROPERTIES FIXTURES_REQUIRED tests_cleanup)
# tests cleanup fixtures, keep repos with server log files
add_test(NAME tests_kill_server COMMAND ${TEST_KILL_SERVER_COMMAND})
add_test(NAME tests_clear_state COMMAND ${TEST_CLEAR_STATE_COMMAND})
set_tests_properties(tests_kill_server PROPERTIES FIXTURES_CLEANUP tests_cleanup)
set_tests_properties(tests_clear_state PROPERTIES FIXTURES_CLEANUP tests_cleanup DEPENDS tests_kill_server)
endif()
endforeach()

# valgrind tests
if(ENABLE_VALGRIND_TESTS)
# add tests with their attributes
foreach(test_name IN LISTS TESTS)
add_test(NAME ${test_name}_valgrind COMMAND valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1 ${CMAKE_CURRENT_BINARY_DIR}/${test_name} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set(test_name "${test_name}_valgrind")
add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}> WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "MALLOC_CHECK_=3")
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "TEST_NAME=${test_name}")
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "SYSREPOCTL_EXECUTABLE=${SYSREPOCTL_EXECUTABLE}")
if(${CMAKE_VERSION} VERSION_GREATER "3.7")
set_tests_properties(${test_name} PROPERTIES FIXTURES_REQUIRED tests_cleanup)
endif()
endforeach()

# valgrind tests
if(ENABLE_VALGRIND_TESTS)
foreach(test_name IN LISTS TESTS)
add_test(NAME ${test_name}_valgrind COMMAND valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1 ${CMAKE_CURRENT_BINARY_DIR}/${test_name} WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set(test_name "${test_name}_valgrind")
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "TEST_NAME=${test_name}")
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "SYSREPOCTL_EXECUTABLE=${SYSREPOCTL_EXECUTABLE}")
if(${CMAKE_VERSION} VERSION_GREATER "3.7")
set_tests_properties(${test_name} PROPERTIES FIXTURES_REQUIRED tests_cleanup)
endif()
endforeach()
endif()

# phony target for clearing all sysrepo test data
add_custom_target(test_clean
COMMAND ${TEST_KILL_SERVER_COMMAND}
COMMAND ${TEST_CLEAR_STATE_COMMAND}
COMMAND rm -rf ${CMAKE_CURRENT_BINARY_DIR}/repositories
)
endif()

# propagate vars to parent
set(TESTS ${TESTS} PARENT_SCOPE)
set(TEST_SRC ${TEST_SRC} PARENT_SCOPE)
set(TEST_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE)
set(TEST_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE)

# phony target for clearing all sysrepo test data
add_custom_target(test_clean
COMMAND ${TEST_KILL_SERVER_COMMAND}
COMMAND ${TEST_CLEAR_STATE_COMMAND}
COMMAND rm -rf ${CMAKE_CURRENT_BINARY_DIR}/repositories
)

0 comments on commit ea7bc89

Please sign in to comment.