Skip to content

Commit 03fed58

Browse files
committed
fixup tests
Signed-off-by: William Woodall <william@osrfoundation.org>
1 parent 66d408b commit 03fed58

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

launch/launch/actions/execute_process.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Module for the ExecuteProcess action."""
1616

17+
import platform
1718
import shlex
1819
from typing import Dict
1920
from typing import Iterable
@@ -34,6 +35,8 @@
3435
from ..substitution import Substitution
3536
from ..substitutions import TextSubstitution
3637

38+
g_is_windows = 'win' in platform.system().lower()
39+
3740

3841
@expose_action('executable')
3942
class ExecuteProcess(ExecuteLocal):
@@ -266,7 +269,7 @@ def _append_arg():
266269
for sub in parser.parse_substitution(cmd):
267270
if isinstance(sub, TextSubstitution):
268271
try:
269-
tokens = shlex.split(sub.text)
272+
tokens = shlex.split(sub.text, posix=(not g_is_windows))
270273
except Exception:
271274
logger = launch.logging.get_logger(cls.__name__)
272275
logger.error(f"Failed to parse token '{sub.text}' of cmd '{cmd}'")

launch/launch/descriptions/executable.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""Module for a description of an Executable."""
1919

2020
import os
21+
import platform
2122
import re
2223
import shlex
2324
import threading
@@ -37,6 +38,7 @@
3738

3839
_executable_process_counter_lock = threading.Lock()
3940
_executable_process_counter = 0 # in Python3, this number is unbounded (no rollover)
41+
g_is_windows = 'win' in platform.system().lower()
4042

4143

4244
class Executable:
@@ -179,7 +181,9 @@ def prepare(self, context: LaunchContext, action: Action):
179181
# Apply if filter regex matches (empty regex matches all strings)
180182
should_apply_prefix = re.match(prefix_filter, os.path.basename(cmd[0])) is not None
181183
if should_apply_prefix:
182-
cmd = shlex.split(perform_substitutions(context, self.__prefix)) + cmd
184+
cmd = shlex.split(
185+
perform_substitutions(context, self.__prefix), posix=(not g_is_windows)
186+
) + cmd
183187
self.__final_cmd = cmd
184188
name = os.path.basename(cmd[0]) if self.__name is None \
185189
else perform_substitutions(context, self.__name)

launch/launch/substitutions/command.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""Module for the Command substitution."""
1616

17-
import os
17+
import platform
1818
import shlex
1919
import subprocess
2020
from typing import List
@@ -30,6 +30,8 @@
3030
from ..some_substitutions_type import SomeSubstitutionsType
3131
from ..substitution import Substitution
3232

33+
g_is_windows = 'win' in platform.system().lower()
34+
3335

3436
@expose_substitution('command')
3537
class Command(Substitution):
@@ -94,10 +96,7 @@ def perform(self, context: LaunchContext) -> Text:
9496
from ..utilities import perform_substitutions # import here to avoid loop
9597
command_str = perform_substitutions(context, self.command)
9698
command: Union[str, List[str]]
97-
if os.name != 'nt':
98-
command = shlex.split(command_str)
99-
else:
100-
command = command_str
99+
command = shlex.split(command_str, posix=(not g_is_windows))
101100
on_stderr = perform_substitutions(context, self.on_stderr)
102101
if on_stderr not in ('fail', 'ignore', 'warn', 'capture'):
103102
raise SubstitutionFailure(

launch_xml/test/launch_xml/executable.xml

Lines changed: 0 additions & 5 deletions
This file was deleted.

launch_xml/test/launch_xml/test_executable.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,35 @@
1616

1717
import io
1818
import os
19-
from pathlib import Path
2019
import sys
21-
import textwrap
2220

2321
from launch import LaunchService
2422
from launch.actions import Shutdown
2523
from launch.frontend import Parser
2624

2725
import pytest
2826

27+
test_executable_xml = f"""
28+
<launch>
29+
<executable
30+
cmd="{sys.executable} --version"
31+
cwd="/" name="my_ls" shell="true" output="log"
32+
emulate_tty="true" sigkill_timeout="4.0" sigterm_timeout="7.0"
33+
launch-prefix="$(env LAUNCH_PREFIX '')"
34+
>
35+
<env name="var" value="1"/>
36+
</executable>
37+
</launch>
38+
"""
39+
2940

3041
def test_executable():
3142
"""Parse node xml example."""
32-
xml_file = str(Path(__file__).parent / 'executable.xml')
33-
root_entity, parser = Parser.load(xml_file)
43+
root_entity, parser = Parser.load(io.StringIO(test_executable_xml))
3444
ld = parser.parse_description(root_entity)
3545
executable = ld.entities[0]
3646
cmd = [i[0].perform(None) for i in executable.cmd]
37-
assert cmd == ['ls', '-l', '-a', '-s']
47+
assert cmd == [sys.executable, '--version']
3848
assert executable.cwd[0].perform(None) == '/'
3949
assert executable.name[0].perform(None) == 'my_ls'
4050
assert executable.shell is True
@@ -50,20 +60,21 @@ def test_executable():
5060
ls = LaunchService()
5161
ls.include_launch_description(ld)
5262
assert 0 == ls.run()
63+
assert executable.return_code == 0
64+
65+
66+
test_executable_wrong_subtag_xml = """
67+
<launch>
68+
<executable cmd="some_command --that-does-not-matter">
69+
<env name="var" value="1"/>
70+
<whats_this/>
71+
</executable>
72+
</launch>
73+
"""
5374

5475

5576
def test_executable_wrong_subtag():
56-
xml_file = \
57-
"""\
58-
<launch>
59-
<executable cmd="ls -l -a -s" cwd="/" name="my_ls" shell="true" output="log" launch-prefix="$(env LAUNCH_PREFIX '')">
60-
<env name="var" value="1"/>
61-
<whats_this/>
62-
</executable>
63-
</launch>
64-
""" # noqa, line too long
65-
xml_file = textwrap.dedent(xml_file)
66-
root_entity, parser = Parser.load(io.StringIO(xml_file))
77+
root_entity, parser = Parser.load(io.StringIO(test_executable_wrong_subtag_xml))
6778
with pytest.raises(ValueError) as excinfo:
6879
parser.parse_description(root_entity)
6980
assert '`executable`' in str(excinfo.value)

0 commit comments

Comments
 (0)