-
Notifications
You must be signed in to change notification settings - Fork 914
/
rostest-extras.cmake.em
140 lines (126 loc) · 5.12 KB
/
rostest-extras.cmake.em
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
find_package(catkin REQUIRED)
_generate_function_if_testing_is_disabled("add_rostest")
include(CMakeParseArguments)
function(add_rostest file)
_warn_if_skip_testing("add_rostest")
@[if DEVELSPACE]@
# bin in develspace
set(ROSTEST_EXE "${PYTHON_EXECUTABLE} @(PROJECT_SOURCE_DIR)/scripts/rostest")
@[else]@
# bin in installspace
set(ROSTEST_EXE "${PYTHON_EXECUTABLE} ${rostest_DIR}/../../../@(CATKIN_GLOBAL_BIN_DESTINATION)/rostest")
@[end if]@
cmake_parse_arguments(_rostest "" "WORKING_DIRECTORY" "ARGS;DEPENDENCIES" ${ARGN})
# Check that the file exists, #1621
set(_file_name _file_name-NOTFOUND)
if(IS_ABSOLUTE ${file})
set(_file_name ${file})
else()
find_file(_file_name ${file}
PATHS ${CMAKE_CURRENT_SOURCE_DIR}
NO_DEFAULT_PATH
NO_CMAKE_FIND_ROOT_PATH) # for cross-compilation. thanks jeremy.
if(NOT _file_name)
message(FATAL_ERROR "Can't find rostest file \"${file}\"")
endif()
endif()
# strip PROJECT_SOURCE_DIR and PROJECT_BINARY_DIR from absolute filename to get unique test name (as rostest does it internally)
set(_testname ${_file_name})
rostest__strip_prefix(_testname "${PROJECT_SOURCE_DIR}/")
rostest__strip_prefix(_testname "${PROJECT_BINARY_DIR}/")
# to support registering the same test with different ARGS
# append the args to the test name
if(_rostest_ARGS)
get_filename_component(_ext ${_testname} EXT)
get_filename_component(_testname ${_testname} NAME_WE)
foreach(arg ${_rostest_ARGS})
string(REPLACE ":=" "_" arg_string "${arg}")
set(_testname "${_testname}__${arg_string}")
endforeach()
set(_testname "${_testname}${_ext}")
endif()
string(REPLACE "/" "_" _testname ${_testname})
get_filename_component(_output_name ${_testname} NAME_WE)
set(_output_name "${_output_name}.xml")
string(REPLACE ";" " " _rostest_ARGS "${_rostest_ARGS}")
set(cmd "${ROSTEST_EXE} --pkgdir=${PROJECT_SOURCE_DIR} --package=${PROJECT_NAME} --results-filename ${_output_name} --results-base-dir \"${CATKIN_TEST_RESULTS_DIR}\" ${_file_name} ${_rostest_ARGS}")
catkin_run_tests_target("rostest" ${_testname} "rostest-${_output_name}" COMMAND ${cmd} WORKING_DIRECTORY ${_rostest_WORKING_DIRECTORY} DEPENDENCIES ${_rostest_DEPENDENCIES})
endfunction()
# This is an internal function, use add_rostest_gtest or
# add_rostest_gmock instead
#
# :param type: "gtest" or "gmock"
# The remaining arguments are the same as for add_rostest_gtest
# and add_rostest_gmock
#
function(_add_rostest_google_test type target launch_file)
if (NOT "${type}" STREQUAL "gtest" AND NOT "${type}" STREQUAL "gmock")
message(FATAL_ERROR
"Invalid use of _add_rostest_google_test function, "
"first argument must be 'gtest' or 'gmock'")
return()
endif()
string(TOUPPER "${type}" type_upper)
if("${ARGN}" STREQUAL "")
message(FATAL_ERROR "add_rostest_${type}() needs at least one file argument to compile a ${type_upper} executable")
endif()
if(${type_upper}_FOUND)
include_directories(${${type_upper}_INCLUDE_DIRS})
add_executable(${target} EXCLUDE_FROM_ALL ${ARGN})
target_link_libraries(${target} ${${type_upper}_LIBRARIES})
if(TARGET tests)
add_dependencies(tests ${target})
endif()
add_rostest(${launch_file} DEPENDENCIES ${target})
endif()
endfunction()
#
# Register the launch file with add_rostest() and compile all
# passed files into a GTest binary.
#
# .. note:: The function does nothing if GTest was not found. The
# target is only compiled when tests are built and linked against
# the GTest libraries.
#
# :param target: target name of the GTest executable
# :type target: string
# :param launch_file: the relative path to the roslaunch file
# :type launch_file: string
# :param ARGN: the files to compile into a GTest executable
# :type ARGN: list of files
#
function(add_rostest_gtest target launch_file)
_add_rostest_google_test("gtest" ${target} ${launch_file} ${ARGN})
endfunction()
#
# Register the launch file with add_rostest() and compile all
# passed files into a GMock binary.
#
# .. note:: The function does nothing if GMock was not found. The
# target is only compiled when tests are built and linked against
# the GMock libraries.
#
# :param target: target name of the GMock executable
# :type target: string
# :param launch_file: the relative path to the roslaunch file
# :type launch_file: string
# :param ARGN: the files to compile into a GMock executable
# :type ARGN: list of files
#
function(add_rostest_gmock target launch_file)
_add_rostest_google_test("gmock" ${target} ${launch_file} ${ARGN})
endfunction()
macro(rostest__strip_prefix var prefix)
string(LENGTH ${prefix} prefix_length)
string(LENGTH ${${var}} var_length)
if(${var_length} GREATER ${prefix_length})
string(SUBSTRING "${${var}}" 0 ${prefix_length} var_prefix)
if("${var_prefix}" STREQUAL "${prefix}")
# passing length -1 does not work for CMake < 2.8.5
# http://public.kitware.com/Bug/view.php?id=10740
string(LENGTH "${${var}}" _rest)
math(EXPR _rest "${_rest} - ${prefix_length}")
string(SUBSTRING "${${var}}" ${prefix_length} ${_rest} ${var})
endif()
endif()
endmacro()