0001 import unittest 0002 from test import test_support 0003 0004 import posixpath, os 0005 from posixpath import realpath, abspath, join, dirname, basename 0006 0007 # An absolute path to a temporary filename for testing. We can't rely on TESTFN 0008 # being an absolute path, so we need this. 0009 0010 ABSTFN = abspath(test_support.TESTFN) 0011 0012 class PosixPathTest(unittest.TestCase): 0013 0014 def assertIs(self, a, b): 0015 self.assert_(a is b) 0016 0017 def test_normcase(self): 0018 # Check that normcase() is idempotent 0019 p = "FoO/./BaR" 0020 p = posixpath.normcase(p) 0021 self.assertEqual(p, posixpath.normcase(p)) 0022 0023 self.assertRaises(TypeError, posixpath.normcase) 0024 0025 def test_join(self): 0026 self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), "/bar/baz") 0027 self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz") 0028 self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), "/foo/bar/baz/") 0029 0030 self.assertRaises(TypeError, posixpath.join) 0031 0032 def test_splitdrive(self): 0033 self.assertEqual(posixpath.splitdrive("/foo/bar"), ("", "/foo/bar")) 0034 0035 self.assertRaises(TypeError, posixpath.splitdrive) 0036 0037 def test_split(self): 0038 self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar")) 0039 self.assertEqual(posixpath.split("/"), ("/", "")) 0040 self.assertEqual(posixpath.split("foo"), ("", "foo")) 0041 self.assertEqual(posixpath.split("////foo"), ("////", "foo")) 0042 self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar")) 0043 0044 self.assertRaises(TypeError, posixpath.split) 0045 0046 def test_splitext(self): 0047 self.assertEqual(posixpath.splitext("foo.ext"), ("foo", ".ext")) 0048 self.assertEqual(posixpath.splitext("/foo/foo.ext"), ("/foo/foo", ".ext")) 0049 self.assertEqual(posixpath.splitext(".ext"), ("", ".ext")) 0050 self.assertEqual(posixpath.splitext("/foo.ext/foo"), ("/foo.ext/foo", "")) 0051 self.assertEqual(posixpath.splitext("foo.ext/"), ("foo.ext/", "")) 0052 self.assertEqual(posixpath.splitext(""), ("", "")) 0053 self.assertEqual(posixpath.splitext("foo.bar.ext"), ("foo.bar", ".ext")) 0054 0055 self.assertRaises(TypeError, posixpath.splitext) 0056 0057 def test_isabs(self): 0058 self.assertIs(posixpath.isabs(""), False) 0059 self.assertIs(posixpath.isabs("/"), True) 0060 self.assertIs(posixpath.isabs("/foo"), True) 0061 self.assertIs(posixpath.isabs("/foo/bar"), True) 0062 self.assertIs(posixpath.isabs("foo/bar"), False) 0063 0064 self.assertRaises(TypeError, posixpath.isabs) 0065 0066 def test_splitdrive(self): 0067 self.assertEqual(posixpath.splitdrive("/foo/bar"), ("", "/foo/bar")) 0068 0069 self.assertRaises(TypeError, posixpath.splitdrive) 0070 0071 def test_basename(self): 0072 self.assertEqual(posixpath.basename("/foo/bar"), "bar") 0073 self.assertEqual(posixpath.basename("/"), "") 0074 self.assertEqual(posixpath.basename("foo"), "foo") 0075 self.assertEqual(posixpath.basename("////foo"), "foo") 0076 self.assertEqual(posixpath.basename("//foo//bar"), "bar") 0077 0078 self.assertRaises(TypeError, posixpath.basename) 0079 0080 def test_dirname(self): 0081 self.assertEqual(posixpath.dirname("/foo/bar"), "/foo") 0082 self.assertEqual(posixpath.dirname("/"), "/") 0083 self.assertEqual(posixpath.dirname("foo"), "") 0084 self.assertEqual(posixpath.dirname("////foo"), "////") 0085 self.assertEqual(posixpath.dirname("//foo//bar"), "//foo") 0086 0087 self.assertRaises(TypeError, posixpath.dirname) 0088 0089 def test_commonprefix(self): 0090 self.assertEqual( 0091 posixpath.commonprefix([]), 0092 "" 0093 ) 0094 self.assertEqual( 0095 posixpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"]), 0096 "/home/swen" 0097 ) 0098 self.assertEqual( 0099 posixpath.commonprefix(["/home/swen/spam", "/home/swen/eggs"]), 0100 "/home/swen/" 0101 ) 0102 self.assertEqual( 0103 posixpath.commonprefix(["/home/swen/spam", "/home/swen/spam"]), 0104 "/home/swen/spam" 0105 ) 0106 0107 def test_getsize(self): 0108 f = open(test_support.TESTFN, "wb") 0109 try: 0110 f.write("foo") 0111 f.close() 0112 self.assertEqual(posixpath.getsize(test_support.TESTFN), 3) 0113 finally: 0114 if not f.closed: 0115 f.close() 0116 os.remove(test_support.TESTFN) 0117 0118 def test_time(self): 0119 f = open(test_support.TESTFN, "wb") 0120 try: 0121 f.write("foo") 0122 f.close() 0123 f = open(test_support.TESTFN, "ab") 0124 f.write("bar") 0125 f.close() 0126 f = open(test_support.TESTFN, "rb") 0127 d = f.read() 0128 f.close() 0129 self.assertEqual(d, "foobar") 0130 0131 self.assert_( 0132 posixpath.getctime(test_support.TESTFN) <= 0133 posixpath.getmtime(test_support.TESTFN) 0134 ) 0135 finally: 0136 if not f.closed: 0137 f.close() 0138 os.remove(test_support.TESTFN) 0139 0140 def test_islink(self): 0141 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False) 0142 f = open(test_support.TESTFN + "1", "wb") 0143 try: 0144 f.write("foo") 0145 f.close() 0146 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False) 0147 if hasattr(os, "symlink"): 0148 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2") 0149 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True) 0150 os.remove(test_support.TESTFN + "1") 0151 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True) 0152 self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False) 0153 self.assertIs(posixpath.lexists(test_support.TESTFN + "2"), True) 0154 finally: 0155 if not f.close(): 0156 f.close() 0157 try: 0158 os.remove(test_support.TESTFN + "1") 0159 except os.error: 0160 pass 0161 try: 0162 os.remove(test_support.TESTFN + "2") 0163 except os.error: 0164 pass 0165 0166 self.assertRaises(TypeError, posixpath.islink) 0167 0168 def test_exists(self): 0169 self.assertIs(posixpath.exists(test_support.TESTFN), False) 0170 f = open(test_support.TESTFN, "wb") 0171 try: 0172 f.write("foo") 0173 f.close() 0174 self.assertIs(posixpath.exists(test_support.TESTFN), True) 0175 self.assertIs(posixpath.lexists(test_support.TESTFN), True) 0176 finally: 0177 if not f.close(): 0178 f.close() 0179 try: 0180 os.remove(test_support.TESTFN) 0181 except os.error: 0182 pass 0183 0184 self.assertRaises(TypeError, posixpath.exists) 0185 0186 def test_isdir(self): 0187 self.assertIs(posixpath.isdir(test_support.TESTFN), False) 0188 f = open(test_support.TESTFN, "wb") 0189 try: 0190 f.write("foo") 0191 f.close() 0192 self.assertIs(posixpath.isdir(test_support.TESTFN), False) 0193 os.remove(test_support.TESTFN) 0194 os.mkdir(test_support.TESTFN) 0195 self.assertIs(posixpath.isdir(test_support.TESTFN), True) 0196 os.rmdir(test_support.TESTFN) 0197 finally: 0198 if not f.close(): 0199 f.close() 0200 try: 0201 os.remove(test_support.TESTFN) 0202 except os.error: 0203 pass 0204 try: 0205 os.rmdir(test_support.TESTFN) 0206 except os.error: 0207 pass 0208 0209 self.assertRaises(TypeError, posixpath.isdir) 0210 0211 def test_isfile(self): 0212 self.assertIs(posixpath.isfile(test_support.TESTFN), False) 0213 f = open(test_support.TESTFN, "wb") 0214 try: 0215 f.write("foo") 0216 f.close() 0217 self.assertIs(posixpath.isfile(test_support.TESTFN), True) 0218 os.remove(test_support.TESTFN) 0219 os.mkdir(test_support.TESTFN) 0220 self.assertIs(posixpath.isfile(test_support.TESTFN), False) 0221 os.rmdir(test_support.TESTFN) 0222 finally: 0223 if not f.close(): 0224 f.close() 0225 try: 0226 os.remove(test_support.TESTFN) 0227 except os.error: 0228 pass 0229 try: 0230 os.rmdir(test_support.TESTFN) 0231 except os.error: 0232 pass 0233 0234 self.assertRaises(TypeError, posixpath.isdir) 0235 0236 def test_samefile(self): 0237 f = open(test_support.TESTFN + "1", "wb") 0238 try: 0239 f.write("foo") 0240 f.close() 0241 self.assertIs( 0242 posixpath.samefile( 0243 test_support.TESTFN + "1", 0244 test_support.TESTFN + "1" 0245 ), 0246 True 0247 ) 0248 # If we don't have links, assume that os.stat doesn't return resonable 0249 # inode information and thus, that samefile() doesn't work 0250 if hasattr(os, "symlink"): 0251 os.symlink( 0252 test_support.TESTFN + "1", 0253 test_support.TESTFN + "2" 0254 ) 0255 self.assertIs( 0256 posixpath.samefile( 0257 test_support.TESTFN + "1", 0258 test_support.TESTFN + "2" 0259 ), 0260 True 0261 ) 0262 os.remove(test_support.TESTFN + "2") 0263 f = open(test_support.TESTFN + "2", "wb") 0264 f.write("bar") 0265 f.close() 0266 self.assertIs( 0267 posixpath.samefile( 0268 test_support.TESTFN + "1", 0269 test_support.TESTFN + "2" 0270 ), 0271 False 0272 ) 0273 finally: 0274 if not f.close(): 0275 f.close() 0276 try: 0277 os.remove(test_support.TESTFN + "1") 0278 except os.error: 0279 pass 0280 try: 0281 os.remove(test_support.TESTFN + "2") 0282 except os.error: 0283 pass 0284 0285 self.assertRaises(TypeError, posixpath.samefile) 0286 0287 def test_samestat(self): 0288 f = open(test_support.TESTFN + "1", "wb") 0289 try: 0290 f.write("foo") 0291 f.close() 0292 self.assertIs( 0293 posixpath.samestat( 0294 os.stat(test_support.TESTFN + "1"), 0295 os.stat(test_support.TESTFN + "1") 0296 ), 0297 True 0298 ) 0299 # If we don't have links, assume that os.stat() doesn't return resonable 0300 # inode information and thus, that samefile() doesn't work 0301 if hasattr(os, "symlink"): 0302 if hasattr(os, "symlink"): 0303 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2") 0304 self.assertIs( 0305 posixpath.samestat( 0306 os.stat(test_support.TESTFN + "1"), 0307 os.stat(test_support.TESTFN + "2") 0308 ), 0309 True 0310 ) 0311 os.remove(test_support.TESTFN + "2") 0312 f = open(test_support.TESTFN + "2", "wb") 0313 f.write("bar") 0314 f.close() 0315 self.assertIs( 0316 posixpath.samestat( 0317 os.stat(test_support.TESTFN + "1"), 0318 os.stat(test_support.TESTFN + "2") 0319 ), 0320 False 0321 ) 0322 finally: 0323 if not f.close(): 0324 f.close() 0325 try: 0326 os.remove(test_support.TESTFN + "1") 0327 except os.error: 0328 pass 0329 try: 0330 os.remove(test_support.TESTFN + "2") 0331 except os.error: 0332 pass 0333 0334 self.assertRaises(TypeError, posixpath.samestat) 0335 0336 def test_ismount(self): 0337 self.assertIs(posixpath.ismount("/"), True) 0338 0339 self.assertRaises(TypeError, posixpath.ismount) 0340 0341 def test_expanduser(self): 0342 self.assertEqual(posixpath.expanduser("foo"), "foo") 0343 try: 0344 import pwd 0345 except ImportError: 0346 pass 0347 else: 0348 self.assert_(isinstance(posixpath.expanduser("~/"), basestring)) 0349 # if home directory == root directory, this test makes no sense 0350 if posixpath.expanduser("~") != '/': 0351 self.assertEqual( 0352 posixpath.expanduser("~") + "/", 0353 posixpath.expanduser("~/") 0354 ) 0355 self.assert_(isinstance(posixpath.expanduser("~root/"), basestring)) 0356 self.assert_(isinstance(posixpath.expanduser("~foo/"), basestring)) 0357 0358 self.assertRaises(TypeError, posixpath.expanduser) 0359 0360 def test_expandvars(self): 0361 oldenv = os.environ.copy() 0362 try: 0363 os.environ.clear() 0364 os.environ["foo"] = "bar" 0365 os.environ["{foo"] = "baz1" 0366 os.environ["{foo}"] = "baz2" 0367 self.assertEqual(posixpath.expandvars("foo"), "foo") 0368 self.assertEqual(posixpath.expandvars("$foo bar"), "bar bar") 0369 self.assertEqual(posixpath.expandvars("${foo}bar"), "barbar") 0370 self.assertEqual(posixpath.expandvars("$[foo]bar"), "$[foo]bar") 0371 self.assertEqual(posixpath.expandvars("$bar bar"), "$bar bar") 0372 self.assertEqual(posixpath.expandvars("$?bar"), "$?bar") 0373 self.assertEqual(posixpath.expandvars("${foo}bar"), "barbar") 0374 self.assertEqual(posixpath.expandvars("$foo}bar"), "bar}bar") 0375 self.assertEqual(posixpath.expandvars("${foo"), "${foo") 0376 self.assertEqual(posixpath.expandvars("${{foo}}"), "baz1}") 0377 finally: 0378 os.environ.clear() 0379 os.environ.update(oldenv) 0380 0381 self.assertRaises(TypeError, posixpath.expandvars) 0382 0383 def test_normpath(self): 0384 self.assertEqual(posixpath.normpath(""), ".") 0385 self.assertEqual(posixpath.normpath("/"), "/") 0386 self.assertEqual(posixpath.normpath("//"), "//") 0387 self.assertEqual(posixpath.normpath("///"), "/") 0388 self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar") 0389 self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz") 0390 self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar") 0391 0392 self.assertRaises(TypeError, posixpath.normpath) 0393 0394 def test_abspath(self): 0395 self.assert_("foo" in posixpath.abspath("foo")) 0396 0397 self.assertRaises(TypeError, posixpath.abspath) 0398 0399 def test_realpath(self): 0400 self.assert_("foo" in realpath("foo")) 0401 self.assertRaises(TypeError, posixpath.realpath) 0402 0403 if hasattr(os, "symlink"): 0404 def test_realpath_basic(self): 0405 # Basic operation. 0406 try: 0407 os.symlink(ABSTFN+"1", ABSTFN) 0408 self.assertEqual(realpath(ABSTFN), ABSTFN+"1") 0409 finally: 0410 self.safe_remove(ABSTFN) 0411 0412 def test_realpath_symlink_loops(self): 0413 # Bug #930024, return the path unchanged if we get into an infinite 0414 # symlink loop. 0415 try: 0416 old_path = abspath('.') 0417 os.symlink(ABSTFN, ABSTFN) 0418 self.assertEqual(realpath(ABSTFN), ABSTFN) 0419 0420 os.symlink(ABSTFN+"1", ABSTFN+"2") 0421 os.symlink(ABSTFN+"2", ABSTFN+"1") 0422 self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1") 0423 self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2") 0424 0425 # Test using relative path as well. 0426 os.chdir(dirname(ABSTFN)) 0427 self.assertEqual(realpath(basename(ABSTFN)), ABSTFN) 0428 finally: 0429 os.chdir(old_path) 0430 self.safe_remove(ABSTFN) 0431 self.safe_remove(ABSTFN+"1") 0432 self.safe_remove(ABSTFN+"2") 0433 0434 def test_realpath_resolve_parents(self): 0435 # We also need to resolve any symlinks in the parents of a relative 0436 # path passed to realpath. E.g.: current working directory is 0437 # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call 0438 # realpath("a"). This should return /usr/share/doc/a/. 0439 try: 0440 old_path = abspath('.') 0441 os.mkdir(ABSTFN) 0442 os.mkdir(ABSTFN + "/y") 0443 os.symlink(ABSTFN + "/y", ABSTFN + "/k") 0444 0445 os.chdir(ABSTFN + "/k") 0446 self.assertEqual(realpath("a"), ABSTFN + "/y/a") 0447 finally: 0448 os.chdir(old_path) 0449 self.safe_remove(ABSTFN + "/k") 0450 self.safe_rmdir(ABSTFN + "/y") 0451 self.safe_rmdir(ABSTFN) 0452 0453 def test_realpath_resolve_before_normalizing(self): 0454 # Bug #990669: Symbolic links should be resolved before we 0455 # normalize the path. E.g.: if we have directories 'a', 'k' and 'y' 0456 # in the following hierarchy: 0457 # a/k/y 0458 # 0459 # and a symbolic link 'link-y' pointing to 'y' in directory 'a', 0460 # then realpath("link-y/..") should return 'k', not 'a'. 0461 try: 0462 old_path = abspath('.') 0463 os.mkdir(ABSTFN) 0464 os.mkdir(ABSTFN + "/k") 0465 os.mkdir(ABSTFN + "/k/y") 0466 os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y") 0467 0468 # Absolute path. 0469 self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k") 0470 # Relative path. 0471 os.chdir(dirname(ABSTFN)) 0472 self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), ABSTFN + "/k") 0473 finally: 0474 os.chdir(old_path) 0475 self.safe_remove(ABSTFN + "/link-y") 0476 self.safe_rmdir(ABSTFN + "/k/y") 0477 self.safe_rmdir(ABSTFN + "/k") 0478 self.safe_rmdir(ABSTFN) 0479 0480 # Convenience functions for removing temporary files. 0481 def pass_os_error(self, func, filename): 0482 try: func(filename) 0483 except OSError: pass 0484 0485 def safe_remove(self, filename): 0486 self.pass_os_error(os.remove, filename) 0487 0488 def safe_rmdir(self, dirname): 0489 self.pass_os_error(os.rmdir, dirname) 0490 0491 def test_main(): 0492 test_support.run_unittest(PosixPathTest) 0493 0494 if __name__=="__main__": 0495 test_main() 0496
Generated by PyXR 0.9.4