blob: fbb18fd22bd02bf35d02822b20730321784c17ef [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
10# An absolute path to a temporary filename for testing. We can't rely on TESTFN
11# being an absolute path, so we need this.
12
13ABSTFN = abspath(test_support.TESTFN)
Skip Montanaroe809b002000-07-12 00:20:08 +000014
Serhiy Storchakac8e75ba2013-02-18 13:32:06 +020015def skip_if_ABSTFN_contains_backslash(test):
16 """
17 On Windows, posixpath.abspath still returns paths with backslashes
18 instead of posix forward slashes. If this is the case, several tests
19 fail, so skip them.
20 """
21 found_backslash = '\\' in ABSTFN
22 msg = "ABSTFN is not a posix path - tests fail"
23 return [test, unittest.skip(msg)(test)][found_backslash]
24
Collin Winterdbead562007-03-10 02:23:40 +000025def safe_rmdir(dirname):
26 try:
27 os.rmdir(dirname)
28 except OSError:
29 pass
30
Brett Cannonb47243a2003-06-16 21:54:50 +000031class PosixPathTest(unittest.TestCase):
Skip Montanaroe809b002000-07-12 00:20:08 +000032
Collin Winterdbead562007-03-10 02:23:40 +000033 def setUp(self):
34 self.tearDown()
35
36 def tearDown(self):
37 for suffix in ["", "1", "2"]:
38 test_support.unlink(test_support.TESTFN + suffix)
39 safe_rmdir(test_support.TESTFN + suffix)
Tim Petersea5962f2007-03-12 18:07:52 +000040
Brett Cannonb47243a2003-06-16 21:54:50 +000041 def test_join(self):
42 self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), "/bar/baz")
43 self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz")
44 self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), "/foo/bar/baz/")
Skip Montanaroe809b002000-07-12 00:20:08 +000045
Brett Cannonb47243a2003-06-16 21:54:50 +000046 def test_split(self):
47 self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))
48 self.assertEqual(posixpath.split("/"), ("/", ""))
49 self.assertEqual(posixpath.split("foo"), ("", "foo"))
50 self.assertEqual(posixpath.split("////foo"), ("////", "foo"))
51 self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar"))
52
Martin v. Löwis05c075d2007-03-07 11:04:33 +000053 def splitextTest(self, path, filename, ext):
54 self.assertEqual(posixpath.splitext(path), (filename, ext))
55 self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext))
56 self.assertEqual(posixpath.splitext("abc/" + path), ("abc/" + filename, ext))
57 self.assertEqual(posixpath.splitext("abc.def/" + path), ("abc.def/" + filename, ext))
58 self.assertEqual(posixpath.splitext("/abc.def/" + path), ("/abc.def/" + filename, ext))
59 self.assertEqual(posixpath.splitext(path + "/"), (filename + ext + "/", ""))
Brett Cannonb47243a2003-06-16 21:54:50 +000060
Martin v. Löwis05c075d2007-03-07 11:04:33 +000061 def test_splitext(self):
62 self.splitextTest("foo.bar", "foo", ".bar")
63 self.splitextTest("foo.boo.bar", "foo.boo", ".bar")
64 self.splitextTest("foo.boo.biff.bar", "foo.boo.biff", ".bar")
65 self.splitextTest(".csh.rc", ".csh", ".rc")
66 self.splitextTest("nodots", "nodots", "")
67 self.splitextTest(".cshrc", ".cshrc", "")
68 self.splitextTest("...manydots", "...manydots", "")
69 self.splitextTest("...manydots.ext", "...manydots", ".ext")
70 self.splitextTest(".", ".", "")
71 self.splitextTest("..", "..", "")
72 self.splitextTest("........", "........", "")
73 self.splitextTest("", "", "")
Brett Cannonb47243a2003-06-16 21:54:50 +000074
75 def test_isabs(self):
76 self.assertIs(posixpath.isabs(""), False)
77 self.assertIs(posixpath.isabs("/"), True)
78 self.assertIs(posixpath.isabs("/foo"), True)
79 self.assertIs(posixpath.isabs("/foo/bar"), True)
80 self.assertIs(posixpath.isabs("foo/bar"), False)
81
Brett Cannonb47243a2003-06-16 21:54:50 +000082 def test_basename(self):
83 self.assertEqual(posixpath.basename("/foo/bar"), "bar")
84 self.assertEqual(posixpath.basename("/"), "")
85 self.assertEqual(posixpath.basename("foo"), "foo")
86 self.assertEqual(posixpath.basename("////foo"), "foo")
87 self.assertEqual(posixpath.basename("//foo//bar"), "bar")
88
Brett Cannonb47243a2003-06-16 21:54:50 +000089 def test_dirname(self):
90 self.assertEqual(posixpath.dirname("/foo/bar"), "/foo")
91 self.assertEqual(posixpath.dirname("/"), "/")
92 self.assertEqual(posixpath.dirname("foo"), "")
93 self.assertEqual(posixpath.dirname("////foo"), "////")
94 self.assertEqual(posixpath.dirname("//foo//bar"), "//foo")
95
Tim Petersea5962f2007-03-12 18:07:52 +000096 def test_islink(self):
Brett Cannonb47243a2003-06-16 21:54:50 +000097 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
98 f = open(test_support.TESTFN + "1", "wb")
99 try:
100 f.write("foo")
101 f.close()
102 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
103 if hasattr(os, "symlink"):
104 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
105 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
106 os.remove(test_support.TESTFN + "1")
107 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
108 self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False)
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000109 self.assertIs(posixpath.lexists(test_support.TESTFN + "2"), True)
Brett Cannonb47243a2003-06-16 21:54:50 +0000110 finally:
111 if not f.close():
112 f.close()
Brett Cannonb47243a2003-06-16 21:54:50 +0000113
Collin Winterdbead562007-03-10 02:23:40 +0000114 def test_samefile(self):
115 f = open(test_support.TESTFN + "1", "wb")
116 try:
117 f.write("foo")
118 f.close()
119 self.assertIs(
120 posixpath.samefile(
121 test_support.TESTFN + "1",
122 test_support.TESTFN + "1"
123 ),
124 True
125 )
Benjamin Peterson8a1a17b2012-11-30 16:12:15 -0500126
127 # If we don't have links, assume that os.stat doesn't return
128 # reasonable inode information and thus, that samefile() doesn't
129 # work.
Collin Winterdbead562007-03-10 02:23:40 +0000130 if hasattr(os, "symlink"):
131 os.symlink(
132 test_support.TESTFN + "1",
133 test_support.TESTFN + "2"
134 )
135 self.assertIs(
136 posixpath.samefile(
137 test_support.TESTFN + "1",
138 test_support.TESTFN + "2"
139 ),
140 True
141 )
142 os.remove(test_support.TESTFN + "2")
143 f = open(test_support.TESTFN + "2", "wb")
144 f.write("bar")
Brett Cannonb47243a2003-06-16 21:54:50 +0000145 f.close()
146 self.assertIs(
147 posixpath.samefile(
148 test_support.TESTFN + "1",
Brett Cannonb47243a2003-06-16 21:54:50 +0000149 test_support.TESTFN + "2"
Collin Winterdbead562007-03-10 02:23:40 +0000150 ),
151 False
152 )
153 finally:
154 if not f.close():
155 f.close()
Brett Cannonb47243a2003-06-16 21:54:50 +0000156
Brett Cannonb47243a2003-06-16 21:54:50 +0000157 def test_samestat(self):
158 f = open(test_support.TESTFN + "1", "wb")
159 try:
160 f.write("foo")
161 f.close()
162 self.assertIs(
163 posixpath.samestat(
164 os.stat(test_support.TESTFN + "1"),
165 os.stat(test_support.TESTFN + "1")
166 ),
167 True
168 )
Benjamin Peterson71966052012-11-30 16:13:14 -0500169 # If we don't have links, assume that os.stat() doesn't return
170 # reasonable inode information and thus, that samestat() doesn't
171 # work.
Brett Cannonb47243a2003-06-16 21:54:50 +0000172 if hasattr(os, "symlink"):
Benjamin Peterson8a1a17b2012-11-30 16:12:15 -0500173 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
174 self.assertIs(
175 posixpath.samestat(
176 os.stat(test_support.TESTFN + "1"),
177 os.stat(test_support.TESTFN + "2")
178 ),
179 True
180 )
181 os.remove(test_support.TESTFN + "2")
Brett Cannonb47243a2003-06-16 21:54:50 +0000182 f = open(test_support.TESTFN + "2", "wb")
183 f.write("bar")
184 f.close()
185 self.assertIs(
186 posixpath.samestat(
187 os.stat(test_support.TESTFN + "1"),
188 os.stat(test_support.TESTFN + "2")
189 ),
190 False
191 )
192 finally:
193 if not f.close():
194 f.close()
Brett Cannonb47243a2003-06-16 21:54:50 +0000195
Brett Cannonb47243a2003-06-16 21:54:50 +0000196 def test_ismount(self):
197 self.assertIs(posixpath.ismount("/"), True)
198
Brett Cannonb47243a2003-06-16 21:54:50 +0000199 def test_expanduser(self):
200 self.assertEqual(posixpath.expanduser("foo"), "foo")
Serhiy Storchaka7dc8e1e2016-05-03 22:15:29 +0300201 with test_support.EnvironmentVarGuard() as env:
202 for home in '/', '', '//', '///':
203 env['HOME'] = home
204 self.assertEqual(posixpath.expanduser("~"), "/")
205 self.assertEqual(posixpath.expanduser("~/"), "/")
206 self.assertEqual(posixpath.expanduser("~/foo"), "/foo")
Brett Cannonb47243a2003-06-16 21:54:50 +0000207 try:
208 import pwd
209 except ImportError:
210 pass
211 else:
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000212 self.assertIsInstance(posixpath.expanduser("~/"), basestring)
Neal Norwitz168e73d2003-07-01 03:33:31 +0000213 # if home directory == root directory, this test makes no sense
214 if posixpath.expanduser("~") != '/':
215 self.assertEqual(
216 posixpath.expanduser("~") + "/",
217 posixpath.expanduser("~/")
218 )
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000219 self.assertIsInstance(posixpath.expanduser("~root/"), basestring)
220 self.assertIsInstance(posixpath.expanduser("~foo/"), basestring)
Brett Cannonb47243a2003-06-16 21:54:50 +0000221
Walter Dörwald4b965f62009-04-26 20:51:44 +0000222 with test_support.EnvironmentVarGuard() as env:
Serhiy Storchaka7dc8e1e2016-05-03 22:15:29 +0300223 # expanduser should fall back to using the password database
224 del env['HOME']
225 home = pwd.getpwuid(os.getuid()).pw_dir
226 # $HOME can end with a trailing /, so strip it (see #17809)
227 home = home.rstrip("/") or '/'
228 self.assertEqual(posixpath.expanduser("~"), home)
Georg Brandl3f0ef202009-04-05 14:48:49 +0000229
Brett Cannonb47243a2003-06-16 21:54:50 +0000230 def test_normpath(self):
231 self.assertEqual(posixpath.normpath(""), ".")
232 self.assertEqual(posixpath.normpath("/"), "/")
233 self.assertEqual(posixpath.normpath("//"), "//")
234 self.assertEqual(posixpath.normpath("///"), "/")
235 self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar")
236 self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")
237 self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
238
Serhiy Storchakac8e75ba2013-02-18 13:32:06 +0200239 @skip_if_ABSTFN_contains_backslash
Serhiy Storchaka142d2bc2013-02-18 12:20:44 +0200240 def test_realpath_curdir(self):
241 self.assertEqual(realpath('.'), os.getcwd())
242 self.assertEqual(realpath('./.'), os.getcwd())
243 self.assertEqual(realpath('/'.join(['.'] * 100)), os.getcwd())
244
Serhiy Storchakac8e75ba2013-02-18 13:32:06 +0200245 @skip_if_ABSTFN_contains_backslash
Serhiy Storchaka142d2bc2013-02-18 12:20:44 +0200246 def test_realpath_pardir(self):
247 self.assertEqual(realpath('..'), dirname(os.getcwd()))
248 self.assertEqual(realpath('../..'), dirname(dirname(os.getcwd())))
249 self.assertEqual(realpath('/'.join(['..'] * 100)), '/')
250
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000251 if hasattr(os, "symlink"):
252 def test_realpath_basic(self):
253 # Basic operation.
254 try:
255 os.symlink(ABSTFN+"1", ABSTFN)
256 self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
257 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000258 test_support.unlink(ABSTFN)
Tim Petersa45cacf2004-08-20 03:47:14 +0000259
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000260 def test_realpath_symlink_loops(self):
261 # Bug #930024, return the path unchanged if we get into an infinite
262 # symlink loop.
263 try:
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000264 os.symlink(ABSTFN, ABSTFN)
265 self.assertEqual(realpath(ABSTFN), ABSTFN)
266
267 os.symlink(ABSTFN+"1", ABSTFN+"2")
268 os.symlink(ABSTFN+"2", ABSTFN+"1")
269 self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1")
270 self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2")
271
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200272 self.assertEqual(realpath(ABSTFN+"1/x"), ABSTFN+"1/x")
273 self.assertEqual(realpath(ABSTFN+"1/.."), dirname(ABSTFN))
274 self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x")
275 os.symlink(ABSTFN+"x", ABSTFN+"y")
276 self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"),
277 ABSTFN + "y")
278 self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"),
279 ABSTFN + "1")
280
281 os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a")
282 self.assertEqual(realpath(ABSTFN+"a"), ABSTFN+"a/b")
283
284 os.symlink("../" + basename(dirname(ABSTFN)) + "/" +
285 basename(ABSTFN) + "c", ABSTFN+"c")
286 self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c")
287
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000288 # Test using relative path as well.
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300289 with support.change_cwd(dirname(ABSTFN)):
290 self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000291 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000292 test_support.unlink(ABSTFN)
293 test_support.unlink(ABSTFN+"1")
294 test_support.unlink(ABSTFN+"2")
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200295 test_support.unlink(ABSTFN+"y")
296 test_support.unlink(ABSTFN+"c")
Ezio Melottic86e8662013-03-01 20:56:13 +0200297 test_support.unlink(ABSTFN+"a")
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200298
299 def test_realpath_repeated_indirect_symlinks(self):
300 # Issue #6975.
301 try:
302 os.mkdir(ABSTFN)
303 os.symlink('../' + basename(ABSTFN), ABSTFN + '/self')
304 os.symlink('self/self/self', ABSTFN + '/link')
305 self.assertEqual(realpath(ABSTFN + '/link'), ABSTFN)
306 finally:
307 test_support.unlink(ABSTFN + '/self')
308 test_support.unlink(ABSTFN + '/link')
309 safe_rmdir(ABSTFN)
310
311 def test_realpath_deep_recursion(self):
312 depth = 10
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200313 try:
314 os.mkdir(ABSTFN)
315 for i in range(depth):
316 os.symlink('/'.join(['%d' % i] * 10), ABSTFN + '/%d' % (i + 1))
317 os.symlink('.', ABSTFN + '/0')
318 self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN)
319
320 # Test using relative path as well.
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300321 with support.change_cwd(ABSTFN):
322 self.assertEqual(realpath('%d' % depth), ABSTFN)
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200323 finally:
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200324 for i in range(depth + 1):
325 test_support.unlink(ABSTFN + '/%d' % i)
326 safe_rmdir(ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000327
Tim Petersa45cacf2004-08-20 03:47:14 +0000328 def test_realpath_resolve_parents(self):
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000329 # We also need to resolve any symlinks in the parents of a relative
330 # path passed to realpath. E.g.: current working directory is
331 # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
332 # realpath("a"). This should return /usr/share/doc/a/.
333 try:
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000334 os.mkdir(ABSTFN)
335 os.mkdir(ABSTFN + "/y")
336 os.symlink(ABSTFN + "/y", ABSTFN + "/k")
337
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300338 with support.change_cwd(ABSTFN + "/k"):
339 self.assertEqual(realpath("a"), ABSTFN + "/y/a")
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000340 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000341 test_support.unlink(ABSTFN + "/k")
342 safe_rmdir(ABSTFN + "/y")
343 safe_rmdir(ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000344
345 def test_realpath_resolve_before_normalizing(self):
346 # Bug #990669: Symbolic links should be resolved before we
347 # normalize the path. E.g.: if we have directories 'a', 'k' and 'y'
348 # in the following hierarchy:
349 # a/k/y
350 #
351 # and a symbolic link 'link-y' pointing to 'y' in directory 'a',
352 # then realpath("link-y/..") should return 'k', not 'a'.
353 try:
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000354 os.mkdir(ABSTFN)
355 os.mkdir(ABSTFN + "/k")
356 os.mkdir(ABSTFN + "/k/y")
357 os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y")
358
359 # Absolute path.
360 self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k")
361 # Relative path.
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300362 with support.change_cwd(dirname(ABSTFN)):
363 self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."),
364 ABSTFN + "/k")
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000365 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000366 test_support.unlink(ABSTFN + "/link-y")
367 safe_rmdir(ABSTFN + "/k/y")
368 safe_rmdir(ABSTFN + "/k")
369 safe_rmdir(ABSTFN)
Tim Peters5d36a552005-06-03 22:40:27 +0000370
Georg Brandl268e61c2005-06-03 14:28:50 +0000371 def test_realpath_resolve_first(self):
372 # Bug #1213894: The first component of the path, if not absolute,
373 # must be resolved too.
374
375 try:
Georg Brandl268e61c2005-06-03 14:28:50 +0000376 os.mkdir(ABSTFN)
377 os.mkdir(ABSTFN + "/k")
378 os.symlink(ABSTFN, ABSTFN + "link")
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300379 with support.change_cwd(dirname(ABSTFN)):
380 base = basename(ABSTFN)
381 self.assertEqual(realpath(base + "link"), ABSTFN)
382 self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
Georg Brandl268e61c2005-06-03 14:28:50 +0000383 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000384 test_support.unlink(ABSTFN + "link")
385 safe_rmdir(ABSTFN + "/k")
386 safe_rmdir(ABSTFN)
Brett Cannonb47243a2003-06-16 21:54:50 +0000387
Collin Winter6f187742007-03-16 22:16:08 +0000388 def test_relpath(self):
Collin Winter75c7eb42007-03-23 22:24:39 +0000389 (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
390 try:
391 curdir = os.path.split(os.getcwd())[-1]
392 self.assertRaises(ValueError, posixpath.relpath, "")
393 self.assertEqual(posixpath.relpath("a"), "a")
394 self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a")
395 self.assertEqual(posixpath.relpath("a/b"), "a/b")
396 self.assertEqual(posixpath.relpath("../a/b"), "../a/b")
397 self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a")
398 self.assertEqual(posixpath.relpath("a/b", "../c"), "../"+curdir+"/a/b")
399 self.assertEqual(posixpath.relpath("a", "b/c"), "../../a")
Georg Brandl183a0842008-01-06 14:27:15 +0000400 self.assertEqual(posixpath.relpath("a", "a"), ".")
Hirokazu Yamamoto50f7d7e2010-10-18 13:55:29 +0000401 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat')
402 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/foo/bar"), 'bat')
403 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/"), 'foo/bar/bat')
404 self.assertEqual(posixpath.relpath("/", "/foo/bar/bat"), '../../..')
405 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x"), '../foo/bar/bat')
406 self.assertEqual(posixpath.relpath("/x", "/foo/bar/bat"), '../../../x')
407 self.assertEqual(posixpath.relpath("/", "/"), '.')
408 self.assertEqual(posixpath.relpath("/a", "/a"), '.')
409 self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.')
Collin Winter75c7eb42007-03-23 22:24:39 +0000410 finally:
411 os.getcwd = real_getcwd
Collin Winter6f187742007-03-16 22:16:08 +0000412
Serhiy Storchaka2bd8b222015-02-13 12:02:05 +0200413 @test_support.requires_unicode
414 def test_expandvars_nonascii_word(self):
415 encoding = sys.getfilesystemencoding()
416 # Non-ASCII word characters
417 letters = test_support.u(r'\xe6\u0130\u0141\u03c6\u041a\u05d0\u062a\u0e01')
418 uwnonascii = letters.encode(encoding, 'ignore').decode(encoding)[:3]
419 swnonascii = uwnonascii.encode(encoding)
420 if not swnonascii:
Serhiy Storchaka3be0d0e2015-02-13 12:47:08 +0200421 self.skipTest('Needs non-ASCII word characters')
Serhiy Storchaka2bd8b222015-02-13 12:02:05 +0200422 with test_support.EnvironmentVarGuard() as env:
423 env.clear()
424 env[swnonascii] = 'baz' + swnonascii
425 self.assertEqual(posixpath.expandvars(u'$%s bar' % uwnonascii),
426 u'baz%s bar' % uwnonascii)
427
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000428
429class PosixCommonTest(test_genericpath.CommonTest):
Florent Xicluna985478d2010-03-06 18:52:52 +0000430 pathmodule = posixpath
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000431 attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat']
432
433
Brett Cannonb47243a2003-06-16 21:54:50 +0000434def test_main():
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000435 test_support.run_unittest(PosixPathTest, PosixCommonTest)
436
Brett Cannonb47243a2003-06-16 21:54:50 +0000437
438if __name__=="__main__":
439 test_main()