Skip to content

Commit 1ba52b9

Browse files
committed
Add tests for ament_python_install_package building with cmake
Signed-off-by: R Kent James <[email protected]>
1 parent 17d4da4 commit 1ba52b9

File tree

30 files changed

+833
-0
lines changed

30 files changed

+833
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build/
2+
install/
3+
log/
4+
__pycache__/
5+
*.py[codz]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright 2025 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required(VERSION 3.12)
16+
17+
project(ament_cmake_python_test)
18+
19+
find_package(ament_cmake_core REQUIRED)
20+
21+
# Uncomment to debug CMake variables
22+
#
23+
# get_cmake_property(_variableNames VARIABLES)
24+
# list (SORT _variableNames)
25+
# foreach (_variableName ${_variableNames})
26+
# message(STATUS "${_variableName}=${${_variableName}}")
27+
# endforeach()
28+
29+
if(BUILD_TESTING)
30+
find_package(ament_lint_auto REQUIRED)
31+
ament_lint_auto_find_test_dependencies()
32+
33+
find_package(ament_cmake_pytest REQUIRED)
34+
find_package(ament_cmake_python REQUIRED)
35+
find_package(ament_cmake REQUIRED)
36+
ament_get_python_install_dir(PYTHON_INSTALL_DIR)
37+
38+
set(_pytest_tests
39+
test/test_packages.py
40+
# Add other test files here
41+
)
42+
43+
set(_env SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR})
44+
list(APPEND _env PYTHON_INSTALL_DIR=${PYTHON_INSTALL_DIR})
45+
list(APPEND _env PYEGG_VERSION=py${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR})
46+
foreach(_test_path ${_pytest_tests})
47+
get_filename_component(_test_name ${_test_path} NAME_WE)
48+
ament_add_pytest_test(${_test_name} ${_test_path}
49+
APPEND_ENV PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}
50+
ENV ${_env}
51+
TIMEOUT 360
52+
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
53+
)
54+
endforeach()
55+
56+
find_package(ament_lint_auto REQUIRED)
57+
# the following line skips the linter which checks for copyrights
58+
# comment the line when a copyright and license is added to all source files
59+
set(ament_cmake_copyright_FOUND TRUE)
60+
# the following line skips cpplint (only works in a git repo)
61+
# comment the line when this package is in a git repo and when
62+
# a copyright and license is added to all source files
63+
set(ament_cmake_cpplint_FOUND TRUE)
64+
ament_lint_auto_find_test_dependencies()
65+
66+
endif()
67+
ament_package()

ament_cmake_python_test/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# ament_cmake_python_test
2+
3+
This package exists solely to test the ament_cmake_python package.
4+
5+
Packages to test are prepared as typical ros-style packages (e.g. with packages.xml). Some of these packages
6+
exist in subdirectory `test/packages`, others are generated dynamically. The dynamically generated packages, as well
7+
as the `build` and `install` subdirectories, are generated in a user's pytest temporary directories (which is typically
8+
at /tmp/pytest-of-{username}/pytest-NN)
9+
10+
Testing can be initiated using normal ROS test commmands, that is `colcon test --packages-select ament_cmake_python_test`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
<name>ament_cmake_python_test</name>
5+
<version>0.0.1</version>
6+
<description>Test package for ament_cmake_python.</description>
7+
8+
<maintainer email="[email protected]">Chris Lalancette</maintainer>
9+
<license>Apache License 2.0</license>
10+
11+
<author email="[email protected]">R. Kent James</author>
12+
<author email="[email protected]">Nadav Elkabets</author>
13+
14+
<buildtool_depend>ament_cmake_core</buildtool_depend>
15+
<build_depend>ament_cmake_python</build_depend>
16+
<build_depend>ament_cmake</build_depend>
17+
18+
<test_depend>python3-jinja2</test_depend>
19+
<test_depend>rosidl_default_generators</test_depend>
20+
<test_depend>ament_cmake_pytest</test_depend>
21+
<test_depend>ament_cmake_python</test_depend>
22+
<test_depend>ament_cmake</test_depend>
23+
24+
<test_depend>ament_lint_auto</test_depend>
25+
<test_depend>ament_lint_common</test_depend>
26+
27+
<test_depend>python3-pytest</test_depend>
28+
29+
<buildtool_export_depend>ament_cmake_core</buildtool_export_depend>
30+
31+
<export>
32+
<build_type>ament_cmake</build_type>
33+
</export>
34+
</package>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Copyright 2025 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Options used to generate test packages from a template."""
16+
17+
# Set True to generate both a python package and a msg package for each python package
18+
# This is useful to test that python and msg packages can coexist in the same package
19+
# See issue #514 and PR #587
20+
COMBINE_PYTHON_WITH_MSG = False
21+
22+
# Set True to test ordering sensitivity of python and msg generation in CMakeLists.txt
23+
TEST_ORDERING_SENSITIVITY = False
24+
25+
DEFAULT_OPTIONS = {
26+
'name': 'SET_ME',
27+
'version': None,
28+
'description': 'SET_ME',
29+
'setup_cfg': None,
30+
'destination': None,
31+
'symlink_install': False,
32+
'python_subdir': None,
33+
'has_python': True,
34+
'has_python_before': False, # if both python and msg are in the same package
35+
'has_msg': False,
36+
'scripts_destination': None,
37+
}
38+
39+
TESTS_OPTIONS = [
40+
{
41+
'name': 'python_package',
42+
'description': 'Package with python code',
43+
},
44+
{
45+
'name': 'msg_package',
46+
'description': 'Package with only msg files',
47+
'has_msg': True,
48+
'has_python': False,
49+
},
50+
{
51+
'name': 'python_package_symlink',
52+
'description': 'Package with python code, installed with symlink in build',
53+
'symlink_install': True,
54+
},
55+
{
56+
'name': 'python_package_rename',
57+
'description': 'Package with python code, installed from alternate directory name',
58+
'python_subdir': 'renamed_dir',
59+
},
60+
{
61+
'name': 'python_package_version',
62+
'description': 'Package with python code, specifying version',
63+
'version': '6.7.89',
64+
},
65+
{
66+
'name': 'python_package_setup',
67+
'description': 'Package with python code, using setup.cfg for metadata',
68+
'setup_cfg': 'config/setup.cfg',
69+
},
70+
{
71+
'name': 'python_package_destination',
72+
'description': 'Package with python code, installed to alternate destination',
73+
'destination': 'new_destination',
74+
},
75+
{
76+
'name': 'python_package_with_scripts',
77+
'description': 'Package with python code',
78+
'scripts_destination': 'lib/python_package_with_scripts',
79+
},
80+
]
81+
82+
83+
def get_options():
84+
"""Return a list of options dictionaries for generating test packages."""
85+
tests_options = []
86+
for options in TESTS_OPTIONS:
87+
options = DEFAULT_OPTIONS | options
88+
tests_options.append(options)
89+
90+
if COMBINE_PYTHON_WITH_MSG:
91+
if options['name'].startswith('python_package'):
92+
msg_options = options.copy()
93+
msg_options['name'] += '_with_msg'
94+
msg_options['description'] += 'and msg files'
95+
msg_options['has_msg'] = True
96+
tests_options.append(msg_options)
97+
98+
# This only makes sense when both python and msg are in the same package
99+
if TEST_ORDERING_SENSITIVITY:
100+
if options['name'].startswith('msg_package'):
101+
py_options = options.copy()
102+
py_options['name'] += '_with_python_before'
103+
py_options['description'] += ' and python code before msg'
104+
py_options['has_python_before'] = True
105+
tests_options.append(py_options)
106+
107+
return tests_options

ament_cmake_python_test/test/packages/AMENT_IGNORE

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2025 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required(VERSION 3.12)
16+
17+
project(ament_python_test_package)
18+
19+
find_package(ament_cmake_core REQUIRED)
20+
21+
find_package(ament_cmake_python REQUIRED)
22+
23+
ament_python_install_package(${PROJECT_NAME})
24+
25+
ament_package()

ament_cmake_python_test/test/packages/ament_python_test_package/ament_python_test_package/__init__.py

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2025 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
class Bar:
17+
def __init__() -> None:
18+
pass
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
<name>ament_python_test_package</name>
5+
<version>0.0.0</version>
6+
<description>Minimal python package to test python cmake install</description>
7+
8+
<maintainer email="[email protected]">Nadav Elkabets</maintainer>
9+
10+
<license>Apache License 2.0</license>
11+
12+
<buildtool_depend>ament_cmake_core</buildtool_depend>
13+
<build_depend>ament_cmake_python</build_depend>
14+
15+
<buildtool_export_depend>ament_cmake_core</buildtool_export_depend>
16+
17+
<export>
18+
<build_type>ament_cmake</build_type>
19+
</export>
20+
</package>

0 commit comments

Comments
 (0)