mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
#17699: Fix the new getpass test failures on windows.
Patch by Zachary Ware.
This commit is contained in:
parent
eae41af356
commit
f1c42538e4
1 changed files with 25 additions and 12 deletions
|
@ -1,11 +1,19 @@
|
||||||
import getpass
|
import getpass
|
||||||
import os
|
import os
|
||||||
import termios
|
|
||||||
import unittest
|
import unittest
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
from test import support
|
from test import support
|
||||||
|
|
||||||
|
try:
|
||||||
|
import termios
|
||||||
|
except ImportError:
|
||||||
|
termios = None
|
||||||
|
try:
|
||||||
|
import pwd
|
||||||
|
except ImportError:
|
||||||
|
pwd = None
|
||||||
|
|
||||||
@mock.patch('os.environ')
|
@mock.patch('os.environ')
|
||||||
class GetpassGetuserTest(unittest.TestCase):
|
class GetpassGetuserTest(unittest.TestCase):
|
||||||
|
|
||||||
|
@ -16,7 +24,10 @@ class GetpassGetuserTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_username_priorities_of_env_values(self, environ):
|
def test_username_priorities_of_env_values(self, environ):
|
||||||
environ.get.return_value = None
|
environ.get.return_value = None
|
||||||
|
try:
|
||||||
getpass.getuser()
|
getpass.getuser()
|
||||||
|
except ImportError: # in case there's no pwd module
|
||||||
|
pass
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
environ.get.call_args_list,
|
environ.get.call_args_list,
|
||||||
[mock.call(x) for x in ('LOGNAME', 'USER', 'LNAME', 'USERNAME')])
|
[mock.call(x) for x in ('LOGNAME', 'USER', 'LNAME', 'USERNAME')])
|
||||||
|
@ -24,6 +35,7 @@ class GetpassGetuserTest(unittest.TestCase):
|
||||||
def test_username_falls_back_to_pwd(self, environ):
|
def test_username_falls_back_to_pwd(self, environ):
|
||||||
expected_name = 'some_name'
|
expected_name = 'some_name'
|
||||||
environ.get.return_value = None
|
environ.get.return_value = None
|
||||||
|
if pwd:
|
||||||
with mock.patch('os.getuid') as uid, \
|
with mock.patch('os.getuid') as uid, \
|
||||||
mock.patch('pwd.getpwuid') as getpw:
|
mock.patch('pwd.getpwuid') as getpw:
|
||||||
uid.return_value = 42
|
uid.return_value = 42
|
||||||
|
@ -31,6 +43,8 @@ class GetpassGetuserTest(unittest.TestCase):
|
||||||
self.assertEqual(expected_name,
|
self.assertEqual(expected_name,
|
||||||
getpass.getuser())
|
getpass.getuser())
|
||||||
getpw.assert_called_once_with(42)
|
getpw.assert_called_once_with(42)
|
||||||
|
else:
|
||||||
|
self.assertRaises(ImportError, getpass.getuser)
|
||||||
|
|
||||||
|
|
||||||
class GetpassRawinputTest(unittest.TestCase):
|
class GetpassRawinputTest(unittest.TestCase):
|
||||||
|
@ -68,9 +82,8 @@ class GetpassRawinputTest(unittest.TestCase):
|
||||||
# the password input be taken directly from the tty, and that it not be echoed
|
# the password input be taken directly from the tty, and that it not be echoed
|
||||||
# on the screen, unless we are falling back to stderr/stdin.
|
# on the screen, unless we are falling back to stderr/stdin.
|
||||||
|
|
||||||
# Some of these might run on other platforms, but play it safe.
|
# Some of these might run on platforms without termios, but play it safe.
|
||||||
@unittest.skipUnless(os.name == 'posix',
|
@unittest.skipUnless(termios, 'tests require system with termios')
|
||||||
'tests are for the unix version of getpass')
|
|
||||||
class UnixGetpassTest(unittest.TestCase):
|
class UnixGetpassTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_uses_tty_directly(self):
|
def test_uses_tty_directly(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue