MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / test_restore_termios_settings

Function test_restore_termios_settings

tests/test_cmd2.py:1076–1112  ·  view source on GitHub ↗

Test that terminal settings are restored after a command and that errors are suppressed.

(base_app, monkeypatch, is_tty, settings_set, raised_exception, should_call)

Source from the content-addressed store, hash-verified

1074 ],
1075)
1076def test_restore_termios_settings(base_app, monkeypatch, is_tty, settings_set, raised_exception, should_call):
1077 """Test that terminal settings are restored after a command and that errors are suppressed."""
1078 import io
1079 import termios # Mock termios since it's imported within the method
1080
1081 termios_mock = mock.MagicMock()
1082 # The error attribute needs to be the actual exception for isinstance checks
1083 termios_mock.error = termios.error
1084 monkeypatch.setitem(sys.modules, "termios", termios_mock)
1085
1086 # Set the exception to be raised by tcsetattr
1087 if raised_exception == "termios_error":
1088 termios_mock.tcsetattr.side_effect = termios.error("test termios error")
1089 elif raised_exception == "unsupported_operation":
1090 termios_mock.tcsetattr.side_effect = io.UnsupportedOperation("test io error")
1091
1092 # Set initial termios settings so the logic will run
1093 if settings_set:
1094 termios_settings = ["dummy settings"]
1095 base_app._initial_termios_settings = termios_settings
1096 else:
1097 base_app._initial_termios_settings = None
1098 termios_settings = None # for the assert
1099
1100 # Mock stdin to make it look like a TTY
1101 monkeypatch.setattr(base_app.stdin, "isatty", lambda: is_tty)
1102 monkeypatch.setattr(base_app.stdin, "fileno", lambda: 0)
1103
1104 # Run a command to trigger _run_cmdfinalization_hooks
1105 # This should not raise an exception
1106 base_app.onecmd_plus_hooks("help")
1107
1108 # Verify that tcsetattr was called with the correct arguments
1109 if should_call:
1110 termios_mock.tcsetattr.assert_called_once_with(0, termios_mock.TCSANOW, termios_settings)
1111 else:
1112 termios_mock.tcsetattr.assert_not_called()
1113
1114
1115def test_sigint_handler(base_app) -> None:

Callers

nothing calls this directly

Calls 2

onecmd_plus_hooksMethod · 0.80
errorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…