diff --git a/CHANGELOG.md b/CHANGELOG.md index a45eaff0a..73728f807 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed selective enabling of highlighting when disabled in the `Console` https://github.com/Textualize/rich/issues/3419 - Fixed BrokenPipeError writing an error message https://github.com/Textualize/rich/pull/3468 - Fixed superfluous space above Markdown tables https://github.com/Textualize/rich/pull/3469 +- Fixed issue with record and capture interaction ### Changed diff --git a/rich/console.py b/rich/console.py index cdfac2827..9e697327b 100644 --- a/rich/console.py +++ b/rich/console.py @@ -2029,7 +2029,7 @@ def _write_buffer(self) -> None: """Write the buffer to the output file.""" with self._lock: - if self.record: + if self.record and not self._buffer_index: with self._record_buffer_lock: self._record_buffer.extend(self._buffer[:]) diff --git a/tests/test_console.py b/tests/test_console.py index 4b4da8fe5..0340fc90e 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -381,23 +381,6 @@ def test_capture(): assert capture.get() == "Hello\n" -def test_capture_and_record(capsys): - recorder = Console(record=True) - recorder.print("ABC") - - with recorder.capture() as capture: - recorder.print("Hello") - - assert capture.get() == "Hello\n" - - recorded_text = recorder.export_text() - out, err = capsys.readouterr() - - assert recorded_text == "ABC\nHello\n" - assert capture.get() == "Hello\n" - assert out == "ABC\n" - - def test_input(monkeypatch, capsys): def fake_input(prompt=""): console.file.write(prompt) @@ -1038,3 +1021,24 @@ def test_brokenpipeerror() -> None: proc2.wait() assert proc1.returncode == 1 assert proc2.returncode == 0 + + +def test_capture_and_record() -> None: + """Regression test for https://github.com/Textualize/rich/issues/2563""" + + console = Console(record=True) + print("Before Capture started:") + console.print("[blue underline]Print 0") + with console.capture() as capture: + console.print("[blue underline]Print 1") + console.print("[blue underline]Print 2") + console.print("[blue underline]Print 3") + console.print("[blue underline]Print 4") + + capture_content = capture.get() + print(repr(capture_content)) + assert capture_content == "Print 1\nPrint 2\nPrint 3\nPrint 4\n" + + recorded_content = console.export_text() + print(repr(recorded_content)) + assert recorded_content == "Print 0\n"