2 from test
import test_support
5 from posixpath
import realpath
, abspath
, join
, dirname
, basename
, relpath
7 # An absolute path to a temporary filename for testing. We can't rely on TESTFN
8 # being an absolute path, so we need this.
10 ABSTFN
= abspath(test_support
.TESTFN
)
12 def safe_rmdir(dirname
):
18 class PosixPathTest(unittest
.TestCase
):
24 for suffix
in ["", "1", "2"]:
25 test_support
.unlink(test_support
.TESTFN
+ suffix
)
26 safe_rmdir(test_support
.TESTFN
+ suffix
)
28 def assertIs(self
, a
, b
):
29 self
.assertTrue(a
is b
)
31 def test_normcase(self
):
32 # Check that normcase() is idempotent
34 p
= posixpath
.normcase(p
)
35 self
.assertEqual(p
, posixpath
.normcase(p
))
37 self
.assertRaises(TypeError, posixpath
.normcase
)
40 self
.assertEqual(posixpath
.join("/foo", "bar", "/bar", "baz"), "/bar/baz")
41 self
.assertEqual(posixpath
.join("/foo", "bar", "baz"), "/foo/bar/baz")
42 self
.assertEqual(posixpath
.join("/foo/", "bar/", "baz/"), "/foo/bar/baz/")
44 self
.assertRaises(TypeError, posixpath
.join
)
46 def test_splitdrive(self
):
47 self
.assertEqual(posixpath
.splitdrive("/foo/bar"), ("", "/foo/bar"))
49 self
.assertRaises(TypeError, posixpath
.splitdrive
)
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"))
58 self
.assertRaises(TypeError, posixpath
.split
)
60 def splitextTest(self
, path
, filename
, ext
):
61 self
.assertEqual(posixpath
.splitext(path
), (filename
, ext
))
62 self
.assertEqual(posixpath
.splitext("/" + path
), ("/" + filename
, ext
))
63 self
.assertEqual(posixpath
.splitext("abc/" + path
), ("abc/" + filename
, ext
))
64 self
.assertEqual(posixpath
.splitext("abc.def/" + path
), ("abc.def/" + filename
, ext
))
65 self
.assertEqual(posixpath
.splitext("/abc.def/" + path
), ("/abc.def/" + filename
, ext
))
66 self
.assertEqual(posixpath
.splitext(path
+ "/"), (filename
+ ext
+ "/", ""))
68 def test_splitext(self
):
69 self
.splitextTest("foo.bar", "foo", ".bar")
70 self
.splitextTest("foo.boo.bar", "foo.boo", ".bar")
71 self
.splitextTest("foo.boo.biff.bar", "foo.boo.biff", ".bar")
72 self
.splitextTest(".csh.rc", ".csh", ".rc")
73 self
.splitextTest("nodots", "nodots", "")
74 self
.splitextTest(".cshrc", ".cshrc", "")
75 self
.splitextTest("...manydots", "...manydots", "")
76 self
.splitextTest("...manydots.ext", "...manydots", ".ext")
77 self
.splitextTest(".", ".", "")
78 self
.splitextTest("..", "..", "")
79 self
.splitextTest("........", "........", "")
80 self
.splitextTest("", "", "")
81 self
.assertRaises(TypeError, posixpath
.splitext
)
84 self
.assertIs(posixpath
.isabs(""), False)
85 self
.assertIs(posixpath
.isabs("/"), True)
86 self
.assertIs(posixpath
.isabs("/foo"), True)
87 self
.assertIs(posixpath
.isabs("/foo/bar"), True)
88 self
.assertIs(posixpath
.isabs("foo/bar"), False)
90 self
.assertRaises(TypeError, posixpath
.isabs
)
92 def test_splitdrive(self
):
93 self
.assertEqual(posixpath
.splitdrive("/foo/bar"), ("", "/foo/bar"))
95 self
.assertRaises(TypeError, posixpath
.splitdrive
)
97 def test_basename(self
):
98 self
.assertEqual(posixpath
.basename("/foo/bar"), "bar")
99 self
.assertEqual(posixpath
.basename("/"), "")
100 self
.assertEqual(posixpath
.basename("foo"), "foo")
101 self
.assertEqual(posixpath
.basename("////foo"), "foo")
102 self
.assertEqual(posixpath
.basename("//foo//bar"), "bar")
104 self
.assertRaises(TypeError, posixpath
.basename
)
106 def test_dirname(self
):
107 self
.assertEqual(posixpath
.dirname("/foo/bar"), "/foo")
108 self
.assertEqual(posixpath
.dirname("/"), "/")
109 self
.assertEqual(posixpath
.dirname("foo"), "")
110 self
.assertEqual(posixpath
.dirname("////foo"), "////")
111 self
.assertEqual(posixpath
.dirname("//foo//bar"), "//foo")
113 self
.assertRaises(TypeError, posixpath
.dirname
)
115 def test_commonprefix(self
):
117 posixpath
.commonprefix([]),
121 posixpath
.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
125 posixpath
.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
129 posixpath
.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
133 testlist
= ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', 'aXc', 'abd', 'ab', 'aX', 'abcX']
136 p
= posixpath
.commonprefix([s1
, s2
])
137 self
.assertTrue(s1
.startswith(p
))
138 self
.assertTrue(s2
.startswith(p
))
141 self
.assertNotEqual(s1
[n
:n
+1], s2
[n
:n
+1])
143 def test_getsize(self
):
144 f
= open(test_support
.TESTFN
, "wb")
148 self
.assertEqual(posixpath
.getsize(test_support
.TESTFN
), 3)
154 f
= open(test_support
.TESTFN
, "wb")
158 f
= open(test_support
.TESTFN
, "ab")
161 f
= open(test_support
.TESTFN
, "rb")
164 self
.assertEqual(d
, "foobar")
167 posixpath
.getctime(test_support
.TESTFN
) <=
168 posixpath
.getmtime(test_support
.TESTFN
)
174 def test_islink(self
):
175 self
.assertIs(posixpath
.islink(test_support
.TESTFN
+ "1"), False)
176 f
= open(test_support
.TESTFN
+ "1", "wb")
180 self
.assertIs(posixpath
.islink(test_support
.TESTFN
+ "1"), False)
181 if hasattr(os
, "symlink"):
182 os
.symlink(test_support
.TESTFN
+ "1", test_support
.TESTFN
+ "2")
183 self
.assertIs(posixpath
.islink(test_support
.TESTFN
+ "2"), True)
184 os
.remove(test_support
.TESTFN
+ "1")
185 self
.assertIs(posixpath
.islink(test_support
.TESTFN
+ "2"), True)
186 self
.assertIs(posixpath
.exists(test_support
.TESTFN
+ "2"), False)
187 self
.assertIs(posixpath
.lexists(test_support
.TESTFN
+ "2"), True)
192 self
.assertRaises(TypeError, posixpath
.islink
)
194 def test_exists(self
):
195 self
.assertIs(posixpath
.exists(test_support
.TESTFN
), False)
196 f
= open(test_support
.TESTFN
, "wb")
200 self
.assertIs(posixpath
.exists(test_support
.TESTFN
), True)
201 self
.assertIs(posixpath
.lexists(test_support
.TESTFN
), True)
206 self
.assertRaises(TypeError, posixpath
.exists
)
208 def test_isdir(self
):
209 self
.assertIs(posixpath
.isdir(test_support
.TESTFN
), False)
210 f
= open(test_support
.TESTFN
, "wb")
214 self
.assertIs(posixpath
.isdir(test_support
.TESTFN
), False)
215 os
.remove(test_support
.TESTFN
)
216 os
.mkdir(test_support
.TESTFN
)
217 self
.assertIs(posixpath
.isdir(test_support
.TESTFN
), True)
218 os
.rmdir(test_support
.TESTFN
)
223 self
.assertRaises(TypeError, posixpath
.isdir
)
225 def test_isfile(self
):
226 self
.assertIs(posixpath
.isfile(test_support
.TESTFN
), False)
227 f
= open(test_support
.TESTFN
, "wb")
231 self
.assertIs(posixpath
.isfile(test_support
.TESTFN
), True)
232 os
.remove(test_support
.TESTFN
)
233 os
.mkdir(test_support
.TESTFN
)
234 self
.assertIs(posixpath
.isfile(test_support
.TESTFN
), False)
235 os
.rmdir(test_support
.TESTFN
)
240 self
.assertRaises(TypeError, posixpath
.isdir
)
242 def test_samefile(self
):
243 f
= open(test_support
.TESTFN
+ "1", "wb")
249 test_support
.TESTFN
+ "1",
250 test_support
.TESTFN
+ "1"
254 # If we don't have links, assume that os.stat doesn't return resonable
255 # inode information and thus, that samefile() doesn't work
256 if hasattr(os
, "symlink"):
258 test_support
.TESTFN
+ "1",
259 test_support
.TESTFN
+ "2"
263 test_support
.TESTFN
+ "1",
264 test_support
.TESTFN
+ "2"
268 os
.remove(test_support
.TESTFN
+ "2")
269 f
= open(test_support
.TESTFN
+ "2", "wb")
274 test_support
.TESTFN
+ "1",
275 test_support
.TESTFN
+ "2"
283 self
.assertRaises(TypeError, posixpath
.samefile
)
285 def test_samestat(self
):
286 f
= open(test_support
.TESTFN
+ "1", "wb")
292 os
.stat(test_support
.TESTFN
+ "1"),
293 os
.stat(test_support
.TESTFN
+ "1")
297 # If we don't have links, assume that os.stat() doesn't return resonable
298 # inode information and thus, that samefile() doesn't work
299 if hasattr(os
, "symlink"):
300 if hasattr(os
, "symlink"):
301 os
.symlink(test_support
.TESTFN
+ "1", test_support
.TESTFN
+ "2")
304 os
.stat(test_support
.TESTFN
+ "1"),
305 os
.stat(test_support
.TESTFN
+ "2")
309 os
.remove(test_support
.TESTFN
+ "2")
310 f
= open(test_support
.TESTFN
+ "2", "wb")
315 os
.stat(test_support
.TESTFN
+ "1"),
316 os
.stat(test_support
.TESTFN
+ "2")
324 self
.assertRaises(TypeError, posixpath
.samestat
)
326 def test_ismount(self
):
327 self
.assertIs(posixpath
.ismount("/"), True)
329 self
.assertRaises(TypeError, posixpath
.ismount
)
331 def test_expanduser(self
):
332 self
.assertEqual(posixpath
.expanduser("foo"), "foo")
338 self
.assertTrue(isinstance(posixpath
.expanduser("~/"), basestring
))
339 # if home directory == root directory, this test makes no sense
340 if posixpath
.expanduser("~") != '/':
342 posixpath
.expanduser("~") + "/",
343 posixpath
.expanduser("~/")
345 self
.assertTrue(isinstance(posixpath
.expanduser("~root/"), basestring
))
346 self
.assertTrue(isinstance(posixpath
.expanduser("~foo/"), basestring
))
348 with test_support
.EnvironmentVarGuard() as env
:
350 self
.assertEqual(posixpath
.expanduser("~"), "/")
352 self
.assertRaises(TypeError, posixpath
.expanduser
)
354 def test_expandvars(self
):
355 with test_support
.EnvironmentVarGuard() as env
:
359 env
["{foo}"] = "baz2"
360 self
.assertEqual(posixpath
.expandvars("foo"), "foo")
361 self
.assertEqual(posixpath
.expandvars("$foo bar"), "bar bar")
362 self
.assertEqual(posixpath
.expandvars("${foo}bar"), "barbar")
363 self
.assertEqual(posixpath
.expandvars("$[foo]bar"), "$[foo]bar")
364 self
.assertEqual(posixpath
.expandvars("$bar bar"), "$bar bar")
365 self
.assertEqual(posixpath
.expandvars("$?bar"), "$?bar")
366 self
.assertEqual(posixpath
.expandvars("${foo}bar"), "barbar")
367 self
.assertEqual(posixpath
.expandvars("$foo}bar"), "bar}bar")
368 self
.assertEqual(posixpath
.expandvars("${foo"), "${foo")
369 self
.assertEqual(posixpath
.expandvars("${{foo}}"), "baz1}")
370 self
.assertEqual(posixpath
.expandvars("$foo$foo"), "barbar")
371 self
.assertEqual(posixpath
.expandvars("$bar$bar"), "$bar$bar")
373 self
.assertRaises(TypeError, posixpath
.expandvars
)
375 def test_normpath(self
):
376 self
.assertEqual(posixpath
.normpath(""), ".")
377 self
.assertEqual(posixpath
.normpath("/"), "/")
378 self
.assertEqual(posixpath
.normpath("//"), "//")
379 self
.assertEqual(posixpath
.normpath("///"), "/")
380 self
.assertEqual(posixpath
.normpath("///foo/.//bar//"), "/foo/bar")
381 self
.assertEqual(posixpath
.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")
382 self
.assertEqual(posixpath
.normpath("///..//./foo/.//bar"), "/foo/bar")
384 # Issue 5827: Make sure normpath preserves unicode
385 for path
in (u
'', u
'.', u
'/', u
'\\', u
'///foo/.//bar//'):
386 self
.assertTrue(isinstance(posixpath
.normpath(path
), unicode),
387 'normpath() returned str instead of unicode')
389 self
.assertRaises(TypeError, posixpath
.normpath
)
391 def test_abspath(self
):
392 self
.assertTrue("foo" in posixpath
.abspath("foo"))
394 self
.assertRaises(TypeError, posixpath
.abspath
)
396 def test_realpath(self
):
397 self
.assertTrue("foo" in realpath("foo"))
398 self
.assertRaises(TypeError, posixpath
.realpath
)
400 if hasattr(os
, "symlink"):
401 def test_realpath_basic(self
):
404 os
.symlink(ABSTFN
+"1", ABSTFN
)
405 self
.assertEqual(realpath(ABSTFN
), ABSTFN
+"1")
407 test_support
.unlink(ABSTFN
)
409 def test_realpath_symlink_loops(self
):
410 # Bug #930024, return the path unchanged if we get into an infinite
413 old_path
= abspath('.')
414 os
.symlink(ABSTFN
, ABSTFN
)
415 self
.assertEqual(realpath(ABSTFN
), ABSTFN
)
417 os
.symlink(ABSTFN
+"1", ABSTFN
+"2")
418 os
.symlink(ABSTFN
+"2", ABSTFN
+"1")
419 self
.assertEqual(realpath(ABSTFN
+"1"), ABSTFN
+"1")
420 self
.assertEqual(realpath(ABSTFN
+"2"), ABSTFN
+"2")
422 # Test using relative path as well.
423 os
.chdir(dirname(ABSTFN
))
424 self
.assertEqual(realpath(basename(ABSTFN
)), ABSTFN
)
427 test_support
.unlink(ABSTFN
)
428 test_support
.unlink(ABSTFN
+"1")
429 test_support
.unlink(ABSTFN
+"2")
431 def test_realpath_resolve_parents(self
):
432 # We also need to resolve any symlinks in the parents of a relative
433 # path passed to realpath. E.g.: current working directory is
434 # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
435 # realpath("a"). This should return /usr/share/doc/a/.
437 old_path
= abspath('.')
439 os
.mkdir(ABSTFN
+ "/y")
440 os
.symlink(ABSTFN
+ "/y", ABSTFN
+ "/k")
442 os
.chdir(ABSTFN
+ "/k")
443 self
.assertEqual(realpath("a"), ABSTFN
+ "/y/a")
446 test_support
.unlink(ABSTFN
+ "/k")
447 safe_rmdir(ABSTFN
+ "/y")
450 def test_realpath_resolve_before_normalizing(self
):
451 # Bug #990669: Symbolic links should be resolved before we
452 # normalize the path. E.g.: if we have directories 'a', 'k' and 'y'
453 # in the following hierarchy:
456 # and a symbolic link 'link-y' pointing to 'y' in directory 'a',
457 # then realpath("link-y/..") should return 'k', not 'a'.
459 old_path
= abspath('.')
461 os
.mkdir(ABSTFN
+ "/k")
462 os
.mkdir(ABSTFN
+ "/k/y")
463 os
.symlink(ABSTFN
+ "/k/y", ABSTFN
+ "/link-y")
466 self
.assertEqual(realpath(ABSTFN
+ "/link-y/.."), ABSTFN
+ "/k")
468 os
.chdir(dirname(ABSTFN
))
469 self
.assertEqual(realpath(basename(ABSTFN
) + "/link-y/.."), ABSTFN
+ "/k")
472 test_support
.unlink(ABSTFN
+ "/link-y")
473 safe_rmdir(ABSTFN
+ "/k/y")
474 safe_rmdir(ABSTFN
+ "/k")
477 def test_realpath_resolve_first(self
):
478 # Bug #1213894: The first component of the path, if not absolute,
479 # must be resolved too.
482 old_path
= abspath('.')
484 os
.mkdir(ABSTFN
+ "/k")
485 os
.symlink(ABSTFN
, ABSTFN
+ "link")
486 os
.chdir(dirname(ABSTFN
))
488 base
= basename(ABSTFN
)
489 self
.assertEqual(realpath(base
+ "link"), ABSTFN
)
490 self
.assertEqual(realpath(base
+ "link/k"), ABSTFN
+ "/k")
493 test_support
.unlink(ABSTFN
+ "link")
494 safe_rmdir(ABSTFN
+ "/k")
497 def test_relpath(self
):
498 (real_getcwd
, os
.getcwd
) = (os
.getcwd
, lambda: r
"/home/user/bar")
500 curdir
= os
.path
.split(os
.getcwd())[-1]
501 self
.assertRaises(ValueError, posixpath
.relpath
, "")
502 self
.assertEqual(posixpath
.relpath("a"), "a")
503 self
.assertEqual(posixpath
.relpath(posixpath
.abspath("a")), "a")
504 self
.assertEqual(posixpath
.relpath("a/b"), "a/b")
505 self
.assertEqual(posixpath
.relpath("../a/b"), "../a/b")
506 self
.assertEqual(posixpath
.relpath("a", "../b"), "../"+curdir
+"/a")
507 self
.assertEqual(posixpath
.relpath("a/b", "../c"), "../"+curdir
+"/a/b")
508 self
.assertEqual(posixpath
.relpath("a", "b/c"), "../../a")
509 self
.assertEqual(posixpath
.relpath("a", "a"), ".")
511 os
.getcwd
= real_getcwd
514 test_support
.run_unittest(PosixPathTest
)
516 if __name__
=="__main__":