@@ -2058,6 +2058,19 @@ def test_poutput_ansi_terminal(outsim_app) -> None:
20582058 assert out == expected
20592059
20602060
2061+ def test_broken_pipe_error (outsim_app , monkeypatch , capsys ):
2062+ write_mock = mock .MagicMock ()
2063+ write_mock .side_effect = BrokenPipeError
2064+ monkeypatch .setattr ("cmd2.utils.StdSim.write" , write_mock )
2065+
2066+ outsim_app .broken_pipe_warning = "The pipe broke"
2067+ outsim_app .poutput ("My test string" )
2068+
2069+ out , err = capsys .readouterr ()
2070+ assert not out
2071+ assert outsim_app .broken_pipe_warning in err
2072+
2073+
20612074# These are invalid names for aliases and macros
20622075invalid_command_name = [
20632076 '""' , # Blank name
@@ -2519,8 +2532,35 @@ def test_pexcept_not_exception(base_app, capsys) -> None:
25192532 assert err .startswith ("\x1b [91mEXCEPTION of type 'bool' occurred with message: False" )
25202533
25212534
2522- def test_ppaged (outsim_app ) -> None :
2523- """ppaged() will just call poutput() since a pager won't run while testing."""
2535+ @pytest .mark .parametrize ('chop' , [True , False ])
2536+ def test_ppaged_with_pager (outsim_app , monkeypatch , chop ) -> None :
2537+ """Force ppaged() to run the pager by mocking an actual terminal state."""
2538+
2539+ # Make it look like we're in a terminal
2540+ stdin_mock = mock .MagicMock ()
2541+ stdin_mock .isatty .return_value = True
2542+ monkeypatch .setattr (outsim_app , "stdin" , stdin_mock )
2543+
2544+ stdout_mock = mock .MagicMock ()
2545+ stdout_mock .isatty .return_value = True
2546+ monkeypatch .setattr (outsim_app , "stdout" , stdout_mock )
2547+
2548+ if not sys .platform .startswith ('win' ) and os .environ .get ("TERM" ) is None :
2549+ monkeypatch .setenv ('TERM' , 'simulated' )
2550+
2551+ # This will force ppaged to call Popen to run a pager
2552+ popen_mock = mock .MagicMock (name = 'Popen' )
2553+ monkeypatch .setattr ("subprocess.Popen" , popen_mock )
2554+ outsim_app .ppaged ("Test" , chop = chop )
2555+
2556+ # Verify the correct pager was run
2557+ expected_cmd = outsim_app .pager_chop if chop else outsim_app .pager
2558+ assert len (popen_mock .call_args_list ) == 1
2559+ assert expected_cmd == popen_mock .call_args_list [0 ].args [0 ]
2560+
2561+
2562+ def test_ppaged_no_pager (outsim_app ) -> None :
2563+ """Since we're not in a fully-functional terminal, ppaged() will just call poutput()."""
25242564 msg = 'testing...'
25252565 end = '\n '
25262566 outsim_app .ppaged (msg )
0 commit comments