diff --git a/ros2cli_test_interfaces/CMakeLists.txt b/ros2cli_test_interfaces/CMakeLists.txt index b3c905927..f7e2008c8 100644 --- a/ros2cli_test_interfaces/CMakeLists.txt +++ b/ros2cli_test_interfaces/CMakeLists.txt @@ -10,7 +10,9 @@ find_package(rosidl_default_generators REQUIRED) rosidl_generate_interfaces(${PROJECT_NAME} "action/ShortVariedMultiNested.action" + "msg/IdlOnly.idl" "msg/ShortVaried.msg" + "msg/ShortVariedIdlNested.msg" "msg/ShortVariedMultiNested.msg" "msg/ShortVariedNested.msg" "srv/ShortVariedMultiNested.srv" diff --git a/ros2cli_test_interfaces/msg/IdlOnly.idl b/ros2cli_test_interfaces/msg/IdlOnly.idl new file mode 100644 index 000000000..e1ea063a1 --- /dev/null +++ b/ros2cli_test_interfaces/msg/IdlOnly.idl @@ -0,0 +1,7 @@ +module ros2cli_test_interfaces { + module msg { + struct IdlOnly { + uint32 value; + }; + }; +}; diff --git a/ros2cli_test_interfaces/msg/ShortVariedIdlNested.msg b/ros2cli_test_interfaces/msg/ShortVariedIdlNested.msg new file mode 100644 index 000000000..30b45757a --- /dev/null +++ b/ros2cli_test_interfaces/msg/ShortVariedIdlNested.msg @@ -0,0 +1 @@ +IdlOnly idl_only diff --git a/ros2interface/ros2interface/verb/show.py b/ros2interface/ros2interface/verb/show.py index aec3eb641..94aeb7d09 100644 --- a/ros2interface/ros2interface/verb/show.py +++ b/ros2interface/ros2interface/verb/show.py @@ -104,7 +104,10 @@ def _is_nested(self) -> bool: return False -def _get_interface_lines(interface_identifier: str) -> typing.Iterable[InterfaceTextLine]: +def _get_interface_lines( + interface_identifier: str, + file_path: str, +) -> typing.Iterable[InterfaceTextLine]: parts: typing.List[str] = interface_identifier.split('/') if len(parts) != 3: raise ValueError( @@ -112,7 +115,6 @@ def _get_interface_lines(interface_identifier: str) -> typing.Iterable[Interface ) pkg_name, _, msg_name = parts - file_path = get_interface_path(interface_identifier) with open(file_path) as file_handler: for line in file_handler: yield InterfaceTextLine( @@ -147,7 +149,18 @@ def _show_interface( is_show_nested_comments: bool = False, indent_level: int = 0 ): - for line in _get_interface_lines(interface_identifier): + file_path = get_interface_path(interface_identifier) + if file_path.endswith('.idl'): + with open(file_path) as file_handler: + content = file_handler.read() + indent_string = indent_level * '\t' + for line in content.splitlines(): + print(f'{indent_string}{line}' if line else '') + if not content or not content.endswith('\n'): + print() + return + + for line in _get_interface_lines(interface_identifier, file_path): _print_interface_line( line, is_show_comments=is_show_comments, indent_level=indent_level) diff --git a/ros2interface/test/test_cli.py b/ros2interface/test/test_cli.py index cc654b48c..047283440 100644 --- a/ros2interface/test/test_cli.py +++ b/ros2interface/test/test_cli.py @@ -302,6 +302,47 @@ def test_show_message(self): strict=True ) + def test_show_idl_message(self): + with self.launch_interface_command( + arguments=['show', 'ros2cli_test_interfaces/msg/IdlOnly'] + ) as interface_command: + assert interface_command.wait_for_shutdown(timeout=2) + assert interface_command.exit_code == launch_testing.asserts.EXIT_OK + assert launch_testing.tools.expect_output( + expected_lines=[ + 'module ros2cli_test_interfaces {', + ' module msg {', + ' struct IdlOnly {', + ' uint32 value;', + ' };', + ' };', + '};', + ], + text=interface_command.output, + strict=True + ) + + def test_show_message_with_idl_nested_type(self): + with self.launch_interface_command( + arguments=['show', 'ros2cli_test_interfaces/msg/ShortVariedIdlNested'] + ) as interface_command: + assert interface_command.wait_for_shutdown(timeout=2) + assert interface_command.exit_code == launch_testing.asserts.EXIT_OK + assert launch_testing.tools.expect_output( + expected_lines=[ + 'IdlOnly idl_only', + '\tmodule ros2cli_test_interfaces {', + '\t module msg {', + '\t struct IdlOnly {', + '\t uint32 value;', + '\t };', + '\t };', + '\t};', + ], + text=interface_command.output, + strict=True + ) + def test_show_message_with_all_comments(self): with self.launch_interface_command( arguments=[