Skip to content

Commit

Permalink
Fix record and capture
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Aug 26, 2024
1 parent 0ba607d commit a3b27a3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[:])

Expand Down
38 changes: 21 additions & 17 deletions tests/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"

0 comments on commit a3b27a3

Please sign in to comment.