blob: 599c85a4771afcb47a33030e8fee197508f64ce0 [file] [log] [blame]
Brett Cannonb47243a2003-06-16 21:54:50 +00001import unittest
Florent Xiclunac9c79782010-03-08 12:24:53 +00002from test import support, test_genericpath
Skip Montanaroe809b002000-07-12 00:20:08 +00003
Hynek Schlawackc5a45662012-07-17 13:05:43 +02004import itertools
Brian Curtind40e6f72010-07-08 21:39:08 +00005import posixpath
6import os
7import sys
Georg Brandl89fad142010-03-14 10:23:39 +00008from posixpath import realpath, abspath, dirname, basename
Johannes Gijsbers4ec40642004-08-14 15:01:53 +00009
Michael Foord07926f02011-03-16 17:19:16 -040010try:
11 import posix
12except ImportError:
13 posix = None
14
Johannes Gijsbers4ec40642004-08-14 15:01:53 +000015# An absolute path to a temporary filename for testing. We can't rely on TESTFN
16# being an absolute path, so we need this.
17
Benjamin Petersonee8712c2008-05-20 21:35:26 +000018ABSTFN = abspath(support.TESTFN)
Skip Montanaroe809b002000-07-12 00:20:08 +000019
Brian Curtind40e6f72010-07-08 21:39:08 +000020def skip_if_ABSTFN_contains_backslash(test):
21 """
22 On Windows, posixpath.abspath still returns paths with backslashes
23 instead of posix forward slashes. If this is the case, several tests
24 fail, so skip them.
25 """
26 found_backslash = '\\' in ABSTFN
27 msg = "ABSTFN is not a posix path - tests fail"
28 return [test, unittest.skip(msg)(test)][found_backslash]
29
Guido van Rossumd8faa362007-04-27 19:54:29 +000030def safe_rmdir(dirname):
31 try:
32 os.rmdir(dirname)
33 except OSError:
34 pass
35
Brett Cannonb47243a2003-06-16 21:54:50 +000036class PosixPathTest(unittest.TestCase):
Skip Montanaroe809b002000-07-12 00:20:08 +000037
Guido van Rossumd8faa362007-04-27 19:54:29 +000038 def setUp(self):
39 self.tearDown()
40
41 def tearDown(self):
42 for suffix in ["", "1", "2"]:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000043 support.unlink(support.TESTFN + suffix)
44 safe_rmdir(support.TESTFN + suffix)
Guido van Rossumd8faa362007-04-27 19:54:29 +000045
Brett Cannonb47243a2003-06-16 21:54:50 +000046 def test_join(self):
Guido van Rossumf0af3e32008-10-02 18:55:37 +000047 self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"),
48 "/bar/baz")
Brett Cannonb47243a2003-06-16 21:54:50 +000049 self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz")
Guido van Rossumf0af3e32008-10-02 18:55:37 +000050 self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"),
51 "/foo/bar/baz/")
52
53 self.assertEqual(posixpath.join(b"/foo", b"bar", b"/bar", b"baz"),
54 b"/bar/baz")
55 self.assertEqual(posixpath.join(b"/foo", b"bar", b"baz"),
56 b"/foo/bar/baz")
57 self.assertEqual(posixpath.join(b"/foo/", b"bar/", b"baz/"),
58 b"/foo/bar/baz/")
Skip Montanaroe809b002000-07-12 00:20:08 +000059
Hynek Schlawackc5a45662012-07-17 13:05:43 +020060 def check_error_msg(list_of_args, msg):
61 """Check posixpath.join raises friendly TypeErrors."""
62 for args in (item for perm in list_of_args
63 for item in itertools.permutations(perm)):
Hynek Schlawack7cdc2bd2012-07-17 10:48:19 +020064 with self.assertRaises(TypeError) as cm:
65 posixpath.join(*args)
Hynek Schlawackc5a45662012-07-17 13:05:43 +020066 self.assertEqual(msg, cm.exception.args[0])
Hynek Schlawack7cdc2bd2012-07-17 10:48:19 +020067
Hynek Schlawackc5a45662012-07-17 13:05:43 +020068 check_error_msg([[b'bytes', 'str'], [bytearray(b'bytes'), 'str']],
69 "Can't mix strings and bytes in path components.")
70 # regression, see #15377
71 with self.assertRaises(TypeError) as cm:
Hynek Schlawack0b350c62012-07-17 14:28:44 +020072 posixpath.join(None, 'str')
Hynek Schlawackc5a45662012-07-17 13:05:43 +020073 self.assertNotEqual("Can't mix strings and bytes in path components.",
74 cm.exception.args[0])
Skip Montanaroe809b002000-07-12 00:20:08 +000075
Brett Cannonb47243a2003-06-16 21:54:50 +000076 def test_split(self):
77 self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))
78 self.assertEqual(posixpath.split("/"), ("/", ""))
79 self.assertEqual(posixpath.split("foo"), ("", "foo"))
80 self.assertEqual(posixpath.split("////foo"), ("////", "foo"))
81 self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar"))
82
Guido van Rossumf0af3e32008-10-02 18:55:37 +000083 self.assertEqual(posixpath.split(b"/foo/bar"), (b"/foo", b"bar"))
84 self.assertEqual(posixpath.split(b"/"), (b"/", b""))
85 self.assertEqual(posixpath.split(b"foo"), (b"", b"foo"))
86 self.assertEqual(posixpath.split(b"////foo"), (b"////", b"foo"))
87 self.assertEqual(posixpath.split(b"//foo//bar"), (b"//foo", b"bar"))
88
Guido van Rossumd8faa362007-04-27 19:54:29 +000089 def splitextTest(self, path, filename, ext):
90 self.assertEqual(posixpath.splitext(path), (filename, ext))
91 self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext))
Guido van Rossumf0af3e32008-10-02 18:55:37 +000092 self.assertEqual(posixpath.splitext("abc/" + path),
93 ("abc/" + filename, ext))
94 self.assertEqual(posixpath.splitext("abc.def/" + path),
95 ("abc.def/" + filename, ext))
96 self.assertEqual(posixpath.splitext("/abc.def/" + path),
97 ("/abc.def/" + filename, ext))
98 self.assertEqual(posixpath.splitext(path + "/"),
99 (filename + ext + "/", ""))
100
101 path = bytes(path, "ASCII")
102 filename = bytes(filename, "ASCII")
103 ext = bytes(ext, "ASCII")
104
105 self.assertEqual(posixpath.splitext(path), (filename, ext))
106 self.assertEqual(posixpath.splitext(b"/" + path),
107 (b"/" + filename, ext))
108 self.assertEqual(posixpath.splitext(b"abc/" + path),
109 (b"abc/" + filename, ext))
110 self.assertEqual(posixpath.splitext(b"abc.def/" + path),
111 (b"abc.def/" + filename, ext))
112 self.assertEqual(posixpath.splitext(b"/abc.def/" + path),
113 (b"/abc.def/" + filename, ext))
114 self.assertEqual(posixpath.splitext(path + b"/"),
115 (filename + ext + b"/", b""))
Brett Cannonb47243a2003-06-16 21:54:50 +0000116
Guido van Rossumd8faa362007-04-27 19:54:29 +0000117 def test_splitext(self):
118 self.splitextTest("foo.bar", "foo", ".bar")
119 self.splitextTest("foo.boo.bar", "foo.boo", ".bar")
120 self.splitextTest("foo.boo.biff.bar", "foo.boo.biff", ".bar")
121 self.splitextTest(".csh.rc", ".csh", ".rc")
122 self.splitextTest("nodots", "nodots", "")
123 self.splitextTest(".cshrc", ".cshrc", "")
124 self.splitextTest("...manydots", "...manydots", "")
125 self.splitextTest("...manydots.ext", "...manydots", ".ext")
126 self.splitextTest(".", ".", "")
127 self.splitextTest("..", "..", "")
128 self.splitextTest("........", "........", "")
129 self.splitextTest("", "", "")
Brett Cannonb47243a2003-06-16 21:54:50 +0000130
131 def test_isabs(self):
132 self.assertIs(posixpath.isabs(""), False)
133 self.assertIs(posixpath.isabs("/"), True)
134 self.assertIs(posixpath.isabs("/foo"), True)
135 self.assertIs(posixpath.isabs("/foo/bar"), True)
136 self.assertIs(posixpath.isabs("foo/bar"), False)
137
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000138 self.assertIs(posixpath.isabs(b""), False)
139 self.assertIs(posixpath.isabs(b"/"), True)
140 self.assertIs(posixpath.isabs(b"/foo"), True)
141 self.assertIs(posixpath.isabs(b"/foo/bar"), True)
142 self.assertIs(posixpath.isabs(b"foo/bar"), False)
143
Brett Cannonb47243a2003-06-16 21:54:50 +0000144 def test_basename(self):
145 self.assertEqual(posixpath.basename("/foo/bar"), "bar")
146 self.assertEqual(posixpath.basename("/"), "")
147 self.assertEqual(posixpath.basename("foo"), "foo")
148 self.assertEqual(posixpath.basename("////foo"), "foo")
149 self.assertEqual(posixpath.basename("//foo//bar"), "bar")
150
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000151 self.assertEqual(posixpath.basename(b"/foo/bar"), b"bar")
152 self.assertEqual(posixpath.basename(b"/"), b"")
153 self.assertEqual(posixpath.basename(b"foo"), b"foo")
154 self.assertEqual(posixpath.basename(b"////foo"), b"foo")
155 self.assertEqual(posixpath.basename(b"//foo//bar"), b"bar")
156
Brett Cannonb47243a2003-06-16 21:54:50 +0000157 def test_dirname(self):
158 self.assertEqual(posixpath.dirname("/foo/bar"), "/foo")
159 self.assertEqual(posixpath.dirname("/"), "/")
160 self.assertEqual(posixpath.dirname("foo"), "")
161 self.assertEqual(posixpath.dirname("////foo"), "////")
162 self.assertEqual(posixpath.dirname("//foo//bar"), "//foo")
163
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000164 self.assertEqual(posixpath.dirname(b"/foo/bar"), b"/foo")
165 self.assertEqual(posixpath.dirname(b"/"), b"/")
166 self.assertEqual(posixpath.dirname(b"foo"), b"")
167 self.assertEqual(posixpath.dirname(b"////foo"), b"////")
168 self.assertEqual(posixpath.dirname(b"//foo//bar"), b"//foo")
169
Brett Cannonb47243a2003-06-16 21:54:50 +0000170 def test_islink(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000171 self.assertIs(posixpath.islink(support.TESTFN + "1"), False)
Michael Foord07926f02011-03-16 17:19:16 -0400172 self.assertIs(posixpath.lexists(support.TESTFN + "2"), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000173 f = open(support.TESTFN + "1", "wb")
Brett Cannonb47243a2003-06-16 21:54:50 +0000174 try:
Guido van Rossum7dcb8442007-08-27 23:26:56 +0000175 f.write(b"foo")
Brett Cannonb47243a2003-06-16 21:54:50 +0000176 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000177 self.assertIs(posixpath.islink(support.TESTFN + "1"), False)
Brian Curtin3b4499c2010-12-28 14:31:47 +0000178 if support.can_symlink():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000179 os.symlink(support.TESTFN + "1", support.TESTFN + "2")
180 self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
181 os.remove(support.TESTFN + "1")
182 self.assertIs(posixpath.islink(support.TESTFN + "2"), True)
183 self.assertIs(posixpath.exists(support.TESTFN + "2"), False)
184 self.assertIs(posixpath.lexists(support.TESTFN + "2"), True)
Brett Cannonb47243a2003-06-16 21:54:50 +0000185 finally:
186 if not f.close():
187 f.close()
Brett Cannonb47243a2003-06-16 21:54:50 +0000188
Brian Curtind40e6f72010-07-08 21:39:08 +0000189 @staticmethod
190 def _create_file(filename):
191 with open(filename, 'wb') as f:
192 f.write(b'foo')
193
Guido van Rossumd8faa362007-04-27 19:54:29 +0000194 def test_samefile(self):
Brian Curtind40e6f72010-07-08 21:39:08 +0000195 test_fn = support.TESTFN + "1"
196 self._create_file(test_fn)
197 self.assertTrue(posixpath.samefile(test_fn, test_fn))
198 self.assertRaises(TypeError, posixpath.samefile)
199
200 @unittest.skipIf(
201 sys.platform.startswith('win'),
202 "posixpath.samefile does not work on links in Windows")
Brian Curtin52173d42010-12-02 18:29:18 +0000203 @unittest.skipUnless(hasattr(os, "symlink"),
204 "Missing symlink implementation")
Brian Curtind40e6f72010-07-08 21:39:08 +0000205 def test_samefile_on_links(self):
206 test_fn1 = support.TESTFN + "1"
207 test_fn2 = support.TESTFN + "2"
208 self._create_file(test_fn1)
209
210 os.symlink(test_fn1, test_fn2)
211 self.assertTrue(posixpath.samefile(test_fn1, test_fn2))
212 os.remove(test_fn2)
213
214 self._create_file(test_fn2)
215 self.assertFalse(posixpath.samefile(test_fn1, test_fn2))
216
Brett Cannonb47243a2003-06-16 21:54:50 +0000217
Brett Cannonb47243a2003-06-16 21:54:50 +0000218 def test_samestat(self):
Brian Curtind40e6f72010-07-08 21:39:08 +0000219 test_fn = support.TESTFN + "1"
220 self._create_file(test_fn)
221 test_fns = [test_fn]*2
222 stats = map(os.stat, test_fns)
223 self.assertTrue(posixpath.samestat(*stats))
224
225 @unittest.skipIf(
226 sys.platform.startswith('win'),
227 "posixpath.samestat does not work on links in Windows")
Brian Curtin52173d42010-12-02 18:29:18 +0000228 @unittest.skipUnless(hasattr(os, "symlink"),
229 "Missing symlink implementation")
Brian Curtind40e6f72010-07-08 21:39:08 +0000230 def test_samestat_on_links(self):
231 test_fn1 = support.TESTFN + "1"
232 test_fn2 = support.TESTFN + "2"
Brian Curtin16633fa2010-07-09 13:54:27 +0000233 self._create_file(test_fn1)
Brian Curtind40e6f72010-07-08 21:39:08 +0000234 test_fns = (test_fn1, test_fn2)
235 os.symlink(*test_fns)
236 stats = map(os.stat, test_fns)
237 self.assertTrue(posixpath.samestat(*stats))
238 os.remove(test_fn2)
239
240 self._create_file(test_fn2)
241 stats = map(os.stat, test_fns)
242 self.assertFalse(posixpath.samestat(*stats))
243
244 self.assertRaises(TypeError, posixpath.samestat)
Brett Cannonb47243a2003-06-16 21:54:50 +0000245
Brett Cannonb47243a2003-06-16 21:54:50 +0000246 def test_ismount(self):
247 self.assertIs(posixpath.ismount("/"), True)
Michael Foord07926f02011-03-16 17:19:16 -0400248 self.assertIs(posixpath.ismount(b"/"), True)
249
250 def test_ismount_non_existent(self):
251 # Non-existent mountpoint.
252 self.assertIs(posixpath.ismount(ABSTFN), False)
253 try:
254 os.mkdir(ABSTFN)
255 self.assertIs(posixpath.ismount(ABSTFN), False)
256 finally:
257 safe_rmdir(ABSTFN)
258
259 @unittest.skipUnless(support.can_symlink(),
260 "Test requires symlink support")
261 def test_ismount_symlinks(self):
262 # Symlinks are never mountpoints.
263 try:
264 os.symlink("/", ABSTFN)
265 self.assertIs(posixpath.ismount(ABSTFN), False)
266 finally:
267 os.unlink(ABSTFN)
268
269 @unittest.skipIf(posix is None, "Test requires posix module")
270 def test_ismount_different_device(self):
271 # Simulate the path being on a different device from its parent by
272 # mocking out st_dev.
273 save_lstat = os.lstat
274 def fake_lstat(path):
275 st_ino = 0
276 st_dev = 0
277 if path == ABSTFN:
278 st_dev = 1
279 st_ino = 1
280 return posix.stat_result((0, st_ino, st_dev, 0, 0, 0, 0, 0, 0, 0))
281 try:
282 os.lstat = fake_lstat
283 self.assertIs(posixpath.ismount(ABSTFN), True)
284 finally:
285 os.lstat = save_lstat
Brett Cannonb47243a2003-06-16 21:54:50 +0000286
Brett Cannonb47243a2003-06-16 21:54:50 +0000287 def test_expanduser(self):
288 self.assertEqual(posixpath.expanduser("foo"), "foo")
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000289 self.assertEqual(posixpath.expanduser(b"foo"), b"foo")
Brett Cannonb47243a2003-06-16 21:54:50 +0000290 try:
291 import pwd
292 except ImportError:
293 pass
294 else:
Ezio Melottie9615932010-01-24 19:26:24 +0000295 self.assertIsInstance(posixpath.expanduser("~/"), str)
296 self.assertIsInstance(posixpath.expanduser(b"~/"), bytes)
Neal Norwitz168e73d2003-07-01 03:33:31 +0000297 # if home directory == root directory, this test makes no sense
298 if posixpath.expanduser("~") != '/':
299 self.assertEqual(
300 posixpath.expanduser("~") + "/",
301 posixpath.expanduser("~/")
302 )
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000303 self.assertEqual(
304 posixpath.expanduser(b"~") + b"/",
305 posixpath.expanduser(b"~/")
306 )
Ezio Melottie9615932010-01-24 19:26:24 +0000307 self.assertIsInstance(posixpath.expanduser("~root/"), str)
308 self.assertIsInstance(posixpath.expanduser("~foo/"), str)
309 self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes)
310 self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes)
Brett Cannonb47243a2003-06-16 21:54:50 +0000311
Hirokazu Yamamoto71959632009-04-27 01:44:28 +0000312 with support.EnvironmentVarGuard() as env:
Walter Dörwald155374d2009-05-01 19:58:58 +0000313 env['HOME'] = '/'
Walter Dörwaldb525e182009-04-26 21:39:21 +0000314 self.assertEqual(posixpath.expanduser("~"), "/")
Jesus Cea7f0d8882012-05-10 05:10:50 +0200315 self.assertEqual(posixpath.expanduser("~/foo"), "/foo")
Michael Foord07926f02011-03-16 17:19:16 -0400316 # expanduser should fall back to using the password database
317 del env['HOME']
318 home = pwd.getpwuid(os.getuid()).pw_dir
319 self.assertEqual(posixpath.expanduser("~"), home)
Benjamin Petersonef3e4c22009-04-11 19:48:14 +0000320
Brett Cannonb47243a2003-06-16 21:54:50 +0000321 def test_normpath(self):
322 self.assertEqual(posixpath.normpath(""), ".")
323 self.assertEqual(posixpath.normpath("/"), "/")
324 self.assertEqual(posixpath.normpath("//"), "//")
325 self.assertEqual(posixpath.normpath("///"), "/")
326 self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar")
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000327 self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"),
328 "/foo/baz")
Brett Cannonb47243a2003-06-16 21:54:50 +0000329 self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
330
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000331 self.assertEqual(posixpath.normpath(b""), b".")
332 self.assertEqual(posixpath.normpath(b"/"), b"/")
333 self.assertEqual(posixpath.normpath(b"//"), b"//")
334 self.assertEqual(posixpath.normpath(b"///"), b"/")
335 self.assertEqual(posixpath.normpath(b"///foo/.//bar//"), b"/foo/bar")
336 self.assertEqual(posixpath.normpath(b"///foo/.//bar//.//..//.//baz"),
337 b"/foo/baz")
338 self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"),
339 b"/foo/bar")
340
Brian Curtin52173d42010-12-02 18:29:18 +0000341 @unittest.skipUnless(hasattr(os, "symlink"),
342 "Missing symlink implementation")
Brian Curtind40e6f72010-07-08 21:39:08 +0000343 @skip_if_ABSTFN_contains_backslash
344 def test_realpath_basic(self):
345 # Basic operation.
346 try:
347 os.symlink(ABSTFN+"1", ABSTFN)
348 self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
349 finally:
350 support.unlink(ABSTFN)
Tim Petersa45cacf2004-08-20 03:47:14 +0000351
Brian Curtin52173d42010-12-02 18:29:18 +0000352 @unittest.skipUnless(hasattr(os, "symlink"),
353 "Missing symlink implementation")
Brian Curtind40e6f72010-07-08 21:39:08 +0000354 @skip_if_ABSTFN_contains_backslash
Michael Foord07926f02011-03-16 17:19:16 -0400355 def test_realpath_relative(self):
356 try:
357 os.symlink(posixpath.relpath(ABSTFN+"1"), ABSTFN)
358 self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
359 finally:
360 support.unlink(ABSTFN)
361
362 @unittest.skipUnless(hasattr(os, "symlink"),
363 "Missing symlink implementation")
364 @skip_if_ABSTFN_contains_backslash
Brian Curtind40e6f72010-07-08 21:39:08 +0000365 def test_realpath_symlink_loops(self):
366 # Bug #930024, return the path unchanged if we get into an infinite
367 # symlink loop.
368 try:
369 old_path = abspath('.')
370 os.symlink(ABSTFN, ABSTFN)
371 self.assertEqual(realpath(ABSTFN), ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000372
Brian Curtind40e6f72010-07-08 21:39:08 +0000373 os.symlink(ABSTFN+"1", ABSTFN+"2")
374 os.symlink(ABSTFN+"2", ABSTFN+"1")
375 self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1")
376 self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2")
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000377
Brian Curtind40e6f72010-07-08 21:39:08 +0000378 # Test using relative path as well.
379 os.chdir(dirname(ABSTFN))
380 self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
381 finally:
382 os.chdir(old_path)
383 support.unlink(ABSTFN)
384 support.unlink(ABSTFN+"1")
385 support.unlink(ABSTFN+"2")
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000386
Brian Curtin52173d42010-12-02 18:29:18 +0000387 @unittest.skipUnless(hasattr(os, "symlink"),
388 "Missing symlink implementation")
Brian Curtind40e6f72010-07-08 21:39:08 +0000389 @skip_if_ABSTFN_contains_backslash
390 def test_realpath_resolve_parents(self):
391 # We also need to resolve any symlinks in the parents of a relative
392 # path passed to realpath. E.g.: current working directory is
393 # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
394 # realpath("a"). This should return /usr/share/doc/a/.
395 try:
396 old_path = abspath('.')
397 os.mkdir(ABSTFN)
398 os.mkdir(ABSTFN + "/y")
399 os.symlink(ABSTFN + "/y", ABSTFN + "/k")
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000400
Brian Curtind40e6f72010-07-08 21:39:08 +0000401 os.chdir(ABSTFN + "/k")
402 self.assertEqual(realpath("a"), ABSTFN + "/y/a")
403 finally:
404 os.chdir(old_path)
405 support.unlink(ABSTFN + "/k")
406 safe_rmdir(ABSTFN + "/y")
407 safe_rmdir(ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000408
Brian Curtin52173d42010-12-02 18:29:18 +0000409 @unittest.skipUnless(hasattr(os, "symlink"),
410 "Missing symlink implementation")
Brian Curtind40e6f72010-07-08 21:39:08 +0000411 @skip_if_ABSTFN_contains_backslash
412 def test_realpath_resolve_before_normalizing(self):
413 # Bug #990669: Symbolic links should be resolved before we
414 # normalize the path. E.g.: if we have directories 'a', 'k' and 'y'
415 # in the following hierarchy:
416 # a/k/y
417 #
418 # and a symbolic link 'link-y' pointing to 'y' in directory 'a',
419 # then realpath("link-y/..") should return 'k', not 'a'.
420 try:
421 old_path = abspath('.')
422 os.mkdir(ABSTFN)
423 os.mkdir(ABSTFN + "/k")
424 os.mkdir(ABSTFN + "/k/y")
425 os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y")
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000426
Brian Curtind40e6f72010-07-08 21:39:08 +0000427 # Absolute path.
428 self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k")
429 # Relative path.
430 os.chdir(dirname(ABSTFN))
431 self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."),
432 ABSTFN + "/k")
433 finally:
434 os.chdir(old_path)
435 support.unlink(ABSTFN + "/link-y")
436 safe_rmdir(ABSTFN + "/k/y")
437 safe_rmdir(ABSTFN + "/k")
438 safe_rmdir(ABSTFN)
Tim Peters5d36a552005-06-03 22:40:27 +0000439
Brian Curtin52173d42010-12-02 18:29:18 +0000440 @unittest.skipUnless(hasattr(os, "symlink"),
441 "Missing symlink implementation")
Brian Curtind40e6f72010-07-08 21:39:08 +0000442 @skip_if_ABSTFN_contains_backslash
443 def test_realpath_resolve_first(self):
444 # Bug #1213894: The first component of the path, if not absolute,
445 # must be resolved too.
Georg Brandl268e61c2005-06-03 14:28:50 +0000446
Brian Curtind40e6f72010-07-08 21:39:08 +0000447 try:
448 old_path = abspath('.')
449 os.mkdir(ABSTFN)
450 os.mkdir(ABSTFN + "/k")
451 os.symlink(ABSTFN, ABSTFN + "link")
452 os.chdir(dirname(ABSTFN))
Tim Peters5d36a552005-06-03 22:40:27 +0000453
Brian Curtind40e6f72010-07-08 21:39:08 +0000454 base = basename(ABSTFN)
455 self.assertEqual(realpath(base + "link"), ABSTFN)
456 self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
457 finally:
458 os.chdir(old_path)
459 support.unlink(ABSTFN + "link")
460 safe_rmdir(ABSTFN + "/k")
461 safe_rmdir(ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000462
Guido van Rossumd8faa362007-04-27 19:54:29 +0000463 def test_relpath(self):
464 (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
465 try:
466 curdir = os.path.split(os.getcwd())[-1]
467 self.assertRaises(ValueError, posixpath.relpath, "")
468 self.assertEqual(posixpath.relpath("a"), "a")
469 self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a")
470 self.assertEqual(posixpath.relpath("a/b"), "a/b")
471 self.assertEqual(posixpath.relpath("../a/b"), "../a/b")
472 self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a")
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000473 self.assertEqual(posixpath.relpath("a/b", "../c"),
474 "../"+curdir+"/a/b")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000475 self.assertEqual(posixpath.relpath("a", "b/c"), "../../a")
Christian Heimesfaf2f632008-01-06 16:59:19 +0000476 self.assertEqual(posixpath.relpath("a", "a"), ".")
Hirokazu Yamamotob08820a2010-10-18 12:13:18 +0000477 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat')
478 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/foo/bar"), 'bat')
479 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/"), 'foo/bar/bat')
480 self.assertEqual(posixpath.relpath("/", "/foo/bar/bat"), '../../..')
481 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x"), '../foo/bar/bat')
482 self.assertEqual(posixpath.relpath("/x", "/foo/bar/bat"), '../../../x')
483 self.assertEqual(posixpath.relpath("/", "/"), '.')
484 self.assertEqual(posixpath.relpath("/a", "/a"), '.')
485 self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.')
Guido van Rossumd8faa362007-04-27 19:54:29 +0000486 finally:
487 os.getcwd = real_getcwd
Brett Cannonb47243a2003-06-16 21:54:50 +0000488
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000489 def test_relpath_bytes(self):
490 (real_getcwdb, os.getcwdb) = (os.getcwdb, lambda: br"/home/user/bar")
491 try:
492 curdir = os.path.split(os.getcwdb())[-1]
493 self.assertRaises(ValueError, posixpath.relpath, b"")
494 self.assertEqual(posixpath.relpath(b"a"), b"a")
495 self.assertEqual(posixpath.relpath(posixpath.abspath(b"a")), b"a")
496 self.assertEqual(posixpath.relpath(b"a/b"), b"a/b")
497 self.assertEqual(posixpath.relpath(b"../a/b"), b"../a/b")
498 self.assertEqual(posixpath.relpath(b"a", b"../b"),
499 b"../"+curdir+b"/a")
500 self.assertEqual(posixpath.relpath(b"a/b", b"../c"),
501 b"../"+curdir+b"/a/b")
502 self.assertEqual(posixpath.relpath(b"a", b"b/c"), b"../../a")
503 self.assertEqual(posixpath.relpath(b"a", b"a"), b".")
Hirokazu Yamamotob08820a2010-10-18 12:13:18 +0000504 self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x/y/z"), b'../../../foo/bar/bat')
505 self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/foo/bar"), b'bat')
506 self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/"), b'foo/bar/bat')
507 self.assertEqual(posixpath.relpath(b"/", b"/foo/bar/bat"), b'../../..')
508 self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x"), b'../foo/bar/bat')
509 self.assertEqual(posixpath.relpath(b"/x", b"/foo/bar/bat"), b'../../../x')
510 self.assertEqual(posixpath.relpath(b"/", b"/"), b'.')
511 self.assertEqual(posixpath.relpath(b"/a", b"/a"), b'.')
512 self.assertEqual(posixpath.relpath(b"/a/b", b"/a/b"), b'.')
Guido van Rossumf0af3e32008-10-02 18:55:37 +0000513
514 self.assertRaises(TypeError, posixpath.relpath, b"bytes", "str")
515 self.assertRaises(TypeError, posixpath.relpath, "str", b"bytes")
516 finally:
517 os.getcwdb = real_getcwdb
518
Michael Foord07926f02011-03-16 17:19:16 -0400519 def test_sameopenfile(self):
520 fname = support.TESTFN + "1"
521 with open(fname, "wb") as a, open(fname, "wb") as b:
522 self.assertTrue(posixpath.sameopenfile(a.fileno(), b.fileno()))
523
Florent Xiclunac9c79782010-03-08 12:24:53 +0000524
525class PosixCommonTest(test_genericpath.CommonTest):
526 pathmodule = posixpath
527 attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat']
528
529
Brett Cannonb47243a2003-06-16 21:54:50 +0000530def test_main():
Florent Xiclunac9c79782010-03-08 12:24:53 +0000531 support.run_unittest(PosixPathTest, PosixCommonTest)
532
Brett Cannonb47243a2003-06-16 21:54:50 +0000533
534if __name__=="__main__":
535 test_main()