Skip to content

Commit d8a323b

Browse files
committed
add tests for split_arguments argument in ExecuteLocal
Signed-off-by: William Woodall <[email protected]>
1 parent 159ee9c commit d8a323b

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

launch/launch/actions/execute_local.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ def __init__(
8383
*,
8484
process_description: Executable,
8585
shell: bool = False,
86-
split_arguments: SomeSubstitutionsType = LaunchConfiguration(
87-
'split_arguments', default=False),
86+
split_arguments: Union[
87+
SomeSubstitutionsType,
88+
bool
89+
] = LaunchConfiguration(
90+
'split_arguments', default='False'),
8891
sigterm_timeout: SomeSubstitutionsType = LaunchConfiguration(
8992
'sigterm_timeout', default=5),
9093
sigkill_timeout: SomeSubstitutionsType = LaunchConfiguration(
@@ -209,6 +212,8 @@ def __init__(
209212
super().__init__(**kwargs)
210213
self.__process_description = process_description
211214
self.__shell = shell
215+
if isinstance(split_arguments, bool):
216+
split_arguments = 'True' if split_arguments else 'False'
212217
self.__split_arguments = normalize_to_list_of_substitutions(split_arguments)
213218
self.__sigterm_timeout = normalize_to_list_of_substitutions(sigterm_timeout)
214219
self.__sigkill_timeout = normalize_to_list_of_substitutions(sigkill_timeout)
@@ -633,8 +638,8 @@ async def __execute_process(self, context: LaunchContext) -> None:
633638
if returncode == 0:
634639
self.__logger.info('process has finished cleanly [pid {}]'.format(pid))
635640
else:
636-
self.__logger.error("process has died [pid {}, exit code {}, cmd '{}'].".format(
637-
pid, returncode, ' '.join(filter(lambda part: part.strip(), cmd))
641+
self.__logger.error("process has died [pid {}, exit code {}, cmd ['{}']].".format(
642+
pid, returncode, "', '".join(cmd)
638643
))
639644
await context.emit_event(
640645
ProcessExited(returncode=returncode, **process_event_args)

launch/test/launch/test_execute_process.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,70 @@ def test_execute_process_prefix_filter_override_in_launch_file():
318318
test_process.execute(lc)
319319
assert 'echo' in test_process.process_details['cmd'] and \
320320
'time' not in test_process.process_details['cmd']
321+
322+
323+
preamble = [sys.executable, os.path.join(os.path.dirname(__file__), 'argv_echo.py')]
324+
325+
326+
@pytest.mark.parametrize('test_parameters', [
327+
# This case will result in 2 arguments, keeping the --some-arg and "some string" together.
328+
{
329+
'cmd': preamble + ['--some-arg "some string"'],
330+
'shell': False,
331+
'split_arguments': False,
332+
'expected_number_of_args': 2,
333+
},
334+
# This case will split the --some-arg and "some string" up, resulting in 3 args.
335+
{
336+
'cmd': preamble + ['--some-arg "some string"'],
337+
'shell': False,
338+
'split_arguments': True,
339+
'expected_number_of_args': 3,
340+
},
341+
# This case the split_arguments is ignored, due to shell=True,
342+
# and produces again 3 arguments, not 4.
343+
{
344+
'cmd': preamble + ['--some-arg "some string"'],
345+
'shell': True,
346+
'split_arguments': True,
347+
'expected_number_of_args': 3,
348+
},
349+
# This is the "normal" shell=True behavior.
350+
{
351+
'cmd': preamble + ['--some-arg "some string"'],
352+
'shell': True,
353+
'split_arguments': False,
354+
'expected_number_of_args': 3,
355+
},
356+
# Test single argument for cmd (still a list), which will require shell=True.
357+
{
358+
'cmd': [' '.join(preamble + ['--some-arg "some string"'])],
359+
'shell': True,
360+
'split_arguments': False,
361+
'expected_number_of_args': 3,
362+
},
363+
# This case also ignores split_arguments.
364+
{
365+
'cmd': [' '.join(preamble + ['--some-arg "some string"'])],
366+
'shell': True,
367+
'split_arguments': True,
368+
'expected_number_of_args': 3,
369+
},
370+
])
371+
def test_execute_process_split_arguments(test_parameters):
372+
"""Test the use of the split_arguments option."""
373+
execute_process_action = ExecuteProcess(
374+
cmd=test_parameters['cmd'],
375+
output='screen',
376+
shell=test_parameters['shell'],
377+
split_arguments=test_parameters['split_arguments'],
378+
)
379+
380+
ld = LaunchDescription([
381+
execute_process_action,
382+
])
383+
ls = LaunchService()
384+
ls.include_launch_description(ld)
385+
assert 0 == ls.run(shutdown_when_idle=True)
386+
assert execute_process_action.return_code == test_parameters['expected_number_of_args'], \
387+
execute_process_action.process_details['cmd']

0 commit comments

Comments
 (0)