Skip to content

Commit

Permalink
Raise error in wait_output if stdout is already being consumed (#998)
Browse files Browse the repository at this point in the history
Error on already used stdout in wait_output
  • Loading branch information
weiiwang01 committed Aug 28, 2023
1 parent 470a535 commit c9bba5b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ops/pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,12 @@ def wait_output(self) -> Tuple[AnyStr, Optional[AnyStr]]:
ChangeError: if there was an error starting or running the process.
ExecError: if the process exits with a non-zero exit code.
"""
if self.stdout is None:
raise TypeError(
"can't use wait_output() when exec was called with the stdout argument; "
"use wait() instead"
)

if self._encoding is not None:
out = io.StringIO()
err = io.StringIO() if self.stderr is not None else None
Expand Down
9 changes: 9 additions & 0 deletions test/test_pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -2925,6 +2925,15 @@ def test_wait_output_send_stdin_bytes(self):
('TXT', '{"command":"end"}'),
])

def test_wait_output_no_stdout(self):
stdio, stderr, _ = self.add_responses('123', 0)
stdio.receives.append('{"command":"end"}')
stderr.receives.append('{"command":"end"}')
stdout_buffer = io.BytesIO()
process = self.client.exec(["echo", "FOOBAR"], stdout=stdout_buffer, encoding=None)
with self.assertRaises(TypeError):
process.wait_output()

def test_wait_output_bad_command(self):
stdio, stderr, _ = self.add_responses('123', 0)
stdio.receives.append(b'Python 3.8.10\n')
Expand Down

0 comments on commit c9bba5b

Please sign in to comment.