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
3 changes: 2 additions & 1 deletion ros2plugin/ros2plugin/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def get_package_names_with_plugin_resource_types():
"""
packages = []
for plugin in get_registered_plugin_resource_list():
packages += list(get_resources(plugin).keys())
# A package exporting plugins for several base classes is listed only once.
packages += [name for name in get_resources(plugin) if name not in packages]
return packages


Expand Down
18 changes: 10 additions & 8 deletions ros2plugin/ros2plugin/verb/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,17 @@ def main(self, *, args):
)
continue

for elem in tree.iter():
if elem.tag == 'class':
try:
plugin_name = elem.attrib['name']
except KeyError:
plugin_name = elem.attrib['type']
plugins.append(PluginInfo(
plugin_name, elem.attrib['type'], elem.attrib['base_class_type'])
for elem in tree.iter('class'):
plugin_type = elem.get('type')
base_class_type = elem.get('base_class_type')
if plugin_type is None or base_class_type is None:
print(
f"Skipping class without 'type' or 'base_class_type' "
f'in {plugin_xml}.'
)
continue
plugins.append(PluginInfo(
elem.get('name', plugin_type), plugin_type, base_class_type))

if any(plugins):
print(*[f'\t{p.name} [{p.type}] (base: {p.base})' for p in plugins], sep='\n')
88 changes: 88 additions & 0 deletions ros2plugin/test/test_list_regressions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# 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

import pytest

from ros2plugin.api import get_package_names_with_plugin_resource_types
from ros2plugin.verb.list import ListVerb


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


@pytest.fixture
def prefix(tmp_path, monkeypatch):
"""Create an empty install prefix and make it the only ament prefix."""
monkeypatch.setenv('AMENT_PREFIX_PATH', str(tmp_path))
return tmp_path


def _add_plugins(prefix, package, base_package, xml_name, xml_content):
index = prefix / 'share' / 'ament_index' / 'resource_index'
_write(index / 'packages' / package)
_write(
index / f'{base_package}__pluginlib__plugin' / package,
f'share/{package}/{xml_name}\n')
_write(prefix / 'share' / package / xml_name, xml_content)


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


def test_package_with_several_base_classes_is_listed_once(prefix, capsys):
_add_plugins(
prefix, 'pkg', 'base_a', 'a.xml',
'<library path="pkg_plugins">'
'<class name="pkg/a" type="pkg::A" base_class_type="base_a::A"/>'
'</library>')
_add_plugins(
prefix, 'pkg', 'base_b', 'b.xml',
'<library path="pkg_plugins">'
'<class name="pkg/b" type="pkg::B" base_class_type="base_b::B"/>'
'</library>')

assert get_package_names_with_plugin_resource_types() == ['pkg']
assert _list(capsys, packages=True) == ['pkg']

output = _list(capsys)
assert output[0] == 'pkg:'
assert sorted(output[1:]) == [
'\tpkg/a [pkg::A] (base: base_a::A)',
'\tpkg/b [pkg::B] (base: base_b::B)',
]


def test_class_with_missing_attributes_is_skipped(prefix, capsys):
_add_plugins(
prefix, 'pkg', 'base', 'plugins.xml',
'<library path="pkg_plugins">'
'<class name="pkg/no_type" base_class_type="base::Base"/>'
'<class name="pkg/no_base" type="pkg::NoBase"/>'
'<class name="pkg/foo" type="pkg::Foo" base_class_type="base::Base"/>'
'</library>')

xml = prefix / 'share' / 'pkg' / 'plugins.xml'
skipped = f"Skipping class without 'type' or 'base_class_type' in {xml}."
assert _list(capsys) == [
'pkg:',
skipped,
skipped,
'\tpkg/foo [pkg::Foo] (base: base::Base)',
]