Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport merge #381 to humble #490

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 57 additions & 0 deletions ament_cmake_pytest/CMakeLists.txt
Expand Up @@ -4,6 +4,63 @@ 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")
set(SKIP_TEST_ARG "")
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.")
set(SKIP_TEST_ARG SKIP_TEST)
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}"
${SKIP_TEST_ARG}
)
set_tests_properties(
pytest
PROPERTIES
LABELS "pytest"
)
endif()

ament_package(
CONFIG_EXTRAS "ament_cmake_pytest-extras.cmake"
)
Expand Down
2 changes: 2 additions & 0 deletions ament_cmake_pytest/package.xml
Expand Up @@ -13,6 +13,8 @@
<author>Dirk Thomas</author>

<buildtool_depend>ament_cmake_core</buildtool_depend>
<buildtool_depend>ament_cmake_test</buildtool_depend>
<buildtool_depend>python3-pytest</buildtool_depend>

<buildtool_export_depend>ament_cmake_core</buildtool_export_depend>
<buildtool_export_depend>ament_cmake_test</buildtool_export_depend>
Expand Down
24 changes: 24 additions & 0 deletions ament_cmake_pytest/test/test_pyunicode.py
@@ -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
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