Skip to content

Commit 2f78038

Browse files
CarstenGrohmannphilpep
authored andcommitted
Extend CommandResult documentation
1 parent 4807473 commit 2f78038

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

testinfra/backend/base.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ class HostSpec:
3434

3535

3636
class CommandResult:
37+
"""Object that encapsulates all returned details of the command execution.
38+
39+
Example:
40+
41+
>>> cmd = host.run("ls -l /etc/passwd")
42+
>>> cmd.rc
43+
0
44+
>>> cmd.stdout
45+
'-rw-r--r-- 1 root root 1790 Feb 11 00:28 /etc/passwd\\n'
46+
>>> cmd.stderr
47+
''
48+
>>> cmd.succeeded
49+
True
50+
>>> cmd.failed
51+
False
52+
"""
53+
3754
def __init__(
3855
self,
3956
backend: "BaseBackend",
@@ -82,24 +99,44 @@ def rc(self) -> int:
8299

83100
@property
84101
def stdout(self) -> str:
102+
"""Gets standard output (stdout) stream of an executed command
103+
104+
>>> host.run("mkdir -v new_directory").stdout
105+
mkdir: created directory 'new_directory'
106+
"""
85107
if self._stdout is None:
86108
self._stdout = self._backend.decode(self._stdout_bytes)
87109
return self._stdout
88110

89111
@property
90112
def stderr(self) -> str:
113+
"""Gets standard error (stderr) stream of an executed command
114+
115+
>>> host.run("mkdir new_directory").stderr
116+
mkdir: cannot create directory 'new_directory': File exists
117+
"""
91118
if self._stderr is None:
92119
self._stderr = self._backend.decode(self._stderr_bytes)
93120
return self._stderr
94121

95122
@property
96123
def stdout_bytes(self) -> bytes:
124+
"""Gets standard output (stdout) stream of an executed command as bytes
125+
126+
>>> host.run("mkdir -v new_directory").stdout_bytes
127+
b"mkdir: created directory 'new_directory'"
128+
"""
97129
if self._stdout_bytes is None:
98130
self._stdout_bytes = self._backend.encode(self._stdout)
99131
return self._stdout_bytes
100132

101133
@property
102134
def stderr_bytes(self) -> bytes:
135+
"""Gets standard error (stderr) stream of an executed command as bytes
136+
137+
>>> host.run("mkdir new_directory").stderr_bytes
138+
b"mkdir: cannot create directory 'new_directory': File exists"
139+
"""
103140
if self._stderr_bytes is None:
104141
self._stderr_bytes = self._backend.encode(self._stderr)
105142
return self._stderr_bytes

0 commit comments

Comments
 (0)