Skip to content

Commit 215ddb6

Browse files
Joshua WhitleyJWhitleyWork
Joshua Whitley
andauthored
Add ament_auto_add_gtest (#344)
* Add ament_auto_add_gtest * Add ament_auto_find_test_dependencies * Add found build dependencies to ament_auto_add_gtest * Adding dependency on ament_cmake_gtest and fixing lints Signed-off-by: Joshua Whitley <[email protected]> Co-authored-by: Joshua Whitley <[email protected]>
1 parent f69df01 commit 215ddb6

File tree

6 files changed

+160
-2
lines changed

6 files changed

+160
-2
lines changed

ament_cmake_auto/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.12)
33
project(ament_cmake_auto NONE)
44

55
find_package(ament_cmake REQUIRED)
6+
find_package(ament_cmake_gtest REQUIRED)
67

78
ament_package(
89
CONFIG_EXTRAS "ament_cmake_auto-extras.cmake"

ament_cmake_auto/ament_cmake_auto-extras.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
find_package(ament_cmake QUIET REQUIRED)
1818

1919
include("${ament_cmake_auto_DIR}/ament_auto_add_executable.cmake")
20+
include("${ament_cmake_auto_DIR}/ament_auto_add_gtest.cmake")
2021
include("${ament_cmake_auto_DIR}/ament_auto_add_library.cmake")
2122
include("${ament_cmake_auto_DIR}/ament_auto_generate_code.cmake")
2223
include("${ament_cmake_auto_DIR}/ament_auto_find_build_dependencies.cmake")
24+
include("${ament_cmake_auto_DIR}/ament_auto_find_test_dependencies.cmake")
2325
include("${ament_cmake_auto_DIR}/ament_auto_package.cmake")
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Copyright 2021 Whitley Software Services, LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
#
16+
# Add a gtest with all found test dependencies.
17+
#
18+
# Call add_executable(target ARGN), link it against the gtest libraries
19+
# and all found test dependencies, and then register the executable as a test.
20+
#
21+
# If gtest is not available the specified target is not being created and
22+
# therefore the target existence should be checked before being used.
23+
#
24+
# :param target: the target name which will also be used as the test name
25+
# :type target: string
26+
# :param ARGN: the list of source files
27+
# :type ARGN: list of strings
28+
# :param RUNNER: the path to the test runner script (default:
29+
# see ament_add_test).
30+
# :type RUNNER: string
31+
# :param TIMEOUT: the test timeout in seconds,
32+
# default defined by ``ament_add_test()``
33+
# :type TIMEOUT: integer
34+
# :param WORKING_DIRECTORY: the working directory for invoking the
35+
# executable in, default defined by ``ament_add_test()``
36+
# :type WORKING_DIRECTORY: string
37+
# :param SKIP_LINKING_MAIN_LIBRARIES: if set skip linking against the gtest
38+
# main libraries
39+
# :type SKIP_LINKING_MAIN_LIBRARIES: option
40+
# :param SKIP_TEST: if set mark the test as being skipped
41+
# :type SKIP_TEST: option
42+
# :param ENV: list of env vars to set; listed as ``VAR=value``
43+
# :type ENV: list of strings
44+
# :param APPEND_ENV: list of env vars to append if already set, otherwise set;
45+
# listed as ``VAR=value``
46+
# :type APPEND_ENV: list of strings
47+
# :param APPEND_LIBRARY_DIRS: list of library dirs to append to the appropriate
48+
# OS specific env var, a la LD_LIBRARY_PATH
49+
# :type APPEND_LIBRARY_DIRS: list of strings
50+
#
51+
# @public
52+
#
53+
macro(ament_auto_add_gtest target)
54+
cmake_parse_arguments(_ARGN
55+
"SKIP_LINKING_MAIN_LIBRARIES;SKIP_TEST"
56+
"RUNNER;TIMEOUT;WORKING_DIRECTORY"
57+
"APPEND_ENV;APPEND_LIBRARY_DIRS;ENV"
58+
${ARGN})
59+
if(NOT _ARGN_UNPARSED_ARGUMENTS)
60+
message(FATAL_ERROR
61+
"ament_auto_add_gtest() must be invoked with at least one source file")
62+
endif()
63+
64+
# add executable
65+
set(_argn_executable ${_ARGN_UNPARSED_ARGUMENTS})
66+
if(_ARG_SKIP_LINKING_MAIN_LIBRARIES)
67+
list(APPEND _argn_executable "SKIP_LINKING_MAIN_LIBRARIES")
68+
endif()
69+
ament_add_gtest_executable("${target}" ${_argn_executable})
70+
71+
# add include directory of this package if it exists
72+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/include")
73+
target_include_directories("${target}" PUBLIC
74+
"${CMAKE_CURRENT_SOURCE_DIR}/include")
75+
endif()
76+
77+
# link against other libraries of this package
78+
if(NOT ${PROJECT_NAME}_LIBRARIES STREQUAL "")
79+
target_link_libraries("${target}" ${${PROJECT_NAME}_LIBRARIES})
80+
endif()
81+
82+
# add exported information from found dependencies
83+
ament_target_dependencies(${target}
84+
${${PROJECT_NAME}_FOUND_BUILD_DEPENDS}
85+
${${PROJECT_NAME}_FOUND_TEST_DEPENDS}
86+
)
87+
88+
# add test
89+
set(_argn_test "")
90+
if(_ARG_RUNNER)
91+
list(APPEND _argn_test "RUNNER" "${_ARG_RUNNER}")
92+
endif()
93+
if(_ARG_TIMEOUT)
94+
list(APPEND _argn_test "TIMEOUT" "${_ARG_TIMEOUT}")
95+
endif()
96+
if(_ARG_WORKING_DIRECTORY)
97+
list(APPEND _argn_test "WORKING_DIRECTORY" "${_ARG_WORKING_DIRECTORY}")
98+
endif()
99+
if(_ARG_SKIP_TEST)
100+
list(APPEND _argn_test "SKIP_TEST")
101+
endif()
102+
if(_ARG_ENV)
103+
list(APPEND _argn_test "ENV" ${_ARG_ENV})
104+
endif()
105+
if(_ARG_APPEND_ENV)
106+
list(APPEND _argn_test "APPEND_ENV" ${_ARG_APPEND_ENV})
107+
endif()
108+
if(_ARG_APPEND_LIBRARY_DIRS)
109+
list(APPEND _argn_test "APPEND_LIBRARY_DIRS" ${_ARG_APPEND_LIBRARY_DIRS})
110+
endif()
111+
ament_add_gtest_test("${target}" ${_argn_test})
112+
endmacro()

ament_cmake_auto/cmake/ament_auto_find_build_dependencies.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ macro(ament_auto_find_build_dependencies)
5454
if(_unknown_packages)
5555
string(REPLACE ";" ", " _unknown_packages_str "${_unknown_packages}")
5656
message(FATAL_ERROR "ament_auto_find_build_dependencies() called with "
57-
"required packages that are not listed as a build/buildtool dependency in "
58-
"the package.xml: ${_unknown_packages_str}")
57+
"required packages that are not listed as a build/buildtool dependency "
58+
"in the package.xml: ${_unknown_packages_str}")
5959
endif()
6060

6161
# try to find_package() all build dependencies
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2021 Whitley Software Services, LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
#
16+
# Invoke find_package() for all test dependencies.
17+
#
18+
# All found package names are appended to the
19+
# ``${PROJECT_NAME}_FOUND_TEST_DEPENDS`` variables.
20+
#
21+
# @public
22+
#
23+
macro(ament_auto_find_test_dependencies)
24+
set(_ARGN "${ARGN}")
25+
if(_ARGN)
26+
message(FATAL_ERROR "ament_lint_auto_find_test_dependencies() called with "
27+
"unused arguments: ${_ARGN}")
28+
endif()
29+
30+
if(NOT _AMENT_PACKAGE_NAME)
31+
ament_package_xml()
32+
endif()
33+
34+
# try to find_package() all test dependencies
35+
foreach(_dep ${${PROJECT_NAME}_TEST_DEPENDS})
36+
find_package(${_dep} QUIET)
37+
if(${_dep}_FOUND)
38+
list(APPEND ${PROJECT_NAME}_FOUND_TEST_DEPENDS ${_dep})
39+
endif()
40+
endforeach()
41+
endmacro()

ament_cmake_auto/package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
<author>Dirk Thomas</author>
1111

1212
<buildtool_depend>ament_cmake</buildtool_depend>
13+
<buildtool_depend>ament_cmake_gtest</buildtool_depend>
1314
<buildtool_export_depend>ament_cmake</buildtool_export_depend>
15+
<buildtool_export_depend>ament_cmake_gtest</buildtool_export_depend>
1416

1517
<export>
1618
<build_type>ament_cmake</build_type>

0 commit comments

Comments
 (0)