1 | n/a | import os |
---|
2 | n/a | import posixpath |
---|
3 | n/a | import unittest |
---|
4 | n/a | import warnings |
---|
5 | n/a | from posixpath import realpath, abspath, dirname, basename |
---|
6 | n/a | from test import support, test_genericpath |
---|
7 | n/a | |
---|
8 | n/a | try: |
---|
9 | n/a | import posix |
---|
10 | n/a | except ImportError: |
---|
11 | n/a | posix = None |
---|
12 | n/a | |
---|
13 | n/a | # An absolute path to a temporary filename for testing. We can't rely on TESTFN |
---|
14 | n/a | # being an absolute path, so we need this. |
---|
15 | n/a | |
---|
16 | n/a | ABSTFN = abspath(support.TESTFN) |
---|
17 | n/a | |
---|
18 | n/a | def skip_if_ABSTFN_contains_backslash(test): |
---|
19 | n/a | """ |
---|
20 | n/a | On Windows, posixpath.abspath still returns paths with backslashes |
---|
21 | n/a | instead of posix forward slashes. If this is the case, several tests |
---|
22 | n/a | fail, so skip them. |
---|
23 | n/a | """ |
---|
24 | n/a | found_backslash = '\\' in ABSTFN |
---|
25 | n/a | msg = "ABSTFN is not a posix path - tests fail" |
---|
26 | n/a | return [test, unittest.skip(msg)(test)][found_backslash] |
---|
27 | n/a | |
---|
28 | n/a | def safe_rmdir(dirname): |
---|
29 | n/a | try: |
---|
30 | n/a | os.rmdir(dirname) |
---|
31 | n/a | except OSError: |
---|
32 | n/a | pass |
---|
33 | n/a | |
---|
34 | n/a | class PosixPathTest(unittest.TestCase): |
---|
35 | n/a | |
---|
36 | n/a | def setUp(self): |
---|
37 | n/a | self.tearDown() |
---|
38 | n/a | |
---|
39 | n/a | def tearDown(self): |
---|
40 | n/a | for suffix in ["", "1", "2"]: |
---|
41 | n/a | support.unlink(support.TESTFN + suffix) |
---|
42 | n/a | safe_rmdir(support.TESTFN + suffix) |
---|
43 | n/a | |
---|
44 | n/a | def test_join(self): |
---|
45 | n/a | self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), |
---|
46 | n/a | "/bar/baz") |
---|
47 | n/a | self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz") |
---|
48 | n/a | self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), |
---|
49 | n/a | "/foo/bar/baz/") |
---|
50 | n/a | |
---|
51 | n/a | self.assertEqual(posixpath.join(b"/foo", b"bar", b"/bar", b"baz"), |
---|
52 | n/a | b"/bar/baz") |
---|
53 | n/a | self.assertEqual(posixpath.join(b"/foo", b"bar", b"baz"), |
---|
54 | n/a | b"/foo/bar/baz") |
---|
55 | n/a | self.assertEqual(posixpath.join(b"/foo/", b"bar/", b"baz/"), |
---|
56 | n/a | b"/foo/bar/baz/") |
---|
57 | n/a | |
---|
58 | n/a | def test_split(self): |
---|
59 | n/a | self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar")) |
---|
60 | n/a | self.assertEqual(posixpath.split("/"), ("/", "")) |
---|
61 | n/a | self.assertEqual(posixpath.split("foo"), ("", "foo")) |
---|
62 | n/a | self.assertEqual(posixpath.split("////foo"), ("////", "foo")) |
---|
63 | n/a | self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar")) |
---|
64 | n/a | |
---|
65 | n/a | self.assertEqual(posixpath.split(b"/foo/bar"), (b"/foo", b"bar")) |
---|
66 | n/a | self.assertEqual(posixpath.split(b"/"), (b"/", b"")) |
---|
67 | n/a | self.assertEqual(posixpath.split(b"foo"), (b"", b"foo")) |
---|
68 | n/a | self.assertEqual(posixpath.split(b"////foo"), (b"////", b"foo")) |
---|
69 | n/a | self.assertEqual(posixpath.split(b"//foo//bar"), (b"//foo", b"bar")) |
---|
70 | n/a | |
---|
71 | n/a | def splitextTest(self, path, filename, ext): |
---|
72 | n/a | self.assertEqual(posixpath.splitext(path), (filename, ext)) |
---|
73 | n/a | self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext)) |
---|
74 | n/a | self.assertEqual(posixpath.splitext("abc/" + path), |
---|
75 | n/a | ("abc/" + filename, ext)) |
---|
76 | n/a | self.assertEqual(posixpath.splitext("abc.def/" + path), |
---|
77 | n/a | ("abc.def/" + filename, ext)) |
---|
78 | n/a | self.assertEqual(posixpath.splitext("/abc.def/" + path), |
---|
79 | n/a | ("/abc.def/" + filename, ext)) |
---|
80 | n/a | self.assertEqual(posixpath.splitext(path + "/"), |
---|
81 | n/a | (filename + ext + "/", "")) |
---|
82 | n/a | |
---|
83 | n/a | path = bytes(path, "ASCII") |
---|
84 | n/a | filename = bytes(filename, "ASCII") |
---|
85 | n/a | ext = bytes(ext, "ASCII") |
---|
86 | n/a | |
---|
87 | n/a | self.assertEqual(posixpath.splitext(path), (filename, ext)) |
---|
88 | n/a | self.assertEqual(posixpath.splitext(b"/" + path), |
---|
89 | n/a | (b"/" + filename, ext)) |
---|
90 | n/a | self.assertEqual(posixpath.splitext(b"abc/" + path), |
---|
91 | n/a | (b"abc/" + filename, ext)) |
---|
92 | n/a | self.assertEqual(posixpath.splitext(b"abc.def/" + path), |
---|
93 | n/a | (b"abc.def/" + filename, ext)) |
---|
94 | n/a | self.assertEqual(posixpath.splitext(b"/abc.def/" + path), |
---|
95 | n/a | (b"/abc.def/" + filename, ext)) |
---|
96 | n/a | self.assertEqual(posixpath.splitext(path + b"/"), |
---|
97 | n/a | (filename + ext + b"/", b"")) |
---|
98 | n/a | |
---|
99 | n/a | def test_splitext(self): |
---|
100 | n/a | self.splitextTest("foo.bar", "foo", ".bar") |
---|
101 | n/a | self.splitextTest("foo.boo.bar", "foo.boo", ".bar") |
---|
102 | n/a | self.splitextTest("foo.boo.biff.bar", "foo.boo.biff", ".bar") |
---|
103 | n/a | self.splitextTest(".csh.rc", ".csh", ".rc") |
---|
104 | n/a | self.splitextTest("nodots", "nodots", "") |
---|
105 | n/a | self.splitextTest(".cshrc", ".cshrc", "") |
---|
106 | n/a | self.splitextTest("...manydots", "...manydots", "") |
---|
107 | n/a | self.splitextTest("...manydots.ext", "...manydots", ".ext") |
---|
108 | n/a | self.splitextTest(".", ".", "") |
---|
109 | n/a | self.splitextTest("..", "..", "") |
---|
110 | n/a | self.splitextTest("........", "........", "") |
---|
111 | n/a | self.splitextTest("", "", "") |
---|
112 | n/a | |
---|
113 | n/a | def test_isabs(self): |
---|
114 | n/a | self.assertIs(posixpath.isabs(""), False) |
---|
115 | n/a | self.assertIs(posixpath.isabs("/"), True) |
---|
116 | n/a | self.assertIs(posixpath.isabs("/foo"), True) |
---|
117 | n/a | self.assertIs(posixpath.isabs("/foo/bar"), True) |
---|
118 | n/a | self.assertIs(posixpath.isabs("foo/bar"), False) |
---|
119 | n/a | |
---|
120 | n/a | self.assertIs(posixpath.isabs(b""), False) |
---|
121 | n/a | self.assertIs(posixpath.isabs(b"/"), True) |
---|
122 | n/a | self.assertIs(posixpath.isabs(b"/foo"), True) |
---|
123 | n/a | self.assertIs(posixpath.isabs(b"/foo/bar"), True) |
---|
124 | n/a | self.assertIs(posixpath.isabs(b"foo/bar"), False) |
---|
125 | n/a | |
---|
126 | n/a | def test_basename(self): |
---|
127 | n/a | self.assertEqual(posixpath.basename("/foo/bar"), "bar") |
---|
128 | n/a | self.assertEqual(posixpath.basename("/"), "") |
---|
129 | n/a | self.assertEqual(posixpath.basename("foo"), "foo") |
---|
130 | n/a | self.assertEqual(posixpath.basename("////foo"), "foo") |
---|
131 | n/a | self.assertEqual(posixpath.basename("//foo//bar"), "bar") |
---|
132 | n/a | |
---|
133 | n/a | self.assertEqual(posixpath.basename(b"/foo/bar"), b"bar") |
---|
134 | n/a | self.assertEqual(posixpath.basename(b"/"), b"") |
---|
135 | n/a | self.assertEqual(posixpath.basename(b"foo"), b"foo") |
---|
136 | n/a | self.assertEqual(posixpath.basename(b"////foo"), b"foo") |
---|
137 | n/a | self.assertEqual(posixpath.basename(b"//foo//bar"), b"bar") |
---|
138 | n/a | |
---|
139 | n/a | def test_dirname(self): |
---|
140 | n/a | self.assertEqual(posixpath.dirname("/foo/bar"), "/foo") |
---|
141 | n/a | self.assertEqual(posixpath.dirname("/"), "/") |
---|
142 | n/a | self.assertEqual(posixpath.dirname("foo"), "") |
---|
143 | n/a | self.assertEqual(posixpath.dirname("////foo"), "////") |
---|
144 | n/a | self.assertEqual(posixpath.dirname("//foo//bar"), "//foo") |
---|
145 | n/a | |
---|
146 | n/a | self.assertEqual(posixpath.dirname(b"/foo/bar"), b"/foo") |
---|
147 | n/a | self.assertEqual(posixpath.dirname(b"/"), b"/") |
---|
148 | n/a | self.assertEqual(posixpath.dirname(b"foo"), b"") |
---|
149 | n/a | self.assertEqual(posixpath.dirname(b"////foo"), b"////") |
---|
150 | n/a | self.assertEqual(posixpath.dirname(b"//foo//bar"), b"//foo") |
---|
151 | n/a | |
---|
152 | n/a | def test_islink(self): |
---|
153 | n/a | self.assertIs(posixpath.islink(support.TESTFN + "1"), False) |
---|
154 | n/a | self.assertIs(posixpath.lexists(support.TESTFN + "2"), False) |
---|
155 | n/a | f = open(support.TESTFN + "1", "wb") |
---|
156 | n/a | try: |
---|
157 | n/a | f.write(b"foo") |
---|
158 | n/a | f.close() |
---|
159 | n/a | self.assertIs(posixpath.islink(support.TESTFN + "1"), False) |
---|
160 | n/a | if support.can_symlink(): |
---|
161 | n/a | os.symlink(support.TESTFN + "1", support.TESTFN + "2") |
---|
162 | n/a | self.assertIs(posixpath.islink(support.TESTFN + "2"), True) |
---|
163 | n/a | os.remove(support.TESTFN + "1") |
---|
164 | n/a | self.assertIs(posixpath.islink(support.TESTFN + "2"), True) |
---|
165 | n/a | self.assertIs(posixpath.exists(support.TESTFN + "2"), False) |
---|
166 | n/a | self.assertIs(posixpath.lexists(support.TESTFN + "2"), True) |
---|
167 | n/a | finally: |
---|
168 | n/a | if not f.close(): |
---|
169 | n/a | f.close() |
---|
170 | n/a | |
---|
171 | n/a | def test_ismount(self): |
---|
172 | n/a | self.assertIs(posixpath.ismount("/"), True) |
---|
173 | n/a | with warnings.catch_warnings(): |
---|
174 | n/a | warnings.simplefilter("ignore", DeprecationWarning) |
---|
175 | n/a | self.assertIs(posixpath.ismount(b"/"), True) |
---|
176 | n/a | |
---|
177 | n/a | def test_ismount_non_existent(self): |
---|
178 | n/a | # Non-existent mountpoint. |
---|
179 | n/a | self.assertIs(posixpath.ismount(ABSTFN), False) |
---|
180 | n/a | try: |
---|
181 | n/a | os.mkdir(ABSTFN) |
---|
182 | n/a | self.assertIs(posixpath.ismount(ABSTFN), False) |
---|
183 | n/a | finally: |
---|
184 | n/a | safe_rmdir(ABSTFN) |
---|
185 | n/a | |
---|
186 | n/a | @unittest.skipUnless(support.can_symlink(), |
---|
187 | n/a | "Test requires symlink support") |
---|
188 | n/a | def test_ismount_symlinks(self): |
---|
189 | n/a | # Symlinks are never mountpoints. |
---|
190 | n/a | try: |
---|
191 | n/a | os.symlink("/", ABSTFN) |
---|
192 | n/a | self.assertIs(posixpath.ismount(ABSTFN), False) |
---|
193 | n/a | finally: |
---|
194 | n/a | os.unlink(ABSTFN) |
---|
195 | n/a | |
---|
196 | n/a | @unittest.skipIf(posix is None, "Test requires posix module") |
---|
197 | n/a | def test_ismount_different_device(self): |
---|
198 | n/a | # Simulate the path being on a different device from its parent by |
---|
199 | n/a | # mocking out st_dev. |
---|
200 | n/a | save_lstat = os.lstat |
---|
201 | n/a | def fake_lstat(path): |
---|
202 | n/a | st_ino = 0 |
---|
203 | n/a | st_dev = 0 |
---|
204 | n/a | if path == ABSTFN: |
---|
205 | n/a | st_dev = 1 |
---|
206 | n/a | st_ino = 1 |
---|
207 | n/a | return posix.stat_result((0, st_ino, st_dev, 0, 0, 0, 0, 0, 0, 0)) |
---|
208 | n/a | try: |
---|
209 | n/a | os.lstat = fake_lstat |
---|
210 | n/a | self.assertIs(posixpath.ismount(ABSTFN), True) |
---|
211 | n/a | finally: |
---|
212 | n/a | os.lstat = save_lstat |
---|
213 | n/a | |
---|
214 | n/a | @unittest.skipIf(posix is None, "Test requires posix module") |
---|
215 | n/a | def test_ismount_directory_not_readable(self): |
---|
216 | n/a | # issue #2466: Simulate ismount run on a directory that is not |
---|
217 | n/a | # readable, which used to return False. |
---|
218 | n/a | save_lstat = os.lstat |
---|
219 | n/a | def fake_lstat(path): |
---|
220 | n/a | st_ino = 0 |
---|
221 | n/a | st_dev = 0 |
---|
222 | n/a | if path.startswith(ABSTFN) and path != ABSTFN: |
---|
223 | n/a | # ismount tries to read something inside the ABSTFN directory; |
---|
224 | n/a | # simulate this being forbidden (no read permission). |
---|
225 | n/a | raise OSError("Fake [Errno 13] Permission denied") |
---|
226 | n/a | if path == ABSTFN: |
---|
227 | n/a | st_dev = 1 |
---|
228 | n/a | st_ino = 1 |
---|
229 | n/a | return posix.stat_result((0, st_ino, st_dev, 0, 0, 0, 0, 0, 0, 0)) |
---|
230 | n/a | try: |
---|
231 | n/a | os.lstat = fake_lstat |
---|
232 | n/a | self.assertIs(posixpath.ismount(ABSTFN), True) |
---|
233 | n/a | finally: |
---|
234 | n/a | os.lstat = save_lstat |
---|
235 | n/a | |
---|
236 | n/a | def test_expanduser(self): |
---|
237 | n/a | self.assertEqual(posixpath.expanduser("foo"), "foo") |
---|
238 | n/a | self.assertEqual(posixpath.expanduser(b"foo"), b"foo") |
---|
239 | n/a | with support.EnvironmentVarGuard() as env: |
---|
240 | n/a | for home in '/', '', '//', '///': |
---|
241 | n/a | with self.subTest(home=home): |
---|
242 | n/a | env['HOME'] = home |
---|
243 | n/a | self.assertEqual(posixpath.expanduser("~"), "/") |
---|
244 | n/a | self.assertEqual(posixpath.expanduser("~/"), "/") |
---|
245 | n/a | self.assertEqual(posixpath.expanduser("~/foo"), "/foo") |
---|
246 | n/a | try: |
---|
247 | n/a | import pwd |
---|
248 | n/a | except ImportError: |
---|
249 | n/a | pass |
---|
250 | n/a | else: |
---|
251 | n/a | self.assertIsInstance(posixpath.expanduser("~/"), str) |
---|
252 | n/a | self.assertIsInstance(posixpath.expanduser(b"~/"), bytes) |
---|
253 | n/a | # if home directory == root directory, this test makes no sense |
---|
254 | n/a | if posixpath.expanduser("~") != '/': |
---|
255 | n/a | self.assertEqual( |
---|
256 | n/a | posixpath.expanduser("~") + "/", |
---|
257 | n/a | posixpath.expanduser("~/") |
---|
258 | n/a | ) |
---|
259 | n/a | self.assertEqual( |
---|
260 | n/a | posixpath.expanduser(b"~") + b"/", |
---|
261 | n/a | posixpath.expanduser(b"~/") |
---|
262 | n/a | ) |
---|
263 | n/a | self.assertIsInstance(posixpath.expanduser("~root/"), str) |
---|
264 | n/a | self.assertIsInstance(posixpath.expanduser("~foo/"), str) |
---|
265 | n/a | self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes) |
---|
266 | n/a | self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes) |
---|
267 | n/a | |
---|
268 | n/a | with support.EnvironmentVarGuard() as env: |
---|
269 | n/a | # expanduser should fall back to using the password database |
---|
270 | n/a | del env['HOME'] |
---|
271 | n/a | home = pwd.getpwuid(os.getuid()).pw_dir |
---|
272 | n/a | # $HOME can end with a trailing /, so strip it (see #17809) |
---|
273 | n/a | home = home.rstrip("/") or '/' |
---|
274 | n/a | self.assertEqual(posixpath.expanduser("~"), home) |
---|
275 | n/a | |
---|
276 | n/a | def test_normpath(self): |
---|
277 | n/a | self.assertEqual(posixpath.normpath(""), ".") |
---|
278 | n/a | self.assertEqual(posixpath.normpath("/"), "/") |
---|
279 | n/a | self.assertEqual(posixpath.normpath("//"), "//") |
---|
280 | n/a | self.assertEqual(posixpath.normpath("///"), "/") |
---|
281 | n/a | self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar") |
---|
282 | n/a | self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), |
---|
283 | n/a | "/foo/baz") |
---|
284 | n/a | self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar") |
---|
285 | n/a | |
---|
286 | n/a | self.assertEqual(posixpath.normpath(b""), b".") |
---|
287 | n/a | self.assertEqual(posixpath.normpath(b"/"), b"/") |
---|
288 | n/a | self.assertEqual(posixpath.normpath(b"//"), b"//") |
---|
289 | n/a | self.assertEqual(posixpath.normpath(b"///"), b"/") |
---|
290 | n/a | self.assertEqual(posixpath.normpath(b"///foo/.//bar//"), b"/foo/bar") |
---|
291 | n/a | self.assertEqual(posixpath.normpath(b"///foo/.//bar//.//..//.//baz"), |
---|
292 | n/a | b"/foo/baz") |
---|
293 | n/a | self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"), |
---|
294 | n/a | b"/foo/bar") |
---|
295 | n/a | |
---|
296 | n/a | @skip_if_ABSTFN_contains_backslash |
---|
297 | n/a | def test_realpath_curdir(self): |
---|
298 | n/a | self.assertEqual(realpath('.'), os.getcwd()) |
---|
299 | n/a | self.assertEqual(realpath('./.'), os.getcwd()) |
---|
300 | n/a | self.assertEqual(realpath('/'.join(['.'] * 100)), os.getcwd()) |
---|
301 | n/a | |
---|
302 | n/a | self.assertEqual(realpath(b'.'), os.getcwdb()) |
---|
303 | n/a | self.assertEqual(realpath(b'./.'), os.getcwdb()) |
---|
304 | n/a | self.assertEqual(realpath(b'/'.join([b'.'] * 100)), os.getcwdb()) |
---|
305 | n/a | |
---|
306 | n/a | @skip_if_ABSTFN_contains_backslash |
---|
307 | n/a | def test_realpath_pardir(self): |
---|
308 | n/a | self.assertEqual(realpath('..'), dirname(os.getcwd())) |
---|
309 | n/a | self.assertEqual(realpath('../..'), dirname(dirname(os.getcwd()))) |
---|
310 | n/a | self.assertEqual(realpath('/'.join(['..'] * 100)), '/') |
---|
311 | n/a | |
---|
312 | n/a | self.assertEqual(realpath(b'..'), dirname(os.getcwdb())) |
---|
313 | n/a | self.assertEqual(realpath(b'../..'), dirname(dirname(os.getcwdb()))) |
---|
314 | n/a | self.assertEqual(realpath(b'/'.join([b'..'] * 100)), b'/') |
---|
315 | n/a | |
---|
316 | n/a | @unittest.skipUnless(hasattr(os, "symlink"), |
---|
317 | n/a | "Missing symlink implementation") |
---|
318 | n/a | @skip_if_ABSTFN_contains_backslash |
---|
319 | n/a | def test_realpath_basic(self): |
---|
320 | n/a | # Basic operation. |
---|
321 | n/a | try: |
---|
322 | n/a | os.symlink(ABSTFN+"1", ABSTFN) |
---|
323 | n/a | self.assertEqual(realpath(ABSTFN), ABSTFN+"1") |
---|
324 | n/a | finally: |
---|
325 | n/a | support.unlink(ABSTFN) |
---|
326 | n/a | |
---|
327 | n/a | @unittest.skipUnless(hasattr(os, "symlink"), |
---|
328 | n/a | "Missing symlink implementation") |
---|
329 | n/a | @skip_if_ABSTFN_contains_backslash |
---|
330 | n/a | def test_realpath_relative(self): |
---|
331 | n/a | try: |
---|
332 | n/a | os.symlink(posixpath.relpath(ABSTFN+"1"), ABSTFN) |
---|
333 | n/a | self.assertEqual(realpath(ABSTFN), ABSTFN+"1") |
---|
334 | n/a | finally: |
---|
335 | n/a | support.unlink(ABSTFN) |
---|
336 | n/a | |
---|
337 | n/a | @unittest.skipUnless(hasattr(os, "symlink"), |
---|
338 | n/a | "Missing symlink implementation") |
---|
339 | n/a | @skip_if_ABSTFN_contains_backslash |
---|
340 | n/a | def test_realpath_symlink_loops(self): |
---|
341 | n/a | # Bug #930024, return the path unchanged if we get into an infinite |
---|
342 | n/a | # symlink loop. |
---|
343 | n/a | try: |
---|
344 | n/a | os.symlink(ABSTFN, ABSTFN) |
---|
345 | n/a | self.assertEqual(realpath(ABSTFN), ABSTFN) |
---|
346 | n/a | |
---|
347 | n/a | os.symlink(ABSTFN+"1", ABSTFN+"2") |
---|
348 | n/a | os.symlink(ABSTFN+"2", ABSTFN+"1") |
---|
349 | n/a | self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1") |
---|
350 | n/a | self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2") |
---|
351 | n/a | |
---|
352 | n/a | self.assertEqual(realpath(ABSTFN+"1/x"), ABSTFN+"1/x") |
---|
353 | n/a | self.assertEqual(realpath(ABSTFN+"1/.."), dirname(ABSTFN)) |
---|
354 | n/a | self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x") |
---|
355 | n/a | os.symlink(ABSTFN+"x", ABSTFN+"y") |
---|
356 | n/a | self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"), |
---|
357 | n/a | ABSTFN + "y") |
---|
358 | n/a | self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"), |
---|
359 | n/a | ABSTFN + "1") |
---|
360 | n/a | |
---|
361 | n/a | os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a") |
---|
362 | n/a | self.assertEqual(realpath(ABSTFN+"a"), ABSTFN+"a/b") |
---|
363 | n/a | |
---|
364 | n/a | os.symlink("../" + basename(dirname(ABSTFN)) + "/" + |
---|
365 | n/a | basename(ABSTFN) + "c", ABSTFN+"c") |
---|
366 | n/a | self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c") |
---|
367 | n/a | |
---|
368 | n/a | # Test using relative path as well. |
---|
369 | n/a | with support.change_cwd(dirname(ABSTFN)): |
---|
370 | n/a | self.assertEqual(realpath(basename(ABSTFN)), ABSTFN) |
---|
371 | n/a | finally: |
---|
372 | n/a | support.unlink(ABSTFN) |
---|
373 | n/a | support.unlink(ABSTFN+"1") |
---|
374 | n/a | support.unlink(ABSTFN+"2") |
---|
375 | n/a | support.unlink(ABSTFN+"y") |
---|
376 | n/a | support.unlink(ABSTFN+"c") |
---|
377 | n/a | support.unlink(ABSTFN+"a") |
---|
378 | n/a | |
---|
379 | n/a | @unittest.skipUnless(hasattr(os, "symlink"), |
---|
380 | n/a | "Missing symlink implementation") |
---|
381 | n/a | @skip_if_ABSTFN_contains_backslash |
---|
382 | n/a | def test_realpath_repeated_indirect_symlinks(self): |
---|
383 | n/a | # Issue #6975. |
---|
384 | n/a | try: |
---|
385 | n/a | os.mkdir(ABSTFN) |
---|
386 | n/a | os.symlink('../' + basename(ABSTFN), ABSTFN + '/self') |
---|
387 | n/a | os.symlink('self/self/self', ABSTFN + '/link') |
---|
388 | n/a | self.assertEqual(realpath(ABSTFN + '/link'), ABSTFN) |
---|
389 | n/a | finally: |
---|
390 | n/a | support.unlink(ABSTFN + '/self') |
---|
391 | n/a | support.unlink(ABSTFN + '/link') |
---|
392 | n/a | safe_rmdir(ABSTFN) |
---|
393 | n/a | |
---|
394 | n/a | @unittest.skipUnless(hasattr(os, "symlink"), |
---|
395 | n/a | "Missing symlink implementation") |
---|
396 | n/a | @skip_if_ABSTFN_contains_backslash |
---|
397 | n/a | def test_realpath_deep_recursion(self): |
---|
398 | n/a | depth = 10 |
---|
399 | n/a | try: |
---|
400 | n/a | os.mkdir(ABSTFN) |
---|
401 | n/a | for i in range(depth): |
---|
402 | n/a | os.symlink('/'.join(['%d' % i] * 10), ABSTFN + '/%d' % (i + 1)) |
---|
403 | n/a | os.symlink('.', ABSTFN + '/0') |
---|
404 | n/a | self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN) |
---|
405 | n/a | |
---|
406 | n/a | # Test using relative path as well. |
---|
407 | n/a | with support.change_cwd(ABSTFN): |
---|
408 | n/a | self.assertEqual(realpath('%d' % depth), ABSTFN) |
---|
409 | n/a | finally: |
---|
410 | n/a | for i in range(depth + 1): |
---|
411 | n/a | support.unlink(ABSTFN + '/%d' % i) |
---|
412 | n/a | safe_rmdir(ABSTFN) |
---|
413 | n/a | |
---|
414 | n/a | @unittest.skipUnless(hasattr(os, "symlink"), |
---|
415 | n/a | "Missing symlink implementation") |
---|
416 | n/a | @skip_if_ABSTFN_contains_backslash |
---|
417 | n/a | def test_realpath_resolve_parents(self): |
---|
418 | n/a | # We also need to resolve any symlinks in the parents of a relative |
---|
419 | n/a | # path passed to realpath. E.g.: current working directory is |
---|
420 | n/a | # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call |
---|
421 | n/a | # realpath("a"). This should return /usr/share/doc/a/. |
---|
422 | n/a | try: |
---|
423 | n/a | os.mkdir(ABSTFN) |
---|
424 | n/a | os.mkdir(ABSTFN + "/y") |
---|
425 | n/a | os.symlink(ABSTFN + "/y", ABSTFN + "/k") |
---|
426 | n/a | |
---|
427 | n/a | with support.change_cwd(ABSTFN + "/k"): |
---|
428 | n/a | self.assertEqual(realpath("a"), ABSTFN + "/y/a") |
---|
429 | n/a | finally: |
---|
430 | n/a | support.unlink(ABSTFN + "/k") |
---|
431 | n/a | safe_rmdir(ABSTFN + "/y") |
---|
432 | n/a | safe_rmdir(ABSTFN) |
---|
433 | n/a | |
---|
434 | n/a | @unittest.skipUnless(hasattr(os, "symlink"), |
---|
435 | n/a | "Missing symlink implementation") |
---|
436 | n/a | @skip_if_ABSTFN_contains_backslash |
---|
437 | n/a | def test_realpath_resolve_before_normalizing(self): |
---|
438 | n/a | # Bug #990669: Symbolic links should be resolved before we |
---|
439 | n/a | # normalize the path. E.g.: if we have directories 'a', 'k' and 'y' |
---|
440 | n/a | # in the following hierarchy: |
---|
441 | n/a | # a/k/y |
---|
442 | n/a | # |
---|
443 | n/a | # and a symbolic link 'link-y' pointing to 'y' in directory 'a', |
---|
444 | n/a | # then realpath("link-y/..") should return 'k', not 'a'. |
---|
445 | n/a | try: |
---|
446 | n/a | os.mkdir(ABSTFN) |
---|
447 | n/a | os.mkdir(ABSTFN + "/k") |
---|
448 | n/a | os.mkdir(ABSTFN + "/k/y") |
---|
449 | n/a | os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y") |
---|
450 | n/a | |
---|
451 | n/a | # Absolute path. |
---|
452 | n/a | self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k") |
---|
453 | n/a | # Relative path. |
---|
454 | n/a | with support.change_cwd(dirname(ABSTFN)): |
---|
455 | n/a | self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), |
---|
456 | n/a | ABSTFN + "/k") |
---|
457 | n/a | finally: |
---|
458 | n/a | support.unlink(ABSTFN + "/link-y") |
---|
459 | n/a | safe_rmdir(ABSTFN + "/k/y") |
---|
460 | n/a | safe_rmdir(ABSTFN + "/k") |
---|
461 | n/a | safe_rmdir(ABSTFN) |
---|
462 | n/a | |
---|
463 | n/a | @unittest.skipUnless(hasattr(os, "symlink"), |
---|
464 | n/a | "Missing symlink implementation") |
---|
465 | n/a | @skip_if_ABSTFN_contains_backslash |
---|
466 | n/a | def test_realpath_resolve_first(self): |
---|
467 | n/a | # Bug #1213894: The first component of the path, if not absolute, |
---|
468 | n/a | # must be resolved too. |
---|
469 | n/a | |
---|
470 | n/a | try: |
---|
471 | n/a | os.mkdir(ABSTFN) |
---|
472 | n/a | os.mkdir(ABSTFN + "/k") |
---|
473 | n/a | os.symlink(ABSTFN, ABSTFN + "link") |
---|
474 | n/a | with support.change_cwd(dirname(ABSTFN)): |
---|
475 | n/a | base = basename(ABSTFN) |
---|
476 | n/a | self.assertEqual(realpath(base + "link"), ABSTFN) |
---|
477 | n/a | self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k") |
---|
478 | n/a | finally: |
---|
479 | n/a | support.unlink(ABSTFN + "link") |
---|
480 | n/a | safe_rmdir(ABSTFN + "/k") |
---|
481 | n/a | safe_rmdir(ABSTFN) |
---|
482 | n/a | |
---|
483 | n/a | def test_relpath(self): |
---|
484 | n/a | (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar") |
---|
485 | n/a | try: |
---|
486 | n/a | curdir = os.path.split(os.getcwd())[-1] |
---|
487 | n/a | self.assertRaises(ValueError, posixpath.relpath, "") |
---|
488 | n/a | self.assertEqual(posixpath.relpath("a"), "a") |
---|
489 | n/a | self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a") |
---|
490 | n/a | self.assertEqual(posixpath.relpath("a/b"), "a/b") |
---|
491 | n/a | self.assertEqual(posixpath.relpath("../a/b"), "../a/b") |
---|
492 | n/a | self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a") |
---|
493 | n/a | self.assertEqual(posixpath.relpath("a/b", "../c"), |
---|
494 | n/a | "../"+curdir+"/a/b") |
---|
495 | n/a | self.assertEqual(posixpath.relpath("a", "b/c"), "../../a") |
---|
496 | n/a | self.assertEqual(posixpath.relpath("a", "a"), ".") |
---|
497 | n/a | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat') |
---|
498 | n/a | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/foo/bar"), 'bat') |
---|
499 | n/a | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/"), 'foo/bar/bat') |
---|
500 | n/a | self.assertEqual(posixpath.relpath("/", "/foo/bar/bat"), '../../..') |
---|
501 | n/a | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x"), '../foo/bar/bat') |
---|
502 | n/a | self.assertEqual(posixpath.relpath("/x", "/foo/bar/bat"), '../../../x') |
---|
503 | n/a | self.assertEqual(posixpath.relpath("/", "/"), '.') |
---|
504 | n/a | self.assertEqual(posixpath.relpath("/a", "/a"), '.') |
---|
505 | n/a | self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.') |
---|
506 | n/a | finally: |
---|
507 | n/a | os.getcwd = real_getcwd |
---|
508 | n/a | |
---|
509 | n/a | def test_relpath_bytes(self): |
---|
510 | n/a | (real_getcwdb, os.getcwdb) = (os.getcwdb, lambda: br"/home/user/bar") |
---|
511 | n/a | try: |
---|
512 | n/a | curdir = os.path.split(os.getcwdb())[-1] |
---|
513 | n/a | self.assertRaises(ValueError, posixpath.relpath, b"") |
---|
514 | n/a | self.assertEqual(posixpath.relpath(b"a"), b"a") |
---|
515 | n/a | self.assertEqual(posixpath.relpath(posixpath.abspath(b"a")), b"a") |
---|
516 | n/a | self.assertEqual(posixpath.relpath(b"a/b"), b"a/b") |
---|
517 | n/a | self.assertEqual(posixpath.relpath(b"../a/b"), b"../a/b") |
---|
518 | n/a | self.assertEqual(posixpath.relpath(b"a", b"../b"), |
---|
519 | n/a | b"../"+curdir+b"/a") |
---|
520 | n/a | self.assertEqual(posixpath.relpath(b"a/b", b"../c"), |
---|
521 | n/a | b"../"+curdir+b"/a/b") |
---|
522 | n/a | self.assertEqual(posixpath.relpath(b"a", b"b/c"), b"../../a") |
---|
523 | n/a | self.assertEqual(posixpath.relpath(b"a", b"a"), b".") |
---|
524 | n/a | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x/y/z"), b'../../../foo/bar/bat') |
---|
525 | n/a | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/foo/bar"), b'bat') |
---|
526 | n/a | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/"), b'foo/bar/bat') |
---|
527 | n/a | self.assertEqual(posixpath.relpath(b"/", b"/foo/bar/bat"), b'../../..') |
---|
528 | n/a | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x"), b'../foo/bar/bat') |
---|
529 | n/a | self.assertEqual(posixpath.relpath(b"/x", b"/foo/bar/bat"), b'../../../x') |
---|
530 | n/a | self.assertEqual(posixpath.relpath(b"/", b"/"), b'.') |
---|
531 | n/a | self.assertEqual(posixpath.relpath(b"/a", b"/a"), b'.') |
---|
532 | n/a | self.assertEqual(posixpath.relpath(b"/a/b", b"/a/b"), b'.') |
---|
533 | n/a | |
---|
534 | n/a | self.assertRaises(TypeError, posixpath.relpath, b"bytes", "str") |
---|
535 | n/a | self.assertRaises(TypeError, posixpath.relpath, "str", b"bytes") |
---|
536 | n/a | finally: |
---|
537 | n/a | os.getcwdb = real_getcwdb |
---|
538 | n/a | |
---|
539 | n/a | def test_commonpath(self): |
---|
540 | n/a | def check(paths, expected): |
---|
541 | n/a | self.assertEqual(posixpath.commonpath(paths), expected) |
---|
542 | n/a | self.assertEqual(posixpath.commonpath([os.fsencode(p) for p in paths]), |
---|
543 | n/a | os.fsencode(expected)) |
---|
544 | n/a | def check_error(exc, paths): |
---|
545 | n/a | self.assertRaises(exc, posixpath.commonpath, paths) |
---|
546 | n/a | self.assertRaises(exc, posixpath.commonpath, |
---|
547 | n/a | [os.fsencode(p) for p in paths]) |
---|
548 | n/a | |
---|
549 | n/a | self.assertRaises(ValueError, posixpath.commonpath, []) |
---|
550 | n/a | check_error(ValueError, ['/usr', 'usr']) |
---|
551 | n/a | check_error(ValueError, ['usr', '/usr']) |
---|
552 | n/a | |
---|
553 | n/a | check(['/usr/local'], '/usr/local') |
---|
554 | n/a | check(['/usr/local', '/usr/local'], '/usr/local') |
---|
555 | n/a | check(['/usr/local/', '/usr/local'], '/usr/local') |
---|
556 | n/a | check(['/usr/local/', '/usr/local/'], '/usr/local') |
---|
557 | n/a | check(['/usr//local', '//usr/local'], '/usr/local') |
---|
558 | n/a | check(['/usr/./local', '/./usr/local'], '/usr/local') |
---|
559 | n/a | check(['/', '/dev'], '/') |
---|
560 | n/a | check(['/usr', '/dev'], '/') |
---|
561 | n/a | check(['/usr/lib/', '/usr/lib/python3'], '/usr/lib') |
---|
562 | n/a | check(['/usr/lib/', '/usr/lib64/'], '/usr') |
---|
563 | n/a | |
---|
564 | n/a | check(['/usr/lib', '/usr/lib64'], '/usr') |
---|
565 | n/a | check(['/usr/lib/', '/usr/lib64'], '/usr') |
---|
566 | n/a | |
---|
567 | n/a | check(['spam'], 'spam') |
---|
568 | n/a | check(['spam', 'spam'], 'spam') |
---|
569 | n/a | check(['spam', 'alot'], '') |
---|
570 | n/a | check(['and/jam', 'and/spam'], 'and') |
---|
571 | n/a | check(['and//jam', 'and/spam//'], 'and') |
---|
572 | n/a | check(['and/./jam', './and/spam'], 'and') |
---|
573 | n/a | check(['and/jam', 'and/spam', 'alot'], '') |
---|
574 | n/a | check(['and/jam', 'and/spam', 'and'], 'and') |
---|
575 | n/a | |
---|
576 | n/a | check([''], '') |
---|
577 | n/a | check(['', 'spam/alot'], '') |
---|
578 | n/a | check_error(ValueError, ['', '/spam/alot']) |
---|
579 | n/a | |
---|
580 | n/a | self.assertRaises(TypeError, posixpath.commonpath, |
---|
581 | n/a | [b'/usr/lib/', '/usr/lib/python3']) |
---|
582 | n/a | self.assertRaises(TypeError, posixpath.commonpath, |
---|
583 | n/a | [b'/usr/lib/', 'usr/lib/python3']) |
---|
584 | n/a | self.assertRaises(TypeError, posixpath.commonpath, |
---|
585 | n/a | [b'usr/lib/', '/usr/lib/python3']) |
---|
586 | n/a | self.assertRaises(TypeError, posixpath.commonpath, |
---|
587 | n/a | ['/usr/lib/', b'/usr/lib/python3']) |
---|
588 | n/a | self.assertRaises(TypeError, posixpath.commonpath, |
---|
589 | n/a | ['/usr/lib/', b'usr/lib/python3']) |
---|
590 | n/a | self.assertRaises(TypeError, posixpath.commonpath, |
---|
591 | n/a | ['usr/lib/', b'/usr/lib/python3']) |
---|
592 | n/a | |
---|
593 | n/a | |
---|
594 | n/a | class PosixCommonTest(test_genericpath.CommonTest, unittest.TestCase): |
---|
595 | n/a | pathmodule = posixpath |
---|
596 | n/a | attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat'] |
---|
597 | n/a | |
---|
598 | n/a | |
---|
599 | n/a | class PathLikeTests(unittest.TestCase): |
---|
600 | n/a | |
---|
601 | n/a | path = posixpath |
---|
602 | n/a | |
---|
603 | n/a | class PathLike: |
---|
604 | n/a | def __init__(self, path=''): |
---|
605 | n/a | self.path = path |
---|
606 | n/a | def __fspath__(self): |
---|
607 | n/a | if isinstance(self.path, BaseException): |
---|
608 | n/a | raise self.path |
---|
609 | n/a | else: |
---|
610 | n/a | return self.path |
---|
611 | n/a | |
---|
612 | n/a | def setUp(self): |
---|
613 | n/a | self.file_name = support.TESTFN.lower() |
---|
614 | n/a | self.file_path = self.PathLike(support.TESTFN) |
---|
615 | n/a | self.addCleanup(support.unlink, self.file_name) |
---|
616 | n/a | with open(self.file_name, 'xb', 0) as file: |
---|
617 | n/a | file.write(b"test_posixpath.PathLikeTests") |
---|
618 | n/a | |
---|
619 | n/a | def assertPathEqual(self, func): |
---|
620 | n/a | self.assertEqual(func(self.file_path), func(self.file_name)) |
---|
621 | n/a | |
---|
622 | n/a | def test_path_normcase(self): |
---|
623 | n/a | self.assertPathEqual(self.path.normcase) |
---|
624 | n/a | |
---|
625 | n/a | def test_path_isabs(self): |
---|
626 | n/a | self.assertPathEqual(self.path.isabs) |
---|
627 | n/a | |
---|
628 | n/a | def test_path_join(self): |
---|
629 | n/a | self.assertEqual(self.path.join('a', self.PathLike('b'), 'c'), |
---|
630 | n/a | self.path.join('a', 'b', 'c')) |
---|
631 | n/a | |
---|
632 | n/a | def test_path_split(self): |
---|
633 | n/a | self.assertPathEqual(self.path.split) |
---|
634 | n/a | |
---|
635 | n/a | def test_path_splitext(self): |
---|
636 | n/a | self.assertPathEqual(self.path.splitext) |
---|
637 | n/a | |
---|
638 | n/a | def test_path_splitdrive(self): |
---|
639 | n/a | self.assertPathEqual(self.path.splitdrive) |
---|
640 | n/a | |
---|
641 | n/a | def test_path_basename(self): |
---|
642 | n/a | self.assertPathEqual(self.path.basename) |
---|
643 | n/a | |
---|
644 | n/a | def test_path_dirname(self): |
---|
645 | n/a | self.assertPathEqual(self.path.dirname) |
---|
646 | n/a | |
---|
647 | n/a | def test_path_islink(self): |
---|
648 | n/a | self.assertPathEqual(self.path.islink) |
---|
649 | n/a | |
---|
650 | n/a | def test_path_lexists(self): |
---|
651 | n/a | self.assertPathEqual(self.path.lexists) |
---|
652 | n/a | |
---|
653 | n/a | def test_path_ismount(self): |
---|
654 | n/a | self.assertPathEqual(self.path.ismount) |
---|
655 | n/a | |
---|
656 | n/a | def test_path_expanduser(self): |
---|
657 | n/a | self.assertPathEqual(self.path.expanduser) |
---|
658 | n/a | |
---|
659 | n/a | def test_path_expandvars(self): |
---|
660 | n/a | self.assertPathEqual(self.path.expandvars) |
---|
661 | n/a | |
---|
662 | n/a | def test_path_normpath(self): |
---|
663 | n/a | self.assertPathEqual(self.path.normpath) |
---|
664 | n/a | |
---|
665 | n/a | def test_path_abspath(self): |
---|
666 | n/a | self.assertPathEqual(self.path.abspath) |
---|
667 | n/a | |
---|
668 | n/a | def test_path_realpath(self): |
---|
669 | n/a | self.assertPathEqual(self.path.realpath) |
---|
670 | n/a | |
---|
671 | n/a | def test_path_relpath(self): |
---|
672 | n/a | self.assertPathEqual(self.path.relpath) |
---|
673 | n/a | |
---|
674 | n/a | def test_path_commonpath(self): |
---|
675 | n/a | common_path = self.path.commonpath([self.file_path, self.file_name]) |
---|
676 | n/a | self.assertEqual(common_path, self.file_name) |
---|
677 | n/a | |
---|
678 | n/a | |
---|
679 | n/a | if __name__=="__main__": |
---|
680 | n/a | unittest.main() |
---|