diff options
author | Semyon Moroz <donbarbos@proton.me> | 2025-05-06 15:56:20 +0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-06 14:56:20 +0300 |
commit | bf8bbe9a813dd9fc2dd14be06df172b7d26ca1af (patch) | |
tree | bdee42507770fa676f4096347272baf97f7c16aa /Lib/test/test_getpass.py | |
parent | 53e6d76aa30eb760fb8ff788815f22a0e6c101cd (diff) | |
download | cpython-bf8bbe9a813dd9fc2dd14be06df172b7d26ca1af.tar.gz cpython-bf8bbe9a813dd9fc2dd14be06df172b7d26ca1af.zip |
gh-77065: Add optional keyword-only argument `echo_char` for `getpass.getpass` (#130496)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Diffstat (limited to 'Lib/test/test_getpass.py')
-rw-r--r-- | Lib/test/test_getpass.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Lib/test/test_getpass.py b/Lib/test/test_getpass.py index 80dda2caaa3..ab36535a1cf 100644 --- a/Lib/test/test_getpass.py +++ b/Lib/test/test_getpass.py @@ -161,6 +161,45 @@ class UnixGetpassTest(unittest.TestCase): self.assertIn('Warning', stderr.getvalue()) self.assertIn('Password:', stderr.getvalue()) + def test_echo_char_replaces_input_with_asterisks(self): + mock_result = '*************' + with mock.patch('os.open') as os_open, \ + mock.patch('io.FileIO'), \ + mock.patch('io.TextIOWrapper') as textio, \ + mock.patch('termios.tcgetattr'), \ + mock.patch('termios.tcsetattr'), \ + mock.patch('getpass._raw_input') as mock_input: + os_open.return_value = 3 + mock_input.return_value = mock_result + + result = getpass.unix_getpass(echo_char='*') + mock_input.assert_called_once_with('Password: ', textio(), + input=textio(), echo_char='*') + self.assertEqual(result, mock_result) + + def test_raw_input_with_echo_char(self): + passwd = 'my1pa$$word!' + mock_input = StringIO(f'{passwd}\n') + mock_output = StringIO() + with mock.patch('sys.stdin', mock_input), \ + mock.patch('sys.stdout', mock_output): + result = getpass._raw_input('Password: ', mock_output, mock_input, + '*') + self.assertEqual(result, passwd) + self.assertEqual('Password: ************', mock_output.getvalue()) + + def test_control_chars_with_echo_char(self): + passwd = 'pass\twd\b' + expect_result = 'pass\tw' + mock_input = StringIO(f'{passwd}\n') + mock_output = StringIO() + with mock.patch('sys.stdin', mock_input), \ + mock.patch('sys.stdout', mock_output): + result = getpass._raw_input('Password: ', mock_output, mock_input, + '*') + self.assertEqual(result, expect_result) + self.assertEqual('Password: *******\x08 \x08', mock_output.getvalue()) + if __name__ == "__main__": unittest.main() |