blob: a554e3ae62a34c0785d56fe8f95aad8e1b4b32b2 [file] [log] [blame]
Jan Kundrátd012b322024-01-12 18:37:05 +01001find_program(SYSREPOCTL sysrepoctl)
2
3function(sysrepo_test)
4 cmake_parse_arguments(TEST "" "NAME;FIXTURE;RESOURCE_LOCK" "COMMAND;LIBRARIES" ${ARGN})
5
6 add_executable(test-${TEST_NAME} ${CMAKE_SOURCE_DIR}/tests/${TEST_NAME}.cpp)
7 target_link_libraries(test-${TEST_NAME} ${TEST_LIBRARIES})
8 target_include_directories(test-${TEST_NAME}
9 PUBLIC
10 ${CMAKE_CURRENT_SOURCE_DIR}
11 PRIVATE
12 ${CMAKE_BINARY_DIR}
13 )
14
15 if(NOT CMAKE_CROSSCOMPILING)
16 if(TEST_COMMAND)
17 add_test(NAME test-${TEST_NAME} COMMAND ${TEST_COMMAND})
18 else()
19 add_test(NAME test-${TEST_NAME} COMMAND test-${TEST_NAME})
20 endif()
21 endif()
22
23 if(TEST_RESOURCE_LOCK)
24 set_tests_properties(test-${TEST_NAME} PROPERTIES RESOURCE_LOCK "${TEST_RESOURCE_LOCK}")
25 endif()
26
27 if(TEST_FIXTURE)
28 set(test_name_preinit sysrepo:preinit:${TEST_NAME})
29 set(test_name_init sysrepo:prep:${TEST_NAME})
30 set(test_name_cleanup sysrepo:clean:${TEST_NAME})
31 set(fixture_name sysrepo:env:${TEST_NAME})
32 set(SYSREPO_REPOSITORY_PATH ${CMAKE_CURRENT_BINARY_DIR}/test_repositories/test_${TEST_NAME})
33 set(SYSREPO_SHM_PREFIX ${CMAKE_PROJECT_NAME}_${TEST_NAME}_)
34 set(test_cleanup_command ${CMAKE_COMMAND}
35 -DTHIS_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
36 -DTEST_NAME=${TEST_NAME}
37 -DSYSREPO_SHM_PREFIX=${SYSREPO_SHM_PREFIX}
38 -P ${PROJECT_SOURCE_DIR}/cmake/SysrepoClean.cmake
39 )
40
41 add_test(NAME ${test_name_preinit} COMMAND ${test_cleanup_command})
42
43 add_test(NAME ${test_name_init}
44 COMMAND ${SYSREPOCTL}
45 --search-dirs ${CMAKE_CURRENT_SOURCE_DIR}/yang:${CMAKE_CURRENT_SOURCE_DIR}/tests/yang
46 ${${TEST_FIXTURE}})
47
48 add_test(NAME ${test_name_cleanup} COMMAND ${test_cleanup_command})
49
50 set_tests_properties(${test_name_preinit} PROPERTIES FIXTURES_SETUP ${fixture_name})
51 set_tests_properties(${test_name_init} PROPERTIES FIXTURES_SETUP ${fixture_name} DEPENDS ${test_name_preinit})
52 set_tests_properties(${test_name_cleanup} PROPERTIES FIXTURES_CLEANUP ${fixture_name})
53 set_tests_properties(test-${TEST_NAME} PROPERTIES FIXTURES_REQUIRED ${fixture_name})
54
55 set_property(TEST test-${TEST_NAME} ${test_name_init} APPEND PROPERTY ENVIRONMENT
56 "SYSREPO_REPOSITORY_PATH=${SYSREPO_REPOSITORY_PATH}"
57 "SYSREPO_SHM_PREFIX=${SYSREPO_SHM_PREFIX}"
58 )
59 endif()
60endfunction()