Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions ros2plugin/test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright 2026 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 pytest


PLUGINS_XML = """\
<library path="pkg_a_plugins">
<class name="pkg_a/foo" type="pkg_a::Foo" base_class_type="base_pkg::Base">
<description>Foo plugin</description>
</class>
<class type="pkg_a::Bar" base_class_type="base_pkg::Base"/>
</library>
"""

OTHER_PLUGINS_XML = """\
<library path="pkg_d_plugins">
<class name="pkg_d/baz" type="pkg_d::Baz" base_class_type="other_pkg::Other"/>
</library>
"""


def _write(path, content=''):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content)


@pytest.fixture
def plugin_prefix(tmp_path, monkeypatch):
"""
Create an install prefix with plugin resources and make it the only ament prefix.

- pkg_a exports two plugins for base_pkg (one of them without a name).
- pkg_b registers a plugin XML that does not exist.
- pkg_c registers a plugin XML that is not valid XML.
- pkg_d exports a plugin for other_pkg, with an extra ';' field in the resource.
- pkg_ghost registers plugins but is not a known package.
"""
prefix = tmp_path / 'install'
index = prefix / 'share' / 'ament_index' / 'resource_index'
for package in ('pkg_a', 'pkg_b', 'pkg_c', 'pkg_d'):
_write(index / 'packages' / package)

base = index / 'base_pkg__pluginlib__plugin'
_write(base / 'pkg_a', 'share/pkg_a/plugins.xml\n')
_write(base / 'pkg_b', 'share/pkg_b/missing.xml\n')
_write(base / 'pkg_c', 'share/pkg_c/broken.xml\n')
_write(base / 'pkg_ghost', 'share/pkg_ghost/plugins.xml\n')
_write(
index / 'other_pkg__pluginlib__plugin' / 'pkg_d',
'share/pkg_d/plugins.xml;ignored\n')

_write(prefix / 'share' / 'pkg_a' / 'plugins.xml', PLUGINS_XML)
_write(prefix / 'share' / 'pkg_c' / 'broken.xml', '<library')
_write(prefix / 'share' / 'pkg_d' / 'plugins.xml', OTHER_PLUGINS_XML)

monkeypatch.setenv('AMENT_PREFIX_PATH', str(prefix))
return prefix
61 changes: 61 additions & 0 deletions ros2plugin/test/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright 2026 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 pytest

from ros2plugin.api import get_package_names_with_plugin_resource_types
from ros2plugin.api import get_package_plugin_resource
from ros2plugin.api import get_registered_plugin_resource_list
from ros2plugin.api import get_registered_plugin_resources
from ros2plugin.api import is_plugin_resource_type


def test_is_plugin_resource_type():
assert is_plugin_resource_type('base_pkg__pluginlib__plugin')
assert not is_plugin_resource_type('packages')
with pytest.raises(AssertionError):
is_plugin_resource_type('')


def test_get_registered_plugin_resource_list(plugin_prefix):
assert sorted(get_registered_plugin_resource_list()) == [
'base_pkg__pluginlib__plugin', 'other_pkg__pluginlib__plugin']


def test_get_package_names_with_plugin_resource_types(plugin_prefix):
assert sorted(get_package_names_with_plugin_resource_types()) == [
'pkg_a', 'pkg_b', 'pkg_c', 'pkg_d', 'pkg_ghost']


def test_get_package_plugin_resource(plugin_prefix):
assert get_package_plugin_resource(package_name='pkg_a') == ['share/pkg_a/plugins.xml']
# Only the part before ';' is the path to the plugin XML.
assert get_package_plugin_resource(package_name='pkg_d') == ['share/pkg_d/plugins.xml']
assert get_package_plugin_resource(package_name='unknown_pkg') == []


def test_get_registered_plugin_resources(plugin_prefix):
assert sorted(get_registered_plugin_resources()) == [
('pkg_a', ['share/pkg_a/plugins.xml']),
('pkg_b', ['share/pkg_b/missing.xml']),
('pkg_c', ['share/pkg_c/broken.xml']),
('pkg_d', ['share/pkg_d/plugins.xml']),
('pkg_ghost', ['share/pkg_ghost/plugins.xml']),
]


def test_no_plugins(tmp_path, monkeypatch):
monkeypatch.setenv('AMENT_PREFIX_PATH', str(tmp_path))
assert get_registered_plugin_resource_list() == []
assert get_registered_plugin_resources() == []
41 changes: 41 additions & 0 deletions ros2plugin/test/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2026 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 os
import shutil
import subprocess

import pytest


def run_ros2_plugin(*args):
ros2 = shutil.which('ros2')
if ros2 is None:
pytest.skip("'ros2' executable not found")
return subprocess.run(
[ros2, 'plugin', *args], env=os.environ.copy(),
capture_output=True, text=True, timeout=60)


def test_cli_list_packages(plugin_prefix):
result = run_ros2_plugin('list', '--packages')
assert result.returncode == 0, result.stderr
assert result.stdout.splitlines() == [
'pkg_a', 'pkg_b', 'pkg_c', 'pkg_d', 'pkg_ghost']


def test_cli_without_verb(plugin_prefix):
result = run_ros2_plugin()
assert result.returncode == 0, result.stderr
assert 'list' in result.stdout
72 changes: 72 additions & 0 deletions ros2plugin/test/test_verb_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright 2026 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 argparse

from ros2plugin.verb.list import ListVerb


def run_list(capsys, *, packages=False, package=None):
ListVerb().main(args=argparse.Namespace(packages=packages, package=package))
return capsys.readouterr().out.splitlines()


def test_add_arguments():
parser = argparse.ArgumentParser()
ListVerb().add_arguments(parser, 'ros2 plugin list')
args = parser.parse_args(['--package', 'pkg_a'])
assert not args.packages
assert args.package == 'pkg_a'
assert parser.parse_args(['--packages']).packages


def test_list_packages(plugin_prefix, capsys):
assert run_list(capsys, packages=True) == [
'pkg_a', 'pkg_b', 'pkg_c', 'pkg_d', 'pkg_ghost']


def test_list_single_package(plugin_prefix, capsys):
# Plugins without a name are listed by their type.
assert run_list(capsys, package='pkg_a') == [
'pkg_a:',
'\tpkg_a/foo [pkg_a::Foo] (base: base_pkg::Base)',
'\tpkg_a::Bar [pkg_a::Bar] (base: base_pkg::Base)',
]


def test_list_unknown_package(plugin_prefix, capsys):
assert run_list(capsys, package='unknown_pkg') == []


def test_list_all(plugin_prefix, capsys):
output = run_list(capsys)

assert output[:3] == [
'pkg_a:',
'\tpkg_a/foo [pkg_a::Foo] (base: base_pkg::Base)',
'\tpkg_a::Bar [pkg_a::Bar] (base: base_pkg::Base)',
]
# Problems with one package are reported and the others are still listed.
assert output[3:5] == ['pkg_b:', 'XML manifest missing.xml not found.']
assert output[5] == 'pkg_c:'
assert output[6] == (
'Failed to parse plugin XML file: ' +
str(plugin_prefix / 'share' / 'pkg_c' / 'broken.xml'))
assert output[7].startswith('XML error: ')
assert output[8:] == [
'pkg_d:',
'\tpkg_d/baz [pkg_d::Baz] (base: other_pkg::Other)',
'pkg_ghost:',
'Package pkg_ghost not found.',
]