Skip to content
Prev Previous commit
Next Next commit
Maintain context manager syntax
  • Loading branch information
jtranq committed Feb 25, 2025
commit ca46d3d2d53551373cf8da48b02d7c89eae95456
14 changes: 4 additions & 10 deletions Lib/test/test_getpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
try:
import msvcrt
except ImportError:
mscvrt = None
msvcrt = None

@mock.patch('os.environ')
class GetpassGetuserTest(unittest.TestCase):
Comment thread
jtranq marked this conversation as resolved.
Expand Down Expand Up @@ -185,16 +185,11 @@ def test_uses_msvcrt_directly(self):
mock_stream.flush.assert_called()

def test_handles_backspace(self):
with (
mock.patch('msvcrt.getch') as getch,
mock.patch('msvcrt.putch') as putch,
):
with mock.patch('msvcrt.getch') as getch, \
mock.patch('msvcrt.putch') as putch:
getch.side_effect = [b'a', b'b', b'\b', b'c', b'\r']
result = getpass.win_getpass()
self.assertEqual(result, 'ac')
# Verify putch was called to handle the backspace (erase character)
# The exact sequence depends on the implementation, but should include
# calls to handle the backspace character
putch.assert_any_call(b'\b')

def test_handles_ctrl_c(self):
Expand All @@ -214,8 +209,7 @@ def test_flushes_stream_after_input(self):

def test_falls_back_to_fallback_if_msvcrt_raises(self):
with mock.patch('msvcrt.getch') as getch, \
mock.patch('getpass.fallback_getpass') as fallback:
# Make getch raise an exception to trigger the fallback
mock.patch('getpass.fallback_getpass') as fallback:
getch.side_effect = RuntimeError("Simulated msvcrt failure")
mock_stream = mock.Mock(spec=StringIO)
getpass.win_getpass(stream=mock_stream)
Expand Down