diff --git a/ros2plugin/ros2plugin/api/__init__.py b/ros2plugin/ros2plugin/api/__init__.py index 673f5db1..5f8207ea 100644 --- a/ros2plugin/ros2plugin/api/__init__.py +++ b/ros2plugin/ros2plugin/api/__init__.py @@ -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 diff --git a/ros2plugin/ros2plugin/verb/list.py b/ros2plugin/ros2plugin/verb/list.py index e175c574..adf014f0 100644 --- a/ros2plugin/ros2plugin/verb/list.py +++ b/ros2plugin/ros2plugin/verb/list.py @@ -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') diff --git a/ros2plugin/test/test_list_regressions.py b/ros2plugin/test/test_list_regressions.py new file mode 100644 index 00000000..e25705f0 --- /dev/null +++ b/ros2plugin/test/test_list_regressions.py @@ -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', + '' + '' + '') + _add_plugins( + prefix, 'pkg', 'base_b', 'b.xml', + '' + '' + '') + + 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', + '' + '' + '' + '' + '') + + 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)', + ]