MCPcopy Create free account
hub / github.com/python-cmd2/cmd2 / test_ppaged_terminal_restoration_exceptions

Function test_ppaged_terminal_restoration_exceptions

tests/test_cmd2.py:3345–3385  ·  view source on GitHub ↗

Test that terminal restoration in ppaged() handles exceptions gracefully.

(outsim_app, monkeypatch)

Source from the content-addressed store, hash-verified

3343
3344@pytest.mark.skipif(sys.platform.startswith("win"), reason="termios is not available on Windows")
3345def test_ppaged_terminal_restoration_exceptions(outsim_app, monkeypatch) -> None:
3346 """Test that terminal restoration in ppaged() handles exceptions gracefully."""
3347 # Make it look like we're in a terminal
3348 stdin_mock = mock.MagicMock()
3349 stdin_mock.isatty.return_value = True
3350 stdin_mock.fileno.return_value = 0
3351 monkeypatch.setattr(outsim_app, "stdin", stdin_mock)
3352
3353 stdout_mock = mock.MagicMock()
3354 stdout_mock.isatty.return_value = True
3355 monkeypatch.setattr(outsim_app, "stdout", stdout_mock)
3356
3357 if not sys.platform.startswith("win") and os.environ.get("TERM") is None:
3358 monkeypatch.setenv("TERM", "simulated")
3359
3360 # Mock termios and make it raise an error
3361 termios_mock = mock.MagicMock()
3362 import termios
3363
3364 termios_mock.error = termios.error
3365 termios_mock.tcsetattr.side_effect = termios.error("Restoration failed")
3366 monkeypatch.setitem(sys.modules, "termios", termios_mock)
3367
3368 monkeypatch.setitem(sys.modules, "signal", mock.MagicMock())
3369
3370 # Mock os.tcsetpgrp and os.getpgrp to prevent OSError before tcsetattr
3371 monkeypatch.setattr(os, "tcsetpgrp", mock.Mock(), raising=False)
3372 monkeypatch.setattr(os, "getpgrp", mock.Mock(return_value=123), raising=False)
3373
3374 # Mock subprocess.Popen
3375 popen_mock = mock.MagicMock(name="Popen")
3376 monkeypatch.setattr("subprocess.Popen", popen_mock)
3377
3378 # Set initial termios settings
3379 outsim_app._initial_termios_settings = ["dummy settings"]
3380
3381 # Call ppaged - should not raise exception
3382 outsim_app.ppaged("Test")
3383
3384 # Verify tcsetattr was attempted
3385 assert termios_mock.tcsetattr.called
3386
3387
3388@pytest.mark.skipif(sys.platform.startswith("win"), reason="termios is not available on Windows")

Callers

nothing calls this directly

Calls 3

ppagedMethod · 0.80
getMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…