From be887a4b05e98218d46a5e05980d8c23e90ed671 Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Wed, 20 Dec 2023 20:46:29 -0500 Subject: [PATCH] Split ament_add_gmock into _executable and _test. (#497) This way we can use one part without the other when we need to. Signed-off-by: Chris Lalancette --- .../ament_cmake_gmock-extras.cmake | 2 + ament_cmake_gmock/cmake/ament_add_gmock.cmake | 84 ++++--------- .../cmake/ament_add_gmock_executable.cmake | 56 +++++++++ .../cmake/ament_add_gmock_test.cmake | 117 ++++++++++++++++++ .../cmake/ament_add_gtest_executable.cmake | 2 +- .../cmake/ament_add_gtest_test.cmake | 1 - 6 files changed, 201 insertions(+), 61 deletions(-) create mode 100644 ament_cmake_gmock/cmake/ament_add_gmock_executable.cmake create mode 100644 ament_cmake_gmock/cmake/ament_add_gmock_test.cmake diff --git a/ament_cmake_gmock/ament_cmake_gmock-extras.cmake b/ament_cmake_gmock/ament_cmake_gmock-extras.cmake index 631d0591..d6125540 100644 --- a/ament_cmake_gmock/ament_cmake_gmock-extras.cmake +++ b/ament_cmake_gmock/ament_cmake_gmock-extras.cmake @@ -117,4 +117,6 @@ macro(_ament_cmake_gmock_find_gmock) endmacro() include("${ament_cmake_gmock_DIR}/ament_add_gmock.cmake") +include("${ament_cmake_gmock_DIR}/ament_add_gmock_executable.cmake") +include("${ament_cmake_gmock_DIR}/ament_add_gmock_test.cmake") include("${ament_cmake_gmock_DIR}/ament_find_gmock.cmake") diff --git a/ament_cmake_gmock/cmake/ament_add_gmock.cmake b/ament_cmake_gmock/cmake/ament_add_gmock.cmake index 5b56e129..0fcee0b4 100644 --- a/ament_cmake_gmock/cmake/ament_add_gmock.cmake +++ b/ament_cmake_gmock/cmake/ament_add_gmock.cmake @@ -47,79 +47,45 @@ # @public # macro(ament_add_gmock target) - _ament_cmake_gmock_find_gmock() - if(GMOCK_FOUND) - _ament_add_gmock("${target}" ${ARGN}) - endif() -endmacro() - -function(_ament_add_gmock target) - cmake_parse_arguments(ARG + cmake_parse_arguments(_ARG "SKIP_LINKING_MAIN_LIBRARIES;SKIP_TEST" "RUNNER;TIMEOUT;WORKING_DIRECTORY" "APPEND_ENV;APPEND_LIBRARY_DIRS;ENV" ${ARGN}) - if(NOT ARG_UNPARSED_ARGUMENTS) + if(NOT _ARG_UNPARSED_ARGUMENTS) message(FATAL_ERROR "ament_add_gmock() must be invoked with at least one source file") endif() - # should be EXCLUDE_FROM_ALL if it would be possible - # to add this target as a dependency to the "test" target - add_executable("${target}" ${ARG_UNPARSED_ARGUMENTS}) - target_include_directories("${target}" SYSTEM PRIVATE "${GMOCK_INCLUDE_DIRS}") - if(NOT ARG_SKIP_LINKING_MAIN_LIBRARIES) - target_link_libraries("${target}" ${GMOCK_MAIN_LIBRARIES}) + # add executable + set(_argn_executable ${_ARG_UNPARSED_ARGUMENTS}) + if(_ARG_SKIP_LINKING_MAIN_LIBRARIES) + list(APPEND _argn_executable "SKIP_LINKING_MAIN_LIBRARIES") endif() - target_link_libraries("${target}" ${GMOCK_LIBRARIES}) + ament_add_gmock_executable("${target}" ${_argn_executable}) - set(executable "$") - set(result_file "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${target}.gtest.xml") - set(cmd - "${executable}" - "--gtest_output=xml:${result_file}") - if(ARG_ENV) - set(ARG_ENV "ENV" ${ARG_ENV}) + # add test + set(_argn_test "") + if(_ARG_RUNNER) + list(APPEND _argn_test "RUNNER" "${_ARG_RUNNER}") endif() - if(ARG_APPEND_ENV) - set(ARG_APPEND_ENV "APPEND_ENV" ${ARG_APPEND_ENV}) + if(_ARG_TIMEOUT) + list(APPEND _argn_test "TIMEOUT" "${_ARG_TIMEOUT}") endif() - if(ARG_APPEND_LIBRARY_DIRS) - set(ARG_APPEND_LIBRARY_DIRS "APPEND_LIBRARY_DIRS" ${ARG_APPEND_LIBRARY_DIRS}) + if(_ARG_WORKING_DIRECTORY) + list(APPEND _argn_test "WORKING_DIRECTORY" "${_ARG_WORKING_DIRECTORY}") endif() - # Options come out TRUE or FALSE but need to be passed as value or empty - if(ARG_SKIP_TEST) - set(ARG_SKIP_TEST "SKIP_TEST") - else() - set(ARG_SKIP_TEST "") + if(_ARG_SKIP_TEST) + list(APPEND _argn_test "SKIP_TEST") endif() - if(ARG_RUNNER) - set(ARG_RUNNER "RUNNER" ${ARG_RUNNER}) + if(_ARG_ENV) + list(APPEND _argn_test "ENV" ${_ARG_ENV}) endif() - if(ARG_TIMEOUT) - set(ARG_TIMEOUT "TIMEOUT" ${ARG_TIMEOUT}) + if(_ARG_APPEND_ENV) + list(APPEND _argn_test "APPEND_ENV" ${_ARG_APPEND_ENV}) endif() - if(ARG_WORKING_DIRECTORY) - set(ARG_WORKING_DIRECTORY "WORKING_DIRECTORY" "${ARG_WORKING_DIRECTORY}") + if(_ARG_APPEND_LIBRARY_DIRS) + list(APPEND _argn_test "APPEND_LIBRARY_DIRS" ${_ARG_APPEND_LIBRARY_DIRS}) endif() - - ament_add_test( - "${target}" - COMMAND ${cmd} - OUTPUT_FILE "${CMAKE_BINARY_DIR}/ament_cmake_gmock/${target}.txt" - RESULT_FILE "${result_file}" - ${ARG_RUNNER} - ${ARG_ENV} - ${ARG_APPEND_ENV} - ${ARG_APPEND_LIBRARY_DIRS} - ${ARG_SKIP_TEST} - ${ARG_TIMEOUT} - ${ARG_WORKING_DIRECTORY} - ) - set_tests_properties( - "${target}" - PROPERTIES - REQUIRED_FILES "${executable}" - LABELS "gmock" - ) -endfunction() + ament_add_gmock_test("${target}" ${_argn_test}) +endmacro() diff --git a/ament_cmake_gmock/cmake/ament_add_gmock_executable.cmake b/ament_cmake_gmock/cmake/ament_add_gmock_executable.cmake new file mode 100644 index 00000000..a1fcaea4 --- /dev/null +++ b/ament_cmake_gmock/cmake/ament_add_gmock_executable.cmake @@ -0,0 +1,56 @@ +# Copyright 2014-2015 Open Source Robotics Foundation, Inc. +# +# 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 an executable using gmock. +# +# Call add_executable(target ARGN) and link it against the gmock library. +# It does not register the executable as a test. +# +# If gmock is not available the specified target will not be 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 SKIP_LINKING_MAIN_LIBRARIES: if set skip linking against the gmock +# main libraries +# :type SKIP_LINKING_MAIN_LIBRARIES: option +# +# @public +# +macro(ament_add_gmock_executable target) + _ament_cmake_gmock_find_gmock() + if(GMOCK_FOUND) + _ament_add_gmock_executable("${target}" ${ARGN}) + endif() +endmacro() + +function(_ament_add_gmock_executable target) + cmake_parse_arguments(ARG "SKIP_LINKING_MAIN_LIBRARIES" "" "" ${ARGN}) + if(NOT ARG_UNPARSED_ARGUMENTS) + message(FATAL_ERROR + "ament_add_gmock_executable() must be invoked with at least one source file") + endif() + + # should be EXCLUDE_FROM_ALL if it would be possible + # to add this target as a dependency to the "test" target + add_executable("${target}" ${ARG_UNPARSED_ARGUMENTS}) + target_include_directories("${target}" SYSTEM PRIVATE "${GMOCK_INCLUDE_DIRS}") + if(NOT ARG_SKIP_LINKING_MAIN_LIBRARIES) + target_link_libraries("${target}" ${GMOCK_MAIN_LIBRARIES}) + endif() + target_link_libraries("${target}" ${GMOCK_LIBRARIES}) +endfunction() diff --git a/ament_cmake_gmock/cmake/ament_add_gmock_test.cmake b/ament_cmake_gmock/cmake/ament_add_gmock_test.cmake new file mode 100644 index 00000000..46c56cdf --- /dev/null +++ b/ament_cmake_gmock/cmake/ament_add_gmock_test.cmake @@ -0,0 +1,117 @@ +# Copyright 2014-2015 Open Source Robotics Foundation, Inc. +# +# 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 an existing executable using gmock as a test. +# +# Register an executable created with ament_add_gmock_executable() as a test. +# If the specified target does not exist the registration is skipped. +# +# :param target: the target name which will also be used as the test name +# if TEST_NAME is not set +# :type target: string +# :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 TEST_NAME: the name of the test +# :type TEST_NAME: string +# :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 +# +function(ament_add_gmock_test target) + if(NOT TARGET ${target}) + return() + endif() + + cmake_parse_arguments(ARG + "SKIP_TEST" + "RUNNER;TIMEOUT;WORKING_DIRECTORY;TEST_NAME" + "APPEND_ENV;APPEND_LIBRARY_DIRS;ENV" + ${ARGN}) + if(ARG_UNPARSED_ARGUMENTS) + message(FATAL_ERROR + "ament_add_gmock_test() called with unused arguments: ${ARGN}") + endif() + + if(ARG_TEST_NAME) + set(TEST_NAME "${ARG_TEST_NAME}") + else() + set(TEST_NAME "${target}") + endif() + + set(executable "$") + set(result_file "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/${target}.gtest.xml") + set(cmd + "${executable}" + "--gtest_output=xml:${result_file}") + if(ARG_ENV) + set(ARG_ENV "ENV" ${ARG_ENV}) + endif() + if(ARG_APPEND_ENV) + set(ARG_APPEND_ENV "APPEND_ENV" ${ARG_APPEND_ENV}) + endif() + if(ARG_APPEND_LIBRARY_DIRS) + set(ARG_APPEND_LIBRARY_DIRS "APPEND_LIBRARY_DIRS" ${ARG_APPEND_LIBRARY_DIRS}) + endif() + if(ARG_RUNNER) + set(ARG_RUNNER "RUNNER" ${ARG_RUNNER}) + endif() + if(ARG_TIMEOUT) + set(ARG_TIMEOUT "TIMEOUT" ${ARG_TIMEOUT}) + endif() + if(ARG_WORKING_DIRECTORY) + set(ARG_WORKING_DIRECTORY "WORKING_DIRECTORY" "${ARG_WORKING_DIRECTORY}") + endif() + # Options come out TRUE or FALSE but need to be passed as value or empty + if(ARG_SKIP_TEST) + set(ARG_SKIP_TEST "SKIP_TEST") + else() + set(ARG_SKIP_TEST "") + endif() + + ament_add_test( + "${TEST_NAME}" + COMMAND ${cmd} + OUTPUT_FILE "${CMAKE_BINARY_DIR}/ament_cmake_gmock/${TEST_NAME}.txt" + RESULT_FILE "${result_file}" + ${ARG_RUNNER} + ${ARG_SKIP_TEST} + ${ARG_ENV} + ${ARG_APPEND_ENV} + ${ARG_APPEND_LIBRARY_DIRS} + ${ARG_TIMEOUT} + ${ARG_WORKING_DIRECTORY} + ) + set_tests_properties( + "${TEST_NAME}" + PROPERTIES + REQUIRED_FILES "${executable}" + LABELS "gmock" + ) +endfunction() diff --git a/ament_cmake_gtest/cmake/ament_add_gtest_executable.cmake b/ament_cmake_gtest/cmake/ament_add_gtest_executable.cmake index e287b895..644a23b0 100644 --- a/ament_cmake_gtest/cmake/ament_add_gtest_executable.cmake +++ b/ament_cmake_gtest/cmake/ament_add_gtest_executable.cmake @@ -18,7 +18,7 @@ # Call add_executable(target ARGN) and link it against the gtest libraries. # It does not register the executable as a test. # -# If gtest is not available the specified target is not being created and +# If gtest is not available the specified target will 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 diff --git a/ament_cmake_gtest/cmake/ament_add_gtest_test.cmake b/ament_cmake_gtest/cmake/ament_add_gtest_test.cmake index 95d176b6..4a3feae0 100644 --- a/ament_cmake_gtest/cmake/ament_add_gtest_test.cmake +++ b/ament_cmake_gtest/cmake/ament_add_gtest_test.cmake @@ -59,7 +59,6 @@ function(ament_add_gtest_test target) "ament_add_gtest_test() called with unused arguments: ${ARGN}") endif() - if(ARG_TEST_NAME) set(TEST_NAME "${ARG_TEST_NAME}") else()