10
10
from test import support
11
11
12
12
13
- def safe_rmdir (dirname ):
14
- try :
15
- os .rmdir (dirname )
16
- except OSError :
17
- pass
13
+ def create_file (filename , data = b'foo' ):
14
+ with open (filename , 'xb' , 0 ) as fp :
15
+ fp .write (data )
18
16
19
17
20
18
class GenericTest :
@@ -97,52 +95,47 @@ def test_commonprefix(self):
97
95
self .assertNotEqual (s1 [n :n + 1 ], s2 [n :n + 1 ])
98
96
99
97
def test_getsize (self ):
100
- f = open (support .TESTFN , "wb" )
101
- try :
102
- f .write (b"foo" )
103
- f .close ()
104
- self .assertEqual (self .pathmodule .getsize (support .TESTFN ), 3 )
105
- finally :
106
- if not f .closed :
107
- f .close ()
108
- support .unlink (support .TESTFN )
98
+ filename = support .TESTFN
99
+ self .addCleanup (support .unlink , filename )
109
100
110
- def test_time (self ):
111
- f = open (support .TESTFN , "wb" )
112
- try :
113
- f .write (b"foo" )
114
- f .close ()
115
- f = open (support .TESTFN , "ab" )
101
+ create_file (filename , b'Hello' )
102
+ self .assertEqual (self .pathmodule .getsize (filename ), 5 )
103
+ os .remove (filename )
104
+
105
+ create_file (filename , b'Hello World!' )
106
+ self .assertEqual (self .pathmodule .getsize (filename ), 12 )
107
+
108
+ def test_filetime (self ):
109
+ filename = support .TESTFN
110
+ self .addCleanup (support .unlink , filename )
111
+
112
+ create_file (filename , b'foo' )
113
+
114
+ with open (filename , "ab" , 0 ) as f :
116
115
f .write (b"bar" )
117
- f .close ()
118
- f = open (support .TESTFN , "rb" )
119
- d = f .read ()
120
- f .close ()
121
- self .assertEqual (d , b"foobar" )
122
-
123
- self .assertLessEqual (
124
- self .pathmodule .getctime (support .TESTFN ),
125
- self .pathmodule .getmtime (support .TESTFN )
126
- )
127
- finally :
128
- if not f .closed :
129
- f .close ()
130
- support .unlink (support .TESTFN )
116
+
117
+ with open (filename , "rb" , 0 ) as f :
118
+ data = f .read ()
119
+ self .assertEqual (data , b"foobar" )
120
+
121
+ self .assertLessEqual (
122
+ self .pathmodule .getctime (filename ),
123
+ self .pathmodule .getmtime (filename )
124
+ )
131
125
132
126
def test_exists (self ):
133
- self .assertIs (self .pathmodule .exists (support .TESTFN ), False )
134
- f = open (support .TESTFN , "wb" )
135
- try :
127
+ filename = support .TESTFN
128
+ self .addCleanup (support .unlink , filename )
129
+
130
+ self .assertIs (self .pathmodule .exists (filename ), False )
131
+
132
+ with open (filename , "xb" ) as f :
136
133
f .write (b"foo" )
137
- f .close ()
138
- self .assertIs (self .pathmodule .exists (support .TESTFN ), True )
139
- if not self .pathmodule == genericpath :
140
- self .assertIs (self .pathmodule .lexists (support .TESTFN ),
141
- True )
142
- finally :
143
- if not f .close ():
144
- f .close ()
145
- support .unlink (support .TESTFN )
134
+
135
+ self .assertIs (self .pathmodule .exists (filename ), True )
136
+
137
+ if not self .pathmodule == genericpath :
138
+ self .assertIs (self .pathmodule .lexists (filename ), True )
146
139
147
140
@unittest .skipUnless (hasattr (os , "pipe" ), "requires os.pipe()" )
148
141
def test_exists_fd (self ):
@@ -154,53 +147,66 @@ def test_exists_fd(self):
154
147
os .close (w )
155
148
self .assertFalse (self .pathmodule .exists (r ))
156
149
157
- def test_isdir (self ):
158
- self .assertIs (self .pathmodule .isdir (support .TESTFN ), False )
159
- f = open (support .TESTFN , "wb" )
160
- try :
161
- f .write (b"foo" )
162
- f .close ()
163
- self .assertIs (self .pathmodule .isdir (support .TESTFN ), False )
164
- os .remove (support .TESTFN )
165
- os .mkdir (support .TESTFN )
166
- self .assertIs (self .pathmodule .isdir (support .TESTFN ), True )
167
- os .rmdir (support .TESTFN )
168
- finally :
169
- if not f .close ():
170
- f .close ()
171
- support .unlink (support .TESTFN )
172
- safe_rmdir (support .TESTFN )
173
-
174
- def test_isfile (self ):
175
- self .assertIs (self .pathmodule .isfile (support .TESTFN ), False )
176
- f = open (support .TESTFN , "wb" )
177
- try :
178
- f .write (b"foo" )
179
- f .close ()
180
- self .assertIs (self .pathmodule .isfile (support .TESTFN ), True )
181
- os .remove (support .TESTFN )
182
- os .mkdir (support .TESTFN )
183
- self .assertIs (self .pathmodule .isfile (support .TESTFN ), False )
184
- os .rmdir (support .TESTFN )
185
- finally :
186
- if not f .close ():
187
- f .close ()
188
- support .unlink (support .TESTFN )
189
- safe_rmdir (support .TESTFN )
150
+ def test_isdir_file (self ):
151
+ filename = support .TESTFN
152
+ self .addCleanup (support .unlink , filename )
153
+ self .assertIs (self .pathmodule .isdir (filename ), False )
154
+
155
+ create_file (filename )
156
+ self .assertIs (self .pathmodule .isdir (filename ), False )
157
+
158
+ def test_isdir_dir (self ):
159
+ filename = support .TESTFN
160
+ self .addCleanup (support .rmdir , filename )
161
+ self .assertIs (self .pathmodule .isdir (filename ), False )
162
+
163
+ os .mkdir (filename )
164
+ self .assertIs (self .pathmodule .isdir (filename ), True )
165
+
166
+ def test_isfile_file (self ):
167
+ filename = support .TESTFN
168
+ self .addCleanup (support .unlink , filename )
169
+ self .assertIs (self .pathmodule .isfile (filename ), False )
190
170
191
- @staticmethod
192
- def _create_file (filename ):
193
- with open (filename , 'wb' ) as f :
194
- f .write (b'foo' )
171
+ create_file (filename )
172
+ self .assertIs (self .pathmodule .isfile (filename ), True )
173
+
174
+ def test_isfile_dir (self ):
175
+ filename = support .TESTFN
176
+ self .addCleanup (support .rmdir , filename )
177
+ self .assertIs (self .pathmodule .isfile (filename ), False )
178
+
179
+ os .mkdir (filename )
180
+ self .assertIs (self .pathmodule .isfile (filename ), False )
195
181
196
182
def test_samefile (self ):
197
- try :
198
- test_fn = support .TESTFN + "1"
199
- self ._create_file (test_fn )
200
- self .assertTrue (self .pathmodule .samefile (test_fn , test_fn ))
201
- self .assertRaises (TypeError , self .pathmodule .samefile )
202
- finally :
203
- os .remove (test_fn )
183
+ file1 = support .TESTFN
184
+ file2 = support .TESTFN + "2"
185
+ self .addCleanup (support .unlink , file1 )
186
+ self .addCleanup (support .unlink , file2 )
187
+
188
+ create_file (file1 )
189
+ self .assertTrue (self .pathmodule .samefile (file1 , file1 ))
190
+
191
+ create_file (file2 )
192
+ self .assertFalse (self .pathmodule .samefile (file1 , file2 ))
193
+
194
+ self .assertRaises (TypeError , self .pathmodule .samefile )
195
+
196
+ def _test_samefile_on_link_func (self , func ):
197
+ test_fn1 = support .TESTFN
198
+ test_fn2 = support .TESTFN + "2"
199
+ self .addCleanup (support .unlink , test_fn1 )
200
+ self .addCleanup (support .unlink , test_fn2 )
201
+
202
+ create_file (test_fn1 )
203
+
204
+ func (test_fn1 , test_fn2 )
205
+ self .assertTrue (self .pathmodule .samefile (test_fn1 , test_fn2 ))
206
+ os .remove (test_fn2 )
207
+
208
+ create_file (test_fn2 )
209
+ self .assertFalse (self .pathmodule .samefile (test_fn1 , test_fn2 ))
204
210
205
211
@support .skip_unless_symlink
206
212
def test_samefile_on_symlink (self ):
@@ -209,31 +215,37 @@ def test_samefile_on_symlink(self):
209
215
def test_samefile_on_link (self ):
210
216
self ._test_samefile_on_link_func (os .link )
211
217
212
- def _test_samefile_on_link_func (self , func ):
213
- try :
214
- test_fn1 = support .TESTFN + "1 "
215
- test_fn2 = support .TESTFN + "2"
216
- self ._create_file ( test_fn1 )
218
+ def test_samestat (self ):
219
+ test_fn1 = support . TESTFN
220
+ test_fn2 = support .TESTFN + "2 "
221
+ self . addCleanup ( support .unlink , test_fn1 )
222
+ self .addCleanup ( support . unlink , test_fn2 )
217
223
218
- func (test_fn1 , test_fn2 )
219
- self . assertTrue ( self . pathmodule . samefile ( test_fn1 , test_fn2 ) )
220
- os .remove ( test_fn2 )
224
+ create_file (test_fn1 )
225
+ stat1 = os . stat ( test_fn1 )
226
+ self . assertTrue ( self . pathmodule . samestat ( stat1 , os .stat ( test_fn1 )) )
221
227
222
- self ._create_file (test_fn2 )
223
- self .assertFalse (self .pathmodule .samefile (test_fn1 , test_fn2 ))
224
- finally :
225
- os .remove (test_fn1 )
226
- os .remove (test_fn2 )
228
+ create_file (test_fn2 )
229
+ stat2 = os .stat (test_fn2 )
230
+ self .assertFalse (self .pathmodule .samestat (stat1 , stat2 ))
227
231
228
- def test_samestat (self ):
229
- try :
230
- test_fn = support .TESTFN + "1"
231
- self ._create_file (test_fn )
232
- test_fns = [test_fn ]* 2
233
- stats = map (os .stat , test_fns )
234
- self .assertTrue (self .pathmodule .samestat (* stats ))
235
- finally :
236
- os .remove (test_fn )
232
+ self .assertRaises (TypeError , self .pathmodule .samestat )
233
+
234
+ def _test_samestat_on_link_func (self , func ):
235
+ test_fn1 = support .TESTFN + "1"
236
+ test_fn2 = support .TESTFN + "2"
237
+ self .addCleanup (support .unlink , test_fn1 )
238
+ self .addCleanup (support .unlink , test_fn2 )
239
+
240
+ create_file (test_fn1 )
241
+ func (test_fn1 , test_fn2 )
242
+ self .assertTrue (self .pathmodule .samestat (os .stat (test_fn1 ),
243
+ os .stat (test_fn2 )))
244
+ os .remove (test_fn2 )
245
+
246
+ create_file (test_fn2 )
247
+ self .assertFalse (self .pathmodule .samestat (os .stat (test_fn1 ),
248
+ os .stat (test_fn2 )))
237
249
238
250
@support .skip_unless_symlink
239
251
def test_samestat_on_symlink (self ):
@@ -242,31 +254,17 @@ def test_samestat_on_symlink(self):
242
254
def test_samestat_on_link (self ):
243
255
self ._test_samestat_on_link_func (os .link )
244
256
245
- def _test_samestat_on_link_func (self , func ):
246
- try :
247
- test_fn1 = support .TESTFN + "1"
248
- test_fn2 = support .TESTFN + "2"
249
- self ._create_file (test_fn1 )
250
- test_fns = (test_fn1 , test_fn2 )
251
- func (* test_fns )
252
- stats = map (os .stat , test_fns )
253
- self .assertTrue (self .pathmodule .samestat (* stats ))
254
- os .remove (test_fn2 )
255
-
256
- self ._create_file (test_fn2 )
257
- stats = map (os .stat , test_fns )
258
- self .assertFalse (self .pathmodule .samestat (* stats ))
259
-
260
- self .assertRaises (TypeError , self .pathmodule .samestat )
261
- finally :
262
- os .remove (test_fn1 )
263
- os .remove (test_fn2 )
264
-
265
257
def test_sameopenfile (self ):
266
- fname = support .TESTFN + "1"
267
- with open (fname , "wb" ) as a , open (fname , "wb" ) as b :
268
- self .assertTrue (self .pathmodule .sameopenfile (
269
- a .fileno (), b .fileno ()))
258
+ filename = support .TESTFN
259
+ self .addCleanup (support .unlink , filename )
260
+ create_file (filename )
261
+
262
+ with open (filename , "rb" , 0 ) as fp1 :
263
+ fd1 = fp1 .fileno ()
264
+ with open (filename , "rb" , 0 ) as fp2 :
265
+ fd2 = fp2 .fileno ()
266
+ self .assertTrue (self .pathmodule .sameopenfile (fd1 , fd2 ))
267
+
270
268
271
269
class TestGenericTest (GenericTest , unittest .TestCase ):
272
270
# Issue 16852: GenericTest can't inherit from unittest.TestCase
0 commit comments