Test that terminal restoration in ppaged() handles OSError gracefully.
(outsim_app, monkeypatch)
| 3421 | |
| 3422 | @pytest.mark.skipif(sys.platform.startswith("win"), reason="termios is not available on Windows") |
| 3423 | def test_ppaged_terminal_restoration_oserror(outsim_app, monkeypatch) -> None: |
| 3424 | """Test that terminal restoration in ppaged() handles OSError gracefully.""" |
| 3425 | # Make it look like we're in a terminal |
| 3426 | stdin_mock = mock.MagicMock() |
| 3427 | stdin_mock.isatty.return_value = True |
| 3428 | stdin_mock.fileno.return_value = 0 |
| 3429 | monkeypatch.setattr(outsim_app, "stdin", stdin_mock) |
| 3430 | |
| 3431 | stdout_mock = mock.MagicMock() |
| 3432 | stdout_mock.isatty.return_value = True |
| 3433 | monkeypatch.setattr(outsim_app, "stdout", stdout_mock) |
| 3434 | |
| 3435 | if not sys.platform.startswith("win") and os.environ.get("TERM") is None: |
| 3436 | monkeypatch.setenv("TERM", "simulated") |
| 3437 | |
| 3438 | # Mock signal |
| 3439 | monkeypatch.setitem(sys.modules, "signal", mock.MagicMock()) |
| 3440 | |
| 3441 | # Mock os.tcsetpgrp to raise OSError |
| 3442 | monkeypatch.setattr(os, "tcsetpgrp", mock.Mock(side_effect=OSError("Permission denied")), raising=False) |
| 3443 | monkeypatch.setattr(os, "getpgrp", mock.Mock(return_value=123), raising=False) |
| 3444 | |
| 3445 | # Mock termios |
| 3446 | termios_mock = mock.MagicMock() |
| 3447 | import termios |
| 3448 | |
| 3449 | termios_mock.error = termios.error |
| 3450 | monkeypatch.setitem(sys.modules, "termios", termios_mock) |
| 3451 | |
| 3452 | # Mock subprocess.Popen |
| 3453 | popen_mock = mock.MagicMock(name="Popen") |
| 3454 | monkeypatch.setattr("subprocess.Popen", popen_mock) |
| 3455 | |
| 3456 | # Set initial termios settings |
| 3457 | outsim_app._initial_termios_settings = ["dummy settings"] |
| 3458 | |
| 3459 | # Call ppaged - should not raise exception |
| 3460 | outsim_app.ppaged("Test") |
| 3461 | |
| 3462 | # Verify tcsetpgrp was attempted and OSError was caught |
| 3463 | assert os.tcsetpgrp.called |
| 3464 | # tcsetattr should have been skipped due to OSError being raised before it |
| 3465 | assert not termios_mock.tcsetattr.called |
| 3466 | |
| 3467 | |
| 3468 | def test_ppretty(base_app: cmd2.Cmd) -> None: |