diff --git a/CMakeLists.txt b/CMakeLists.txt index cc9e45c..7aa55eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -220,4 +220,6 @@ else (build_errors) ${PROJECT_NAME_LOWER}") endif(build_errors) +include(CTest) + add_subdirectory(src) diff --git a/cmake/run_test.cmake b/cmake/run_test.cmake new file mode 100644 index 0000000..a39fccb --- /dev/null +++ b/cmake/run_test.cmake @@ -0,0 +1,27 @@ +# This script is part of the unit test mechanism for ign-tools. +execute_process(COMMAND ruby ${IGN_SCRIPT} + ${TEST_NAME} + OUTPUT_VARIABLE TEST_OUTPUT + ERROR_VARIABLE TEST_ERROR) + +# Copy failure result everytime we run test so we don't accidentally use +# previous results. +execute_process(COMMAND + ${CMAKE_COMMAND} -E copy + "${TEST_NAME}_fail.xml" + "../test_results/${TEST_NAME}.xml" +) + +if (${TEST_OUTPUT} MATCHES "Test executed" AND NOT + ((${TEST_OUTPUT} MATCHES "Library error") OR + (${TEST_ERROR} MATCHES "Library error"))) + execute_process(COMMAND + ${CMAKE_COMMAND} -E copy + "${TEST_NAME}_pass.xml" + "../test_results/${TEST_NAME}.xml" + ) +else() + message(FATAL_ERROR "Failed test. + Output: ${TEST_OUTPUT} + Error: ${TEST_ERROR}") +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a337664..c35a21c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,8 +28,76 @@ if(WIN32) endif() install (TARGETS backward DESTINATION ${LIB_INSTALL_DIR}) +#=============================================================================== +# Used for the installed version. +if(APPLE) + # On macOS, the full path to the library since DYLD_LIBRARY_PATH may not work. + set(backward_library_name ${CMAKE_INSTALL_FULL_LIBDIR}/$) +else() + set(backward_library_name $) +endif() + # Two steps to create `ign`, First using `configure_file`, to interpolate cmake variables. Then # use `file(GENERATE ...)` to use generator expressions -configure_file(ign.in ${PROJECT_BINARY_DIR}/${executable_name}_before_gen) -file(GENERATE OUTPUT ${PROJECT_BINARY_DIR}/$/${executable_name} INPUT ${PROJECT_BINARY_DIR}/${executable_name}_before_gen ) -install (PROGRAMS ${PROJECT_BINARY_DIR}/$/${executable_name} DESTINATION ${BIN_INSTALL_DIR}) +set(ign_script_configured ${PROJECT_BINARY_DIR}/${executable_name}_before_gen) +set(ign_script_generated ${PROJECT_BINARY_DIR}/$/${executable_name}) + +configure_file(ign.in ${ign_script_configured}) +file(GENERATE OUTPUT ${ign_script_generated} INPUT ${ign_script_configured}) +install (PROGRAMS ${ign_script_generated} DESTINATION ${BIN_INSTALL_DIR}) + +#=============================================================================== +# BEGIN TEST ign command +# Generate the ruby script for internal testing. +if(BUILD_TESTING) + set(backward_library_name $) + set(ign_script_configured_test ${PROJECT_BINARY_DIR}/test/bin/${executable_name}_before_gen) + set(ign_script_generated_test ${PROJECT_BINARY_DIR}/$/test/bin/${executable_name}) + + # Two steps to create `ign`, First using `configure_file`, to interpolate cmake variables. Then + # use `file(GENERATE ...)` to use generator expressions + configure_file(ign.in ${ign_script_configured_test}) + file(GENERATE OUTPUT ${ign_script_generated_test} INPUT ${ign_script_configured_test}) + + set(TEST_NAME UNIT_ign_TEST) + + set(ign_library_path "${PROJECT_SOURCE_DIR}/src/cmd${TEST_NAME}.rb") + + set(yaml_output_dir "${PROJECT_BINARY_DIR}/test/conf" ) + set(yaml_output_configured_test "${yaml_output_dir}/${TEST_NAME}.yaml.configured") + set(yaml_output_generated_test "${yaml_output_dir}/$/${TEST_NAME}.yaml") + configure_file( + "${TEST_NAME}.yaml.in" + "${yaml_output_configured_test}" @ONLY) + + file(GENERATE + OUTPUT "${yaml_output_generated_test}" + INPUT "${yaml_output_configured_test}") + + configure_file( + "${PROJECT_SOURCE_DIR}/tools/junit_pass.xml.in" + "${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}_pass.xml" + @ONLY) + + configure_file( + "${PROJECT_SOURCE_DIR}/tools/junit_fail.xml.in" + "${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}_fail.xml" + @ONLY) + + # This simply executes ign. The output will be tested in the check_* test + add_test( + NAME ${TEST_NAME} + COMMAND ${CMAKE_COMMAND} + -DTEST_NAME=${TEST_NAME} + -DIGN_SCRIPT=${ign_script_generated_test} + -P ${PROJECT_SOURCE_DIR}/cmake/run_test.cmake + ) + + set_tests_properties( + ${TEST_NAME} + PROPERTIES + ENVIRONMENT "IGN_CONFIG_PATH=${yaml_output_dir}/$" + ) +endif() +# END TEST +#============================== diff --git a/src/UNIT_ign_TEST.yaml.in b/src/UNIT_ign_TEST.yaml.in new file mode 100644 index 0000000..8faeab7 --- /dev/null +++ b/src/UNIT_ign_TEST.yaml.in @@ -0,0 +1,9 @@ +--- # test commands +format: 1.0.0 +library_name: @PROJECT_NAME@ +library_version: '' +library_path: @ign_library_path@ +commands: + - UNIT_ign_TEST : Test command +--- + diff --git a/src/cmdUNIT_ign_TEST.rb b/src/cmdUNIT_ign_TEST.rb new file mode 100755 index 0000000..09ceaba --- /dev/null +++ b/src/cmdUNIT_ign_TEST.rb @@ -0,0 +1,30 @@ +#!/usr/bin/ruby + +# Copyright (C) 2021 Open Source Robotics Foundation +# +# 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. + +require 'optparse' + +# Constants. +COMMANDS = { 'tools' => 'Test ign tools.\n\n' }.freeze + +# Test Cmd class +class Cmd + # + # Execute the command + # + def execute(_args) + puts 'Test executed' + end +end diff --git a/src/ign.in b/src/ign.in index a11d245..b9c9136 100755 --- a/src/ign.in +++ b/src/ign.in @@ -282,17 +282,10 @@ if ARGV.include?('--versions') end # Start Backward before loading plugins -backward_library_name = if (/darwin/ =~ RUBY_PLATFORM).nil? - '$' - else - # On macOS, the full path to the library since - # DYLD_LIBRARY_PATH may not work. - '$' - end begin - SharedLibInterface::Importer.dlload backward_library_name + SharedLibInterface::Importer.dlload '@backward_library_name@' rescue SharedLibInterface::DLError - puts "Library error: #{backward_library_name} not found. Improved backtrace"\ + warn 'Library error: @backward_library_name@ not found. Improved backtrace'\ ' generation will be disabled' end diff --git a/tools/junit_fail.xml.in b/tools/junit_fail.xml.in new file mode 100644 index 0000000..18196ab --- /dev/null +++ b/tools/junit_fail.xml.in @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/tools/junit_pass.xml.in b/tools/junit_pass.xml.in new file mode 100644 index 0000000..ee86976 --- /dev/null +++ b/tools/junit_pass.xml.in @@ -0,0 +1,6 @@ + + + + + +