Skip to content

Commit

Permalink
backport merge ament#381 to humble
Browse files Browse the repository at this point in the history
Signed-off-by: Alaa El Jawad <ejalaa12@gmail.com>
  • Loading branch information
ejalaa12 committed Nov 6, 2023
1 parent 5713799 commit 6e8bf07
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
55 changes: 55 additions & 0 deletions ament_cmake_pytest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,61 @@ project(ament_cmake_pytest NONE)

find_package(ament_cmake_core REQUIRED)

# Even though we theoretically only need this as a test dependency, we
# have to find it *before* the if(BUILD_TESTING) check since ament_cmake_test
# is the package that defines that option.
find_package(ament_cmake_test REQUIRED)

if(BUILD_TESTING)
# The test we are enabling below is to test that the code in ament_cmake_test can handle
# non-unicode prints on the console. Ideally we would put the test in that package,
# but we can't actually use ament_add_test() from within its own package so
# we put it here instead. For similar reasons, we don't use ament_add_pytest_test() below since
# it is defined in *this* package.

get_executable_path(python_interpreter Python3::Interpreter CONFIGURE)

# Check if pytest is available
set(check_pytest_cmd "${python_interpreter}" "-m" "pytest" "--version")
execute_process(
COMMAND ${check_pytest_cmd}
RESULT_VARIABLE res
OUTPUT_VARIABLE output
ERROR_VARIABLE error)
if(NOT res EQUAL 0)
message(WARNING
"The Python module 'pytest' was not found, pytests cannot be run. "
"On Linux, install the 'python3-pytest' package. "
"On other platforms, install 'pytest' using pip.")
return()
endif()

set(result_file "${AMENT_TEST_RESULTS_DIR}/${PROJECT_NAME}/pytest.xunit.xml")
set(cmd
"${python_interpreter}"
"-u" # unbuffered stdout and stderr
"-m" "pytest"
"${CMAKE_CURRENT_SOURCE_DIR}/test"
# store last failed tests
"-o" "cache_dir=${CMAKE_CURRENT_BINARY_DIR}/ament_cmake_pytest/pytest/.cache"
"-s"
# junit arguments
"--junit-xml=${result_file}"
"--junit-prefix=${PROJECT_NAME}"
)
ament_add_test(
pytest
COMMAND ${cmd}
OUTPUT_FILE "${CMAKE_BINARY_DIR}/ament_cmake_pytest/pytest.txt"
RESULT_FILE "${result_file}"
)
set_tests_properties(
pytest
PROPERTIES
LABELS "pytest"
)
endif()

ament_package(
CONFIG_EXTRAS "ament_cmake_pytest-extras.cmake"
)
Expand Down
24 changes: 24 additions & 0 deletions ament_cmake_pytest/test/test_pyunicode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2023 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.

import sys

def test_print_unicode():
'''
Print a non UTF-8 byte sequence in a test to verify stderr parsing.
This value was originally reported in: https://github.com/colcon/colcon-core/issues/468
'''
sys.stderr.buffer.write(b'val: ')
sys.stderr.buffer.write(b'\xff\x00\xa1')
sys.stderr.buffer.write(b'\n')
2 changes: 1 addition & 1 deletion ament_cmake_test/ament_cmake_test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def log(msg, **kwargs):
decoded_line = line.decode(encoding)
except UnicodeDecodeError:
if i == len(encodings) - 1:
raise
decoded_line = line.decode(encoding, errors='replace')
else:
break
print(decoded_line, end='')
Expand Down

0 comments on commit 6e8bf07

Please sign in to comment.