Skip to content

Commit

Permalink
Add ament_auto_add_gtest (ament#344)
Browse files Browse the repository at this point in the history
* 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 <josh.whitley@autoware.org>
Co-authored-by: Joshua Whitley <whitleysoftwareservices@gmail.com>
  • Loading branch information
Joshua Whitley and JWhitleyWork authored Oct 13, 2021
1 parent bac07e0 commit 6c83d37
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 2 deletions.
1 change: 1 addition & 0 deletions ament_cmake_auto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.12)
project(ament_cmake_auto NONE)

find_package(ament_cmake REQUIRED)
find_package(ament_cmake_gtest REQUIRED)

ament_package(
CONFIG_EXTRAS "ament_cmake_auto-extras.cmake"
Expand Down
2 changes: 2 additions & 0 deletions ament_cmake_auto/ament_cmake_auto-extras.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
find_package(ament_cmake QUIET REQUIRED)

include("${ament_cmake_auto_DIR}/ament_auto_add_executable.cmake")
include("${ament_cmake_auto_DIR}/ament_auto_add_gtest.cmake")
include("${ament_cmake_auto_DIR}/ament_auto_add_library.cmake")
include("${ament_cmake_auto_DIR}/ament_auto_generate_code.cmake")
include("${ament_cmake_auto_DIR}/ament_auto_find_build_dependencies.cmake")
include("${ament_cmake_auto_DIR}/ament_auto_find_test_dependencies.cmake")
include("${ament_cmake_auto_DIR}/ament_auto_package.cmake")
112 changes: 112 additions & 0 deletions ament_cmake_auto/cmake/ament_auto_add_gtest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Copyright 2021 Whitley Software Services, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Add a gtest with all found test dependencies.
#
# Call add_executable(target ARGN), link it against the gtest libraries
# and all found test dependencies, and then register the executable as a test.
#
# If gtest is not available the specified target is not being created and
# therefore the target existence should be checked before being used.
#
# :param target: the target name which will also be used as the test name
# :type target: string
# :param ARGN: the list of source files
# :type ARGN: list of strings
# :param RUNNER: the path to the test runner script (default:
# see ament_add_test).
# :type RUNNER: string
# :param TIMEOUT: the test timeout in seconds,
# default defined by ``ament_add_test()``
# :type TIMEOUT: integer
# :param WORKING_DIRECTORY: the working directory for invoking the
# executable in, default defined by ``ament_add_test()``
# :type WORKING_DIRECTORY: string
# :param SKIP_LINKING_MAIN_LIBRARIES: if set skip linking against the gtest
# main libraries
# :type SKIP_LINKING_MAIN_LIBRARIES: option
# :param SKIP_TEST: if set mark the test as being skipped
# :type SKIP_TEST: option
# :param ENV: list of env vars to set; listed as ``VAR=value``
# :type ENV: list of strings
# :param APPEND_ENV: list of env vars to append if already set, otherwise set;
# listed as ``VAR=value``
# :type APPEND_ENV: list of strings
# :param APPEND_LIBRARY_DIRS: list of library dirs to append to the appropriate
# OS specific env var, a la LD_LIBRARY_PATH
# :type APPEND_LIBRARY_DIRS: list of strings
#
# @public
#
macro(ament_auto_add_gtest target)
cmake_parse_arguments(_ARGN
"SKIP_LINKING_MAIN_LIBRARIES;SKIP_TEST"
"RUNNER;TIMEOUT;WORKING_DIRECTORY"
"APPEND_ENV;APPEND_LIBRARY_DIRS;ENV"
${ARGN})
if(NOT _ARGN_UNPARSED_ARGUMENTS)
message(FATAL_ERROR
"ament_auto_add_gtest() must be invoked with at least one source file")
endif()

# add executable
set(_argn_executable ${_ARGN_UNPARSED_ARGUMENTS})
if(_ARG_SKIP_LINKING_MAIN_LIBRARIES)
list(APPEND _argn_executable "SKIP_LINKING_MAIN_LIBRARIES")
endif()
ament_add_gtest_executable("${target}" ${_argn_executable})

# add include directory of this package if it exists
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_include_directories("${target}" PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/include")
endif()

# link against other libraries of this package
if(NOT ${PROJECT_NAME}_LIBRARIES STREQUAL "")
target_link_libraries("${target}" ${${PROJECT_NAME}_LIBRARIES})
endif()

# add exported information from found dependencies
ament_target_dependencies(${target}
${${PROJECT_NAME}_FOUND_BUILD_DEPENDS}
${${PROJECT_NAME}_FOUND_TEST_DEPENDS}
)

# add test
set(_argn_test "")
if(_ARG_RUNNER)
list(APPEND _argn_test "RUNNER" "${_ARG_RUNNER}")
endif()
if(_ARG_TIMEOUT)
list(APPEND _argn_test "TIMEOUT" "${_ARG_TIMEOUT}")
endif()
if(_ARG_WORKING_DIRECTORY)
list(APPEND _argn_test "WORKING_DIRECTORY" "${_ARG_WORKING_DIRECTORY}")
endif()
if(_ARG_SKIP_TEST)
list(APPEND _argn_test "SKIP_TEST")
endif()
if(_ARG_ENV)
list(APPEND _argn_test "ENV" ${_ARG_ENV})
endif()
if(_ARG_APPEND_ENV)
list(APPEND _argn_test "APPEND_ENV" ${_ARG_APPEND_ENV})
endif()
if(_ARG_APPEND_LIBRARY_DIRS)
list(APPEND _argn_test "APPEND_LIBRARY_DIRS" ${_ARG_APPEND_LIBRARY_DIRS})
endif()
ament_add_gtest_test("${target}" ${_argn_test})
endmacro()
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ macro(ament_auto_find_build_dependencies)
if(_unknown_packages)
string(REPLACE ";" ", " _unknown_packages_str "${_unknown_packages}")
message(FATAL_ERROR "ament_auto_find_build_dependencies() called with "
"required packages that are not listed as a build/buildtool dependency in "
"the package.xml: ${_unknown_packages_str}")
"required packages that are not listed as a build/buildtool dependency "
"in the package.xml: ${_unknown_packages_str}")
endif()

# try to find_package() all build dependencies
Expand Down
41 changes: 41 additions & 0 deletions ament_cmake_auto/cmake/ament_auto_find_test_dependencies.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2021 Whitley Software Services, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Invoke find_package() for all test dependencies.
#
# All found package names are appended to the
# ``${PROJECT_NAME}_FOUND_TEST_DEPENDS`` variables.
#
# @public
#
macro(ament_auto_find_test_dependencies)
set(_ARGN "${ARGN}")
if(_ARGN)
message(FATAL_ERROR "ament_lint_auto_find_test_dependencies() called with "
"unused arguments: ${_ARGN}")
endif()

if(NOT _AMENT_PACKAGE_NAME)
ament_package_xml()
endif()

# try to find_package() all test dependencies
foreach(_dep ${${PROJECT_NAME}_TEST_DEPENDS})
find_package(${_dep} QUIET)
if(${_dep}_FOUND)
list(APPEND ${PROJECT_NAME}_FOUND_TEST_DEPENDS ${_dep})
endif()
endforeach()
endmacro()
2 changes: 2 additions & 0 deletions ament_cmake_auto/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<author>Dirk Thomas</author>

<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>ament_cmake_gtest</buildtool_depend>
<buildtool_export_depend>ament_cmake</buildtool_export_depend>
<buildtool_export_depend>ament_cmake_gtest</buildtool_export_depend>

<export>
<build_type>ament_cmake</build_type>
Expand Down

0 comments on commit 6c83d37

Please sign in to comment.