Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 1 | import unittest |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 2 | from test import support, test_genericpath |
Skip Montanaro | e809b00 | 2000-07-12 00:20:08 +0000 | [diff] [blame] | 3 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4 | import posixpath |
| 5 | import os |
| 6 | import sys |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 7 | from posixpath import realpath, abspath, dirname, basename |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 8 | |
| 9 | # An absolute path to a temporary filename for testing. We can't rely on TESTFN |
| 10 | # being an absolute path, so we need this. |
| 11 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 12 | ABSTFN = abspath(support.TESTFN) |
Skip Montanaro | e809b00 | 2000-07-12 00:20:08 +0000 | [diff] [blame] | 13 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 14 | def skip_if_ABSTFN_contains_backslash(test): |
| 15 | """ |
| 16 | On Windows, posixpath.abspath still returns paths with backslashes |
| 17 | instead of posix forward slashes. If this is the case, several tests |
| 18 | fail, so skip them. |
| 19 | """ |
| 20 | found_backslash = '\\' in ABSTFN |
| 21 | msg = "ABSTFN is not a posix path - tests fail" |
| 22 | return [test, unittest.skip(msg)(test)][found_backslash] |
| 23 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 24 | def safe_rmdir(dirname): |
| 25 | try: |
| 26 | os.rmdir(dirname) |
| 27 | except OSError: |
| 28 | pass |
| 29 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 30 | class PosixPathTest(unittest.TestCase): |
Skip Montanaro | e809b00 | 2000-07-12 00:20:08 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 32 | def setUp(self): |
| 33 | self.tearDown() |
| 34 | |
| 35 | def tearDown(self): |
| 36 | for suffix in ["", "1", "2"]: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 37 | support.unlink(support.TESTFN + suffix) |
| 38 | safe_rmdir(support.TESTFN + suffix) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 39 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 40 | def test_join(self): |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 41 | self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), |
| 42 | "/bar/baz") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 43 | self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 44 | self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), |
| 45 | "/foo/bar/baz/") |
| 46 | |
| 47 | self.assertEqual(posixpath.join(b"/foo", b"bar", b"/bar", b"baz"), |
| 48 | b"/bar/baz") |
| 49 | self.assertEqual(posixpath.join(b"/foo", b"bar", b"baz"), |
| 50 | b"/foo/bar/baz") |
| 51 | self.assertEqual(posixpath.join(b"/foo/", b"bar/", b"baz/"), |
| 52 | b"/foo/bar/baz/") |
Skip Montanaro | e809b00 | 2000-07-12 00:20:08 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 54 | self.assertRaises(TypeError, posixpath.join, b"bytes", "str") |
| 55 | self.assertRaises(TypeError, posixpath.join, "str", b"bytes") |
Skip Montanaro | e809b00 | 2000-07-12 00:20:08 +0000 | [diff] [blame] | 56 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 57 | def test_split(self): |
| 58 | self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar")) |
| 59 | self.assertEqual(posixpath.split("/"), ("/", "")) |
| 60 | self.assertEqual(posixpath.split("foo"), ("", "foo")) |
| 61 | self.assertEqual(posixpath.split("////foo"), ("////", "foo")) |
| 62 | self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar")) |
| 63 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 64 | self.assertEqual(posixpath.split(b"/foo/bar"), (b"/foo", b"bar")) |
| 65 | self.assertEqual(posixpath.split(b"/"), (b"/", b"")) |
| 66 | self.assertEqual(posixpath.split(b"foo"), (b"", b"foo")) |
| 67 | self.assertEqual(posixpath.split(b"////foo"), (b"////", b"foo")) |
| 68 | self.assertEqual(posixpath.split(b"//foo//bar"), (b"//foo", b"bar")) |
| 69 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 70 | def splitextTest(self, path, filename, ext): |
| 71 | self.assertEqual(posixpath.splitext(path), (filename, ext)) |
| 72 | self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext)) |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 73 | self.assertEqual(posixpath.splitext("abc/" + path), |
| 74 | ("abc/" + filename, ext)) |
| 75 | self.assertEqual(posixpath.splitext("abc.def/" + path), |
| 76 | ("abc.def/" + filename, ext)) |
| 77 | self.assertEqual(posixpath.splitext("/abc.def/" + path), |
| 78 | ("/abc.def/" + filename, ext)) |
| 79 | self.assertEqual(posixpath.splitext(path + "/"), |
| 80 | (filename + ext + "/", "")) |
| 81 | |
| 82 | path = bytes(path, "ASCII") |
| 83 | filename = bytes(filename, "ASCII") |
| 84 | ext = bytes(ext, "ASCII") |
| 85 | |
| 86 | self.assertEqual(posixpath.splitext(path), (filename, ext)) |
| 87 | self.assertEqual(posixpath.splitext(b"/" + path), |
| 88 | (b"/" + filename, ext)) |
| 89 | self.assertEqual(posixpath.splitext(b"abc/" + path), |
| 90 | (b"abc/" + filename, ext)) |
| 91 | self.assertEqual(posixpath.splitext(b"abc.def/" + path), |
| 92 | (b"abc.def/" + filename, ext)) |
| 93 | self.assertEqual(posixpath.splitext(b"/abc.def/" + path), |
| 94 | (b"/abc.def/" + filename, ext)) |
| 95 | self.assertEqual(posixpath.splitext(path + b"/"), |
| 96 | (filename + ext + b"/", b"")) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 98 | def test_splitext(self): |
| 99 | self.splitextTest("foo.bar", "foo", ".bar") |
| 100 | self.splitextTest("foo.boo.bar", "foo.boo", ".bar") |
| 101 | self.splitextTest("foo.boo.biff.bar", "foo.boo.biff", ".bar") |
| 102 | self.splitextTest(".csh.rc", ".csh", ".rc") |
| 103 | self.splitextTest("nodots", "nodots", "") |
| 104 | self.splitextTest(".cshrc", ".cshrc", "") |
| 105 | self.splitextTest("...manydots", "...manydots", "") |
| 106 | self.splitextTest("...manydots.ext", "...manydots", ".ext") |
| 107 | self.splitextTest(".", ".", "") |
| 108 | self.splitextTest("..", "..", "") |
| 109 | self.splitextTest("........", "........", "") |
| 110 | self.splitextTest("", "", "") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 111 | |
| 112 | def test_isabs(self): |
| 113 | self.assertIs(posixpath.isabs(""), False) |
| 114 | self.assertIs(posixpath.isabs("/"), True) |
| 115 | self.assertIs(posixpath.isabs("/foo"), True) |
| 116 | self.assertIs(posixpath.isabs("/foo/bar"), True) |
| 117 | self.assertIs(posixpath.isabs("foo/bar"), False) |
| 118 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 119 | self.assertIs(posixpath.isabs(b""), False) |
| 120 | self.assertIs(posixpath.isabs(b"/"), True) |
| 121 | self.assertIs(posixpath.isabs(b"/foo"), True) |
| 122 | self.assertIs(posixpath.isabs(b"/foo/bar"), True) |
| 123 | self.assertIs(posixpath.isabs(b"foo/bar"), False) |
| 124 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 125 | def test_basename(self): |
| 126 | self.assertEqual(posixpath.basename("/foo/bar"), "bar") |
| 127 | self.assertEqual(posixpath.basename("/"), "") |
| 128 | self.assertEqual(posixpath.basename("foo"), "foo") |
| 129 | self.assertEqual(posixpath.basename("////foo"), "foo") |
| 130 | self.assertEqual(posixpath.basename("//foo//bar"), "bar") |
| 131 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 132 | self.assertEqual(posixpath.basename(b"/foo/bar"), b"bar") |
| 133 | self.assertEqual(posixpath.basename(b"/"), b"") |
| 134 | self.assertEqual(posixpath.basename(b"foo"), b"foo") |
| 135 | self.assertEqual(posixpath.basename(b"////foo"), b"foo") |
| 136 | self.assertEqual(posixpath.basename(b"//foo//bar"), b"bar") |
| 137 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 138 | def test_dirname(self): |
| 139 | self.assertEqual(posixpath.dirname("/foo/bar"), "/foo") |
| 140 | self.assertEqual(posixpath.dirname("/"), "/") |
| 141 | self.assertEqual(posixpath.dirname("foo"), "") |
| 142 | self.assertEqual(posixpath.dirname("////foo"), "////") |
| 143 | self.assertEqual(posixpath.dirname("//foo//bar"), "//foo") |
| 144 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 145 | self.assertEqual(posixpath.dirname(b"/foo/bar"), b"/foo") |
| 146 | self.assertEqual(posixpath.dirname(b"/"), b"/") |
| 147 | self.assertEqual(posixpath.dirname(b"foo"), b"") |
| 148 | self.assertEqual(posixpath.dirname(b"////foo"), b"////") |
| 149 | self.assertEqual(posixpath.dirname(b"//foo//bar"), b"//foo") |
| 150 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 151 | def test_islink(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 152 | self.assertIs(posixpath.islink(support.TESTFN + "1"), False) |
| 153 | f = open(support.TESTFN + "1", "wb") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 154 | try: |
Guido van Rossum | 7dcb844 | 2007-08-27 23:26:56 +0000 | [diff] [blame] | 155 | f.write(b"foo") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 156 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 157 | self.assertIs(posixpath.islink(support.TESTFN + "1"), False) |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 158 | if hasattr(os, "symlink"): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 159 | os.symlink(support.TESTFN + "1", support.TESTFN + "2") |
| 160 | self.assertIs(posixpath.islink(support.TESTFN + "2"), True) |
| 161 | os.remove(support.TESTFN + "1") |
| 162 | self.assertIs(posixpath.islink(support.TESTFN + "2"), True) |
| 163 | self.assertIs(posixpath.exists(support.TESTFN + "2"), False) |
| 164 | self.assertIs(posixpath.lexists(support.TESTFN + "2"), True) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 165 | finally: |
| 166 | if not f.close(): |
| 167 | f.close() |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 168 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 169 | @staticmethod |
| 170 | def _create_file(filename): |
| 171 | with open(filename, 'wb') as f: |
| 172 | f.write(b'foo') |
| 173 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 174 | def test_samefile(self): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 175 | test_fn = support.TESTFN + "1" |
| 176 | self._create_file(test_fn) |
| 177 | self.assertTrue(posixpath.samefile(test_fn, test_fn)) |
| 178 | self.assertRaises(TypeError, posixpath.samefile) |
| 179 | |
| 180 | @unittest.skipIf( |
| 181 | sys.platform.startswith('win'), |
| 182 | "posixpath.samefile does not work on links in Windows") |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 183 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 184 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 185 | def test_samefile_on_links(self): |
| 186 | test_fn1 = support.TESTFN + "1" |
| 187 | test_fn2 = support.TESTFN + "2" |
| 188 | self._create_file(test_fn1) |
| 189 | |
| 190 | os.symlink(test_fn1, test_fn2) |
| 191 | self.assertTrue(posixpath.samefile(test_fn1, test_fn2)) |
| 192 | os.remove(test_fn2) |
| 193 | |
| 194 | self._create_file(test_fn2) |
| 195 | self.assertFalse(posixpath.samefile(test_fn1, test_fn2)) |
| 196 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 197 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 198 | def test_samestat(self): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 199 | test_fn = support.TESTFN + "1" |
| 200 | self._create_file(test_fn) |
| 201 | test_fns = [test_fn]*2 |
| 202 | stats = map(os.stat, test_fns) |
| 203 | self.assertTrue(posixpath.samestat(*stats)) |
| 204 | |
| 205 | @unittest.skipIf( |
| 206 | sys.platform.startswith('win'), |
| 207 | "posixpath.samestat does not work on links in Windows") |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 208 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 209 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 210 | def test_samestat_on_links(self): |
| 211 | test_fn1 = support.TESTFN + "1" |
| 212 | test_fn2 = support.TESTFN + "2" |
Brian Curtin | 16633fa | 2010-07-09 13:54:27 +0000 | [diff] [blame] | 213 | self._create_file(test_fn1) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 214 | test_fns = (test_fn1, test_fn2) |
| 215 | os.symlink(*test_fns) |
| 216 | stats = map(os.stat, test_fns) |
| 217 | self.assertTrue(posixpath.samestat(*stats)) |
| 218 | os.remove(test_fn2) |
| 219 | |
| 220 | self._create_file(test_fn2) |
| 221 | stats = map(os.stat, test_fns) |
| 222 | self.assertFalse(posixpath.samestat(*stats)) |
| 223 | |
| 224 | self.assertRaises(TypeError, posixpath.samestat) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 225 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 226 | def test_ismount(self): |
| 227 | self.assertIs(posixpath.ismount("/"), True) |
| 228 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 229 | def test_expanduser(self): |
| 230 | self.assertEqual(posixpath.expanduser("foo"), "foo") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 231 | self.assertEqual(posixpath.expanduser(b"foo"), b"foo") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 232 | try: |
| 233 | import pwd |
| 234 | except ImportError: |
| 235 | pass |
| 236 | else: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 237 | self.assertIsInstance(posixpath.expanduser("~/"), str) |
| 238 | self.assertIsInstance(posixpath.expanduser(b"~/"), bytes) |
Neal Norwitz | 168e73d | 2003-07-01 03:33:31 +0000 | [diff] [blame] | 239 | # if home directory == root directory, this test makes no sense |
| 240 | if posixpath.expanduser("~") != '/': |
| 241 | self.assertEqual( |
| 242 | posixpath.expanduser("~") + "/", |
| 243 | posixpath.expanduser("~/") |
| 244 | ) |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 245 | self.assertEqual( |
| 246 | posixpath.expanduser(b"~") + b"/", |
| 247 | posixpath.expanduser(b"~/") |
| 248 | ) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 249 | self.assertIsInstance(posixpath.expanduser("~root/"), str) |
| 250 | self.assertIsInstance(posixpath.expanduser("~foo/"), str) |
| 251 | self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes) |
| 252 | self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 253 | |
Hirokazu Yamamoto | 7195963 | 2009-04-27 01:44:28 +0000 | [diff] [blame] | 254 | with support.EnvironmentVarGuard() as env: |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 255 | env['HOME'] = '/' |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 256 | self.assertEqual(posixpath.expanduser("~"), "/") |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 257 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 258 | def test_normpath(self): |
| 259 | self.assertEqual(posixpath.normpath(""), ".") |
| 260 | self.assertEqual(posixpath.normpath("/"), "/") |
| 261 | self.assertEqual(posixpath.normpath("//"), "//") |
| 262 | self.assertEqual(posixpath.normpath("///"), "/") |
| 263 | self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 264 | self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), |
| 265 | "/foo/baz") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 266 | self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar") |
| 267 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 268 | self.assertEqual(posixpath.normpath(b""), b".") |
| 269 | self.assertEqual(posixpath.normpath(b"/"), b"/") |
| 270 | self.assertEqual(posixpath.normpath(b"//"), b"//") |
| 271 | self.assertEqual(posixpath.normpath(b"///"), b"/") |
| 272 | self.assertEqual(posixpath.normpath(b"///foo/.//bar//"), b"/foo/bar") |
| 273 | self.assertEqual(posixpath.normpath(b"///foo/.//bar//.//..//.//baz"), |
| 274 | b"/foo/baz") |
| 275 | self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"), |
| 276 | b"/foo/bar") |
| 277 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 278 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 279 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 280 | @skip_if_ABSTFN_contains_backslash |
| 281 | def test_realpath_basic(self): |
| 282 | # Basic operation. |
| 283 | try: |
| 284 | os.symlink(ABSTFN+"1", ABSTFN) |
| 285 | self.assertEqual(realpath(ABSTFN), ABSTFN+"1") |
| 286 | finally: |
| 287 | support.unlink(ABSTFN) |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 288 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 289 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 290 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 291 | @skip_if_ABSTFN_contains_backslash |
| 292 | def test_realpath_symlink_loops(self): |
| 293 | # Bug #930024, return the path unchanged if we get into an infinite |
| 294 | # symlink loop. |
| 295 | try: |
| 296 | old_path = abspath('.') |
| 297 | os.symlink(ABSTFN, ABSTFN) |
| 298 | self.assertEqual(realpath(ABSTFN), ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 299 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 300 | os.symlink(ABSTFN+"1", ABSTFN+"2") |
| 301 | os.symlink(ABSTFN+"2", ABSTFN+"1") |
| 302 | self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1") |
| 303 | self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 304 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 305 | # Test using relative path as well. |
| 306 | os.chdir(dirname(ABSTFN)) |
| 307 | self.assertEqual(realpath(basename(ABSTFN)), ABSTFN) |
| 308 | finally: |
| 309 | os.chdir(old_path) |
| 310 | support.unlink(ABSTFN) |
| 311 | support.unlink(ABSTFN+"1") |
| 312 | support.unlink(ABSTFN+"2") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 313 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 314 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 315 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 316 | @skip_if_ABSTFN_contains_backslash |
| 317 | def test_realpath_resolve_parents(self): |
| 318 | # We also need to resolve any symlinks in the parents of a relative |
| 319 | # path passed to realpath. E.g.: current working directory is |
| 320 | # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call |
| 321 | # realpath("a"). This should return /usr/share/doc/a/. |
| 322 | try: |
| 323 | old_path = abspath('.') |
| 324 | os.mkdir(ABSTFN) |
| 325 | os.mkdir(ABSTFN + "/y") |
| 326 | os.symlink(ABSTFN + "/y", ABSTFN + "/k") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 327 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 328 | os.chdir(ABSTFN + "/k") |
| 329 | self.assertEqual(realpath("a"), ABSTFN + "/y/a") |
| 330 | finally: |
| 331 | os.chdir(old_path) |
| 332 | support.unlink(ABSTFN + "/k") |
| 333 | safe_rmdir(ABSTFN + "/y") |
| 334 | safe_rmdir(ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 335 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 336 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 337 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 338 | @skip_if_ABSTFN_contains_backslash |
| 339 | def test_realpath_resolve_before_normalizing(self): |
| 340 | # Bug #990669: Symbolic links should be resolved before we |
| 341 | # normalize the path. E.g.: if we have directories 'a', 'k' and 'y' |
| 342 | # in the following hierarchy: |
| 343 | # a/k/y |
| 344 | # |
| 345 | # and a symbolic link 'link-y' pointing to 'y' in directory 'a', |
| 346 | # then realpath("link-y/..") should return 'k', not 'a'. |
| 347 | try: |
| 348 | old_path = abspath('.') |
| 349 | os.mkdir(ABSTFN) |
| 350 | os.mkdir(ABSTFN + "/k") |
| 351 | os.mkdir(ABSTFN + "/k/y") |
| 352 | os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 353 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 354 | # Absolute path. |
| 355 | self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k") |
| 356 | # Relative path. |
| 357 | os.chdir(dirname(ABSTFN)) |
| 358 | self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), |
| 359 | ABSTFN + "/k") |
| 360 | finally: |
| 361 | os.chdir(old_path) |
| 362 | support.unlink(ABSTFN + "/link-y") |
| 363 | safe_rmdir(ABSTFN + "/k/y") |
| 364 | safe_rmdir(ABSTFN + "/k") |
| 365 | safe_rmdir(ABSTFN) |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 366 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 367 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 368 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 369 | @skip_if_ABSTFN_contains_backslash |
| 370 | def test_realpath_resolve_first(self): |
| 371 | # Bug #1213894: The first component of the path, if not absolute, |
| 372 | # must be resolved too. |
Georg Brandl | 268e61c | 2005-06-03 14:28:50 +0000 | [diff] [blame] | 373 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 374 | try: |
| 375 | old_path = abspath('.') |
| 376 | os.mkdir(ABSTFN) |
| 377 | os.mkdir(ABSTFN + "/k") |
| 378 | os.symlink(ABSTFN, ABSTFN + "link") |
| 379 | os.chdir(dirname(ABSTFN)) |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 380 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 381 | base = basename(ABSTFN) |
| 382 | self.assertEqual(realpath(base + "link"), ABSTFN) |
| 383 | self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k") |
| 384 | finally: |
| 385 | os.chdir(old_path) |
| 386 | support.unlink(ABSTFN + "link") |
| 387 | safe_rmdir(ABSTFN + "/k") |
| 388 | safe_rmdir(ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 389 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 390 | def test_relpath(self): |
| 391 | (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar") |
| 392 | try: |
| 393 | curdir = os.path.split(os.getcwd())[-1] |
| 394 | self.assertRaises(ValueError, posixpath.relpath, "") |
| 395 | self.assertEqual(posixpath.relpath("a"), "a") |
| 396 | self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a") |
| 397 | self.assertEqual(posixpath.relpath("a/b"), "a/b") |
| 398 | self.assertEqual(posixpath.relpath("../a/b"), "../a/b") |
| 399 | self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 400 | self.assertEqual(posixpath.relpath("a/b", "../c"), |
| 401 | "../"+curdir+"/a/b") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 402 | self.assertEqual(posixpath.relpath("a", "b/c"), "../../a") |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 403 | self.assertEqual(posixpath.relpath("a", "a"), ".") |
Hirokazu Yamamoto | b08820a | 2010-10-18 12:13:18 +0000 | [diff] [blame] | 404 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat') |
| 405 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/foo/bar"), 'bat') |
| 406 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/"), 'foo/bar/bat') |
| 407 | self.assertEqual(posixpath.relpath("/", "/foo/bar/bat"), '../../..') |
| 408 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x"), '../foo/bar/bat') |
| 409 | self.assertEqual(posixpath.relpath("/x", "/foo/bar/bat"), '../../../x') |
| 410 | self.assertEqual(posixpath.relpath("/", "/"), '.') |
| 411 | self.assertEqual(posixpath.relpath("/a", "/a"), '.') |
| 412 | self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 413 | finally: |
| 414 | os.getcwd = real_getcwd |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 415 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 416 | def test_relpath_bytes(self): |
| 417 | (real_getcwdb, os.getcwdb) = (os.getcwdb, lambda: br"/home/user/bar") |
| 418 | try: |
| 419 | curdir = os.path.split(os.getcwdb())[-1] |
| 420 | self.assertRaises(ValueError, posixpath.relpath, b"") |
| 421 | self.assertEqual(posixpath.relpath(b"a"), b"a") |
| 422 | self.assertEqual(posixpath.relpath(posixpath.abspath(b"a")), b"a") |
| 423 | self.assertEqual(posixpath.relpath(b"a/b"), b"a/b") |
| 424 | self.assertEqual(posixpath.relpath(b"../a/b"), b"../a/b") |
| 425 | self.assertEqual(posixpath.relpath(b"a", b"../b"), |
| 426 | b"../"+curdir+b"/a") |
| 427 | self.assertEqual(posixpath.relpath(b"a/b", b"../c"), |
| 428 | b"../"+curdir+b"/a/b") |
| 429 | self.assertEqual(posixpath.relpath(b"a", b"b/c"), b"../../a") |
| 430 | self.assertEqual(posixpath.relpath(b"a", b"a"), b".") |
Hirokazu Yamamoto | b08820a | 2010-10-18 12:13:18 +0000 | [diff] [blame] | 431 | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x/y/z"), b'../../../foo/bar/bat') |
| 432 | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/foo/bar"), b'bat') |
| 433 | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/"), b'foo/bar/bat') |
| 434 | self.assertEqual(posixpath.relpath(b"/", b"/foo/bar/bat"), b'../../..') |
| 435 | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x"), b'../foo/bar/bat') |
| 436 | self.assertEqual(posixpath.relpath(b"/x", b"/foo/bar/bat"), b'../../../x') |
| 437 | self.assertEqual(posixpath.relpath(b"/", b"/"), b'.') |
| 438 | self.assertEqual(posixpath.relpath(b"/a", b"/a"), b'.') |
| 439 | self.assertEqual(posixpath.relpath(b"/a/b", b"/a/b"), b'.') |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 440 | |
| 441 | self.assertRaises(TypeError, posixpath.relpath, b"bytes", "str") |
| 442 | self.assertRaises(TypeError, posixpath.relpath, "str", b"bytes") |
| 443 | finally: |
| 444 | os.getcwdb = real_getcwdb |
| 445 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 446 | |
| 447 | class PosixCommonTest(test_genericpath.CommonTest): |
| 448 | pathmodule = posixpath |
| 449 | attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat'] |
| 450 | |
| 451 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 452 | def test_main(): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 453 | support.run_unittest(PosixPathTest, PosixCommonTest) |
| 454 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 455 | |
| 456 | if __name__=="__main__": |
| 457 | test_main() |