From 051dd0452c6d8156d729bf3fccb578d275428010 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Wed, 15 Jun 2022 17:07:04 +0100 Subject: [PATCH 1/3] Fix interaction between Console capture and record --- rich/console.py | 8 ++++---- tests/test_console.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/rich/console.py b/rich/console.py index eba27611d..230ca769c 100644 --- a/rich/console.py +++ b/rich/console.py @@ -1959,11 +1959,11 @@ def _check_buffer(self) -> None: del self._buffer[:] return with self._lock: - if self._buffer_index == 0: + if self.record: + with self._record_buffer_lock: + self._record_buffer.extend(self._buffer[:]) - if self.record: - with self._record_buffer_lock: - self._record_buffer.extend(self._buffer[:]) + if self._buffer_index == 0: if self.is_jupyter: # pragma: no cover from .jupyter import display diff --git a/tests/test_console.py b/tests/test_console.py index 828d37757..6bb15b655 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -351,6 +351,20 @@ def test_capture(): assert capture.get() == "Hello\n" +def test_capture_and_record(): + recorder = Console(record=True) + recorder.print("ABC") + + with recorder.capture() as capture: + recorder.print("Hello") + + assert capture.get() == "Hello\n" + + text = recorder.export_text() + assert text == "ABC\nHello\n" + assert capture.get() == "Hello\n" + + def test_input(monkeypatch, capsys): def fake_input(prompt=""): console.file.write(prompt) From 3c1159d2e876080affc3da55e55e00170db6a792 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 16 Jun 2022 10:27:05 +0100 Subject: [PATCH 2/3] Ensure the correct stuff is written to stdout when recording and capturing --- tests/test_console.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_console.py b/tests/test_console.py index 6bb15b655..2db88af0b 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -351,7 +351,7 @@ def test_capture(): assert capture.get() == "Hello\n" -def test_capture_and_record(): +def test_capture_and_record(capsys): recorder = Console(record=True) recorder.print("ABC") @@ -360,9 +360,12 @@ def test_capture_and_record(): assert capture.get() == "Hello\n" - text = recorder.export_text() - assert text == "ABC\nHello\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): From d110847eb83aff70cda4a10ec4c67fc66a8d2251 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Thu, 16 Jun 2022 10:31:40 +0100 Subject: [PATCH 3/3] Update Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88e045948..ffaf36b14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Allow exceptions that are raised while a Live is rendered to be displayed and/or processed https://github.com/Textualize/rich/pull/2305 - Fix crashes that can happen with `inspect` when docstrings contain some special control codes https://github.com/Textualize/rich/pull/2294 - Fix edges used in first row of tables when `show_header=False` https://github.com/Textualize/rich/pull/2330 +- Fix interaction between `Capture` contexts and `Console(record=True)` https://github.com/Textualize/rich/pull/2343 ## [12.4.4] - 2022-05-24