MCPcopy
hub / github.com/pallets/click / test_echo_via_pager_streams_each_write

Function test_echo_via_pager_streams_each_write

tests/test_termui.py:730–765  ·  view source on GitHub ↗

Each write is flushed so a slow generator streams to the pager incrementally instead of buffering until the end (issues #3242, #2542).

(monkeypatch)

Source from the content-addressed store, hash-verified

728
729
730def 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
768def test_get_pager_file_pager_missing_binary_falls_back(monkeypatch, tmp_path):

Callers

nothing calls this directly

Calls 3

RecordingStreamClass · 0.85
generateFunction · 0.85
getvalueMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…