From cd57c9c54f17e846f168621209762da638cf40c4 Mon Sep 17 00:00:00 2001 From: Emerson Knapp Date: Tue, 25 Feb 2020 18:22:16 -0800 Subject: [PATCH] Add test and docstrings Signed-off-by: Emerson Knapp --- ros2node/ros2node/api/__init__.py | 8 ++++++++ ros2node/ros2node/verb/list.py | 3 ++- ros2node/test/test_node.py | 27 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/ros2node/ros2node/api/__init__.py b/ros2node/ros2node/api/__init__.py index 0e4b66622..ec6de20ed 100644 --- a/ros2node/ros2node/api/__init__.py +++ b/ros2node/ros2node/api/__init__.py @@ -50,6 +50,14 @@ def parse_node_name(node_name): def annotate_duplicate_nodes(node_names: List[NodeName]) -> Tuple[bool, List[str]]: + """ + Annotate a list of node names with a warnings if names are not unique. + + :param node_names: list of all NodeNames observed in the graph + :return Tuple containing: + bool of whether any duplicate nodes were found + list of all names as strings, with or without annotations, for printing + """ annotation_string = ' (non-unique name)' any_duplicates = False diff --git a/ros2node/ros2node/verb/list.py b/ros2node/ros2node/verb/list.py index b9c9bd080..f9f86f273 100644 --- a/ros2node/ros2node/verb/list.py +++ b/ros2node/ros2node/verb/list.py @@ -40,5 +40,6 @@ def main(self, *, args): elif node_names: has_duplicates, sorted_names = annotate_duplicate_nodes(node_names) if has_duplicates: - print('# WARNING: There are nodes in the graph that share a name') + print('# WARNING: Be aware that are nodes in the graph that share an exact name, ' + 'this can have unintended side effects.') print(*sorted_names, sep='\n') diff --git a/ros2node/test/test_node.py b/ros2node/test/test_node.py index d07addbb8..8e7c1c004 100644 --- a/ros2node/test/test_node.py +++ b/ros2node/test/test_node.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from ros2node.api import annotate_duplicate_nodes +from ros2node.api import NodeName from ros2node.api import parse_node_name @@ -34,3 +36,28 @@ def test_parse_node_name(): assert name.full_name == '/ns/talker' assert name.namespace == '/ns' assert name.name == 'talker' + + +def test_duplicate_names(): + all_unique_names = [ + NodeName('foo', 'ns_foo', 'ns_foo/foo'), + NodeName('bar', 'ns_foo', 'ns_foo/bar'), + ] + + any_duplicates, printable_names = annotate_duplicate_nodes(all_unique_names) + assert not any_duplicates + assert printable_names == [ + 'ns_foo/bar', + 'ns_foo/foo', + ] + + all_unique_names = [ + NodeName('foo', 'ns_foo', 'ns_foo/foo'), + NodeName('foo', 'ns_foo', 'ns_foo/foo'), + ] + any_duplicates, printable_names = annotate_duplicate_nodes(all_unique_names) + assert any_duplicates + assert printable_names == [ + 'ns_foo/foo (non-unique name)', + 'ns_foo/foo (non-unique name)', + ]