Skip to content

Commit

Permalink
Extend CommandResult documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
CarstenGrohmann authored and philpep committed Nov 13, 2023
1 parent 5de375f commit 95fe141
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions testinfra/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ class HostSpec:


class CommandResult:
"""Object that encapsulates all returned details of the command execution.
Example:
>>> cmd = host.run("ls -l /etc/passwd")
>>> cmd.rc
0
>>> cmd.stdout
'-rw-r--r-- 1 root root 1790 Feb 11 00:28 /etc/passwd\\n'
>>> cmd.stderr
''
>>> cmd.succeeded
True
>>> cmd.failed
False
"""

def __init__(
self,
backend: "BaseBackend",
Expand Down Expand Up @@ -82,24 +99,44 @@ def rc(self) -> int:

@property
def stdout(self) -> str:
"""Gets standard output (stdout) stream of an executed command
>>> host.run("mkdir -v new_directory").stdout
mkdir: created directory 'new_directory'
"""
if self._stdout is None:
self._stdout = self._backend.decode(self._stdout_bytes)
return self._stdout

@property
def stderr(self) -> str:
"""Gets standard error (stderr) stream of an executed command
>>> host.run("mkdir new_directory").stderr
mkdir: cannot create directory 'new_directory': File exists
"""
if self._stderr is None:
self._stderr = self._backend.decode(self._stderr_bytes)
return self._stderr

@property
def stdout_bytes(self) -> bytes:
"""Gets standard output (stdout) stream of an executed command as bytes
>>> host.run("mkdir -v new_directory").stdout_bytes
b"mkdir: created directory 'new_directory'"
"""
if self._stdout_bytes is None:
self._stdout_bytes = self._backend.encode(self._stdout)
return self._stdout_bytes

@property
def stderr_bytes(self) -> bytes:
"""Gets standard error (stderr) stream of an executed command as bytes
>>> host.run("mkdir new_directory").stderr_bytes
b"mkdir: cannot create directory 'new_directory': File exists"
"""
if self._stderr_bytes is None:
self._stderr_bytes = self._backend.encode(self._stderr)
return self._stderr_bytes
Expand Down

0 comments on commit 95fe141

Please sign in to comment.