Each write is flushed so a slow generator streams to the pager incrementally instead of buffering until the end (issues #3242, #2542).
(monkeypatch)
| 728 | |
| 729 | |
| 730 | def test_echo_via_pager_streams_each_write(monkeypatch): |
| 731 | """Each write is flushed so a slow generator streams to the pager |
| 732 | incrementally instead of buffering until the end (issues #3242, #2542). |
| 733 | """ |
| 734 | calls = [] |
| 735 | |
| 736 | class RecordingStream(io.StringIO): |
| 737 | def __init__(self): |
| 738 | super().__init__() |
| 739 | self.color = None |
| 740 | |
| 741 | def write(self, s): |
| 742 | calls.append("write") |
| 743 | return super().write(s) |
| 744 | |
| 745 | def flush(self): |
| 746 | calls.append("flush") |
| 747 | |
| 748 | stream = RecordingStream() |
| 749 | monkeypatch.setattr(click._termui_impl, "isatty", lambda _: False) |
| 750 | monkeypatch.setattr(click._termui_impl, "_default_text_stdout", lambda: stream) |
| 751 | |
| 752 | def generate(): |
| 753 | yield "a\n" |
| 754 | yield "b\n" |
| 755 | yield "c\n" |
| 756 | |
| 757 | click.echo_via_pager(generate()) |
| 758 | |
| 759 | # No two writes are adjacent: every chunk is flushed before the next one, |
| 760 | # so the pager sees output as it is produced. |
| 761 | assert not any( |
| 762 | calls[i] == "write" and calls[i + 1] == "write" for i in range(len(calls) - 1) |
| 763 | ) |
| 764 | assert calls.count("write") == 4 # three chunks plus the trailing newline |
| 765 | assert stream.getvalue() == "a\nb\nc\n\n" |
| 766 | |
| 767 | |
| 768 | def test_get_pager_file_pager_missing_binary_falls_back(monkeypatch, tmp_path): |
nothing calls this directly
no test coverage detected
searching dependent graphs…