Skip to content

Commit

Permalink
Fix test_node_frontend (ros2#146)
Browse files Browse the repository at this point in the history
* Fix test_node_frontend

Fix ros2#145

1. Make test names more intelligible by not parameterizing on a crazy long string
2. Don't leak StringIO objects
Signed-off-by: Dan Rose <dan@digilabs.io>

* Some renamings and perform the same fixes on test_push_ros_namespace_frontend

Signed-off-by: Dan Rose <dan@digilabs.io>

* Remove incorrect docstring

Signed-off-by: Dan Rose <dan@digilabs.io>

* Add copyright for files with contributions

Signed-off-by: Dan Rose <dan@digilabs.io>

* Fix formatting

Signed-off-by: Dan Rose <dan@digilabs.io>

* Fix copyrights

Signed-off-by: Dan Rose <dan@digilabs.io>
  • Loading branch information
rotu committed Jun 22, 2020
1 parent 7974bd4 commit dbdc9f0
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 126 deletions.
208 changes: 106 additions & 102 deletions test_launch_ros/test/test_launch_ros/frontend/test_node_frontend.py
@@ -1,4 +1,5 @@
# Copyright 2019 Open Source Robotics Foundation, Inc.
# Copyright 2020 Open Avatar Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,8 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Example of how to parse an xml."""

import io
import pathlib
import sys
Expand All @@ -24,114 +23,119 @@

from launch_ros.utilities import evaluate_parameters

import pytest

yaml_params = str(pathlib.Path(__file__).parent / 'params.yaml')

# Escape backslashes if any to keep them after parsing takes place
yaml_params = yaml_params.replace('\\', '\\\\')
python_executable = sys.executable.replace('\\', '\\\\')

xml_file = \
r"""
<launch>
<let name="a_string" value="\'[2, 5, 8]\'"/>
<let name="a_list" value="[2, 5, 8]"/>
<node pkg="demo_nodes_py" exec="talker_qos" output="screen" name="my_talker" namespace="my_ns" exec_name="my_talker_process" args="--number_of_cycles 1">
<param name="param1" value="ads"/>
<param name="param_group1">
<param name="param_group2">
<param name="param2" value="2"/>

def test_launch_remapping_xml():
xml_file = textwrap.dedent(
r"""
<launch>
<let name="a_string" value="\'[2, 5, 8]\'"/>
<let name="a_list" value="[2, 5, 8]"/>
<node pkg="demo_nodes_py" exec="talker_qos" output="screen" name="my_talker" namespace="my_ns" exec_name="my_talker_process" args="--number_of_cycles 1">
<param name="param1" value="ads"/>
<param name="param_group1">
<param name="param_group2">
<param name="param2" value="2"/>
</param>
<param name="param3" value="2, 5, 8" value-sep=", "/>
<param name="param4" value="$(var a_list)"/>
<param name="param5" value="$(var a_string)"/>
<param name="param6" value="2., 5., 8." value-sep=", "/>
<param name="param7" value="'2', '5', '8'" value-sep=", "/>
<param name="param8" value="''2'', ''5'', ''8''" value-sep=", "/>
<param name="param9" value="\'2\', \'5\', \'8\'" value-sep=", "/>
<param name="param10" value="''asd'', ''bsd'', ''csd''" value-sep=", "/>
<param name="param11" value="'\asd', '\bsd', '\csd'" value-sep=", "/>
<param name="param12" value=""/>
</param>
<param name="param3" value="2, 5, 8" value-sep=", "/>
<param name="param4" value="$(var a_list)"/>
<param name="param5" value="$(var a_string)"/>
<param name="param6" value="2., 5., 8." value-sep=", "/>
<param name="param7" value="'2', '5', '8'" value-sep=", "/>
<param name="param8" value="''2'', ''5'', ''8''" value-sep=", "/>
<param name="param9" value="\'2\', \'5\', \'8\'" value-sep=", "/>
<param name="param10" value="''asd'', ''bsd'', ''csd''" value-sep=", "/>
<param name="param11" value="'\asd', '\bsd', '\csd'" value-sep=", "/>
<param name="param12" value=""/>
</param>
<param from="{}"/>
<env name="var" value="1"/>
<remap from="foo" to="bar"/>
<remap from="baz" to="foobar"/>
</node>
<node exec="{}" args="-c 'import sys; print(sys.argv[1:])'" name="my_listener" namespace="my_ns" output="screen"/>
</launch>
""".format(yaml_params, python_executable) # noqa: E501
xml_file = textwrap.dedent(xml_file)
yaml_file = \
r"""
launch:
- let:
name: 'a_string'
value: "'[2, 5, 8]'"
- let:
name: 'a_list'
value: '[2, 5, 8]'
- node:
pkg: demo_nodes_py
exec: talker_qos
output: screen
name: my_talker
namespace: my_ns
exec_name: my_talker_process
args: '--number_of_cycles 1'
param:
- name: param1
value: ads
- name: param_group1
param:
- name: param_group2
<param from="{}"/>
<env name="var" value="1"/>
<remap from="foo" to="bar"/>
<remap from="baz" to="foobar"/>
</node>
<node exec="{}" args="-c 'import sys; print(sys.argv[1:])'" name="my_listener" namespace="my_ns" output="screen"/>
</launch>
""".format(yaml_params, python_executable)) # noqa: E501

with io.StringIO(xml_file) as f:
check_launch_remapping(f)


def test_launch_remapping_yaml():
yaml_file = textwrap.dedent(
r"""
launch:
- let:
name: 'a_string'
value: "'[2, 5, 8]'"
- let:
name: 'a_list'
value: '[2, 5, 8]'
- node:
pkg: demo_nodes_py
exec: talker_qos
output: screen
name: my_talker
namespace: my_ns
exec_name: my_talker_process
args: '--number_of_cycles 1'
param:
- name: param1
value: ads
- name: param_group1
param:
- name: param2
value: 2
- name: param3
value: [2, 5, 8]
- name: param4
value: $(var a_list)
- name: param5
value: $(var a_string)
- name: param6
value: [2., 5., 8.]
- name: param7
value: ['2', '5', '8']
- name: param8
value: ["'2'", "'5'", "'8'"]
- name: param9
value: ["\\'2\\'", "\\'5\\'", "\\'8\\'"]
- name: param10
value: ["'asd'", "'bsd'", "'csd'"]
- name: param11
value: ['\asd', '\bsd', '\csd']
- name: param12
value: ''
- from: {}
env:
- name: var
value: '1'
remap:
- from: "foo"
to: "bar"
- from: "baz"
to: "foobar"
- node:
exec: {}
output: screen
namespace: my_ns
name: my_listener
args: -c 'import sys; print(sys.argv[1:])'
""".format(yaml_params, python_executable) # noqa: E501
yaml_file = textwrap.dedent(yaml_file)


@pytest.mark.parametrize('file', (xml_file, yaml_file))
def test_node_frontend(file):
"""Parse node xml example."""
root_entity, parser = Parser.load(io.StringIO(file))
- name: param_group2
param:
- name: param2
value: 2
- name: param3
value: [2, 5, 8]
- name: param4
value: $(var a_list)
- name: param5
value: $(var a_string)
- name: param6
value: [2., 5., 8.]
- name: param7
value: ['2', '5', '8']
- name: param8
value: ["'2'", "'5'", "'8'"]
- name: param9
value: ["\\'2\\'", "\\'5\\'", "\\'8\\'"]
- name: param10
value: ["'asd'", "'bsd'", "'csd'"]
- name: param11
value: ['\asd', '\bsd', '\csd']
- name: param12
value: ''
- from: {}
env:
- name: var
value: '1'
remap:
- from: "foo"
to: "bar"
- from: "baz"
to: "foobar"
- node:
exec: {}
output: screen
namespace: my_ns
name: my_listener
args: -c 'import sys; print(sys.argv[1:])'
""".format(yaml_params, python_executable)) # noqa: E501

with io.StringIO(yaml_file) as f:
check_launch_remapping(f)


def check_launch_remapping(file):
root_entity, parser = Parser.load(file)
ld = parser.parse_description(root_entity)
ls = LaunchService()
ls.include_launch_description(ld)
Expand Down
@@ -1,4 +1,5 @@
# Copyright 2019 Open Source Robotics Foundation, Inc.
# Copyright 2020 Open Avatar Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,36 +13,39 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Example of how to parse an xml."""

import io
import textwrap

from launch import LaunchService
from launch.frontend import Parser

import pytest

xml_file = \
r"""
<launch>
<push-ros-namespace namespace="asd"/>
</launch>
"""
xml_file = textwrap.dedent(xml_file)
yaml_file = \
r"""
launch:
- push-ros-namespace:
namespace: 'asd'
"""
yaml_file = textwrap.dedent(yaml_file)


@pytest.mark.parametrize('file', (xml_file, yaml_file))
def test_node_frontend(file):
"""Parse node xml example."""
root_entity, parser = Parser.load(io.StringIO(file))

def test_launch_namespace_yaml():
yaml_file = textwrap.dedent(
r"""
launch:
- push-ros-namespace:
namespace: 'asd'
"""
)
with io.StringIO(yaml_file) as f:
check_launch_namespace(f)


def test_launch_namespace_xml():
xml_file = textwrap.dedent(
r"""
<launch>
<push-ros-namespace namespace="asd"/>
</launch>
"""
)
with io.StringIO(xml_file) as f:
check_launch_namespace(f)


def check_launch_namespace(file):
root_entity, parser = Parser.load(file)
ld = parser.parse_description(root_entity)
ls = LaunchService()
ls.include_launch_description(ld)
Expand Down

0 comments on commit dbdc9f0

Please sign in to comment.