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 self
.assertRaises(TypeError, posixpath
.normpath
)
386 def test_abspath(self
):
387 self
.assertTrue("foo" in posixpath
.abspath("foo"))
389 self
.assertRaises(TypeError, posixpath
.abspath
)
391 def test_realpath(self
):
392 self
.assertTrue("foo" in realpath("foo"))
393 self
.assertRaises(TypeError, posixpath
.realpath
)
395 if hasattr(os
, "symlink"):
396 def test_realpath_basic(self
):
399 os
.symlink(ABSTFN
+"1", ABSTFN
)
400 self
.assertEqual(realpath(ABSTFN
), ABSTFN
+"1")
402 test_support
.unlink(ABSTFN
)
404 def test_realpath_symlink_loops(self
):
405 # Bug #930024, return the path unchanged if we get into an infinite
408 old_path
= abspath('.')
409 os
.symlink(ABSTFN
, ABSTFN
)
410 self
.assertEqual(realpath(ABSTFN
), ABSTFN
)
412 os
.symlink(ABSTFN
+"1", ABSTFN
+"2")
413 os
.symlink(ABSTFN
+"2", ABSTFN
+"1")
414 self
.assertEqual(realpath(ABSTFN
+"1"), ABSTFN
+"1")
415 self
.assertEqual(realpath(ABSTFN
+"2"), ABSTFN
+"2")
417 # Test using relative path as well.
418 os
.chdir(dirname(ABSTFN
))
419 self
.assertEqual(realpath(basename(ABSTFN
)), ABSTFN
)
422 test_support
.unlink(ABSTFN
)
423 test_support
.unlink(ABSTFN
+"1")
424 test_support
.unlink(ABSTFN
+"2")
426 def test_realpath_resolve_parents(self
):
427 # We also need to resolve any symlinks in the parents of a relative
428 # path passed to realpath. E.g.: current working directory is
429 # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
430 # realpath("a"). This should return /usr/share/doc/a/.
432 old_path
= abspath('.')
434 os
.mkdir(ABSTFN
+ "/y")
435 os
.symlink(ABSTFN
+ "/y", ABSTFN
+ "/k")
437 os
.chdir(ABSTFN
+ "/k")
438 self
.assertEqual(realpath("a"), ABSTFN
+ "/y/a")
441 test_support
.unlink(ABSTFN
+ "/k")
442 safe_rmdir(ABSTFN
+ "/y")
445 def test_realpath_resolve_before_normalizing(self
):
446 # Bug #990669: Symbolic links should be resolved before we
447 # normalize the path. E.g.: if we have directories 'a', 'k' and 'y'
448 # in the following hierarchy:
451 # and a symbolic link 'link-y' pointing to 'y' in directory 'a',
452 # then realpath("link-y/..") should return 'k', not 'a'.
454 old_path
= abspath('.')
456 os
.mkdir(ABSTFN
+ "/k")
457 os
.mkdir(ABSTFN
+ "/k/y")
458 os
.symlink(ABSTFN
+ "/k/y", ABSTFN
+ "/link-y")
461 self
.assertEqual(realpath(ABSTFN
+ "/link-y/.."), ABSTFN
+ "/k")
463 os
.chdir(dirname(ABSTFN
))
464 self
.assertEqual(realpath(basename(ABSTFN
) + "/link-y/.."), ABSTFN
+ "/k")
467 test_support
.unlink(ABSTFN
+ "/link-y")
468 safe_rmdir(ABSTFN
+ "/k/y")
469 safe_rmdir(ABSTFN
+ "/k")
472 def test_realpath_resolve_first(self
):
473 # Bug #1213894: The first component of the path, if not absolute,
474 # must be resolved too.
477 old_path
= abspath('.')
479 os
.mkdir(ABSTFN
+ "/k")
480 os
.symlink(ABSTFN
, ABSTFN
+ "link")
481 os
.chdir(dirname(ABSTFN
))
483 base
= basename(ABSTFN
)
484 self
.assertEqual(realpath(base
+ "link"), ABSTFN
)
485 self
.assertEqual(realpath(base
+ "link/k"), ABSTFN
+ "/k")
488 test_support
.unlink(ABSTFN
+ "link")
489 safe_rmdir(ABSTFN
+ "/k")
492 def test_relpath(self
):
493 (real_getcwd
, os
.getcwd
) = (os
.getcwd
, lambda: r
"/home/user/bar")
495 curdir
= os
.path
.split(os
.getcwd())[-1]
496 self
.assertRaises(ValueError, posixpath
.relpath
, "")
497 self
.assertEqual(posixpath
.relpath("a"), "a")
498 self
.assertEqual(posixpath
.relpath(posixpath
.abspath("a")), "a")
499 self
.assertEqual(posixpath
.relpath("a/b"), "a/b")
500 self
.assertEqual(posixpath
.relpath("../a/b"), "../a/b")
501 self
.assertEqual(posixpath
.relpath("a", "../b"), "../"+curdir
+"/a")
502 self
.assertEqual(posixpath
.relpath("a/b", "../c"), "../"+curdir
+"/a/b")
503 self
.assertEqual(posixpath
.relpath("a", "b/c"), "../../a")
504 self
.assertEqual(posixpath
.relpath("a", "a"), ".")
506 os
.getcwd
= real_getcwd
509 test_support
.run_unittest(PosixPathTest
)
511 if __name__
=="__main__":