Check `PAGER` usage, color preservation and edge-cases
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kevin Deldycke · committed by Kevin Deldycke · parent dbdae17 · Browse files · 1 file·+64 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+64 added−1 removed
- tests:+64 added−1 removed
Provenance
- Commit
98302ac- Tree
6c9d3b6- Author
Kevin Deldycke <kevin@deldycke.com>- Committer
Kevin Deldycke <kevin@deldycke.com>- Parent
dbdae17
1 file changed+64 added−1 removed
modified:tests/test_termui.py+64 added-1 removed
632 unmodified lines633 return pager_path634 635 636 def _run_get_pager_file_with_real_pager(monkeypatch, capfd, writer, color=False):637- """Run through the platform pager backend selected by ``PAGER``."""637+ """Run through the pipe pager backend selected by ``PAGER``."""638 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)639 monkeypatch.setitem(640 click._termui_impl.os.environ, "PAGER", _get_real_pager_command()641 )57 unmodified lines699 pytest.param(False, "hello\n", id="strip ansi"),700 pytest.param(True, click.style("hello", fg="red") + "\n", id="preserve ansi"),701 ],702 )703+def test_echo_via_pager_real_pager_handles_ansi(monkeypatch, capfd, color, expected):704+ """``echo_via_pager`` should honor ``color`` like ``get_pager_file``."""705+ monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)706+ monkeypatch.setitem(707+ click._termui_impl.os.environ, "PAGER", _get_real_pager_command()708+ )709+710+ click.echo_via_pager(click.style("hello", fg="red"), color=color)711+712+ out, err = capfd.readouterr()713+ assert err == ""714+ assert out == expected715+716+717+def test_get_pager_file_pager_missing_binary_falls_back(monkeypatch, tmp_path):718+ """``PAGER`` pointing to a nonexistent binary falls back to the text stdout."""719+ pager_out = tmp_path / "pager_out.txt"720+721+ monkeypatch.setitem(722+ click._termui_impl.os.environ,723+ "PAGER",724+ "click-tests-nonexistent-pager-9b3f2",725+ )726+ monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)727+728+ with pager_out.open("w", encoding="utf-8") as text_stream:729+ monkeypatch.setattr(730+ click._termui_impl, "_default_text_stdout", lambda: text_stream731+ )732+733+ with click.get_pager_file() as pager:734+ pager.write("hello\n")735+736+ assert pager_out.read_text(encoding="utf-8") == "hello\n"737+738+739+def test_get_pager_file_pager_unset_falls_back_when_no_default(monkeypatch, tmp_path):740+ """``PAGER`` unset still works when the platform default isn't installed."""741+ pager_out = tmp_path / "pager_out.txt"742+743+ monkeypatch.delitem(click._termui_impl.os.environ, "PAGER", raising=False)744+ monkeypatch.delitem(click._termui_impl.os.environ, "TERM", raising=False)745+ monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)746+ monkeypatch.setattr(shutil, "which", lambda _name: None)747+748+ with pager_out.open("w", encoding="utf-8") as text_stream:749+ monkeypatch.setattr(750+ click._termui_impl, "_default_text_stdout", lambda: text_stream751+ )752+753+ with click.get_pager_file() as pager:754+ pager.write("hello\n")755+756+ assert pager_out.read_text(encoding="utf-8") == "hello\n"757+758+759+@pytest.mark.parametrize(760+ ("color", "expected"),761+ [762+ pytest.param(False, "hello\n", id="strip ansi"),763+ pytest.param(True, click.style("hello", fg="red") + "\n", id="preserve ansi"),764+ ],765+)766 def test_get_pager_file_nullpager_wraps_textio_stream(767 monkeypatch, tmp_path, color, expected768 ):769 """When paging falls back to a real TextIO stream, ``.buffer`` is wrapped."""693 unmodified lines