blob: 686b6b9ff60378eeac6ba1d4e58ab58ffe0f0a2a [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")
Brett Cannonb47243a2003-06-16 21:54:50 +0000201 try:
202 import pwd
203 except ImportError:
204 pass
205 else:
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000206 self.assertIsInstance(posixpath.expanduser("~/"), basestring)
Neal Norwitz168e73d2003-07-01 03:33:31 +0000207 # if home directory == root directory, this test makes no sense
208 if posixpath.expanduser("~") != '/':
209 self.assertEqual(
210 posixpath.expanduser("~") + "/",
211 posixpath.expanduser("~/")
212 )
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000213 self.assertIsInstance(posixpath.expanduser("~root/"), basestring)
214 self.assertIsInstance(posixpath.expanduser("~foo/"), basestring)
Brett Cannonb47243a2003-06-16 21:54:50 +0000215
Walter Dörwald4b965f62009-04-26 20:51:44 +0000216 with test_support.EnvironmentVarGuard() as env:
Walter Dörwald6733bed2009-05-01 17:35:37 +0000217 env['HOME'] = '/'
Walter Dörwald4b965f62009-04-26 20:51:44 +0000218 self.assertEqual(posixpath.expanduser("~"), "/")
Jesus Ceaf2011e32012-05-10 05:01:11 +0200219 self.assertEqual(posixpath.expanduser("~/foo"), "/foo")
Georg Brandl3f0ef202009-04-05 14:48:49 +0000220
Brett Cannonb47243a2003-06-16 21:54:50 +0000221 def test_normpath(self):
222 self.assertEqual(posixpath.normpath(""), ".")
223 self.assertEqual(posixpath.normpath("/"), "/")
224 self.assertEqual(posixpath.normpath("//"), "//")
225 self.assertEqual(posixpath.normpath("///"), "/")
226 self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar")
227 self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")
228 self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
229
Serhiy Storchakac8e75ba2013-02-18 13:32:06 +0200230 @skip_if_ABSTFN_contains_backslash
Serhiy Storchaka142d2bc2013-02-18 12:20:44 +0200231 def test_realpath_curdir(self):
232 self.assertEqual(realpath('.'), os.getcwd())
233 self.assertEqual(realpath('./.'), os.getcwd())
234 self.assertEqual(realpath('/'.join(['.'] * 100)), os.getcwd())
235
Serhiy Storchakac8e75ba2013-02-18 13:32:06 +0200236 @skip_if_ABSTFN_contains_backslash
Serhiy Storchaka142d2bc2013-02-18 12:20:44 +0200237 def test_realpath_pardir(self):
238 self.assertEqual(realpath('..'), dirname(os.getcwd()))
239 self.assertEqual(realpath('../..'), dirname(dirname(os.getcwd())))
240 self.assertEqual(realpath('/'.join(['..'] * 100)), '/')
241
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000242 if hasattr(os, "symlink"):
243 def test_realpath_basic(self):
244 # Basic operation.
245 try:
246 os.symlink(ABSTFN+"1", ABSTFN)
247 self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
248 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000249 test_support.unlink(ABSTFN)
Tim Petersa45cacf2004-08-20 03:47:14 +0000250
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000251 def test_realpath_symlink_loops(self):
252 # Bug #930024, return the path unchanged if we get into an infinite
253 # symlink loop.
254 try:
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000255 os.symlink(ABSTFN, ABSTFN)
256 self.assertEqual(realpath(ABSTFN), ABSTFN)
257
258 os.symlink(ABSTFN+"1", ABSTFN+"2")
259 os.symlink(ABSTFN+"2", ABSTFN+"1")
260 self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1")
261 self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2")
262
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200263 self.assertEqual(realpath(ABSTFN+"1/x"), ABSTFN+"1/x")
264 self.assertEqual(realpath(ABSTFN+"1/.."), dirname(ABSTFN))
265 self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x")
266 os.symlink(ABSTFN+"x", ABSTFN+"y")
267 self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"),
268 ABSTFN + "y")
269 self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"),
270 ABSTFN + "1")
271
272 os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a")
273 self.assertEqual(realpath(ABSTFN+"a"), ABSTFN+"a/b")
274
275 os.symlink("../" + basename(dirname(ABSTFN)) + "/" +
276 basename(ABSTFN) + "c", ABSTFN+"c")
277 self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c")
278
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000279 # Test using relative path as well.
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300280 with support.change_cwd(dirname(ABSTFN)):
281 self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000282 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000283 test_support.unlink(ABSTFN)
284 test_support.unlink(ABSTFN+"1")
285 test_support.unlink(ABSTFN+"2")
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200286 test_support.unlink(ABSTFN+"y")
287 test_support.unlink(ABSTFN+"c")
Ezio Melottic86e8662013-03-01 20:56:13 +0200288 test_support.unlink(ABSTFN+"a")
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200289
290 def test_realpath_repeated_indirect_symlinks(self):
291 # Issue #6975.
292 try:
293 os.mkdir(ABSTFN)
294 os.symlink('../' + basename(ABSTFN), ABSTFN + '/self')
295 os.symlink('self/self/self', ABSTFN + '/link')
296 self.assertEqual(realpath(ABSTFN + '/link'), ABSTFN)
297 finally:
298 test_support.unlink(ABSTFN + '/self')
299 test_support.unlink(ABSTFN + '/link')
300 safe_rmdir(ABSTFN)
301
302 def test_realpath_deep_recursion(self):
303 depth = 10
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200304 try:
305 os.mkdir(ABSTFN)
306 for i in range(depth):
307 os.symlink('/'.join(['%d' % i] * 10), ABSTFN + '/%d' % (i + 1))
308 os.symlink('.', ABSTFN + '/0')
309 self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN)
310
311 # Test using relative path as well.
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300312 with support.change_cwd(ABSTFN):
313 self.assertEqual(realpath('%d' % depth), ABSTFN)
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200314 finally:
Serhiy Storchaka0dd3d302013-02-10 12:21:49 +0200315 for i in range(depth + 1):
316 test_support.unlink(ABSTFN + '/%d' % i)
317 safe_rmdir(ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000318
Tim Petersa45cacf2004-08-20 03:47:14 +0000319 def test_realpath_resolve_parents(self):
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000320 # We also need to resolve any symlinks in the parents of a relative
321 # path passed to realpath. E.g.: current working directory is
322 # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
323 # realpath("a"). This should return /usr/share/doc/a/.
324 try:
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000325 os.mkdir(ABSTFN)
326 os.mkdir(ABSTFN + "/y")
327 os.symlink(ABSTFN + "/y", ABSTFN + "/k")
328
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300329 with support.change_cwd(ABSTFN + "/k"):
330 self.assertEqual(realpath("a"), ABSTFN + "/y/a")
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000331 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000332 test_support.unlink(ABSTFN + "/k")
333 safe_rmdir(ABSTFN + "/y")
334 safe_rmdir(ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000335
336 def test_realpath_resolve_before_normalizing(self):
337 # Bug #990669: Symbolic links should be resolved before we
338 # normalize the path. E.g.: if we have directories 'a', 'k' and 'y'
339 # in the following hierarchy:
340 # a/k/y
341 #
342 # and a symbolic link 'link-y' pointing to 'y' in directory 'a',
343 # then realpath("link-y/..") should return 'k', not 'a'.
344 try:
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000345 os.mkdir(ABSTFN)
346 os.mkdir(ABSTFN + "/k")
347 os.mkdir(ABSTFN + "/k/y")
348 os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y")
349
350 # Absolute path.
351 self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k")
352 # Relative path.
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300353 with support.change_cwd(dirname(ABSTFN)):
354 self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."),
355 ABSTFN + "/k")
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000356 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000357 test_support.unlink(ABSTFN + "/link-y")
358 safe_rmdir(ABSTFN + "/k/y")
359 safe_rmdir(ABSTFN + "/k")
360 safe_rmdir(ABSTFN)
Tim Peters5d36a552005-06-03 22:40:27 +0000361
Georg Brandl268e61c2005-06-03 14:28:50 +0000362 def test_realpath_resolve_first(self):
363 # Bug #1213894: The first component of the path, if not absolute,
364 # must be resolved too.
365
366 try:
Georg Brandl268e61c2005-06-03 14:28:50 +0000367 os.mkdir(ABSTFN)
368 os.mkdir(ABSTFN + "/k")
369 os.symlink(ABSTFN, ABSTFN + "link")
Serhiy Storchaka7c7b4b52015-09-06 14:16:18 +0300370 with support.change_cwd(dirname(ABSTFN)):
371 base = basename(ABSTFN)
372 self.assertEqual(realpath(base + "link"), ABSTFN)
373 self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
Georg Brandl268e61c2005-06-03 14:28:50 +0000374 finally:
Collin Winterdbead562007-03-10 02:23:40 +0000375 test_support.unlink(ABSTFN + "link")
376 safe_rmdir(ABSTFN + "/k")
377 safe_rmdir(ABSTFN)
Brett Cannonb47243a2003-06-16 21:54:50 +0000378
Collin Winter6f187742007-03-16 22:16:08 +0000379 def test_relpath(self):
Collin Winter75c7eb42007-03-23 22:24:39 +0000380 (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
381 try:
382 curdir = os.path.split(os.getcwd())[-1]
383 self.assertRaises(ValueError, posixpath.relpath, "")
384 self.assertEqual(posixpath.relpath("a"), "a")
385 self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a")
386 self.assertEqual(posixpath.relpath("a/b"), "a/b")
387 self.assertEqual(posixpath.relpath("../a/b"), "../a/b")
388 self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a")
389 self.assertEqual(posixpath.relpath("a/b", "../c"), "../"+curdir+"/a/b")
390 self.assertEqual(posixpath.relpath("a", "b/c"), "../../a")
Georg Brandl183a0842008-01-06 14:27:15 +0000391 self.assertEqual(posixpath.relpath("a", "a"), ".")
Hirokazu Yamamoto50f7d7e2010-10-18 13:55:29 +0000392 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat')
393 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/foo/bar"), 'bat')
394 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/"), 'foo/bar/bat')
395 self.assertEqual(posixpath.relpath("/", "/foo/bar/bat"), '../../..')
396 self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x"), '../foo/bar/bat')
397 self.assertEqual(posixpath.relpath("/x", "/foo/bar/bat"), '../../../x')
398 self.assertEqual(posixpath.relpath("/", "/"), '.')
399 self.assertEqual(posixpath.relpath("/a", "/a"), '.')
400 self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.')
Collin Winter75c7eb42007-03-23 22:24:39 +0000401 finally:
402 os.getcwd = real_getcwd
Collin Winter6f187742007-03-16 22:16:08 +0000403
Serhiy Storchaka2bd8b222015-02-13 12:02:05 +0200404 @test_support.requires_unicode
405 def test_expandvars_nonascii_word(self):
406 encoding = sys.getfilesystemencoding()
407 # Non-ASCII word characters
408 letters = test_support.u(r'\xe6\u0130\u0141\u03c6\u041a\u05d0\u062a\u0e01')
409 uwnonascii = letters.encode(encoding, 'ignore').decode(encoding)[:3]
410 swnonascii = uwnonascii.encode(encoding)
411 if not swnonascii:
Serhiy Storchaka3be0d0e2015-02-13 12:47:08 +0200412 self.skipTest('Needs non-ASCII word characters')
Serhiy Storchaka2bd8b222015-02-13 12:02:05 +0200413 with test_support.EnvironmentVarGuard() as env:
414 env.clear()
415 env[swnonascii] = 'baz' + swnonascii
416 self.assertEqual(posixpath.expandvars(u'$%s bar' % uwnonascii),
417 u'baz%s bar' % uwnonascii)
418
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000419
420class PosixCommonTest(test_genericpath.CommonTest):
Florent Xicluna985478d2010-03-06 18:52:52 +0000421 pathmodule = posixpath
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000422 attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat']
423
424
Brett Cannonb47243a2003-06-16 21:54:50 +0000425def test_main():
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000426 test_support.run_unittest(PosixPathTest, PosixCommonTest)
427
Brett Cannonb47243a2003-06-16 21:54:50 +0000428
429if __name__=="__main__":
430 test_main()