@@ -109,62 +109,56 @@ def test_comment_at_end_of_machine_line_pass_has_hash(self):
109
109
def test_security (self ):
110
110
# This test is incomplete since we are normally not run as root and
111
111
# 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 )
129
127
130
128
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 )
137
133
138
134
def test_file_not_found_explicit (self ):
139
135
self .assertRaises (FileNotFoundError , netrc .netrc ,
140
136
file = 'unlikely_netrc' )
141
137
142
138
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 )
168
162
169
163
170
164
if __name__ == "__main__" :
0 commit comments