Skip to content

Commit

Permalink
Add pathlib.Path support to Process #4455
Browse files Browse the repository at this point in the history
  • Loading branch information
pekkaklarck committed Sep 16, 2022
1 parent ef86dd2 commit 26e267c
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 23 deletions.
5 changes: 4 additions & 1 deletion atest/robot/standard_libraries/process/stdin.robot
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ Stdin can be disabled
Stdin can be disabled with None object
Check Test Case ${TESTNAME}

Stdin as file
Stdin as path
Check Test Case ${TESTNAME}

Stdin as `pathlib.Path`
Check Test Case ${TESTNAME}

Stdin as text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ Default stdout and stderr
Custom stdout
Check Test Case ${TESTNAME}

Custom stdout as `pathlib.Path`
Check Test Case ${TESTNAME}

Redirecting stdout to DEVNULL
Check Test Case ${TESTNAME}

Custom stderr
Check Test Case ${TESTNAME}

Custom stderr as `pathlib.Path`
Check Test Case ${TESTNAME}

Custom stdout and stderr
Check Test Case ${TESTNAME}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Start And Wait Process

Change Current Working Directory
${result}= Run Process python -c import os; print(os.path.abspath(os.curdir)) cwd=.
${result2}= Run Process python -c import os; print(os.path.abspath(os.curdir)) cwd=..
${result2}= Run Process python -c import os; print(os.path.abspath(os.curdir)) cwd=${{pathlib.Path('..')}}
Should Not Be Equal ${result.stdout} ${result2.stdout}

Running a process in a shell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ${TEMPFILE} %{TEMPDIR}${/}terminate-process-temp.txt
${STARTED} %{TEMPDIR}${/}some-process-started.txt
${STDOUT} %{TEMPDIR}/process-stdout-file.txt
${STDERR} %{TEMPDIR}/process-stderr-file.txt
${STDIN} %{TEMPDIR}/process-stdin-file.txt
${CWD} %{TEMPDIR}/process-cwd

*** Keywords ***
Expand Down
17 changes: 11 additions & 6 deletions atest/testdata/standard_libraries/process/stdin.robot
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
*** Settings ***
Library OperatingSystem
Library Process
Resource process_resource.robot

*** Test Cases ***
Stdin is PIPE by defauls
Expand Down Expand Up @@ -38,11 +37,17 @@ Stdin can be disabled with None object
Should Be Equal ${process.stdin} ${None}
Should Be Equal ${result.stdout} Hello, world!

Stdin as file
Create File %{TEMPDIR}/stdin.txt Hyvää päivää maailma! encoding=CONSOLE
${result} = Run Process python -c import sys; print(sys.stdin.read()) stdin=%{TEMPDIR}/stdin.txt
Stdin as path
Create File ${STDIN} Hyvää päivää maailma! encoding=CONSOLE
${result} = Run Process python -c import sys; print(sys.stdin.read()) stdin=${STDIN}
Should Be Equal ${result.stdout} Hyvää päivää maailma!
[Teardown] Remove File %{TEMPDIR}/stdin.txt
[Teardown] Remove File ${STDIN}

Stdin as `pathlib.Path`
Create File ${STDIN} Hyvää päivää maailma! encoding=CONSOLE
${result} = Run Process python -c import sys; print(sys.stdin.read()) stdin=${{pathlib.Path($STDIN)}}
Should Be Equal ${result.stdout} Hyvää päivää maailma!
[Teardown] Remove File ${STDIN}

Stdin as text
${result} = Run Process python -c import sys; print(sys.stdin.read()) stdin=Hyvää päivää maailma!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Custom stdout
${result} = Run Stdout Stderr Process stdout=${STDOUT}
Result Should Equal ${result} stdout stderr stdout_path=${STDOUT}

Custom stdout as `pathlib.Path`
${result} = Run Stdout Stderr Process stdout=${{pathlib.Path($STDOUT)}}
Result Should Equal ${result} stdout stderr stdout_path=${STDOUT}

Redirecting stdout to DEVNULL
${result} = Run Stdout Stderr Process stdout=DEVNULL
Should Not Exist ${EXECDIR}/DEVNULL
Expand All @@ -22,6 +26,10 @@ Custom stderr
${result} = Run Stdout Stderr Process stderr=${STDERR}
Result Should Equal ${result} stdout stderr stderr_path=${STDERR}

Custom stderr as `pathlib.Path`
${result} = Run Stdout Stderr Process stderr=${{pathlib.Path($STDERR)}}
Result Should Equal ${result} stdout stderr stderr_path=${STDERR}

Custom stdout and stderr
${result} = Run Stdout Stderr Process stdout=${STDOUT} stderr=${STDERR}
Result Should Equal ${result} stdout stderr stdout_path=${STDOUT} stderr_path=${STDERR}
Expand Down
25 changes: 10 additions & 15 deletions src/robot/libraries/Process.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
# limitations under the License.

import os
import signal as signal_module
import subprocess
import time
from tempfile import TemporaryFile
import signal as signal_module

from robot.utils import (abspath, cmdline2list, ConnectionCache, console_decode,
console_encode, is_list_like, is_string, is_truthy,
NormalizedDict, secs_to_timestr, system_decode,
console_encode, is_list_like, is_pathlike, is_string,
is_truthy, NormalizedDict, secs_to_timestr, system_decode,
system_encode, timestr_to_secs, WINDOWS)
from robot.version import get_version
from robot.api import logger
Expand Down Expand Up @@ -883,7 +883,7 @@ class ProcessConfiguration:

def __init__(self, cwd=None, shell=False, stdout=None, stderr=None, stdin='PIPE',
output_encoding='CONSOLE', alias=None, env=None, **rest):
self.cwd = self._get_cwd(cwd)
self.cwd = os.path.normpath(cwd) if cwd else abspath('.')
self.shell = is_truthy(shell)
self.alias = alias
self.output_encoding = output_encoding
Expand All @@ -892,11 +892,6 @@ def __init__(self, cwd=None, shell=False, stdout=None, stderr=None, stdin='PIPE'
self.stdin_stream = self._get_stdin(stdin)
self.env = self._construct_env(env, rest)

def _get_cwd(self, cwd):
if cwd:
return cwd.replace('/', os.sep)
return abspath('.')

def _new_stream(self, name):
if name == 'DEVNULL':
return open(os.devnull, 'w')
Expand All @@ -913,19 +908,19 @@ def _get_stderr(self, stderr, stdout, stdout_stream):
return self._new_stream(stderr)

def _get_stdin(self, stdin):
if not is_string(stdin):
if is_pathlike(stdin):
stdin = str(stdin)
elif not is_string(stdin):
return stdin
if stdin.upper() == 'NONE':
elif stdin.upper() == 'NONE':
return None
if stdin == 'PIPE':
elif stdin == 'PIPE':
return subprocess.PIPE
path = os.path.normpath(os.path.join(self.cwd, stdin))
if os.path.isfile(path):
return open(path)
stdin_file = TemporaryFile()
if is_string(stdin):
stdin = console_encode(stdin, self.output_encoding, force=True)
stdin_file.write(stdin)
stdin_file.write(console_encode(stdin, self.output_encoding, force=True))
stdin_file.seek(0)
return stdin_file

Expand Down

0 comments on commit 26e267c

Please sign in to comment.