Skip to content

Commit

Permalink
fixup tests
Browse files Browse the repository at this point in the history
Signed-off-by: William Woodall <william@osrfoundation.org>
  • Loading branch information
wjwwood committed Jan 22, 2024
1 parent 1ae72c8 commit 0c54b3a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
5 changes: 4 additions & 1 deletion launch/launch/actions/execute_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""Module for the ExecuteProcess action."""

import platform
import shlex
from typing import Dict
from typing import Iterable
Expand All @@ -34,6 +35,8 @@
from ..substitution import Substitution
from ..substitutions import TextSubstitution

g_is_windows = 'win' in platform.system().lower()


@expose_action('executable')
class ExecuteProcess(ExecuteLocal):
Expand Down Expand Up @@ -266,7 +269,7 @@ def _append_arg():
for sub in parser.parse_substitution(cmd):
if isinstance(sub, TextSubstitution):
try:
tokens = shlex.split(sub.text)
tokens = shlex.split(sub.text, posix=(not g_is_windows))
except Exception:
logger = launch.logging.get_logger(cls.__name__)
logger.error(f"Failed to parse token '{sub.text}' of cmd '{cmd}'")
Expand Down
6 changes: 5 additions & 1 deletion launch/launch/descriptions/executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""Module for a description of an Executable."""

import os
import platform
import re
import shlex
import threading
Expand All @@ -37,6 +38,7 @@

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


class Executable:
Expand Down Expand Up @@ -179,7 +181,9 @@ def prepare(self, context: LaunchContext, action: Action):
# Apply if filter regex matches (empty regex matches all strings)
should_apply_prefix = re.match(prefix_filter, os.path.basename(cmd[0])) is not None
if should_apply_prefix:
cmd = shlex.split(perform_substitutions(context, self.__prefix)) + cmd
cmd = shlex.split(
perform_substitutions(context, self.__prefix), posix=(not g_is_windows)
) + cmd
self.__final_cmd = cmd
name = os.path.basename(cmd[0]) if self.__name is None \
else perform_substitutions(context, self.__name)
Expand Down
9 changes: 4 additions & 5 deletions launch/launch/substitutions/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

"""Module for the Command substitution."""

import os
import platform
import shlex
import subprocess
from typing import List
Expand All @@ -30,6 +30,8 @@
from ..some_substitutions_type import SomeSubstitutionsType
from ..substitution import Substitution

g_is_windows = 'win' in platform.system().lower()


@expose_substitution('command')
class Command(Substitution):
Expand Down Expand Up @@ -94,10 +96,7 @@ def perform(self, context: LaunchContext) -> Text:
from ..utilities import perform_substitutions # import here to avoid loop
command_str = perform_substitutions(context, self.command)
command: Union[str, List[str]]
if os.name != 'nt':
command = shlex.split(command_str)
else:
command = command_str
command = shlex.split(command_str, posix=(not g_is_windows))
on_stderr = perform_substitutions(context, self.on_stderr)
if on_stderr not in ('fail', 'ignore', 'warn', 'capture'):
raise SubstitutionFailure(
Expand Down
5 changes: 0 additions & 5 deletions launch_xml/test/launch_xml/executable.xml

This file was deleted.

43 changes: 27 additions & 16 deletions launch_xml/test/launch_xml/test_executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,34 @@

import io
import os
from pathlib import Path
import sys
import textwrap

from launch import LaunchService
from launch.frontend import Parser

import pytest

test_executable_xml = f"""
<launch>
<executable
cmd="{sys.executable} --version"
cwd="/" name="my_ls" shell="true" output="log"
emulate_tty="true" sigkill_timeout="4.0" sigterm_timeout="7.0"
launch-prefix="$(env LAUNCH_PREFIX '')"
>
<env name="var" value="1"/>
</executable>
</launch>
"""


def test_executable():
"""Parse node xml example."""
xml_file = str(Path(__file__).parent / 'executable.xml')
root_entity, parser = Parser.load(xml_file)
root_entity, parser = Parser.load(io.StringIO(test_executable_xml))
ld = parser.parse_description(root_entity)
executable = ld.entities[0]
cmd = [i[0].perform(None) for i in executable.cmd]
assert cmd == ['ls', '-l', '-a', '-s']
assert cmd == [sys.executable, '--version']
assert executable.cwd[0].perform(None) == '/'
assert executable.name[0].perform(None) == 'my_ls'
assert executable.shell is True
Expand All @@ -49,20 +59,21 @@ def test_executable():
ls = LaunchService()
ls.include_launch_description(ld)
assert 0 == ls.run()
assert executable.return_code == 0


test_executable_wrong_subtag_xml = """
<launch>
<executable cmd="some_command --that-does-not-matter">
<env name="var" value="1"/>
<whats_this/>
</executable>
</launch>
"""


def test_executable_wrong_subtag():
xml_file = \
"""\
<launch>
<executable cmd="ls -l -a -s" cwd="/" name="my_ls" shell="true" output="log" launch-prefix="$(env LAUNCH_PREFIX '')">
<env name="var" value="1"/>
<whats_this/>
</executable>
</launch>
""" # noqa, line too long
xml_file = textwrap.dedent(xml_file)
root_entity, parser = Parser.load(io.StringIO(xml_file))
root_entity, parser = Parser.load(io.StringIO(test_executable_wrong_subtag_xml))
with pytest.raises(ValueError) as excinfo:
parser.parse_description(root_entity)
assert '`executable`' in str(excinfo.value)
Expand Down

0 comments on commit 0c54b3a

Please sign in to comment.