blob: deaa5772835b7b53a2a68bd8a324a3cd2ef425cd [file] [log] [blame]
Brett Cannonb47243a2003-06-16 21:54:50 +00001import unittest
Florent Xiclunadc1531c2010-03-06 18:07:18 +00002from test import test_support, test_genericpath
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +03003from test import test_support as support
Ezio Melotti9e9af212010-02-20 22:34:21 +00004
Serhiy Storchaka2bd8b222015-02-13 12:02:05 +02005import posixpath
6import os
7import sys
Georg Brandlb86d3fa2010-02-07 12:55:12 +00008from posixpath import realpath, abspath, dirname, basename
Johannes Gijsbers4ec40642004-08-14 15:01:53 +00009
R David Murray85783162016-08-23 12:30:28 -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
18ABSTFN = abspath(test_support.TESTFN)
Skip Montanaroe809b002000-07-12 00:20:08 +000019
Serhiy Storchakac8e75ba2013-02-18 13:32:06 +020020def 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
Collin Winterdbead562007-03-10 02:23:40 +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
Collin Winterdbead562007-03-10 02:23:40 +000038 def setUp(self):
39 self.tearDown()
40
41 def tearDown(self):
42 for suffix in ["", "1", "2"]:
43 test_support.unlink(test_support.TESTFN + suffix)
44 safe_rmdir(test_support.TESTFN + suffix)
Tim Petersea5962f2007-03-12 18:07:52 +000045
Brett Cannonb47243a2003-06-16 21:54:50 +000046 def test_join(self):
47 self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), "/bar/baz")
48 self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz")
49 self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), "/foo/bar/baz/")
Skip Montanaroe809b002000-07-12 00:20:08 +000050
Brett Cannonb47243a2003-06-16 21:54:50 +000051 def test_split(self):
52 self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))
53 self.assertEqual(posixpath.split("/"), ("/", ""))
54 self.assertEqual(posixpath.split("foo"), ("", "foo"))
55 self.assertEqual(posixpath.split("////foo"), ("////", "foo"))
56 self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar"))
57
Martin v. Löwis05c075d2007-03-07 11:04:33 +000058 def splitextTest(self, path, filename, ext):
59 self.assertEqual(posixpath.splitext(path), (filename, ext))
60 self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext))
61 self.assertEqual(posixpath.splitext("abc/" + path), ("abc/" + filename, ext))
62 self.assertEqual(posixpath.splitext("abc.def/" + path), ("abc.def/" + filename, ext))
63 self.assertEqual(posixpath.splitext("/abc.def/" + path), ("/abc.def/" + filename, ext))
64 self.assertEqual(posixpath.splitext(path + "/"), (filename + ext + "/", ""))
Brett Cannonb47243a2003-06-16 21:54:50 +000065
Martin v. Löwis05c075d2007-03-07 11:04:33 +000066 def test_splitext(self):
67 self.splitextTest("foo.bar", "foo", ".bar")
68 self.splitextTest("foo.boo.bar", "foo.boo", ".bar")
69 self.splitextTest("foo.boo.biff.bar", "foo.boo.biff", ".bar")
70 self.splitextTest(".csh.rc", ".csh", ".rc")
71 self.splitextTest("nodots", "nodots", "")
72 self.splitextTest(".cshrc", ".cshrc", "")
73 self.splitextTest("...manydots", "...manydots", "")
74 self.splitextTest("...manydots.ext", "...manydots", ".ext")
75 self.splitextTest(".", ".", "")
76 self.splitextTest("..", "..", "")
77 self.splitextTest("........", "........", "")
78 self.splitextTest("", "", "")
Brett Cannonb47243a2003-06-16 21:54:50 +000079
80 def test_isabs(self):
81 self.assertIs(posixpath.isabs(""), False)
82 self.assertIs(posixpath.isabs("/"), True)
83 self.assertIs(posixpath.isabs("/foo"), True)
84 self.assertIs(posixpath.isabs("/foo/bar"), True)
85 self.assertIs(posixpath.isabs("foo/bar"), False)
86
Brett Cannonb47243a2003-06-16 21:54:50 +000087 def test_basename(self):
88 self.assertEqual(posixpath.basename("/foo/bar"), "bar")
89 self.assertEqual(posixpath.basename("/"), "")
90 self.assertEqual(posixpath.basename("foo"), "foo")
91 self.assertEqual(posixpath.basename("////foo"), "foo")
92 self.assertEqual(posixpath.basename("//foo//bar"), "bar")
93
Brett Cannonb47243a2003-06-16 21:54:50 +000094 def test_dirname(self):
95 self.assertEqual(posixpath.dirname("/foo/bar"), "/foo")
96 self.assertEqual(posixpath.dirname("/"), "/")
97 self.assertEqual(posixpath.dirname("foo"), "")
98 self.assertEqual(posixpath.dirname("////foo"), "////")
99 self.assertEqual(posixpath.dirname("//foo//bar"), "//foo")
100
Tim Petersea5962f2007-03-12 18:07:52 +0000101 def test_islink(self):
Brett Cannonb47243a2003-06-16 21:54:50 +0000102 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
103 f = open(test_support.TESTFN + "1", "wb")
104 try:
105 f.write("foo")
106 f.close()
107 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
R David Murray68854fd2016-08-24 08:59:47 -0400108 if hasattr(os, 'symlink'):
Brett Cannonb47243a2003-06-16 21:54:50 +0000109 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
110 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
111 os.remove(test_support.TESTFN + "1")
112 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
113 self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False)
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000114 self.assertIs(posixpath.lexists(test_support.TESTFN + "2"), True)
Brett Cannonb47243a2003-06-16 21:54:50 +0000115 finally:
116 if not f.close():
117 f.close()
Brett Cannonb47243a2003-06-16 21:54:50 +0000118
Collin Winterdbead562007-03-10 02:23:40 +0000119 def test_samefile(self):
120 f = open(test_support.TESTFN + "1", "wb")
121 try:
122 f.write("foo")
123 f.close()
124 self.assertIs(
125 posixpath.samefile(
126 test_support.TESTFN + "1",
127 test_support.TESTFN + "1"
128 ),
129 True
130 )
Benjamin Peterson8a1a17b2012-11-30 16:12:15 -0500131
132 # If we don't have links, assume that os.stat doesn't return
133 # reasonable inode information and thus, that samefile() doesn't
134 # work.
Collin Winterdbead562007-03-10 02:23:40 +0000135 if hasattr(os, "symlink"):
136 os.symlink(
137 test_support.TESTFN + "1",
138 test_support.TESTFN + "2"
139 )
140 self.assertIs(
141 posixpath.samefile(
142 test_support.TESTFN + "1",
143 test_support.TESTFN + "2"
144 ),
145 True
146 )
147 os.remove(test_support.TESTFN + "2")
148 f = open(test_support.TESTFN + "2", "wb")
149 f.write("bar")
Brett Cannonb47243a2003-06-16 21:54:50 +0000150 f.close()
151 self.assertIs(
152 posixpath.samefile(
153 test_support.TESTFN + "1",
Brett Cannonb47243a2003-06-16 21:54:50 +0000154 test_support.TESTFN + "2"
Collin Winterdbead562007-03-10 02:23:40 +0000155 ),
156 False
157 )
158 finally:
159 if not f.close():
160 f.close()
Brett Cannonb47243a2003-06-16 21:54:50 +0000161
Brett Cannonb47243a2003-06-16 21:54:50 +0000162 def test_samestat(self):
163 f = open(test_support.TESTFN + "1", "wb")
164 try:
165 f.write("foo")
166 f.close()
167 self.assertIs(
168 posixpath.samestat(
169 os.stat(test_support.TESTFN + "1"),
170 os.stat(test_support.TESTFN + "1")
171 ),
172 True
173 )
Benjamin Peterson71966052012-11-30 16:13:14 -0500174 # If we don't have links, assume that os.stat() doesn't return
175 # reasonable inode information and thus, that samestat() doesn't
176 # work.
Brett Cannonb47243a2003-06-16 21:54:50 +0000177 if hasattr(os, "symlink"):
Benjamin Peterson8a1a17b2012-11-30 16:12:15 -0500178 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
179 self.assertIs(
180 posixpath.samestat(
181 os.stat(test_support.TESTFN + "1"),
182 os.stat(test_support.TESTFN + "2")
183 ),
184 True
185 )
186 os.remove(test_support.TESTFN + "2")
Brett Cannonb47243a2003-06-16 21:54:50 +0000187 f = open(test_support.TESTFN + "2", "wb")
188 f.write("bar")
189 f.close()
190 self.assertIs(
191 posixpath.samestat(
192 os.stat(test_support.TESTFN + "1"),
193 os.stat(test_support.TESTFN + "2")
194 ),
195 False
196 )
197 finally:
198 if not f.close():
199 f.close()
Brett Cannonb47243a2003-06-16 21:54:50 +0000200
Brett Cannonb47243a2003-06-16 21:54:50 +0000201 def test_ismount(self):
202 self.assertIs(posixpath.ismount("/"), True)
203
R David Murray85783162016-08-23 12:30:28 -0400204 def test_ismount_non_existent(self):
205 # Non-existent mountpoint.
206 self.assertIs(posixpath.ismount(ABSTFN), False)
207 try:
208 os.mkdir(ABSTFN)
209 self.assertIs(posixpath.ismount(ABSTFN), False)
210 finally:
211 safe_rmdir(ABSTFN)
212
R David Murray68854fd2016-08-24 08:59:47 -0400213 @unittest.skipUnless(hasattr(os, 'symlink'),
214 'Requires functional symlink implementation')
R David Murray85783162016-08-23 12:30:28 -0400215 def test_ismount_symlinks(self):
216 # Symlinks are never mountpoints.
217 try:
218 os.symlink("/", ABSTFN)
219 self.assertIs(posixpath.ismount(ABSTFN), False)
220 finally:
221 os.unlink(ABSTFN)
222
223 @unittest.skipIf(posix is None, "Test requires posix module")
224 def test_ismount_different_device(self):
225 # Simulate the path being on a different device from its parent by
226 # mocking out st_dev.
227 save_lstat = os.lstat
228 def fake_lstat(path):
229 st_ino = 0
230 st_dev = 0
231 if path == ABSTFN:
232 st_dev = 1
233 st_ino = 1
234 return posix.stat_result((0, st_ino, st_dev, 0, 0, 0, 0, 0, 0, 0))
235 try:
236 os.lstat = fake_lstat
237 self.assertIs(posixpath.ismount(ABSTFN), True)
238 finally:
239 os.lstat = save_lstat
240
241 @unittest.skipIf(posix is None, "Test requires posix module")
242 def test_ismount_directory_not_readable(self):
243 # issue #2466: Simulate ismount run on a directory that is not
244 # readable, which used to return False.
245 save_lstat = os.lstat
246 def fake_lstat(path):
247 st_ino = 0
248 st_dev = 0
249 if path.startswith(ABSTFN) and path != ABSTFN:
250 # ismount tries to read something inside the ABSTFN directory;
251 # simulate this being forbidden (no read permission).
252 raise OSError("Fake [Errno 13] Permission denied")
253 if path == ABSTFN:
254 st_dev = 1
255 st_ino = 1
256 return posix.stat_result((0, st_ino, st_dev, 0, 0, 0, 0, 0, 0, 0))
257 try:
258 os.lstat = fake_lstat
259 self.assertIs(posixpath.ismount(ABSTFN), True)
260 finally:
261 os.lstat = save_lstat
262
Brett Cannonb47243a2003-06-16 21:54:50 +0000263 def test_expanduser(self):
264 self.assertEqual(posixpath.expanduser("foo"), "foo")
Serhiy Storchaka7dc8e1e2016-05-03 22:15:29 +0300265 with test_support.EnvironmentVarGuard() as env:
266 for home in '/', '', '//', '///':
267 env['HOME'] = home
268 self.assertEqual(posixpath.expanduser("~"), "/")
269 self.assertEqual(posixpath.expanduser("~/"), "/")
270 self.assertEqual(posixpath.expanduser("~/foo"), "/foo")
Brett Cannonb47243a2003-06-16 21:54:50 +0000271 try:
272 import pwd
273 except ImportError:
274 pass
275 else:
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000276 self.assertIsInstance(posixpath.expanduser("~/"), basestring)
Neal Norwitz168e73d2003-07-01 03:33:31 +0000277 # if home directory == root directory, this test makes no sense
278 if posixpath.expanduser("~") != '/':
279 self.assertEqual(
280 posixpath.expanduser("~") + "/",
281 posixpath.expanduser("~/")
282 )
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000283 self.assertIsInstance(posixpath.expanduser("~root/"), basestring)
284 self.assertIsInstance(posixpath.expanduser("~foo/"), basestring)
Brett Cannonb47243a2003-06-16 21:54:50 +0000285
Walter Dörwald4b965f62009-04-26 20:51:44 +0000286 with test_support.EnvironmentVarGuard() as env:
Serhiy Storchaka7dc8e1e2016-05-03 22:15:29 +0300287 # expanduser should fall back to using the password database
288 del env['HOME']
289 home = pwd.getpwuid(os.getuid()).pw_dir
290 # $HOME can end with a trailing /, so strip it (see #17809)
291 home = home.rstrip("/") or '/'
292 self.assertEqual(posixpath.expanduser("~"), home)
Georg Brandl3f0ef202009-04-05 14:48:49 +0000293
Brett Cannonb47243a2003-06-16 21:54:50 +0000294 def test_normpath(self):
295 self.assertEqual(posixpath.normpath(""), ".")
296 self.assertEqual(posixpath.normpath("/"), "/")
297 self.assertEqual(posixpath.normpath("//"), "//")
298 self.assertEqual(posixpath.normpath("///"), "/")
299 self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar")
300 self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")
301 self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
302
Serhiy Storchakac8e75ba2013-02-18 13:32:06 +0200303 @skip_if_ABSTFN_contains_backslash
Serhiy Storchaka142d2bc2013-02-18 12:20:44 +0200304 def test_realpath_curdir(self):
305 self.assertEqual(realpath('.'), os.getcwd())
306 self.assertEqual(realpath('./.'), os.getcwd())
307 self.assertEqual(realpath('/'.join(['.'] * 100)), os.getcwd())
308
Serhiy Storchakac8e75ba2013-02-18 13:32:06 +0200309 @skip_if_ABSTFN_contains_backslash
Serhiy Storchaka142d2bc2013-02-18 12:20:44 +0200310 def test_realpath_pardir(self):
311 self.assertEqual(realpath('..'), dirname(os.getcwd()))
312 self.assertEqual(realpath('../..'), dirname(dirname(os.getcwd())))
313 self.assertEqual(realpath('/'.join(['..'] * 100)), '/')
314
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000315 if hasattr(os, "symlink"):
316 def test_realpath_basic(self):
317 # Basic operation.
318 try:
319 os.symlink(ABSTFN+"1", ABSTFN)
320 self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
321 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000322 test_support.unlink(ABSTFN)
Tim Petersa45cacf2004-08-20 03:47:14 +0000323
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000324 def test_realpath_symlink_loops(self):
325 # Bug #930024, return the path unchanged if we get into an infinite
326 # symlink loop.
327 try:
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000328 os.symlink(ABSTFN, ABSTFN)
329 self.assertEqual(realpath(ABSTFN), ABSTFN)
330
331 os.symlink(ABSTFN+"1", ABSTFN+"2")
332 os.symlink(ABSTFN+"2", ABSTFN+"1")
333 self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1")
334 self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2")
335
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200336 self.assertEqual(realpath(ABSTFN+"1/x"), ABSTFN+"1/x")
337 self.assertEqual(realpath(ABSTFN+"1/.."), dirname(ABSTFN))
338 self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x")
339 os.symlink(ABSTFN+"x", ABSTFN+"y")
340 self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"),
341 ABSTFN + "y")
342 self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"),
343 ABSTFN + "1")
344
345 os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a")
346 self.assertEqual(realpath(ABSTFN+"a"), ABSTFN+"a/b")
347
348 os.symlink("../" + basename(dirname(ABSTFN)) + "/" +
349 basename(ABSTFN) + "c", ABSTFN+"c")
350 self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c")
351
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000352 # Test using relative path as well.
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300353 with support.change_cwd(dirname(ABSTFN)):
354 self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000355 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000356 test_support.unlink(ABSTFN)
357 test_support.unlink(ABSTFN+"1")
358 test_support.unlink(ABSTFN+"2")
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200359 test_support.unlink(ABSTFN+"y")
360 test_support.unlink(ABSTFN+"c")
Ezio Melottic86e8662013-03-01 20:56:13 +0200361 test_support.unlink(ABSTFN+"a")
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200362
363 def test_realpath_repeated_indirect_symlinks(self):
364 # Issue #6975.
365 try:
366 os.mkdir(ABSTFN)
367 os.symlink('../' + basename(ABSTFN), ABSTFN + '/self')
368 os.symlink('self/self/self', ABSTFN + '/link')
369 self.assertEqual(realpath(ABSTFN + '/link'), ABSTFN)
370 finally:
371 test_support.unlink(ABSTFN + '/self')
372 test_support.unlink(ABSTFN + '/link')
373 safe_rmdir(ABSTFN)
374
375 def test_realpath_deep_recursion(self):
376 depth = 10
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200377 try:
378 os.mkdir(ABSTFN)
379 for i in range(depth):
380 os.symlink('/'.join(['%d' % i] * 10), ABSTFN + '/%d' % (i + 1))
381 os.symlink('.', ABSTFN + '/0')
382 self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN)
383
384 # Test using relative path as well.
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300385 with support.change_cwd(ABSTFN):
386 self.assertEqual(realpath('%d' % depth), ABSTFN)
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200387 finally:
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200388 for i in range(depth + 1):
389 test_support.unlink(ABSTFN + '/%d' % i)
390 safe_rmdir(ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000391
Tim Petersa45cacf2004-08-20 03:47:14 +0000392 def test_realpath_resolve_parents(self):
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000393 # We also need to resolve any symlinks in the parents of a relative
394 # path passed to realpath. E.g.: current working directory is
395 # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
396 # realpath("a"). This should return /usr/share/doc/a/.
397 try:
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000398 os.mkdir(ABSTFN)
399 os.mkdir(ABSTFN + "/y")
400 os.symlink(ABSTFN + "/y", ABSTFN + "/k")
401
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300402 with support.change_cwd(ABSTFN + "/k"):
403 self.assertEqual(realpath("a"), ABSTFN + "/y/a")
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000404 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000405 test_support.unlink(ABSTFN + "/k")
406 safe_rmdir(ABSTFN + "/y")
407 safe_rmdir(ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000408
409 def test_realpath_resolve_before_normalizing(self):
410 # Bug #990669: Symbolic links should be resolved before we
411 # normalize the path. E.g.: if we have directories 'a', 'k' and 'y'
412 # in the following hierarchy:
413 # a/k/y
414 #
415 # and a symbolic link 'link-y' pointing to 'y' in directory 'a',
416 # then realpath("link-y/..") should return 'k', not 'a'.
417 try:
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000418 os.mkdir(ABSTFN)
419 os.mkdir(ABSTFN + "/k")
420 os.mkdir(ABSTFN + "/k/y")
421 os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y")
422
423 # Absolute path.
424 self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k")
425 # Relative path.
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300426 with support.change_cwd(dirname(ABSTFN)):
427 self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."),
428 ABSTFN + "/k")
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000429 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000430 test_support.unlink(ABSTFN + "/link-y")
431 safe_rmdir(ABSTFN + "/k/y")
432 safe_rmdir(ABSTFN + "/k")
433 safe_rmdir(ABSTFN)
Tim Peters5d36a552005-06-03 22:40:27 +0000434
Georg Brandl268e61c2005-06-03 14:28:50 +0000435 def test_realpath_resolve_first(self):
436 # Bug #1213894: The first component of the path, if not absolute,
437 # must be resolved too.
438
439 try:
Georg Brandl268e61c2005-06-03 14:28:50 +0000440 os.mkdir(ABSTFN)
441 os.mkdir(ABSTFN + "/k")
442 os.symlink(ABSTFN, ABSTFN + "link")
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300443 with support.change_cwd(dirname(ABSTFN)):
444 base = basename(ABSTFN)
445 self.assertEqual(realpath(base + "link"), ABSTFN)
446 self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
Georg Brandl268e61c2005-06-03 14:28:50 +0000447 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000448 test_support.unlink(ABSTFN + "link")
449 safe_rmdir(ABSTFN + "/k")
450 safe_rmdir(ABSTFN)
Brett Cannonb47243a2003-06-16 21:54:50 +0000451
Collin Winter6f187742007-03-16 22:16:08 +0000452 def test_relpath(self):
Collin Winter75c7eb42007-03-23 22:24:39 +0000453 (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
454 try:
455 curdir = os.path.split(os.getcwd())[-1]
456 self.assertRaises(ValueError, posixpath.relpath, "")
457 self.assertEqual(posixpath.relpath("a"), "a")
458 self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a")
459 self.assertEqual(posixpath.relpath("a/b"), "a/b")
460 self.assertEqual(posixpath.relpath("../a/b"), "../a/b")
461 self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a")
462 self.assertEqual(posixpath.relpath("a/b", "../c"), "../"+curdir+"/a/b")
463 self.assertEqual(posixpath.relpath("a", "b/c"), "../../a")
Georg Brandl183a0842008-01-06 14:27:15 +0000464 self.assertEqual(posixpath.relpath("a", "a"), ".")
Hirokazu Yamamoto50f7d7e2010-10-18 13:55:29 +0000465 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat')
466 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/foo/bar"), 'bat')
467 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/"), 'foo/bar/bat')
468 self.assertEqual(posixpath.relpath("/", "/foo/bar/bat"), '../../..')
469 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x"), '../foo/bar/bat')
470 self.assertEqual(posixpath.relpath("/x", "/foo/bar/bat"), '../../../x')
471 self.assertEqual(posixpath.relpath("/", "/"), '.')
472 self.assertEqual(posixpath.relpath("/a", "/a"), '.')
473 self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.')
Collin Winter75c7eb42007-03-23 22:24:39 +0000474 finally:
475 os.getcwd = real_getcwd
Collin Winter6f187742007-03-16 22:16:08 +0000476
Serhiy Storchaka2bd8b222015-02-13 12:02:05 +0200477 @test_support.requires_unicode
478 def test_expandvars_nonascii_word(self):
479 encoding = sys.getfilesystemencoding()
480 # Non-ASCII word characters
481 letters = test_support.u(r'\xe6\u0130\u0141\u03c6\u041a\u05d0\u062a\u0e01')
482 uwnonascii = letters.encode(encoding, 'ignore').decode(encoding)[:3]
483 swnonascii = uwnonascii.encode(encoding)
484 if not swnonascii:
Serhiy Storchaka3be0d0e2015-02-13 12:47:08 +0200485 self.skipTest('Needs non-ASCII word characters')
Serhiy Storchaka2bd8b222015-02-13 12:02:05 +0200486 with test_support.EnvironmentVarGuard() as env:
487 env.clear()
488 env[swnonascii] = 'baz' + swnonascii
489 self.assertEqual(posixpath.expandvars(u'$%s bar' % uwnonascii),
490 u'baz%s bar' % uwnonascii)
491
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000492
493class PosixCommonTest(test_genericpath.CommonTest):
Florent Xicluna985478d2010-03-06 18:52:52 +0000494 pathmodule = posixpath
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000495 attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat']
496
497
Brett Cannonb47243a2003-06-16 21:54:50 +0000498def test_main():
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000499 test_support.run_unittest(PosixPathTest, PosixCommonTest)
500
Brett Cannonb47243a2003-06-16 21:54:50 +0000501
502if __name__=="__main__":
503 test_main()