Skip to content

Commit 0c54b3a

Browse files
committed
fixup tests
Signed-off-by: William Woodall <[email protected]>
1 parent 1ae72c8 commit 0c54b3a

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,24 +16,34 @@
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.frontend import Parser
2523

2624
import pytest
2725

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

2940
def test_executable():
3041
"""Parse node xml example."""
31-
xml_file = str(Path(__file__).parent / 'executable.xml')
32-
root_entity, parser = Parser.load(xml_file)
42+
root_entity, parser = Parser.load(io.StringIO(test_executable_xml))
3343
ld = parser.parse_description(root_entity)
3444
executable = ld.entities[0]
3545
cmd = [i[0].perform(None) for i in executable.cmd]
36-
assert cmd == ['ls', '-l', '-a', '-s']
46+
assert cmd == [sys.executable, '--version']
3747
assert executable.cwd[0].perform(None) == '/'
3848
assert executable.name[0].perform(None) == 'my_ls'
3949
assert executable.shell is True
@@ -49,20 +59,21 @@ def test_executable():
4959
ls = LaunchService()
5060
ls.include_launch_description(ld)
5161
assert 0 == ls.run()
62+
assert executable.return_code == 0
63+
64+
65+
test_executable_wrong_subtag_xml = """
66+
<launch>
67+
<executable cmd="some_command --that-does-not-matter">
68+
<env name="var" value="1"/>
69+
<whats_this/>
70+
</executable>
71+
</launch>
72+
"""
5273

5374

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

0 commit comments

Comments
 (0)