blob: 20a1fc5ec63d6a2bb0342e62cb00d1795b632a27 [file] [log] [blame]
Brett Cannonb47243a2003-06-16 21:54:50 +00001import unittest
2from test import test_support
Skip Montanaroe809b002000-07-12 00:20:08 +00003
Brett Cannonb47243a2003-06-16 21:54:50 +00004import posixpath, os
Johannes Gijsbers4ec40642004-08-14 15:01:53 +00005from posixpath import realpath, abspath, join, dirname, basename
6
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.
9
10ABSTFN = abspath(test_support.TESTFN)
Skip Montanaroe809b002000-07-12 00:20:08 +000011
Brett Cannonb47243a2003-06-16 21:54:50 +000012class PosixPathTest(unittest.TestCase):
Skip Montanaroe809b002000-07-12 00:20:08 +000013
Brett Cannonb47243a2003-06-16 21:54:50 +000014 def assertIs(self, a, b):
15 self.assert_(a is b)
Skip Montanaroe809b002000-07-12 00:20:08 +000016
Brett Cannonb47243a2003-06-16 21:54:50 +000017 def test_normcase(self):
18 # Check that normcase() is idempotent
19 p = "FoO/./BaR"
20 p = posixpath.normcase(p)
21 self.assertEqual(p, posixpath.normcase(p))
Skip Montanaroe809b002000-07-12 00:20:08 +000022
Brett Cannonb47243a2003-06-16 21:54:50 +000023 self.assertRaises(TypeError, posixpath.normcase)
Skip Montanaroe809b002000-07-12 00:20:08 +000024
Brett Cannonb47243a2003-06-16 21:54:50 +000025 def test_join(self):
26 self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), "/bar/baz")
27 self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz")
28 self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), "/foo/bar/baz/")
Skip Montanaroe809b002000-07-12 00:20:08 +000029
Brett Cannonb47243a2003-06-16 21:54:50 +000030 self.assertRaises(TypeError, posixpath.join)
Skip Montanaroe809b002000-07-12 00:20:08 +000031
Brett Cannonb47243a2003-06-16 21:54:50 +000032 def test_splitdrive(self):
33 self.assertEqual(posixpath.splitdrive("/foo/bar"), ("", "/foo/bar"))
34
35 self.assertRaises(TypeError, posixpath.splitdrive)
36
37 def test_split(self):
38 self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))
39 self.assertEqual(posixpath.split("/"), ("/", ""))
40 self.assertEqual(posixpath.split("foo"), ("", "foo"))
41 self.assertEqual(posixpath.split("////foo"), ("////", "foo"))
42 self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar"))
43
44 self.assertRaises(TypeError, posixpath.split)
45
46 def test_splitext(self):
47 self.assertEqual(posixpath.splitext("foo.ext"), ("foo", ".ext"))
48 self.assertEqual(posixpath.splitext("/foo/foo.ext"), ("/foo/foo", ".ext"))
49 self.assertEqual(posixpath.splitext(".ext"), ("", ".ext"))
50 self.assertEqual(posixpath.splitext("/foo.ext/foo"), ("/foo.ext/foo", ""))
51 self.assertEqual(posixpath.splitext("foo.ext/"), ("foo.ext/", ""))
52 self.assertEqual(posixpath.splitext(""), ("", ""))
53 self.assertEqual(posixpath.splitext("foo.bar.ext"), ("foo.bar", ".ext"))
54
55 self.assertRaises(TypeError, posixpath.splitext)
56
57 def test_isabs(self):
58 self.assertIs(posixpath.isabs(""), False)
59 self.assertIs(posixpath.isabs("/"), True)
60 self.assertIs(posixpath.isabs("/foo"), True)
61 self.assertIs(posixpath.isabs("/foo/bar"), True)
62 self.assertIs(posixpath.isabs("foo/bar"), False)
63
64 self.assertRaises(TypeError, posixpath.isabs)
65
66 def test_splitdrive(self):
67 self.assertEqual(posixpath.splitdrive("/foo/bar"), ("", "/foo/bar"))
68
69 self.assertRaises(TypeError, posixpath.splitdrive)
70
71 def test_basename(self):
72 self.assertEqual(posixpath.basename("/foo/bar"), "bar")
73 self.assertEqual(posixpath.basename("/"), "")
74 self.assertEqual(posixpath.basename("foo"), "foo")
75 self.assertEqual(posixpath.basename("////foo"), "foo")
76 self.assertEqual(posixpath.basename("//foo//bar"), "bar")
77
78 self.assertRaises(TypeError, posixpath.basename)
79
80 def test_dirname(self):
81 self.assertEqual(posixpath.dirname("/foo/bar"), "/foo")
82 self.assertEqual(posixpath.dirname("/"), "/")
83 self.assertEqual(posixpath.dirname("foo"), "")
84 self.assertEqual(posixpath.dirname("////foo"), "////")
85 self.assertEqual(posixpath.dirname("//foo//bar"), "//foo")
86
87 self.assertRaises(TypeError, posixpath.dirname)
88
89 def test_commonprefix(self):
90 self.assertEqual(
91 posixpath.commonprefix([]),
92 ""
93 )
94 self.assertEqual(
95 posixpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
96 "/home/swen"
97 )
98 self.assertEqual(
99 posixpath.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
100 "/home/swen/"
101 )
102 self.assertEqual(
103 posixpath.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
104 "/home/swen/spam"
105 )
106
107 def test_getsize(self):
108 f = open(test_support.TESTFN, "wb")
109 try:
110 f.write("foo")
111 f.close()
112 self.assertEqual(posixpath.getsize(test_support.TESTFN), 3)
113 finally:
114 if not f.closed:
115 f.close()
116 os.remove(test_support.TESTFN)
117
118 def test_time(self):
119 f = open(test_support.TESTFN, "wb")
120 try:
121 f.write("foo")
122 f.close()
123 f = open(test_support.TESTFN, "ab")
124 f.write("bar")
125 f.close()
126 f = open(test_support.TESTFN, "rb")
127 d = f.read()
128 f.close()
129 self.assertEqual(d, "foobar")
130
131 self.assert_(
132 posixpath.getctime(test_support.TESTFN) <=
133 posixpath.getmtime(test_support.TESTFN)
134 )
135 finally:
136 if not f.closed:
137 f.close()
138 os.remove(test_support.TESTFN)
139
140 def test_islink(self):
141 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
142 f = open(test_support.TESTFN + "1", "wb")
143 try:
144 f.write("foo")
145 f.close()
146 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
147 if hasattr(os, "symlink"):
148 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
149 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
150 os.remove(test_support.TESTFN + "1")
151 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
152 self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False)
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000153 self.assertIs(posixpath.lexists(test_support.TESTFN + "2"), True)
Brett Cannonb47243a2003-06-16 21:54:50 +0000154 finally:
155 if not f.close():
156 f.close()
157 try:
158 os.remove(test_support.TESTFN + "1")
159 except os.error:
160 pass
161 try:
162 os.remove(test_support.TESTFN + "2")
163 except os.error:
164 pass
165
166 self.assertRaises(TypeError, posixpath.islink)
167
168 def test_exists(self):
169 self.assertIs(posixpath.exists(test_support.TESTFN), False)
170 f = open(test_support.TESTFN, "wb")
171 try:
172 f.write("foo")
173 f.close()
174 self.assertIs(posixpath.exists(test_support.TESTFN), True)
Johannes Gijsbersae882f72004-08-30 10:19:56 +0000175 self.assertIs(posixpath.lexists(test_support.TESTFN), True)
Brett Cannonb47243a2003-06-16 21:54:50 +0000176 finally:
177 if not f.close():
178 f.close()
179 try:
180 os.remove(test_support.TESTFN)
181 except os.error:
182 pass
183
184 self.assertRaises(TypeError, posixpath.exists)
185
186 def test_isdir(self):
187 self.assertIs(posixpath.isdir(test_support.TESTFN), False)
188 f = open(test_support.TESTFN, "wb")
189 try:
190 f.write("foo")
191 f.close()
192 self.assertIs(posixpath.isdir(test_support.TESTFN), False)
193 os.remove(test_support.TESTFN)
194 os.mkdir(test_support.TESTFN)
195 self.assertIs(posixpath.isdir(test_support.TESTFN), True)
196 os.rmdir(test_support.TESTFN)
197 finally:
198 if not f.close():
199 f.close()
200 try:
201 os.remove(test_support.TESTFN)
202 except os.error:
203 pass
204 try:
205 os.rmdir(test_support.TESTFN)
206 except os.error:
207 pass
208
209 self.assertRaises(TypeError, posixpath.isdir)
210
211 def test_isfile(self):
212 self.assertIs(posixpath.isfile(test_support.TESTFN), False)
213 f = open(test_support.TESTFN, "wb")
214 try:
215 f.write("foo")
216 f.close()
217 self.assertIs(posixpath.isfile(test_support.TESTFN), True)
218 os.remove(test_support.TESTFN)
219 os.mkdir(test_support.TESTFN)
220 self.assertIs(posixpath.isfile(test_support.TESTFN), False)
221 os.rmdir(test_support.TESTFN)
222 finally:
223 if not f.close():
224 f.close()
225 try:
226 os.remove(test_support.TESTFN)
227 except os.error:
228 pass
229 try:
230 os.rmdir(test_support.TESTFN)
231 except os.error:
232 pass
233
234 self.assertRaises(TypeError, posixpath.isdir)
235
236 def test_samefile(self):
237 f = open(test_support.TESTFN + "1", "wb")
238 try:
239 f.write("foo")
240 f.close()
241 self.assertIs(
242 posixpath.samefile(
243 test_support.TESTFN + "1",
244 test_support.TESTFN + "1"
245 ),
246 True
247 )
Tim Peters478c1052003-06-29 05:46:54 +0000248 # If we don't have links, assume that os.stat doesn't return resonable
Brett Cannonb47243a2003-06-16 21:54:50 +0000249 # inode information and thus, that samefile() doesn't work
250 if hasattr(os, "symlink"):
251 os.symlink(
252 test_support.TESTFN + "1",
253 test_support.TESTFN + "2"
254 )
255 self.assertIs(
256 posixpath.samefile(
257 test_support.TESTFN + "1",
258 test_support.TESTFN + "2"
259 ),
260 True
261 )
262 os.remove(test_support.TESTFN + "2")
263 f = open(test_support.TESTFN + "2", "wb")
264 f.write("bar")
265 f.close()
266 self.assertIs(
267 posixpath.samefile(
268 test_support.TESTFN + "1",
269 test_support.TESTFN + "2"
270 ),
271 False
272 )
273 finally:
274 if not f.close():
275 f.close()
276 try:
277 os.remove(test_support.TESTFN + "1")
278 except os.error:
279 pass
280 try:
281 os.remove(test_support.TESTFN + "2")
282 except os.error:
283 pass
284
285 self.assertRaises(TypeError, posixpath.samefile)
286
287 def test_samestat(self):
288 f = open(test_support.TESTFN + "1", "wb")
289 try:
290 f.write("foo")
291 f.close()
292 self.assertIs(
293 posixpath.samestat(
294 os.stat(test_support.TESTFN + "1"),
295 os.stat(test_support.TESTFN + "1")
296 ),
297 True
298 )
Tim Peters478c1052003-06-29 05:46:54 +0000299 # If we don't have links, assume that os.stat() doesn't return resonable
Brett Cannonb47243a2003-06-16 21:54:50 +0000300 # inode information and thus, that samefile() doesn't work
301 if hasattr(os, "symlink"):
302 if hasattr(os, "symlink"):
303 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
304 self.assertIs(
305 posixpath.samestat(
306 os.stat(test_support.TESTFN + "1"),
307 os.stat(test_support.TESTFN + "2")
308 ),
309 True
310 )
311 os.remove(test_support.TESTFN + "2")
312 f = open(test_support.TESTFN + "2", "wb")
313 f.write("bar")
314 f.close()
315 self.assertIs(
316 posixpath.samestat(
317 os.stat(test_support.TESTFN + "1"),
318 os.stat(test_support.TESTFN + "2")
319 ),
320 False
321 )
322 finally:
323 if not f.close():
324 f.close()
325 try:
326 os.remove(test_support.TESTFN + "1")
327 except os.error:
328 pass
329 try:
330 os.remove(test_support.TESTFN + "2")
331 except os.error:
332 pass
333
334 self.assertRaises(TypeError, posixpath.samestat)
335
336 def test_ismount(self):
337 self.assertIs(posixpath.ismount("/"), True)
338
339 self.assertRaises(TypeError, posixpath.ismount)
340
341 def test_expanduser(self):
342 self.assertEqual(posixpath.expanduser("foo"), "foo")
Brett Cannonb47243a2003-06-16 21:54:50 +0000343 try:
344 import pwd
345 except ImportError:
346 pass
347 else:
Walter Dörwalda9da5ae2003-06-19 10:21:14 +0000348 self.assert_(isinstance(posixpath.expanduser("~/"), basestring))
Neal Norwitz168e73d2003-07-01 03:33:31 +0000349 # if home directory == root directory, this test makes no sense
350 if posixpath.expanduser("~") != '/':
351 self.assertEqual(
352 posixpath.expanduser("~") + "/",
353 posixpath.expanduser("~/")
354 )
Brett Cannonb47243a2003-06-16 21:54:50 +0000355 self.assert_(isinstance(posixpath.expanduser("~root/"), basestring))
356 self.assert_(isinstance(posixpath.expanduser("~foo/"), basestring))
357
358 self.assertRaises(TypeError, posixpath.expanduser)
359
360 def test_expandvars(self):
361 oldenv = os.environ.copy()
362 try:
363 os.environ.clear()
364 os.environ["foo"] = "bar"
365 os.environ["{foo"] = "baz1"
366 os.environ["{foo}"] = "baz2"
367 self.assertEqual(posixpath.expandvars("foo"), "foo")
368 self.assertEqual(posixpath.expandvars("$foo bar"), "bar bar")
369 self.assertEqual(posixpath.expandvars("${foo}bar"), "barbar")
370 self.assertEqual(posixpath.expandvars("$[foo]bar"), "$[foo]bar")
371 self.assertEqual(posixpath.expandvars("$bar bar"), "$bar bar")
372 self.assertEqual(posixpath.expandvars("$?bar"), "$?bar")
373 self.assertEqual(posixpath.expandvars("${foo}bar"), "barbar")
374 self.assertEqual(posixpath.expandvars("$foo}bar"), "bar}bar")
375 self.assertEqual(posixpath.expandvars("${foo"), "${foo")
376 self.assertEqual(posixpath.expandvars("${{foo}}"), "baz1}")
Thomas Woutersb2137042007-02-01 18:02:27 +0000377 self.assertEqual(posixpath.expandvars("$foo$foo"), "barbar")
378 self.assertEqual(posixpath.expandvars("$bar$bar"), "$bar$bar")
Brett Cannonb47243a2003-06-16 21:54:50 +0000379 finally:
380 os.environ.clear()
381 os.environ.update(oldenv)
382
383 self.assertRaises(TypeError, posixpath.expandvars)
384
385 def test_normpath(self):
386 self.assertEqual(posixpath.normpath(""), ".")
387 self.assertEqual(posixpath.normpath("/"), "/")
388 self.assertEqual(posixpath.normpath("//"), "//")
389 self.assertEqual(posixpath.normpath("///"), "/")
390 self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar")
391 self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")
392 self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
393
394 self.assertRaises(TypeError, posixpath.normpath)
395
396 def test_abspath(self):
397 self.assert_("foo" in posixpath.abspath("foo"))
398
399 self.assertRaises(TypeError, posixpath.abspath)
400
401 def test_realpath(self):
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000402 self.assert_("foo" in realpath("foo"))
Brett Cannonb47243a2003-06-16 21:54:50 +0000403 self.assertRaises(TypeError, posixpath.realpath)
Tim Petersa45cacf2004-08-20 03:47:14 +0000404
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000405 if hasattr(os, "symlink"):
406 def test_realpath_basic(self):
407 # Basic operation.
408 try:
409 os.symlink(ABSTFN+"1", ABSTFN)
410 self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
411 finally:
412 self.safe_remove(ABSTFN)
Tim Petersa45cacf2004-08-20 03:47:14 +0000413
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000414 def test_realpath_symlink_loops(self):
415 # Bug #930024, return the path unchanged if we get into an infinite
416 # symlink loop.
417 try:
418 old_path = abspath('.')
419 os.symlink(ABSTFN, ABSTFN)
420 self.assertEqual(realpath(ABSTFN), ABSTFN)
421
422 os.symlink(ABSTFN+"1", ABSTFN+"2")
423 os.symlink(ABSTFN+"2", ABSTFN+"1")
424 self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1")
425 self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2")
426
427 # Test using relative path as well.
428 os.chdir(dirname(ABSTFN))
429 self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
430 finally:
431 os.chdir(old_path)
432 self.safe_remove(ABSTFN)
433 self.safe_remove(ABSTFN+"1")
434 self.safe_remove(ABSTFN+"2")
435
Tim Petersa45cacf2004-08-20 03:47:14 +0000436 def test_realpath_resolve_parents(self):
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000437 # We also need to resolve any symlinks in the parents of a relative
438 # path passed to realpath. E.g.: current working directory is
439 # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
440 # realpath("a"). This should return /usr/share/doc/a/.
441 try:
442 old_path = abspath('.')
443 os.mkdir(ABSTFN)
444 os.mkdir(ABSTFN + "/y")
445 os.symlink(ABSTFN + "/y", ABSTFN + "/k")
446
447 os.chdir(ABSTFN + "/k")
448 self.assertEqual(realpath("a"), ABSTFN + "/y/a")
449 finally:
450 os.chdir(old_path)
451 self.safe_remove(ABSTFN + "/k")
452 self.safe_rmdir(ABSTFN + "/y")
453 self.safe_rmdir(ABSTFN)
454
455 def test_realpath_resolve_before_normalizing(self):
456 # Bug #990669: Symbolic links should be resolved before we
457 # normalize the path. E.g.: if we have directories 'a', 'k' and 'y'
458 # in the following hierarchy:
459 # a/k/y
460 #
461 # and a symbolic link 'link-y' pointing to 'y' in directory 'a',
462 # then realpath("link-y/..") should return 'k', not 'a'.
463 try:
464 old_path = abspath('.')
465 os.mkdir(ABSTFN)
466 os.mkdir(ABSTFN + "/k")
467 os.mkdir(ABSTFN + "/k/y")
468 os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y")
469
470 # Absolute path.
471 self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k")
472 # Relative path.
473 os.chdir(dirname(ABSTFN))
474 self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), ABSTFN + "/k")
475 finally:
476 os.chdir(old_path)
477 self.safe_remove(ABSTFN + "/link-y")
478 self.safe_rmdir(ABSTFN + "/k/y")
479 self.safe_rmdir(ABSTFN + "/k")
480 self.safe_rmdir(ABSTFN)
Tim Peters5d36a552005-06-03 22:40:27 +0000481
Georg Brandl268e61c2005-06-03 14:28:50 +0000482 def test_realpath_resolve_first(self):
483 # Bug #1213894: The first component of the path, if not absolute,
484 # must be resolved too.
485
486 try:
487 old_path = abspath('.')
488 os.mkdir(ABSTFN)
489 os.mkdir(ABSTFN + "/k")
490 os.symlink(ABSTFN, ABSTFN + "link")
491 os.chdir(dirname(ABSTFN))
Tim Peters5d36a552005-06-03 22:40:27 +0000492
Georg Brandl268e61c2005-06-03 14:28:50 +0000493 base = basename(ABSTFN)
494 self.assertEqual(realpath(base + "link"), ABSTFN)
495 self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
496 finally:
497 os.chdir(old_path)
498 self.safe_remove(ABSTFN + "link")
499 self.safe_rmdir(ABSTFN + "/k")
500 self.safe_rmdir(ABSTFN)
Johannes Gijsbers4ec40642004-08-14 15:01:53 +0000501
502 # Convenience functions for removing temporary files.
503 def pass_os_error(self, func, filename):
504 try: func(filename)
505 except OSError: pass
506
507 def safe_remove(self, filename):
508 self.pass_os_error(os.remove, filename)
509
510 def safe_rmdir(self, dirname):
511 self.pass_os_error(os.rmdir, dirname)
Brett Cannonb47243a2003-06-16 21:54:50 +0000512
513def test_main():
514 test_support.run_unittest(PosixPathTest)
515
516if __name__=="__main__":
517 test_main()