diff --git a/ops/pebble.py b/ops/pebble.py index f8ce123fe..8d107cb84 100644 --- a/ops/pebble.py +++ b/ops/pebble.py @@ -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 diff --git a/test/test_pebble.py b/test/test_pebble.py index 44e1f7dc4..6c9b6b7fe 100644 --- a/test/test_pebble.py +++ b/test/test_pebble.py @@ -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')