@@ -4210,6 +4210,140 @@ def utf8_console():
42104210 assert events == ["utf-8 console" , "pager started" , "pager exited" , "restored" ]
42114211
42124212
4213+ class FakeTerminal (io .TextIOWrapper ):
4214+ """A real file which says it is a terminal, so that a pager's shell can be given it to write to."""
4215+
4216+ def isatty (self ) -> bool :
4217+ return True
4218+
4219+
4220+ @pytest .fixture
4221+ def paging_app (outsim_app , monkeypatch , tmp_path ):
4222+ """An application which will page, and whatever reaches its terminal."""
4223+ stdin_mock = mock .MagicMock ()
4224+ stdin_mock .isatty .return_value = True
4225+ monkeypatch .setattr (outsim_app , "stdin" , stdin_mock )
4226+ if not sys .platform .startswith ("win" ) and os .environ .get ("TERM" ) is None :
4227+ monkeypatch .setenv ("TERM" , "simulated" )
4228+ with FakeTerminal ((tmp_path / "terminal" ).open ("w+b" ), encoding = "utf-8" ) as terminal :
4229+ monkeypatch .setattr (outsim_app , "stdout" , terminal )
4230+
4231+ def shown () -> str :
4232+ terminal .flush ()
4233+ terminal .seek (0 )
4234+ return terminal .read ()
4235+
4236+ yield outsim_app , shown
4237+
4238+
4239+ @pytest .mark .parametrize ("chop" , [True , False ])
4240+ def test_ppaged_shows_the_output_when_the_pager_cannot_be_run (paging_app , capfd , chop ) -> None :
4241+ """A pager which is not installed must not cost the user the output.
4242+
4243+ The shell reports the missing program and exits. Nothing has been shown by then, and
4244+ without the pager there is nobody else to show it.
4245+ """
4246+ app , shown = paging_app
4247+ app .pager = app .pager_chop = "cmd2-no-such-pager-program --some-option"
4248+
4249+ app .ppaged ("first line\n second line" , chop = chop )
4250+
4251+ assert shown () == "first line\n second line\n "
4252+ # The shell said why there was no pager.
4253+ assert "cmd2-no-such-pager-program" in capfd .readouterr ().err
4254+
4255+
4256+ def test_ppaged_without_a_pager_wraps_as_the_caller_asked (paging_app ) -> None :
4257+ """Chopping turns soft wrap on for a pager to scroll sideways. With no pager, the caller's choice stands."""
4258+ app , shown = paging_app
4259+ app .pager_chop = "cmd2-no-such-pager-program"
4260+
4261+ app .ppaged ("word " * 400 , chop = True , soft_wrap = False )
4262+
4263+ assert len (shown ().splitlines ()) > 1
4264+
4265+
4266+ def test_ppaged_does_not_repeat_output_after_a_pager_which_ran_and_failed (paging_app ) -> None :
4267+ """Only a pager which could not be started is made up for: one which ran has had the output."""
4268+ app , shown = paging_app
4269+ app .pager = f'"{ sys .executable } " -c "import sys; sys.stdin.read(); sys.exit(1)"'
4270+
4271+ app .ppaged ("first line\n second line" )
4272+
4273+ assert shown () == ""
4274+
4275+
4276+ class RendersOnce :
4277+ """A renderable which only has its lines to give the first time it is rendered."""
4278+
4279+ def __init__ (self , * lines : str ) -> None :
4280+ self ._lines = iter (lines )
4281+
4282+ def __rich_console__ (self , console , options ):
4283+ yield from self ._lines
4284+
4285+
4286+ def test_ppaged_does_not_try_again_a_pager_which_cannot_be_run (paging_app , capfd ) -> None :
4287+ """The shell says once that there is no such pager, not every time something is paged.
4288+
4289+ From then on nothing is rendered for a pager either, only for poutput().
4290+ """
4291+ app , shown = paging_app
4292+ app .pager = "cmd2-no-such-pager-program"
4293+
4294+ app .ppaged ("first" )
4295+ assert "cmd2-no-such-pager-program" in capfd .readouterr ().err
4296+
4297+ with mock .patch ("subprocess.Popen" ) as popen_mock :
4298+ app .ppaged (RendersOnce ("second" ))
4299+
4300+ popen_mock .assert_not_called ()
4301+ assert capfd .readouterr ().err == ""
4302+ assert shown () == "first\n second\n "
4303+
4304+
4305+ def test_ppaged_tries_another_pager_after_one_which_cannot_be_run (paging_app ) -> None :
4306+ """Only the command which could not be run is given up on, so setting a pager which works is enough."""
4307+ app , shown = paging_app
4308+ app .pager = "cmd2-no-such-pager-program"
4309+ app .ppaged ("unpaged" )
4310+
4311+ app .pager = f'"{ sys .executable } " -c "import sys; sys.stdout.write(sys.stdin.read().upper())"'
4312+ app .ppaged ("paged" )
4313+
4314+ assert shown () == "unpaged\n PAGED\n "
4315+
4316+
4317+ @pytest .mark .parametrize (
4318+ ("platform" , "command" , "returncode" , "found" , "expected" ),
4319+ [
4320+ pytest .param ("linux" , "less -RXF" , 127 , True , True , id = "a POSIX shell found no such command" ),
4321+ pytest .param ("linux" , "less -RXF" , 126 , True , True , id = "a POSIX shell could not execute it" ),
4322+ pytest .param ("linux" , "less -RXF" , 1 , False , False , id = "a POSIX pager failed by itself" ),
4323+ pytest .param ("linux" , "less -RXF" , 0 , False , False , id = "a POSIX pager succeeded" ),
4324+ pytest .param ("win32" , "less -RXF" , 1 , False , True , id = "cmd.exe found no such command" ),
4325+ pytest .param ("win32" , '"C:\\ Program Files\\ less.exe" -R' , 1 , False , True , id = "cmd.exe found no such quoted path" ),
4326+ pytest .param ("win32" , "more" , 1 , True , False , id = "a Windows pager failed by itself" ),
4327+ pytest .param ("win32" , "less -RXF" , 0 , False , False , id = "a Windows pager succeeded" ),
4328+ pytest .param ("win32" , 'less "unbalanced' , 1 , False , False , id = "a Windows command which cannot be taken apart" ),
4329+ pytest .param ("win32" , "" , 1 , False , False , id = "no Windows command at all" ),
4330+ ],
4331+ )
4332+ def test_pager_failed_to_start (monkeypatch , platform , command , returncode , found , expected ) -> None :
4333+ looked_for = []
4334+
4335+ def which (program : str ) -> str | None :
4336+ looked_for .append (program )
4337+ return program if found else None
4338+
4339+ monkeypatch .setattr (sys , "platform" , platform )
4340+ monkeypatch .setattr ("shutil.which" , which )
4341+
4342+ assert cmd2 .Cmd ._pager_failed_to_start (command , returncode ) is expected
4343+ # A quoted program is looked for by its name, not with its quotes.
4344+ assert all (not program .startswith ('"' ) for program in looked_for )
4345+
4346+
42134347def test_ppaged_no_pager (outsim_app ) -> None :
42144348 """Since we're not in a fully-functional terminal, ppaged() will just call poutput()."""
42154349 msg = "testing..."
0 commit comments