bpo-31904: Fix test_netrc for VxWorks RTOS (GH-21675)

Fix test_netrc on VxWorks: create temporary directories using temp_cwd().
This commit is contained in:
pxinwr 2020-12-02 04:34:42 +08:00 committed by GitHub
parent 1867b462de
commit e483d281bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 44 deletions

View File

@ -109,62 +109,56 @@ class NetrcTestCase(unittest.TestCase):
def test_security(self): def test_security(self):
# This test is incomplete since we are normally not run as root and # This test is incomplete since we are normally not run as root and
# therefore can't test the file ownership being wrong. # therefore can't test the file ownership being wrong.
d = os_helper.TESTFN with os_helper.temp_cwd(None) as d:
os.mkdir(d) fn = os.path.join(d, '.netrc')
self.addCleanup(os_helper.rmtree, d) with open(fn, 'wt') as f:
fn = os.path.join(d, '.netrc') f.write("""\
with open(fn, 'wt') as f: machine foo.domain.com login bar password pass
f.write("""\ default login foo password pass
machine foo.domain.com login bar password pass """)
default login foo password pass with os_helper.EnvironmentVarGuard() as environ:
""") environ.set('HOME', d)
with os_helper.EnvironmentVarGuard() as environ: os.chmod(fn, 0o600)
environ.set('HOME', d) nrc = netrc.netrc()
os.chmod(fn, 0o600) self.assertEqual(nrc.hosts['foo.domain.com'],
nrc = netrc.netrc() ('bar', None, 'pass'))
self.assertEqual(nrc.hosts['foo.domain.com'], os.chmod(fn, 0o622)
('bar', None, 'pass')) self.assertRaises(netrc.NetrcParseError, netrc.netrc)
os.chmod(fn, 0o622)
self.assertRaises(netrc.NetrcParseError, netrc.netrc)
def test_file_not_found_in_home(self): def test_file_not_found_in_home(self):
d = os_helper.TESTFN with os_helper.temp_cwd(None) as d:
os.mkdir(d) with os_helper.EnvironmentVarGuard() as environ:
self.addCleanup(os_helper.rmtree, d) environ.set('HOME', d)
with os_helper.EnvironmentVarGuard() as environ: self.assertRaises(FileNotFoundError, netrc.netrc)
environ.set('HOME', d)
self.assertRaises(FileNotFoundError, netrc.netrc)
def test_file_not_found_explicit(self): def test_file_not_found_explicit(self):
self.assertRaises(FileNotFoundError, netrc.netrc, self.assertRaises(FileNotFoundError, netrc.netrc,
file='unlikely_netrc') file='unlikely_netrc')
def test_home_not_set(self): def test_home_not_set(self):
fake_home = os_helper.TESTFN with os_helper.temp_cwd(None) as fake_home:
os.mkdir(fake_home) fake_netrc_path = os.path.join(fake_home, '.netrc')
self.addCleanup(os_helper.rmtree, fake_home) with open(fake_netrc_path, 'w') as f:
fake_netrc_path = os.path.join(fake_home, '.netrc') f.write('machine foo.domain.com login bar password pass')
with open(fake_netrc_path, 'w') as f: os.chmod(fake_netrc_path, 0o600)
f.write('machine foo.domain.com login bar password pass')
os.chmod(fake_netrc_path, 0o600)
orig_expanduser = os.path.expanduser orig_expanduser = os.path.expanduser
called = [] called = []
def fake_expanduser(s): def fake_expanduser(s):
called.append(s) called.append(s)
with os_helper.EnvironmentVarGuard() as environ: with os_helper.EnvironmentVarGuard() as environ:
environ.set('HOME', fake_home) environ.set('HOME', fake_home)
environ.set('USERPROFILE', fake_home) environ.set('USERPROFILE', fake_home)
result = orig_expanduser(s) result = orig_expanduser(s)
return result return result
with support.swap_attr(os.path, 'expanduser', fake_expanduser): with support.swap_attr(os.path, 'expanduser', fake_expanduser):
nrc = netrc.netrc() nrc = netrc.netrc()
login, account, password = nrc.authenticators('foo.domain.com') login, account, password = nrc.authenticators('foo.domain.com')
self.assertEqual(login, 'bar') self.assertEqual(login, 'bar')
self.assertTrue(called) self.assertTrue(called)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -0,0 +1 @@
Fix test_netrc on VxWorks: create temporary directories using temp_cwd().