99from test import test_genericpath
1010from test .support import import_helper
1111from test .support import os_helper
12- from test .support .os_helper import FakePath
12+ from test .support .os_helper import FakePath , TESTFN
1313from unittest import mock
1414
1515try :
2121# An absolute path to a temporary filename for testing. We can't rely on TESTFN
2222# being an absolute path, so we need this.
2323
24- ABSTFN = abspath (os_helper . TESTFN )
24+ ABSTFN = abspath (TESTFN )
2525
2626def skip_if_ABSTFN_contains_backslash (test ):
2727 """
@@ -33,21 +33,11 @@ def skip_if_ABSTFN_contains_backslash(test):
3333 msg = "ABSTFN is not a posix path - tests fail"
3434 return [test , unittest .skip (msg )(test )][found_backslash ]
3535
36- def safe_rmdir (dirname ):
37- try :
38- os .rmdir (dirname )
39- except OSError :
40- pass
41-
4236class PosixPathTest (unittest .TestCase ):
4337
4438 def setUp (self ):
45- self .tearDown ()
46-
47- def tearDown (self ):
4839 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 ))
5141
5242 def test_join (self ):
5343 fn = posixpath .join
@@ -194,25 +184,28 @@ def test_dirname(self):
194184 self .assertEqual (posixpath .dirname (b"//foo//bar" ), b"//foo" )
195185
196186 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 )
199189
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 :
201192 f .write (b"foo" )
202- self .assertIs (posixpath .islink (os_helper . TESTFN + "1" ), False )
193+ self .assertIs (posixpath .islink (TESTFN + "1" ), False )
203194
204195 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 )
216209
217210 def test_ismount (self ):
218211 self .assertIs (posixpath .ismount ("/" ), True )
@@ -227,7 +220,7 @@ def test_ismount_non_existent(self):
227220 os .mkdir (ABSTFN )
228221 self .assertIs (posixpath .ismount (ABSTFN ), False )
229222 finally :
230- safe_rmdir (ABSTFN )
223+ os_helper . rmdir (ABSTFN )
231224
232225 def test_ismount_invalid_paths (self ):
233226 self .assertIs (posixpath .ismount ('/\udfff ' ), False )
@@ -242,7 +235,7 @@ def test_ismount_symlinks(self):
242235 os .symlink ("/" , ABSTFN )
243236 self .assertIs (posixpath .ismount (ABSTFN ), False )
244237 finally :
245- os .unlink (ABSTFN )
238+ os_helper .unlink (ABSTFN )
246239
247240 @unittest .skipIf (posix is None , "Test requires posix module" )
248241 def test_ismount_different_device (self ):
@@ -576,10 +569,10 @@ def test_realpath_relative(self):
576569 @skip_if_ABSTFN_contains_backslash
577570 def test_realpath_missing_pardir (self ):
578571 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" )
581574 finally :
582- os_helper .unlink (os_helper . TESTFN )
575+ os_helper .unlink (TESTFN )
583576
584577 @os_helper .skip_unless_symlink
585578 @skip_if_ABSTFN_contains_backslash
@@ -675,7 +668,7 @@ def test_realpath_repeated_indirect_symlinks(self):
675668 finally :
676669 os_helper .unlink (ABSTFN + '/self' )
677670 os_helper .unlink (ABSTFN + '/link' )
678- safe_rmdir (ABSTFN )
671+ os_helper . rmdir (ABSTFN )
679672
680673 @os_helper .skip_unless_symlink
681674 @skip_if_ABSTFN_contains_backslash
@@ -694,7 +687,7 @@ def test_realpath_deep_recursion(self):
694687 finally :
695688 for i in range (depth + 1 ):
696689 os_helper .unlink (ABSTFN + '/%d' % i )
697- safe_rmdir (ABSTFN )
690+ os_helper . rmdir (ABSTFN )
698691
699692 @os_helper .skip_unless_symlink
700693 @skip_if_ABSTFN_contains_backslash
@@ -712,8 +705,8 @@ def test_realpath_resolve_parents(self):
712705 self .assertEqual (realpath ("a" ), ABSTFN + "/y/a" )
713706 finally :
714707 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 )
717710
718711 @os_helper .skip_unless_symlink
719712 @skip_if_ABSTFN_contains_backslash
@@ -739,9 +732,9 @@ def test_realpath_resolve_before_normalizing(self):
739732 ABSTFN + "/k" )
740733 finally :
741734 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 )
745738
746739 @os_helper .skip_unless_symlink
747740 @skip_if_ABSTFN_contains_backslash
@@ -759,8 +752,8 @@ def test_realpath_resolve_first(self):
759752 self .assertEqual (realpath (base + "link/k" ), ABSTFN + "/k" )
760753 finally :
761754 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 )
764757
765758 @os_helper .skip_unless_symlink
766759 @skip_if_ABSTFN_contains_backslash
@@ -778,7 +771,7 @@ def test_realpath_unreadable_symlink(self):
778771 realpath (ABSTFN , strict = True )
779772 finally :
780773 os .chmod (ABSTFN , 0o755 , follow_symlinks = False )
781- os .unlink (ABSTFN )
774+ os_helper .unlink (ABSTFN )
782775
783776 @skip_if_ABSTFN_contains_backslash
784777 def test_realpath_nonterminal_file (self ):
@@ -817,6 +810,7 @@ def test_realpath_nonterminal_symlink_to_file(self):
817810 self .assertRaises (NotADirectoryError , realpath , ABSTFN + "/subdir" , strict = True )
818811 finally :
819812 os_helper .unlink (ABSTFN )
813+ os_helper .unlink (ABSTFN + "1" )
820814
821815 @os_helper .skip_unless_symlink
822816 @skip_if_ABSTFN_contains_backslash
@@ -838,6 +832,8 @@ def test_realpath_nonterminal_symlink_to_symlinks_to_file(self):
838832 self .assertRaises (NotADirectoryError , realpath , ABSTFN + "/subdir" , strict = True )
839833 finally :
840834 os_helper .unlink (ABSTFN )
835+ os_helper .unlink (ABSTFN + "1" )
836+ os_helper .unlink (ABSTFN + "2" )
841837
842838 def test_relpath (self ):
843839 (real_getcwd , os .getcwd ) = (os .getcwd , lambda : r"/home/user/bar" )
@@ -963,8 +959,8 @@ class PathLikeTests(unittest.TestCase):
963959 path = posixpath
964960
965961 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 )
968964 self .addCleanup (os_helper .unlink , self .file_name )
969965 with open (self .file_name , 'xb' , 0 ) as file :
970966 file .write (b"test_posixpath.PathLikeTests" )
0 commit comments