1- # Run tests for functions in Python/fileutils.c.
2-
31import os
42import os .path
53import unittest
64import tempfile
75import shutil
8- from test .support import import_helper
6+ from test .support import import_helper , os_helper
97
108# Skip this test if the _testcapi module isn't available.
119_testcapi = import_helper .import_module ('_testinternalcapi' )
@@ -29,86 +27,79 @@ def test_capi_normalize_path(self):
2927
3028
3129class RealpathTests (unittest .TestCase ):
32- """Tests for _Py_wrealpath used by os.path.realpath"""
33-
34- def test_realpath_long_path (self ):
35- """Test that realpath handles paths longer than MAXPATHLEN (4096)"""
36- if os .name == 'nt' :
37- raise unittest .SkipTest ('POSIX-specific test' )
30+ """Tests for _Py_wrealpath used by os.path.realpath."""
3831
39- base = tempfile .mkdtemp ()
40- original_cwd = os .getcwd ()
41- try :
42- os .chdir (base )
32+ def setUp (self ):
33+ self .base = None
4334
44- for i in range (85 ):
45- dirname = f"d{ i :03d} _" + "x" * 44
46- os .mkdir (dirname )
47- os .chdir (dirname )
35+ def tearDown (self ):
36+ if self .base and os .path .exists (self .base ):
37+ shutil .rmtree (self .base , ignore_errors = True )
4838
49- full_path = os .getcwd ()
39+ def test_realpath_long_path (self ):
40+ """Test that realpath handles paths longer than MAXPATHLEN."""
41+ if os .name == 'nt' :
42+ self .skipTest ('POSIX-specific test' )
5043
51- self . assertGreater ( len ( full_path ), 4096 ,
52- f"Path should exceed MAXPATHLEN, got { len ( full_path ) } " )
44+ self . base = tempfile . mkdtemp ()
45+ current_path = self . base
5346
54- # Main test: realpath should not crash on long paths
55- result = os .path .realpath (full_path )
47+ for i in range (25 ):
48+ dirname = f"d{ i :03d} _" + "x" * 195
49+ current_path = os .path .join (current_path , dirname )
50+ try :
51+ os .mkdir (current_path )
52+ except OSError as e :
53+ self .skipTest (f"Cannot create long paths on this platform: { e } " )
5654
57- self .assertTrue (os .path .isabs (result ))
58- self .assertGreater (len (result ), 4096 )
59- # Note: os.path.exists() may fail on very long paths
60- # The important thing is realpath() doesn't crash
55+ full_path = os .path .abspath (current_path )
56+ if len (full_path ) <= 4096 :
57+ self .skipTest (f"Path not long enough ({ len (full_path )} bytes)" )
6158
62- finally :
63- os .chdir ( original_cwd )
64- shutil . rmtree ( base , ignore_errors = True )
59+ result = os . path . realpath ( full_path )
60+ self . assertTrue ( os .path . isabs ( result ) )
61+ self . assertGreater ( len ( result ), 4096 )
6562
6663 def test_realpath_nonexistent_with_strict (self ):
67- """Test that realpath with strict=True raises for nonexistent paths"""
64+ """Test that realpath with strict=True raises for nonexistent paths. """
6865 if os .name == 'nt' :
69- raise unittest .SkipTest ('POSIX-specific test' )
70-
71- base = tempfile .mkdtemp ()
72- try :
73- nonexistent = os .path .join (base , "does_not_exist" , "subdir" )
66+ self .skipTest ('POSIX-specific test' )
7467
75- # Without strict, should return the path
76- result = os .path .realpath (nonexistent , strict = False )
77- self .assertIsNotNone (result )
68+ self .base = tempfile .mkdtemp ()
69+ nonexistent = os .path .join (self .base , "does_not_exist" , "subdir" )
7870
79- # With strict=True, should raise an error
80- with self .assertRaises (OSError ):
81- os .path .realpath (nonexistent , strict = True )
71+ result = os .path .realpath (nonexistent , strict = False )
72+ self .assertIsNotNone (result )
8273
83- finally :
84- shutil . rmtree ( base , ignore_errors = True )
74+ with self . assertRaises ( OSError ) :
75+ os . path . realpath ( nonexistent , strict = True )
8576
77+ @os_helper .skip_unless_symlink
8678 def test_realpath_symlink_long_path (self ):
87- """Test realpath with symlinks in long paths"""
79+ """Test realpath with symlinks in long paths. """
8880 if os .name == 'nt' :
89- raise unittest . SkipTest ('POSIX-specific test' )
81+ self . skipTest ('POSIX-specific test' )
9082
91- base = tempfile .mkdtemp ()
92- try :
93- # Create a long path
94- current = base
95- for i in range (30 ):
96- dirname = f"d{ i :03d} _" + "x" * 44
97- current = os .path .join (current , dirname )
98- os .mkdir (current )
99-
100- # Create a symlink pointing to the long path
101- symlink = os .path .join (base , "link" )
102- os .symlink (current , symlink )
83+ self .base = tempfile .mkdtemp ()
84+ current_path = self .base
10385
104- # Resolve the symlink
105- result = os .path .realpath (symlink )
86+ for i in range (15 ):
87+ dirname = f"d{ i :03d} _" + "x" * 195
88+ current_path = os .path .join (current_path , dirname )
89+ try :
90+ os .mkdir (current_path )
91+ except OSError as e :
92+ self .skipTest (f"Cannot create long paths on this platform: { e } " )
10693
107- self .assertEqual (os .path .normpath (result ), os .path .normpath (current ))
108- self .assertGreater (len (result ), 1500 )
94+ symlink = os .path .join (self .base , "link" )
95+ try :
96+ os .symlink (current_path , symlink )
97+ except (OSError , NotImplementedError ) as e :
98+ self .skipTest (f"Cannot create symlinks on this platform: { e } " )
10999
110- finally :
111- shutil .rmtree (base , ignore_errors = True )
100+ result = os .path .realpath (symlink )
101+ self .assertEqual (os .path .normpath (result ), os .path .normpath (current_path ))
102+ self .assertGreater (len (result ), 1500 )
112103
113104
114105if __name__ == "__main__" :
0 commit comments