Skip to content

Commit

Permalink
Support Path with Split/Join Command Line.
Browse files Browse the repository at this point in the history
Fixes #4749.
  • Loading branch information
pekkaklarck committed Apr 24, 2023
1 parent a950878 commit 65131fc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
6 changes: 6 additions & 0 deletions atest/robot/standard_libraries/process/commandline.robot
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Split command line with unbalanced quotes
Split command line with escaping
Check Test Case ${TESTNAME}

Split command line with pathlib.Path
Check Test Case ${TESTNAME}

Join command line basics
Check Test Case ${TESTNAME}

Expand All @@ -23,3 +26,6 @@ Join command line with internal quotes

Join command line with escaping
Check Test Case ${TESTNAME}

Join command line with non-strings
Check Test Case ${TESTNAME}
12 changes: 11 additions & 1 deletion atest/testdata/standard_libraries/process/commandline.robot
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ Split command line with escaping
\\\\\\"\\\\ \\"\\ escaping=True
"\\\\\\"\\\\" \\"\\ escaping=True

Split command line with pathlib.Path
[Template] Split command line should succeed
${{pathlib.Path($TEMPDIR)}} ${TEMPDIR}

Join command line basics
[Template] Join command line should succeed
FOR ${i} IN RANGE ${BASICS}
Expand All @@ -82,6 +86,11 @@ Join command line with escaping
\\\\\\" \\"
\\\\\\\\\\" \\\\"

Join command line with non-strings
[Template] Join command line should succeed
${TEMPDIR} ${{pathlib.Path($TEMPDIR)}}
-n 42 ${TEMPDIR} -n ${42} ${{pathlib.Path($TEMPDIR)}}

*** Keywords ***
Split command line should succeed
[Arguments] ${input} @{expected} &{config}
Expand All @@ -90,7 +99,8 @@ Split command line should succeed

Split command line should fail
[Arguments] ${input} ${error}=No closing quotation
Run keyword and expect error ValueError: Parsing '${input}' failed: ${error}
Run keyword and expect error
... ValueError: Parsing '${input}' failed: ${error}
... Split command line ${input}

Join command line should succeed
Expand Down
14 changes: 8 additions & 6 deletions src/robot/libraries/Process.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,10 +768,12 @@ def split_command_line(self, args, escaping=False):
"""Splits command line string into a list of arguments.
String is split from spaces, but argument surrounded in quotes may
contain spaces in them. If ``escaping`` is given a true value, then
backslash is treated as an escape character. It can escape unquoted
spaces, quotes inside quotes, and so on, but it also requires using
double backslashes when using Windows paths.
contain spaces in them.
If ``escaping`` is given a true value, then backslash is treated as
an escape character. It can escape unquoted spaces, quotes inside
quotes, and so on, but it also requires using doubling backslashes
in Windows paths and elsewhere.
Examples:
| @{cmd} = | Split Command Line | --option "value with spaces" |
Expand All @@ -786,7 +788,7 @@ def join_command_line(self, *args):
arguments containing spaces are surrounded with quotes, and possible
quotes are escaped with a backslash.
If this keyword is given only one argument and that is a list like
If this keyword is given only one argument and that is a list-like
object, then the values of that list are joined instead.
Example:
Expand All @@ -795,7 +797,7 @@ def join_command_line(self, *args):
"""
if len(args) == 1 and is_list_like(args[0]):
args = args[0]
return subprocess.list2cmdline(args)
return subprocess.list2cmdline(str(a) for a in args)


class ExecutionResult:
Expand Down
3 changes: 3 additions & 0 deletions src/robot/utils/argumentparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import sys
import string
import warnings
from pathlib import Path

from robot.errors import DataError, Information, FrameworkError
from robot.version import get_full_version
Expand All @@ -32,6 +33,8 @@


def cmdline2list(args, escaping=False):
if isinstance(args, Path):
return [str(args)]
lexer = shlex.shlex(args, posix=True)
if is_falsy(escaping):
lexer.escape = ''
Expand Down

0 comments on commit 65131fc

Please sign in to comment.