9
9
from test import test_genericpath
10
10
from test .support import import_helper
11
11
from test .support import os_helper
12
- from test .support .os_helper import FakePath
12
+ from test .support .os_helper import FakePath , TESTFN
13
13
from unittest import mock
14
14
15
15
try :
21
21
# An absolute path to a temporary filename for testing. We can't rely on TESTFN
22
22
# being an absolute path, so we need this.
23
23
24
- ABSTFN = abspath (os_helper . TESTFN )
24
+ ABSTFN = abspath (TESTFN )
25
25
26
26
def skip_if_ABSTFN_contains_backslash (test ):
27
27
"""
@@ -33,21 +33,11 @@ def skip_if_ABSTFN_contains_backslash(test):
33
33
msg = "ABSTFN is not a posix path - tests fail"
34
34
return [test , unittest .skip (msg )(test )][found_backslash ]
35
35
36
- def safe_rmdir (dirname ):
37
- try :
38
- os .rmdir (dirname )
39
- except OSError :
40
- pass
41
-
42
36
class PosixPathTest (unittest .TestCase ):
43
37
44
38
def setUp (self ):
45
- self .tearDown ()
46
-
47
- def tearDown (self ):
48
39
for suffix in ["" , "1" , "2" ]:
49
- os_helper .unlink (os_helper .TESTFN + suffix )
50
- safe_rmdir (os_helper .TESTFN + suffix )
40
+ self .assertFalse (posixpath .lexists (ABSTFN + suffix ))
51
41
52
42
def test_join (self ):
53
43
fn = posixpath .join
@@ -194,25 +184,28 @@ def test_dirname(self):
194
184
self .assertEqual (posixpath .dirname (b"//foo//bar" ), b"//foo" )
195
185
196
186
def test_islink (self ):
197
- self .assertIs (posixpath .islink (os_helper . TESTFN + "1" ), False )
198
- self .assertIs (posixpath .lexists (os_helper . TESTFN + "2" ), False )
187
+ self .assertIs (posixpath .islink (TESTFN + "1" ), False )
188
+ self .assertIs (posixpath .lexists (TESTFN + "2" ), False )
199
189
200
- with open (os_helper .TESTFN + "1" , "wb" ) as f :
190
+ self .addCleanup (os_helper .unlink , TESTFN + "1" )
191
+ with open (TESTFN + "1" , "wb" ) as f :
201
192
f .write (b"foo" )
202
- self .assertIs (posixpath .islink (os_helper . TESTFN + "1" ), False )
193
+ self .assertIs (posixpath .islink (TESTFN + "1" ), False )
203
194
204
195
if os_helper .can_symlink ():
205
- os .symlink (os_helper .TESTFN + "1" , os_helper .TESTFN + "2" )
206
- self .assertIs (posixpath .islink (os_helper .TESTFN + "2" ), True )
207
- os .remove (os_helper .TESTFN + "1" )
208
- self .assertIs (posixpath .islink (os_helper .TESTFN + "2" ), True )
209
- self .assertIs (posixpath .exists (os_helper .TESTFN + "2" ), False )
210
- self .assertIs (posixpath .lexists (os_helper .TESTFN + "2" ), True )
211
-
212
- self .assertIs (posixpath .islink (os_helper .TESTFN + "\udfff " ), False )
213
- self .assertIs (posixpath .islink (os .fsencode (os_helper .TESTFN ) + b"\xff " ), False )
214
- self .assertIs (posixpath .islink (os_helper .TESTFN + "\x00 " ), False )
215
- self .assertIs (posixpath .islink (os .fsencode (os_helper .TESTFN ) + b"\x00 " ), False )
196
+ self .addCleanup (os_helper .unlink , TESTFN + "2" )
197
+ os .symlink (TESTFN + "1" , TESTFN + "2" )
198
+ self .assertIs (posixpath .islink (TESTFN + "2" ), True )
199
+ os .remove (TESTFN + "1" )
200
+ self .assertIs (posixpath .islink (TESTFN + "2" ), True )
201
+ self .assertIs (posixpath .exists (TESTFN + "2" ), False )
202
+ self .assertIs (posixpath .lexists (TESTFN + "2" ), True )
203
+
204
+ def test_islink_invalid_paths (self ):
205
+ self .assertIs (posixpath .islink (TESTFN + "\udfff " ), False )
206
+ self .assertIs (posixpath .islink (os .fsencode (TESTFN ) + b"\xff " ), False )
207
+ self .assertIs (posixpath .islink (TESTFN + "\x00 " ), False )
208
+ self .assertIs (posixpath .islink (os .fsencode (TESTFN ) + b"\x00 " ), False )
216
209
217
210
def test_ismount (self ):
218
211
self .assertIs (posixpath .ismount ("/" ), True )
@@ -227,7 +220,7 @@ def test_ismount_non_existent(self):
227
220
os .mkdir (ABSTFN )
228
221
self .assertIs (posixpath .ismount (ABSTFN ), False )
229
222
finally :
230
- safe_rmdir (ABSTFN )
223
+ os_helper . rmdir (ABSTFN )
231
224
232
225
def test_ismount_invalid_paths (self ):
233
226
self .assertIs (posixpath .ismount ('/\udfff ' ), False )
@@ -242,7 +235,7 @@ def test_ismount_symlinks(self):
242
235
os .symlink ("/" , ABSTFN )
243
236
self .assertIs (posixpath .ismount (ABSTFN ), False )
244
237
finally :
245
- os .unlink (ABSTFN )
238
+ os_helper .unlink (ABSTFN )
246
239
247
240
@unittest .skipIf (posix is None , "Test requires posix module" )
248
241
def test_ismount_different_device (self ):
@@ -576,10 +569,10 @@ def test_realpath_relative(self):
576
569
@skip_if_ABSTFN_contains_backslash
577
570
def test_realpath_missing_pardir (self ):
578
571
try :
579
- os .symlink (os_helper . TESTFN + "1" , os_helper . TESTFN )
580
- self .assertEqual (realpath ("nonexistent/../" + os_helper . TESTFN ), ABSTFN + "1" )
572
+ os .symlink (TESTFN + "1" , TESTFN )
573
+ self .assertEqual (realpath ("nonexistent/../" + TESTFN ), ABSTFN + "1" )
581
574
finally :
582
- os_helper .unlink (os_helper . TESTFN )
575
+ os_helper .unlink (TESTFN )
583
576
584
577
@os_helper .skip_unless_symlink
585
578
@skip_if_ABSTFN_contains_backslash
@@ -675,7 +668,7 @@ def test_realpath_repeated_indirect_symlinks(self):
675
668
finally :
676
669
os_helper .unlink (ABSTFN + '/self' )
677
670
os_helper .unlink (ABSTFN + '/link' )
678
- safe_rmdir (ABSTFN )
671
+ os_helper . rmdir (ABSTFN )
679
672
680
673
@os_helper .skip_unless_symlink
681
674
@skip_if_ABSTFN_contains_backslash
@@ -694,7 +687,7 @@ def test_realpath_deep_recursion(self):
694
687
finally :
695
688
for i in range (depth + 1 ):
696
689
os_helper .unlink (ABSTFN + '/%d' % i )
697
- safe_rmdir (ABSTFN )
690
+ os_helper . rmdir (ABSTFN )
698
691
699
692
@os_helper .skip_unless_symlink
700
693
@skip_if_ABSTFN_contains_backslash
@@ -712,8 +705,8 @@ def test_realpath_resolve_parents(self):
712
705
self .assertEqual (realpath ("a" ), ABSTFN + "/y/a" )
713
706
finally :
714
707
os_helper .unlink (ABSTFN + "/k" )
715
- safe_rmdir (ABSTFN + "/y" )
716
- safe_rmdir (ABSTFN )
708
+ os_helper . rmdir (ABSTFN + "/y" )
709
+ os_helper . rmdir (ABSTFN )
717
710
718
711
@os_helper .skip_unless_symlink
719
712
@skip_if_ABSTFN_contains_backslash
@@ -739,9 +732,9 @@ def test_realpath_resolve_before_normalizing(self):
739
732
ABSTFN + "/k" )
740
733
finally :
741
734
os_helper .unlink (ABSTFN + "/link-y" )
742
- safe_rmdir (ABSTFN + "/k/y" )
743
- safe_rmdir (ABSTFN + "/k" )
744
- safe_rmdir (ABSTFN )
735
+ os_helper . rmdir (ABSTFN + "/k/y" )
736
+ os_helper . rmdir (ABSTFN + "/k" )
737
+ os_helper . rmdir (ABSTFN )
745
738
746
739
@os_helper .skip_unless_symlink
747
740
@skip_if_ABSTFN_contains_backslash
@@ -759,8 +752,8 @@ def test_realpath_resolve_first(self):
759
752
self .assertEqual (realpath (base + "link/k" ), ABSTFN + "/k" )
760
753
finally :
761
754
os_helper .unlink (ABSTFN + "link" )
762
- safe_rmdir (ABSTFN + "/k" )
763
- safe_rmdir (ABSTFN )
755
+ os_helper . rmdir (ABSTFN + "/k" )
756
+ os_helper . rmdir (ABSTFN )
764
757
765
758
@os_helper .skip_unless_symlink
766
759
@skip_if_ABSTFN_contains_backslash
@@ -778,7 +771,7 @@ def test_realpath_unreadable_symlink(self):
778
771
realpath (ABSTFN , strict = True )
779
772
finally :
780
773
os .chmod (ABSTFN , 0o755 , follow_symlinks = False )
781
- os .unlink (ABSTFN )
774
+ os_helper .unlink (ABSTFN )
782
775
783
776
@skip_if_ABSTFN_contains_backslash
784
777
def test_realpath_nonterminal_file (self ):
@@ -817,6 +810,7 @@ def test_realpath_nonterminal_symlink_to_file(self):
817
810
self .assertRaises (NotADirectoryError , realpath , ABSTFN + "/subdir" , strict = True )
818
811
finally :
819
812
os_helper .unlink (ABSTFN )
813
+ os_helper .unlink (ABSTFN + "1" )
820
814
821
815
@os_helper .skip_unless_symlink
822
816
@skip_if_ABSTFN_contains_backslash
@@ -838,6 +832,8 @@ def test_realpath_nonterminal_symlink_to_symlinks_to_file(self):
838
832
self .assertRaises (NotADirectoryError , realpath , ABSTFN + "/subdir" , strict = True )
839
833
finally :
840
834
os_helper .unlink (ABSTFN )
835
+ os_helper .unlink (ABSTFN + "1" )
836
+ os_helper .unlink (ABSTFN + "2" )
841
837
842
838
def test_relpath (self ):
843
839
(real_getcwd , os .getcwd ) = (os .getcwd , lambda : r"/home/user/bar" )
@@ -963,8 +959,8 @@ class PathLikeTests(unittest.TestCase):
963
959
path = posixpath
964
960
965
961
def setUp (self ):
966
- self .file_name = os_helper . TESTFN
967
- self .file_path = FakePath (os_helper . TESTFN )
962
+ self .file_name = TESTFN
963
+ self .file_path = FakePath (TESTFN )
968
964
self .addCleanup (os_helper .unlink , self .file_name )
969
965
with open (self .file_name , 'xb' , 0 ) as file :
970
966
file .write (b"test_posixpath.PathLikeTests" )
0 commit comments