@@ -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,36 @@ 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+ def test_ppaged_with_pager (outsim_app , monkeypatch ) -> None :
2536+ """Force ppaged() to run the pager by mocking an actual terminal state."""
2537+
2538+ # Make it look like we're in a terminal
2539+ stdin_mock = mock .MagicMock ()
2540+ stdin_mock .isatty .return_value = True
2541+ monkeypatch .setattr (outsim_app , "stdin" , stdin_mock )
2542+
2543+ stdout_mock = mock .MagicMock ()
2544+ stdout_mock .isatty .return_value = True
2545+ monkeypatch .setattr (outsim_app , "stdout" , stdout_mock )
2546+
2547+ if not sys .platform .startswith ('win' ):
2548+ orig_term = os .environ .get ("TERM" )
2549+ os .environ .setdefault ('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 = True )
2555+
2556+ # Restore TERM
2557+ if not sys .platform .startswith ('win' ):
2558+ os .environ ['TERM' ] = orig_term
2559+
2560+ popen_mock .assert_called_once ()
2561+
2562+
2563+ def test_ppaged_no_pager (outsim_app ) -> None :
2564+ """Since we're not in a fully-functional terminal, ppaged() will just call poutput()."""
25242565 msg = 'testing...'
25252566 end = '\n '
25262567 outsim_app .ppaged (msg )
0 commit comments