Skip to content

Commit e2a982d

Browse files
authored
Merge pull request #3470 from Textualize/fix-record-and-capture
Fix record and capture
2 parents 0ba607d + 05428ec commit e2a982d

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Fixed selective enabling of highlighting when disabled in the `Console` https://github.com/Textualize/rich/issues/3419
2121
- Fixed BrokenPipeError writing an error message https://github.com/Textualize/rich/pull/3468
2222
- Fixed superfluous space above Markdown tables https://github.com/Textualize/rich/pull/3469
23+
- Fixed issue with record and capture interaction https://github.com/Textualize/rich/pull/3470
2324

2425
### Changed
2526

rich/console.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2029,7 +2029,7 @@ def _write_buffer(self) -> None:
20292029
"""Write the buffer to the output file."""
20302030

20312031
with self._lock:
2032-
if self.record:
2032+
if self.record and not self._buffer_index:
20332033
with self._record_buffer_lock:
20342034
self._record_buffer.extend(self._buffer[:])
20352035

tests/test_console.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -381,23 +381,6 @@ def test_capture():
381381
assert capture.get() == "Hello\n"
382382

383383

384-
def test_capture_and_record(capsys):
385-
recorder = Console(record=True)
386-
recorder.print("ABC")
387-
388-
with recorder.capture() as capture:
389-
recorder.print("Hello")
390-
391-
assert capture.get() == "Hello\n"
392-
393-
recorded_text = recorder.export_text()
394-
out, err = capsys.readouterr()
395-
396-
assert recorded_text == "ABC\nHello\n"
397-
assert capture.get() == "Hello\n"
398-
assert out == "ABC\n"
399-
400-
401384
def test_input(monkeypatch, capsys):
402385
def fake_input(prompt=""):
403386
console.file.write(prompt)
@@ -1038,3 +1021,24 @@ def test_brokenpipeerror() -> None:
10381021
proc2.wait()
10391022
assert proc1.returncode == 1
10401023
assert proc2.returncode == 0
1024+
1025+
1026+
def test_capture_and_record() -> None:
1027+
"""Regression test for https://github.com/Textualize/rich/issues/2563"""
1028+
1029+
console = Console(record=True)
1030+
print("Before Capture started:")
1031+
console.print("[blue underline]Print 0")
1032+
with console.capture() as capture:
1033+
console.print("[blue underline]Print 1")
1034+
console.print("[blue underline]Print 2")
1035+
console.print("[blue underline]Print 3")
1036+
console.print("[blue underline]Print 4")
1037+
1038+
capture_content = capture.get()
1039+
print(repr(capture_content))
1040+
assert capture_content == "Print 1\nPrint 2\nPrint 3\nPrint 4\n"
1041+
1042+
recorded_content = console.export_text()
1043+
print(repr(recorded_content))
1044+
assert recorded_content == "Print 0\n"

0 commit comments

Comments
 (0)