Skip to content

Commit e483d28

Browse files
authored
bpo-31904: Fix test_netrc for VxWorks RTOS (GH-21675)
Fix test_netrc on VxWorks: create temporary directories using temp_cwd().
1 parent 1867b46 commit e483d28

File tree

2 files changed

+43
-48
lines changed

2 files changed

+43
-48
lines changed

Lib/test/test_netrc.py

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -109,62 +109,56 @@ def test_comment_at_end_of_machine_line_pass_has_hash(self):
109109
def test_security(self):
110110
# This test is incomplete since we are normally not run as root and
111111
# therefore can't test the file ownership being wrong.
112-
d = os_helper.TESTFN
113-
os.mkdir(d)
114-
self.addCleanup(os_helper.rmtree, d)
115-
fn = os.path.join(d, '.netrc')
116-
with open(fn, 'wt') as f:
117-
f.write("""\
118-
machine foo.domain.com login bar password pass
119-
default login foo password pass
120-
""")
121-
with os_helper.EnvironmentVarGuard() as environ:
122-
environ.set('HOME', d)
123-
os.chmod(fn, 0o600)
124-
nrc = netrc.netrc()
125-
self.assertEqual(nrc.hosts['foo.domain.com'],
126-
('bar', None, 'pass'))
127-
os.chmod(fn, 0o622)
128-
self.assertRaises(netrc.NetrcParseError, netrc.netrc)
112+
with os_helper.temp_cwd(None) as d:
113+
fn = os.path.join(d, '.netrc')
114+
with open(fn, 'wt') as f:
115+
f.write("""\
116+
machine foo.domain.com login bar password pass
117+
default login foo password pass
118+
""")
119+
with os_helper.EnvironmentVarGuard() as environ:
120+
environ.set('HOME', d)
121+
os.chmod(fn, 0o600)
122+
nrc = netrc.netrc()
123+
self.assertEqual(nrc.hosts['foo.domain.com'],
124+
('bar', None, 'pass'))
125+
os.chmod(fn, 0o622)
126+
self.assertRaises(netrc.NetrcParseError, netrc.netrc)
129127

130128
def test_file_not_found_in_home(self):
131-
d = os_helper.TESTFN
132-
os.mkdir(d)
133-
self.addCleanup(os_helper.rmtree, d)
134-
with os_helper.EnvironmentVarGuard() as environ:
135-
environ.set('HOME', d)
136-
self.assertRaises(FileNotFoundError, netrc.netrc)
129+
with os_helper.temp_cwd(None) as d:
130+
with os_helper.EnvironmentVarGuard() as environ:
131+
environ.set('HOME', d)
132+
self.assertRaises(FileNotFoundError, netrc.netrc)
137133

138134
def test_file_not_found_explicit(self):
139135
self.assertRaises(FileNotFoundError, netrc.netrc,
140136
file='unlikely_netrc')
141137

142138
def test_home_not_set(self):
143-
fake_home = os_helper.TESTFN
144-
os.mkdir(fake_home)
145-
self.addCleanup(os_helper.rmtree, fake_home)
146-
fake_netrc_path = os.path.join(fake_home, '.netrc')
147-
with open(fake_netrc_path, 'w') as f:
148-
f.write('machine foo.domain.com login bar password pass')
149-
os.chmod(fake_netrc_path, 0o600)
150-
151-
orig_expanduser = os.path.expanduser
152-
called = []
153-
154-
def fake_expanduser(s):
155-
called.append(s)
156-
with os_helper.EnvironmentVarGuard() as environ:
157-
environ.set('HOME', fake_home)
158-
environ.set('USERPROFILE', fake_home)
159-
result = orig_expanduser(s)
160-
return result
161-
162-
with support.swap_attr(os.path, 'expanduser', fake_expanduser):
163-
nrc = netrc.netrc()
164-
login, account, password = nrc.authenticators('foo.domain.com')
165-
self.assertEqual(login, 'bar')
166-
167-
self.assertTrue(called)
139+
with os_helper.temp_cwd(None) as fake_home:
140+
fake_netrc_path = os.path.join(fake_home, '.netrc')
141+
with open(fake_netrc_path, 'w') as f:
142+
f.write('machine foo.domain.com login bar password pass')
143+
os.chmod(fake_netrc_path, 0o600)
144+
145+
orig_expanduser = os.path.expanduser
146+
called = []
147+
148+
def fake_expanduser(s):
149+
called.append(s)
150+
with os_helper.EnvironmentVarGuard() as environ:
151+
environ.set('HOME', fake_home)
152+
environ.set('USERPROFILE', fake_home)
153+
result = orig_expanduser(s)
154+
return result
155+
156+
with support.swap_attr(os.path, 'expanduser', fake_expanduser):
157+
nrc = netrc.netrc()
158+
login, account, password = nrc.authenticators('foo.domain.com')
159+
self.assertEqual(login, 'bar')
160+
161+
self.assertTrue(called)
168162

169163

170164
if __name__ == "__main__":
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix test_netrc on VxWorks: create temporary directories using temp_cwd().

0 commit comments

Comments
 (0)