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
self .assertIs (posixpath .ismount ('/\udfff ' ),False )
233
226
self .assertIs (posixpath .ismount (b'/\xff ' ),False )
@@ -241,7 +234,7 @@ def test_ismount_symlinks(self):
241
234
os .symlink ("/" ,ABSTFN )
242
235
self .assertIs (posixpath .ismount (ABSTFN ),False )
243
236
finally :
244
- os .unlink (ABSTFN )
237
+ os_helper .unlink (ABSTFN )
245
238
246
239
@unittest .skipIf (posix is None ,"Test requires posix module" )
247
240
def test_ismount_different_device (self ):
@@ -502,10 +495,10 @@ def test_realpath_relative(self):
502
495
@skip_if_ABSTFN_contains_backslash
503
496
def test_realpath_missing_pardir (self ):
504
497
try :
505
- os .symlink (os_helper . TESTFN + "1" ,os_helper . TESTFN )
506
- self .assertEqual (realpath ("nonexistent/../" + os_helper . TESTFN ),ABSTFN + "1" )
498
+ os .symlink (TESTFN + "1" ,TESTFN )
499
+ self .assertEqual (realpath ("nonexistent/../" + TESTFN ),ABSTFN + "1" )
507
500
finally :
508
- os_helper .unlink (os_helper . TESTFN )
501
+ os_helper .unlink (TESTFN )
509
502
510
503
@os_helper .skip_unless_symlink
511
504
@skip_if_ABSTFN_contains_backslash
@@ -601,7 +594,7 @@ def test_realpath_repeated_indirect_symlinks(self):
601
594
finally :
602
595
os_helper .unlink (ABSTFN + '/self' )
603
596
os_helper .unlink (ABSTFN + '/link' )
604
- safe_rmdir (ABSTFN )
597
+ os_helper . rmdir (ABSTFN )
605
598
606
599
@os_helper .skip_unless_symlink
607
600
@skip_if_ABSTFN_contains_backslash
@@ -620,7 +613,7 @@ def test_realpath_deep_recursion(self):
620
613
finally :
621
614
for i in range (depth + 1 ):
622
615
os_helper .unlink (ABSTFN + '/%d' % i )
623
- safe_rmdir (ABSTFN )
616
+ os_helper . rmdir (ABSTFN )
624
617
625
618
@os_helper .skip_unless_symlink
626
619
@skip_if_ABSTFN_contains_backslash
@@ -638,8 +631,8 @@ def test_realpath_resolve_parents(self):
638
631
self .assertEqual (realpath ("a" ),ABSTFN + "/y/a" )
639
632
finally :
640
633
os_helper .unlink (ABSTFN + "/k" )
641
- safe_rmdir (ABSTFN + "/y" )
642
- safe_rmdir (ABSTFN )
634
+ os_helper . rmdir (ABSTFN + "/y" )
635
+ os_helper . rmdir (ABSTFN )
643
636
644
637
@os_helper .skip_unless_symlink
645
638
@skip_if_ABSTFN_contains_backslash
@@ -665,9 +658,9 @@ def test_realpath_resolve_before_normalizing(self):
665
658
ABSTFN + "/k" )
666
659
finally :
667
660
os_helper .unlink (ABSTFN + "/link-y" )
668
- safe_rmdir (ABSTFN + "/k/y" )
669
- safe_rmdir (ABSTFN + "/k" )
670
- safe_rmdir (ABSTFN )
661
+ os_helper . rmdir (ABSTFN + "/k/y" )
662
+ os_helper . rmdir (ABSTFN + "/k" )
663
+ os_helper . rmdir (ABSTFN )
671
664
672
665
@os_helper .skip_unless_symlink
673
666
@skip_if_ABSTFN_contains_backslash
@@ -685,8 +678,8 @@ def test_realpath_resolve_first(self):
685
678
self .assertEqual (realpath (base + "link/k" ),ABSTFN + "/k" )
686
679
finally :
687
680
os_helper .unlink (ABSTFN + "link" )
688
- safe_rmdir (ABSTFN + "/k" )
689
- safe_rmdir (ABSTFN )
681
+ os_helper . rmdir (ABSTFN + "/k" )
682
+ os_helper . rmdir (ABSTFN )
690
683
691
684
@os_helper .skip_unless_symlink
692
685
@skip_if_ABSTFN_contains_backslash
@@ -704,7 +697,7 @@ def test_realpath_unreadable_symlink(self):
704
697
realpath (ABSTFN ,strict = True )
705
698
finally :
706
699
os .chmod (ABSTFN ,0o755 ,follow_symlinks = False )
707
- os .unlink (ABSTFN )
700
+ os_helper .unlink (ABSTFN )
708
701
709
702
@skip_if_ABSTFN_contains_backslash
710
703
def test_realpath_nonterminal_file (self ):
@@ -743,6 +736,7 @@ def test_realpath_nonterminal_symlink_to_file(self):
743
736
self .assertRaises (NotADirectoryError ,realpath ,ABSTFN + "/subdir" ,strict = True )
744
737
finally :
745
738
os_helper .unlink (ABSTFN )
739
+ os_helper .unlink (ABSTFN + "1" )
746
740
747
741
@os_helper .skip_unless_symlink
748
742
@skip_if_ABSTFN_contains_backslash
@@ -764,6 +758,8 @@ def test_realpath_nonterminal_symlink_to_symlinks_to_file(self):
764
758
self .assertRaises (NotADirectoryError ,realpath ,ABSTFN + "/subdir" ,strict = True )
765
759
finally :
766
760
os_helper .unlink (ABSTFN )
761
+ os_helper .unlink (ABSTFN + "1" )
762
+ os_helper .unlink (ABSTFN + "2" )
767
763
768
764
def test_relpath (self ):
769
765
(real_getcwd ,os .getcwd )= (os .getcwd ,lambda :r"/home/user/bar" )
@@ -889,8 +885,8 @@ class PathLikeTests(unittest.TestCase):
889
885
path = posixpath
890
886
891
887
def setUp (self ):
892
- self .file_name = os_helper . TESTFN
893
- self .file_path = FakePath (os_helper . TESTFN )
888
+ self .file_name = TESTFN
889
+ self .file_path = FakePath (TESTFN )
894
890
self .addCleanup (os_helper .unlink ,self .file_name )
895
891
with open (self .file_name ,'xb' ,0 )as file :
896
892
file .write (b"test_posixpath.PathLikeTests" )