Skip to content

Commit

Permalink
Split ament_add_gmock into _executable and _test. (#497)
Browse files Browse the repository at this point in the history
This way we can use one part without the other when
we need to.

Signed-off-by: Chris Lalancette <[email protected]>
  • Loading branch information
clalancette authored Dec 21, 2023
1 parent 4f6f349 commit be887a4
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 61 deletions.
2 changes: 2 additions & 0 deletions ament_cmake_gmock/ament_cmake_gmock-extras.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
84 changes: 25 additions & 59 deletions ament_cmake_gmock/cmake/ament_add_gmock.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$<TARGET_FILE:${target}>")
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()
56 changes: 56 additions & 0 deletions ament_cmake_gmock/cmake/ament_add_gmock_executable.cmake
Original file line number Diff line number Diff line change
@@ -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()
117 changes: 117 additions & 0 deletions ament_cmake_gmock/cmake/ament_add_gmock_test.cmake
Original file line number Diff line number Diff line change
@@ -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 "$<TARGET_FILE:${target}>")
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()
2 changes: 1 addition & 1 deletion ament_cmake_gtest/cmake/ament_add_gtest_executable.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion ament_cmake_gtest/cmake/ament_add_gtest_test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit be887a4

Please sign in to comment.