Hynek Schlawack | c5a4566 | 2012-07-17 13:05:43 +0200 | [diff] [blame] | 1 | import itertools |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 2 | import os |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 3 | import posixpath |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4 | import sys |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 5 | import unittest |
| 6 | import warnings |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 7 | from posixpath import realpath, abspath, dirname, basename |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 8 | from test import support, test_genericpath |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 9 | |
Michael Foord | 07926f0 | 2011-03-16 17:19:16 -0400 | [diff] [blame] | 10 | try: |
| 11 | import posix |
| 12 | except ImportError: |
| 13 | posix = None |
| 14 | |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 15 | # An absolute path to a temporary filename for testing. We can't rely on TESTFN |
| 16 | # being an absolute path, so we need this. |
| 17 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 18 | ABSTFN = abspath(support.TESTFN) |
Skip Montanaro | e809b00 | 2000-07-12 00:20:08 +0000 | [diff] [blame] | 19 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 20 | def skip_if_ABSTFN_contains_backslash(test): |
| 21 | """ |
| 22 | On Windows, posixpath.abspath still returns paths with backslashes |
| 23 | instead of posix forward slashes. If this is the case, several tests |
| 24 | fail, so skip them. |
| 25 | """ |
| 26 | found_backslash = '\\' in ABSTFN |
| 27 | msg = "ABSTFN is not a posix path - tests fail" |
| 28 | return [test, unittest.skip(msg)(test)][found_backslash] |
| 29 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 30 | def safe_rmdir(dirname): |
| 31 | try: |
| 32 | os.rmdir(dirname) |
| 33 | except OSError: |
| 34 | pass |
| 35 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 36 | class PosixPathTest(unittest.TestCase): |
Skip Montanaro | e809b00 | 2000-07-12 00:20:08 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 38 | def setUp(self): |
| 39 | self.tearDown() |
| 40 | |
| 41 | def tearDown(self): |
| 42 | for suffix in ["", "1", "2"]: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 43 | support.unlink(support.TESTFN + suffix) |
| 44 | safe_rmdir(support.TESTFN + suffix) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 45 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 46 | def test_join(self): |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 47 | self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), |
| 48 | "/bar/baz") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 49 | self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 50 | self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), |
| 51 | "/foo/bar/baz/") |
| 52 | |
| 53 | self.assertEqual(posixpath.join(b"/foo", b"bar", b"/bar", b"baz"), |
| 54 | b"/bar/baz") |
| 55 | self.assertEqual(posixpath.join(b"/foo", b"bar", b"baz"), |
| 56 | b"/foo/bar/baz") |
| 57 | self.assertEqual(posixpath.join(b"/foo/", b"bar/", b"baz/"), |
| 58 | b"/foo/bar/baz/") |
Skip Montanaro | e809b00 | 2000-07-12 00:20:08 +0000 | [diff] [blame] | 59 | |
Hynek Schlawack | c5a4566 | 2012-07-17 13:05:43 +0200 | [diff] [blame] | 60 | def check_error_msg(list_of_args, msg): |
| 61 | """Check posixpath.join raises friendly TypeErrors.""" |
| 62 | for args in (item for perm in list_of_args |
| 63 | for item in itertools.permutations(perm)): |
Hynek Schlawack | 7cdc2bd | 2012-07-17 10:48:19 +0200 | [diff] [blame] | 64 | with self.assertRaises(TypeError) as cm: |
| 65 | posixpath.join(*args) |
Hynek Schlawack | c5a4566 | 2012-07-17 13:05:43 +0200 | [diff] [blame] | 66 | self.assertEqual(msg, cm.exception.args[0]) |
Hynek Schlawack | 7cdc2bd | 2012-07-17 10:48:19 +0200 | [diff] [blame] | 67 | |
Hynek Schlawack | c5a4566 | 2012-07-17 13:05:43 +0200 | [diff] [blame] | 68 | check_error_msg([[b'bytes', 'str'], [bytearray(b'bytes'), 'str']], |
| 69 | "Can't mix strings and bytes in path components.") |
| 70 | # regression, see #15377 |
| 71 | with self.assertRaises(TypeError) as cm: |
Hynek Schlawack | 0b350c6 | 2012-07-17 14:28:44 +0200 | [diff] [blame] | 72 | posixpath.join(None, 'str') |
Hynek Schlawack | c5a4566 | 2012-07-17 13:05:43 +0200 | [diff] [blame] | 73 | self.assertNotEqual("Can't mix strings and bytes in path components.", |
| 74 | cm.exception.args[0]) |
Skip Montanaro | e809b00 | 2000-07-12 00:20:08 +0000 | [diff] [blame] | 75 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 76 | def test_split(self): |
| 77 | self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar")) |
| 78 | self.assertEqual(posixpath.split("/"), ("/", "")) |
| 79 | self.assertEqual(posixpath.split("foo"), ("", "foo")) |
| 80 | self.assertEqual(posixpath.split("////foo"), ("////", "foo")) |
| 81 | self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar")) |
| 82 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 83 | self.assertEqual(posixpath.split(b"/foo/bar"), (b"/foo", b"bar")) |
| 84 | self.assertEqual(posixpath.split(b"/"), (b"/", b"")) |
| 85 | self.assertEqual(posixpath.split(b"foo"), (b"", b"foo")) |
| 86 | self.assertEqual(posixpath.split(b"////foo"), (b"////", b"foo")) |
| 87 | self.assertEqual(posixpath.split(b"//foo//bar"), (b"//foo", b"bar")) |
| 88 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 89 | def splitextTest(self, path, filename, ext): |
| 90 | self.assertEqual(posixpath.splitext(path), (filename, ext)) |
| 91 | self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext)) |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 92 | self.assertEqual(posixpath.splitext("abc/" + path), |
| 93 | ("abc/" + filename, ext)) |
| 94 | self.assertEqual(posixpath.splitext("abc.def/" + path), |
| 95 | ("abc.def/" + filename, ext)) |
| 96 | self.assertEqual(posixpath.splitext("/abc.def/" + path), |
| 97 | ("/abc.def/" + filename, ext)) |
| 98 | self.assertEqual(posixpath.splitext(path + "/"), |
| 99 | (filename + ext + "/", "")) |
| 100 | |
| 101 | path = bytes(path, "ASCII") |
| 102 | filename = bytes(filename, "ASCII") |
| 103 | ext = bytes(ext, "ASCII") |
| 104 | |
| 105 | self.assertEqual(posixpath.splitext(path), (filename, ext)) |
| 106 | self.assertEqual(posixpath.splitext(b"/" + path), |
| 107 | (b"/" + filename, ext)) |
| 108 | self.assertEqual(posixpath.splitext(b"abc/" + path), |
| 109 | (b"abc/" + filename, ext)) |
| 110 | self.assertEqual(posixpath.splitext(b"abc.def/" + path), |
| 111 | (b"abc.def/" + filename, ext)) |
| 112 | self.assertEqual(posixpath.splitext(b"/abc.def/" + path), |
| 113 | (b"/abc.def/" + filename, ext)) |
| 114 | self.assertEqual(posixpath.splitext(path + b"/"), |
| 115 | (filename + ext + b"/", b"")) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 117 | def test_splitext(self): |
| 118 | self.splitextTest("foo.bar", "foo", ".bar") |
| 119 | self.splitextTest("foo.boo.bar", "foo.boo", ".bar") |
| 120 | self.splitextTest("foo.boo.biff.bar", "foo.boo.biff", ".bar") |
| 121 | self.splitextTest(".csh.rc", ".csh", ".rc") |
| 122 | self.splitextTest("nodots", "nodots", "") |
| 123 | self.splitextTest(".cshrc", ".cshrc", "") |
| 124 | self.splitextTest("...manydots", "...manydots", "") |
| 125 | self.splitextTest("...manydots.ext", "...manydots", ".ext") |
| 126 | self.splitextTest(".", ".", "") |
| 127 | self.splitextTest("..", "..", "") |
| 128 | self.splitextTest("........", "........", "") |
| 129 | self.splitextTest("", "", "") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 130 | |
| 131 | def test_isabs(self): |
| 132 | self.assertIs(posixpath.isabs(""), False) |
| 133 | self.assertIs(posixpath.isabs("/"), True) |
| 134 | self.assertIs(posixpath.isabs("/foo"), True) |
| 135 | self.assertIs(posixpath.isabs("/foo/bar"), True) |
| 136 | self.assertIs(posixpath.isabs("foo/bar"), False) |
| 137 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 138 | self.assertIs(posixpath.isabs(b""), False) |
| 139 | self.assertIs(posixpath.isabs(b"/"), True) |
| 140 | self.assertIs(posixpath.isabs(b"/foo"), True) |
| 141 | self.assertIs(posixpath.isabs(b"/foo/bar"), True) |
| 142 | self.assertIs(posixpath.isabs(b"foo/bar"), False) |
| 143 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 144 | def test_basename(self): |
| 145 | self.assertEqual(posixpath.basename("/foo/bar"), "bar") |
| 146 | self.assertEqual(posixpath.basename("/"), "") |
| 147 | self.assertEqual(posixpath.basename("foo"), "foo") |
| 148 | self.assertEqual(posixpath.basename("////foo"), "foo") |
| 149 | self.assertEqual(posixpath.basename("//foo//bar"), "bar") |
| 150 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 151 | self.assertEqual(posixpath.basename(b"/foo/bar"), b"bar") |
| 152 | self.assertEqual(posixpath.basename(b"/"), b"") |
| 153 | self.assertEqual(posixpath.basename(b"foo"), b"foo") |
| 154 | self.assertEqual(posixpath.basename(b"////foo"), b"foo") |
| 155 | self.assertEqual(posixpath.basename(b"//foo//bar"), b"bar") |
| 156 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 157 | def test_dirname(self): |
| 158 | self.assertEqual(posixpath.dirname("/foo/bar"), "/foo") |
| 159 | self.assertEqual(posixpath.dirname("/"), "/") |
| 160 | self.assertEqual(posixpath.dirname("foo"), "") |
| 161 | self.assertEqual(posixpath.dirname("////foo"), "////") |
| 162 | self.assertEqual(posixpath.dirname("//foo//bar"), "//foo") |
| 163 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 164 | self.assertEqual(posixpath.dirname(b"/foo/bar"), b"/foo") |
| 165 | self.assertEqual(posixpath.dirname(b"/"), b"/") |
| 166 | self.assertEqual(posixpath.dirname(b"foo"), b"") |
| 167 | self.assertEqual(posixpath.dirname(b"////foo"), b"////") |
| 168 | self.assertEqual(posixpath.dirname(b"//foo//bar"), b"//foo") |
| 169 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 170 | def test_islink(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 171 | self.assertIs(posixpath.islink(support.TESTFN + "1"), False) |
Michael Foord | 07926f0 | 2011-03-16 17:19:16 -0400 | [diff] [blame] | 172 | self.assertIs(posixpath.lexists(support.TESTFN + "2"), False) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 173 | f = open(support.TESTFN + "1", "wb") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 174 | try: |
Guido van Rossum | 7dcb844 | 2007-08-27 23:26:56 +0000 | [diff] [blame] | 175 | f.write(b"foo") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 176 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 177 | self.assertIs(posixpath.islink(support.TESTFN + "1"), False) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 178 | if support.can_symlink(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 179 | os.symlink(support.TESTFN + "1", support.TESTFN + "2") |
| 180 | self.assertIs(posixpath.islink(support.TESTFN + "2"), True) |
| 181 | os.remove(support.TESTFN + "1") |
| 182 | self.assertIs(posixpath.islink(support.TESTFN + "2"), True) |
| 183 | self.assertIs(posixpath.exists(support.TESTFN + "2"), False) |
| 184 | self.assertIs(posixpath.lexists(support.TESTFN + "2"), True) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 185 | finally: |
| 186 | if not f.close(): |
| 187 | f.close() |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 188 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 189 | def test_ismount(self): |
| 190 | self.assertIs(posixpath.ismount("/"), True) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 191 | with warnings.catch_warnings(): |
| 192 | warnings.simplefilter("ignore", DeprecationWarning) |
| 193 | self.assertIs(posixpath.ismount(b"/"), True) |
Michael Foord | 07926f0 | 2011-03-16 17:19:16 -0400 | [diff] [blame] | 194 | |
| 195 | def test_ismount_non_existent(self): |
| 196 | # Non-existent mountpoint. |
| 197 | self.assertIs(posixpath.ismount(ABSTFN), False) |
| 198 | try: |
| 199 | os.mkdir(ABSTFN) |
| 200 | self.assertIs(posixpath.ismount(ABSTFN), False) |
| 201 | finally: |
| 202 | safe_rmdir(ABSTFN) |
| 203 | |
| 204 | @unittest.skipUnless(support.can_symlink(), |
| 205 | "Test requires symlink support") |
| 206 | def test_ismount_symlinks(self): |
| 207 | # Symlinks are never mountpoints. |
| 208 | try: |
| 209 | os.symlink("/", ABSTFN) |
| 210 | self.assertIs(posixpath.ismount(ABSTFN), False) |
| 211 | finally: |
| 212 | os.unlink(ABSTFN) |
| 213 | |
| 214 | @unittest.skipIf(posix is None, "Test requires posix module") |
| 215 | def test_ismount_different_device(self): |
| 216 | # Simulate the path being on a different device from its parent by |
| 217 | # mocking out st_dev. |
| 218 | save_lstat = os.lstat |
| 219 | def fake_lstat(path): |
| 220 | st_ino = 0 |
| 221 | st_dev = 0 |
| 222 | if path == ABSTFN: |
| 223 | st_dev = 1 |
| 224 | st_ino = 1 |
| 225 | return posix.stat_result((0, st_ino, st_dev, 0, 0, 0, 0, 0, 0, 0)) |
| 226 | try: |
| 227 | os.lstat = fake_lstat |
| 228 | self.assertIs(posixpath.ismount(ABSTFN), True) |
| 229 | finally: |
| 230 | os.lstat = save_lstat |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 231 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 232 | def test_expanduser(self): |
| 233 | self.assertEqual(posixpath.expanduser("foo"), "foo") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 234 | self.assertEqual(posixpath.expanduser(b"foo"), b"foo") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 235 | try: |
| 236 | import pwd |
| 237 | except ImportError: |
| 238 | pass |
| 239 | else: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 240 | self.assertIsInstance(posixpath.expanduser("~/"), str) |
| 241 | self.assertIsInstance(posixpath.expanduser(b"~/"), bytes) |
Neal Norwitz | 168e73d | 2003-07-01 03:33:31 +0000 | [diff] [blame] | 242 | # if home directory == root directory, this test makes no sense |
| 243 | if posixpath.expanduser("~") != '/': |
| 244 | self.assertEqual( |
| 245 | posixpath.expanduser("~") + "/", |
| 246 | posixpath.expanduser("~/") |
| 247 | ) |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 248 | self.assertEqual( |
| 249 | posixpath.expanduser(b"~") + b"/", |
| 250 | posixpath.expanduser(b"~/") |
| 251 | ) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 252 | self.assertIsInstance(posixpath.expanduser("~root/"), str) |
| 253 | self.assertIsInstance(posixpath.expanduser("~foo/"), str) |
| 254 | self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes) |
| 255 | self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 256 | |
Hirokazu Yamamoto | 7195963 | 2009-04-27 01:44:28 +0000 | [diff] [blame] | 257 | with support.EnvironmentVarGuard() as env: |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 258 | env['HOME'] = '/' |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 259 | self.assertEqual(posixpath.expanduser("~"), "/") |
Jesus Cea | 7f0d888 | 2012-05-10 05:10:50 +0200 | [diff] [blame] | 260 | self.assertEqual(posixpath.expanduser("~/foo"), "/foo") |
Michael Foord | 07926f0 | 2011-03-16 17:19:16 -0400 | [diff] [blame] | 261 | # expanduser should fall back to using the password database |
| 262 | del env['HOME'] |
| 263 | home = pwd.getpwuid(os.getuid()).pw_dir |
| 264 | self.assertEqual(posixpath.expanduser("~"), home) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 265 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 266 | def test_normpath(self): |
| 267 | self.assertEqual(posixpath.normpath(""), ".") |
| 268 | self.assertEqual(posixpath.normpath("/"), "/") |
| 269 | self.assertEqual(posixpath.normpath("//"), "//") |
| 270 | self.assertEqual(posixpath.normpath("///"), "/") |
| 271 | self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 272 | self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), |
| 273 | "/foo/baz") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 274 | self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar") |
| 275 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 276 | self.assertEqual(posixpath.normpath(b""), b".") |
| 277 | self.assertEqual(posixpath.normpath(b"/"), b"/") |
| 278 | self.assertEqual(posixpath.normpath(b"//"), b"//") |
| 279 | self.assertEqual(posixpath.normpath(b"///"), b"/") |
| 280 | self.assertEqual(posixpath.normpath(b"///foo/.//bar//"), b"/foo/bar") |
| 281 | self.assertEqual(posixpath.normpath(b"///foo/.//bar//.//..//.//baz"), |
| 282 | b"/foo/baz") |
| 283 | self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"), |
| 284 | b"/foo/bar") |
| 285 | |
Serhiy Storchaka | 1548ed6 | 2013-02-18 13:32:30 +0200 | [diff] [blame] | 286 | @skip_if_ABSTFN_contains_backslash |
Serhiy Storchaka | 467393d | 2013-02-18 12:21:04 +0200 | [diff] [blame] | 287 | def test_realpath_curdir(self): |
| 288 | self.assertEqual(realpath('.'), os.getcwd()) |
| 289 | self.assertEqual(realpath('./.'), os.getcwd()) |
| 290 | self.assertEqual(realpath('/'.join(['.'] * 100)), os.getcwd()) |
| 291 | |
| 292 | self.assertEqual(realpath(b'.'), os.getcwdb()) |
| 293 | self.assertEqual(realpath(b'./.'), os.getcwdb()) |
| 294 | self.assertEqual(realpath(b'/'.join([b'.'] * 100)), os.getcwdb()) |
| 295 | |
Serhiy Storchaka | 1548ed6 | 2013-02-18 13:32:30 +0200 | [diff] [blame] | 296 | @skip_if_ABSTFN_contains_backslash |
Serhiy Storchaka | 467393d | 2013-02-18 12:21:04 +0200 | [diff] [blame] | 297 | def test_realpath_pardir(self): |
| 298 | self.assertEqual(realpath('..'), dirname(os.getcwd())) |
| 299 | self.assertEqual(realpath('../..'), dirname(dirname(os.getcwd()))) |
| 300 | self.assertEqual(realpath('/'.join(['..'] * 100)), '/') |
| 301 | |
| 302 | self.assertEqual(realpath(b'..'), dirname(os.getcwdb())) |
| 303 | self.assertEqual(realpath(b'../..'), dirname(dirname(os.getcwdb()))) |
| 304 | self.assertEqual(realpath(b'/'.join([b'..'] * 100)), b'/') |
| 305 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 306 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 307 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 308 | @skip_if_ABSTFN_contains_backslash |
| 309 | def test_realpath_basic(self): |
| 310 | # Basic operation. |
| 311 | try: |
| 312 | os.symlink(ABSTFN+"1", ABSTFN) |
| 313 | self.assertEqual(realpath(ABSTFN), ABSTFN+"1") |
| 314 | finally: |
| 315 | support.unlink(ABSTFN) |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 316 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 317 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 318 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 319 | @skip_if_ABSTFN_contains_backslash |
Michael Foord | 07926f0 | 2011-03-16 17:19:16 -0400 | [diff] [blame] | 320 | def test_realpath_relative(self): |
| 321 | try: |
| 322 | os.symlink(posixpath.relpath(ABSTFN+"1"), ABSTFN) |
| 323 | self.assertEqual(realpath(ABSTFN), ABSTFN+"1") |
| 324 | finally: |
| 325 | support.unlink(ABSTFN) |
| 326 | |
| 327 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 328 | "Missing symlink implementation") |
| 329 | @skip_if_ABSTFN_contains_backslash |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 330 | def test_realpath_symlink_loops(self): |
| 331 | # Bug #930024, return the path unchanged if we get into an infinite |
| 332 | # symlink loop. |
| 333 | try: |
| 334 | old_path = abspath('.') |
| 335 | os.symlink(ABSTFN, ABSTFN) |
| 336 | self.assertEqual(realpath(ABSTFN), ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 337 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 338 | os.symlink(ABSTFN+"1", ABSTFN+"2") |
| 339 | os.symlink(ABSTFN+"2", ABSTFN+"1") |
| 340 | self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1") |
| 341 | self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 342 | |
Serhiy Storchaka | df32691 | 2013-02-10 12:22:07 +0200 | [diff] [blame] | 343 | self.assertEqual(realpath(ABSTFN+"1/x"), ABSTFN+"1/x") |
| 344 | self.assertEqual(realpath(ABSTFN+"1/.."), dirname(ABSTFN)) |
| 345 | self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x") |
| 346 | os.symlink(ABSTFN+"x", ABSTFN+"y") |
| 347 | self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"), |
| 348 | ABSTFN + "y") |
| 349 | self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"), |
| 350 | ABSTFN + "1") |
| 351 | |
| 352 | os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a") |
| 353 | self.assertEqual(realpath(ABSTFN+"a"), ABSTFN+"a/b") |
| 354 | |
| 355 | os.symlink("../" + basename(dirname(ABSTFN)) + "/" + |
| 356 | basename(ABSTFN) + "c", ABSTFN+"c") |
| 357 | self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c") |
| 358 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 359 | # Test using relative path as well. |
| 360 | os.chdir(dirname(ABSTFN)) |
| 361 | self.assertEqual(realpath(basename(ABSTFN)), ABSTFN) |
| 362 | finally: |
| 363 | os.chdir(old_path) |
| 364 | support.unlink(ABSTFN) |
| 365 | support.unlink(ABSTFN+"1") |
| 366 | support.unlink(ABSTFN+"2") |
Serhiy Storchaka | df32691 | 2013-02-10 12:22:07 +0200 | [diff] [blame] | 367 | support.unlink(ABSTFN+"y") |
| 368 | support.unlink(ABSTFN+"c") |
Ezio Melotti | 136726c | 2013-03-01 20:59:17 +0200 | [diff] [blame] | 369 | support.unlink(ABSTFN+"a") |
Serhiy Storchaka | df32691 | 2013-02-10 12:22:07 +0200 | [diff] [blame] | 370 | |
| 371 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 372 | "Missing symlink implementation") |
| 373 | @skip_if_ABSTFN_contains_backslash |
| 374 | def test_realpath_repeated_indirect_symlinks(self): |
| 375 | # Issue #6975. |
| 376 | try: |
| 377 | os.mkdir(ABSTFN) |
| 378 | os.symlink('../' + basename(ABSTFN), ABSTFN + '/self') |
| 379 | os.symlink('self/self/self', ABSTFN + '/link') |
| 380 | self.assertEqual(realpath(ABSTFN + '/link'), ABSTFN) |
| 381 | finally: |
| 382 | support.unlink(ABSTFN + '/self') |
| 383 | support.unlink(ABSTFN + '/link') |
| 384 | safe_rmdir(ABSTFN) |
| 385 | |
| 386 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 387 | "Missing symlink implementation") |
| 388 | @skip_if_ABSTFN_contains_backslash |
| 389 | def test_realpath_deep_recursion(self): |
| 390 | depth = 10 |
| 391 | old_path = abspath('.') |
| 392 | try: |
| 393 | os.mkdir(ABSTFN) |
| 394 | for i in range(depth): |
| 395 | os.symlink('/'.join(['%d' % i] * 10), ABSTFN + '/%d' % (i + 1)) |
| 396 | os.symlink('.', ABSTFN + '/0') |
| 397 | self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN) |
| 398 | |
| 399 | # Test using relative path as well. |
| 400 | os.chdir(ABSTFN) |
| 401 | self.assertEqual(realpath('%d' % depth), ABSTFN) |
| 402 | finally: |
| 403 | os.chdir(old_path) |
| 404 | for i in range(depth + 1): |
| 405 | support.unlink(ABSTFN + '/%d' % i) |
| 406 | safe_rmdir(ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 407 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 408 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 409 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 410 | @skip_if_ABSTFN_contains_backslash |
| 411 | def test_realpath_resolve_parents(self): |
| 412 | # We also need to resolve any symlinks in the parents of a relative |
| 413 | # path passed to realpath. E.g.: current working directory is |
| 414 | # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call |
| 415 | # realpath("a"). This should return /usr/share/doc/a/. |
| 416 | try: |
| 417 | old_path = abspath('.') |
| 418 | os.mkdir(ABSTFN) |
| 419 | os.mkdir(ABSTFN + "/y") |
| 420 | os.symlink(ABSTFN + "/y", ABSTFN + "/k") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 421 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 422 | os.chdir(ABSTFN + "/k") |
| 423 | self.assertEqual(realpath("a"), ABSTFN + "/y/a") |
| 424 | finally: |
| 425 | os.chdir(old_path) |
| 426 | support.unlink(ABSTFN + "/k") |
| 427 | safe_rmdir(ABSTFN + "/y") |
| 428 | safe_rmdir(ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 429 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 430 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 431 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 432 | @skip_if_ABSTFN_contains_backslash |
| 433 | def test_realpath_resolve_before_normalizing(self): |
| 434 | # Bug #990669: Symbolic links should be resolved before we |
| 435 | # normalize the path. E.g.: if we have directories 'a', 'k' and 'y' |
| 436 | # in the following hierarchy: |
| 437 | # a/k/y |
| 438 | # |
| 439 | # and a symbolic link 'link-y' pointing to 'y' in directory 'a', |
| 440 | # then realpath("link-y/..") should return 'k', not 'a'. |
| 441 | try: |
| 442 | old_path = abspath('.') |
| 443 | os.mkdir(ABSTFN) |
| 444 | os.mkdir(ABSTFN + "/k") |
| 445 | os.mkdir(ABSTFN + "/k/y") |
| 446 | os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 447 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 448 | # Absolute path. |
| 449 | self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k") |
| 450 | # Relative path. |
| 451 | os.chdir(dirname(ABSTFN)) |
| 452 | self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), |
| 453 | ABSTFN + "/k") |
| 454 | finally: |
| 455 | os.chdir(old_path) |
| 456 | support.unlink(ABSTFN + "/link-y") |
| 457 | safe_rmdir(ABSTFN + "/k/y") |
| 458 | safe_rmdir(ABSTFN + "/k") |
| 459 | safe_rmdir(ABSTFN) |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 460 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 461 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 462 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 463 | @skip_if_ABSTFN_contains_backslash |
| 464 | def test_realpath_resolve_first(self): |
| 465 | # Bug #1213894: The first component of the path, if not absolute, |
| 466 | # must be resolved too. |
Georg Brandl | 268e61c | 2005-06-03 14:28:50 +0000 | [diff] [blame] | 467 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 468 | try: |
| 469 | old_path = abspath('.') |
| 470 | os.mkdir(ABSTFN) |
| 471 | os.mkdir(ABSTFN + "/k") |
| 472 | os.symlink(ABSTFN, ABSTFN + "link") |
| 473 | os.chdir(dirname(ABSTFN)) |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 474 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 475 | base = basename(ABSTFN) |
| 476 | self.assertEqual(realpath(base + "link"), ABSTFN) |
| 477 | self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k") |
| 478 | finally: |
| 479 | os.chdir(old_path) |
| 480 | support.unlink(ABSTFN + "link") |
| 481 | safe_rmdir(ABSTFN + "/k") |
| 482 | safe_rmdir(ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 483 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 484 | def test_relpath(self): |
| 485 | (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar") |
| 486 | try: |
| 487 | curdir = os.path.split(os.getcwd())[-1] |
| 488 | self.assertRaises(ValueError, posixpath.relpath, "") |
| 489 | self.assertEqual(posixpath.relpath("a"), "a") |
| 490 | self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a") |
| 491 | self.assertEqual(posixpath.relpath("a/b"), "a/b") |
| 492 | self.assertEqual(posixpath.relpath("../a/b"), "../a/b") |
| 493 | self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 494 | self.assertEqual(posixpath.relpath("a/b", "../c"), |
| 495 | "../"+curdir+"/a/b") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 496 | self.assertEqual(posixpath.relpath("a", "b/c"), "../../a") |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 497 | self.assertEqual(posixpath.relpath("a", "a"), ".") |
Hirokazu Yamamoto | b08820a | 2010-10-18 12:13:18 +0000 | [diff] [blame] | 498 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat') |
| 499 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/foo/bar"), 'bat') |
| 500 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/"), 'foo/bar/bat') |
| 501 | self.assertEqual(posixpath.relpath("/", "/foo/bar/bat"), '../../..') |
| 502 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x"), '../foo/bar/bat') |
| 503 | self.assertEqual(posixpath.relpath("/x", "/foo/bar/bat"), '../../../x') |
| 504 | self.assertEqual(posixpath.relpath("/", "/"), '.') |
| 505 | self.assertEqual(posixpath.relpath("/a", "/a"), '.') |
| 506 | self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 507 | finally: |
| 508 | os.getcwd = real_getcwd |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 509 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 510 | def test_relpath_bytes(self): |
| 511 | (real_getcwdb, os.getcwdb) = (os.getcwdb, lambda: br"/home/user/bar") |
| 512 | try: |
| 513 | curdir = os.path.split(os.getcwdb())[-1] |
| 514 | self.assertRaises(ValueError, posixpath.relpath, b"") |
| 515 | self.assertEqual(posixpath.relpath(b"a"), b"a") |
| 516 | self.assertEqual(posixpath.relpath(posixpath.abspath(b"a")), b"a") |
| 517 | self.assertEqual(posixpath.relpath(b"a/b"), b"a/b") |
| 518 | self.assertEqual(posixpath.relpath(b"../a/b"), b"../a/b") |
| 519 | self.assertEqual(posixpath.relpath(b"a", b"../b"), |
| 520 | b"../"+curdir+b"/a") |
| 521 | self.assertEqual(posixpath.relpath(b"a/b", b"../c"), |
| 522 | b"../"+curdir+b"/a/b") |
| 523 | self.assertEqual(posixpath.relpath(b"a", b"b/c"), b"../../a") |
| 524 | self.assertEqual(posixpath.relpath(b"a", b"a"), b".") |
Hirokazu Yamamoto | b08820a | 2010-10-18 12:13:18 +0000 | [diff] [blame] | 525 | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x/y/z"), b'../../../foo/bar/bat') |
| 526 | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/foo/bar"), b'bat') |
| 527 | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/"), b'foo/bar/bat') |
| 528 | self.assertEqual(posixpath.relpath(b"/", b"/foo/bar/bat"), b'../../..') |
| 529 | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x"), b'../foo/bar/bat') |
| 530 | self.assertEqual(posixpath.relpath(b"/x", b"/foo/bar/bat"), b'../../../x') |
| 531 | self.assertEqual(posixpath.relpath(b"/", b"/"), b'.') |
| 532 | self.assertEqual(posixpath.relpath(b"/a", b"/a"), b'.') |
| 533 | self.assertEqual(posixpath.relpath(b"/a/b", b"/a/b"), b'.') |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 534 | |
| 535 | self.assertRaises(TypeError, posixpath.relpath, b"bytes", "str") |
| 536 | self.assertRaises(TypeError, posixpath.relpath, "str", b"bytes") |
| 537 | finally: |
| 538 | os.getcwdb = real_getcwdb |
| 539 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 540 | |
Ezio Melotti | d0dfe9a | 2013-01-10 03:12:50 +0200 | [diff] [blame] | 541 | class PosixCommonTest(test_genericpath.CommonTest, unittest.TestCase): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 542 | pathmodule = posixpath |
| 543 | attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat'] |
| 544 | |
| 545 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 546 | if __name__=="__main__": |
Ezio Melotti | d0dfe9a | 2013-01-10 03:12:50 +0200 | [diff] [blame] | 547 | unittest.main() |