33import os
44import os .path
55import unittest
6- import tempfile
7- import shutil
8- from test .support import import_helper , os_helper
6+ from test .support import import_helper
97
108# Skip this test if the _testcapi module isn't available.
119_testcapi = import_helper .import_module ('_testinternalcapi' )
@@ -28,90 +26,5 @@ def test_capi_normalize_path(self):
2826 msg = f'input: { filename !r} expected output: { expected !r} ' )
2927
3028
31- class RealpathTests (unittest .TestCase ):
32- """Tests for _Py_wrealpath used by os.path.realpath."""
33-
34- def setUp (self ):
35- self .base = None
36-
37- def tearDown (self ):
38- if self .base and os .path .exists (self .base ):
39- shutil .rmtree (self .base , ignore_errors = True )
40-
41- def test_realpath_long_path (self ):
42- """Test that realpath handles paths longer than MAXPATHLEN."""
43- if os .name == 'nt' :
44- self .skipTest ('POSIX-specific test' )
45-
46- self .base = tempfile .mkdtemp ()
47- current_path = self .base
48-
49- for i in range (25 ):
50- dirname = f"d{ i :03d} _" + "x" * 195
51- current_path = os .path .join (current_path , dirname )
52- try :
53- os .mkdir (current_path )
54- except OSError as e :
55- self .skipTest (f"Cannot create long paths on this platform: { e } " )
56-
57- full_path = os .path .abspath (current_path )
58- if len (full_path ) <= 4096 :
59- self .skipTest (f"Path not long enough ({ len (full_path )} bytes)" )
60-
61- result = os .path .realpath (full_path )
62- self .assertTrue (os .path .isabs (result ))
63- self .assertGreater (len (result ), 4096 )
64-
65- def test_realpath_nonexistent_with_strict (self ):
66- """Test that realpath with strict=True raises for nonexistent paths."""
67- if os .name == 'nt' :
68- self .skipTest ('POSIX-specific test' )
69-
70- self .base = tempfile .mkdtemp ()
71- nonexistent = os .path .join (self .base , "does_not_exist" , "subdir" )
72-
73- result = os .path .realpath (nonexistent , strict = False )
74- self .assertIsNotNone (result )
75-
76- with self .assertRaises (OSError ):
77- os .path .realpath (nonexistent , strict = True )
78-
79- @os_helper .skip_unless_symlink
80- def test_realpath_symlink_long_path (self ):
81- """Test realpath with symlinks in long paths."""
82- if os .name == 'nt' :
83- self .skipTest ('POSIX-specific test' )
84-
85- self .base = tempfile .mkdtemp ()
86- current_path = self .base
87-
88- for i in range (15 ):
89- dirname = f"d{ i :03d} _" + "x" * 195
90- current_path = os .path .join (current_path , dirname )
91- try :
92- os .mkdir (current_path )
93- except OSError as e :
94- self .skipTest (f"Cannot create long paths on this platform: { e } " )
95-
96- symlink = os .path .join (self .base , "link" )
97- try :
98- os .symlink (current_path , symlink )
99- except (OSError , NotImplementedError ) as e :
100- self .skipTest (f"Cannot create symlinks on this platform: { e } " )
101-
102- result = os .path .realpath (symlink )
103-
104- # On some platforms (like Android), realpath may return a different
105- # canonical path due to filesystem mounts/symlinks.
106- # Use samefile() to verify the symlink was resolved correctly.
107- try :
108- self .assertTrue (os .path .samefile (result , current_path ))
109- except (OSError , NotImplementedError ):
110- # If samefile() is not available, just check path length
111- self .assertGreater (len (result ), 1500 )
112-
113- self .assertGreater (len (result ), 1500 )
114-
115-
11629if __name__ == "__main__" :
11730 unittest .main ()
0 commit comments