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 | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 158 | if support.can_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") |
| 183 | @support.skip_unless_symlink |
| 184 | def test_samefile_on_links(self): |
| 185 | test_fn1 = support.TESTFN + "1" |
| 186 | test_fn2 = support.TESTFN + "2" |
| 187 | self._create_file(test_fn1) |
| 188 | |
| 189 | os.symlink(test_fn1, test_fn2) |
| 190 | self.assertTrue(posixpath.samefile(test_fn1, test_fn2)) |
| 191 | os.remove(test_fn2) |
| 192 | |
| 193 | self._create_file(test_fn2) |
| 194 | self.assertFalse(posixpath.samefile(test_fn1, test_fn2)) |
| 195 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 196 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 197 | def test_samestat(self): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 198 | test_fn = support.TESTFN + "1" |
| 199 | self._create_file(test_fn) |
| 200 | test_fns = [test_fn]*2 |
| 201 | stats = map(os.stat, test_fns) |
| 202 | self.assertTrue(posixpath.samestat(*stats)) |
| 203 | |
| 204 | @unittest.skipIf( |
| 205 | sys.platform.startswith('win'), |
| 206 | "posixpath.samestat does not work on links in Windows") |
| 207 | @support.skip_unless_symlink |
| 208 | def test_samestat_on_links(self): |
| 209 | test_fn1 = support.TESTFN + "1" |
| 210 | test_fn2 = support.TESTFN + "2" |
Brian Curtin | 16633fa | 2010-07-09 13:54:27 +0000 | [diff] [blame] | 211 | self._create_file(test_fn1) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 212 | test_fns = (test_fn1, test_fn2) |
| 213 | os.symlink(*test_fns) |
| 214 | stats = map(os.stat, test_fns) |
| 215 | self.assertTrue(posixpath.samestat(*stats)) |
| 216 | os.remove(test_fn2) |
| 217 | |
| 218 | self._create_file(test_fn2) |
| 219 | stats = map(os.stat, test_fns) |
| 220 | self.assertFalse(posixpath.samestat(*stats)) |
| 221 | |
| 222 | self.assertRaises(TypeError, posixpath.samestat) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 223 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 224 | def test_ismount(self): |
| 225 | self.assertIs(posixpath.ismount("/"), True) |
| 226 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 227 | def test_expanduser(self): |
| 228 | self.assertEqual(posixpath.expanduser("foo"), "foo") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 229 | self.assertEqual(posixpath.expanduser(b"foo"), b"foo") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 230 | try: |
| 231 | import pwd |
| 232 | except ImportError: |
| 233 | pass |
| 234 | else: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 235 | self.assertIsInstance(posixpath.expanduser("~/"), str) |
| 236 | self.assertIsInstance(posixpath.expanduser(b"~/"), bytes) |
Neal Norwitz | 168e73d | 2003-07-01 03:33:31 +0000 | [diff] [blame] | 237 | # if home directory == root directory, this test makes no sense |
| 238 | if posixpath.expanduser("~") != '/': |
| 239 | self.assertEqual( |
| 240 | posixpath.expanduser("~") + "/", |
| 241 | posixpath.expanduser("~/") |
| 242 | ) |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 243 | self.assertEqual( |
| 244 | posixpath.expanduser(b"~") + b"/", |
| 245 | posixpath.expanduser(b"~/") |
| 246 | ) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 247 | self.assertIsInstance(posixpath.expanduser("~root/"), str) |
| 248 | self.assertIsInstance(posixpath.expanduser("~foo/"), str) |
| 249 | self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes) |
| 250 | self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 251 | |
Hirokazu Yamamoto | 7195963 | 2009-04-27 01:44:28 +0000 | [diff] [blame] | 252 | with support.EnvironmentVarGuard() as env: |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 253 | env['HOME'] = '/' |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 254 | self.assertEqual(posixpath.expanduser("~"), "/") |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 255 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 256 | def test_normpath(self): |
| 257 | self.assertEqual(posixpath.normpath(""), ".") |
| 258 | self.assertEqual(posixpath.normpath("/"), "/") |
| 259 | self.assertEqual(posixpath.normpath("//"), "//") |
| 260 | self.assertEqual(posixpath.normpath("///"), "/") |
| 261 | self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 262 | self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), |
| 263 | "/foo/baz") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 264 | self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar") |
| 265 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 266 | self.assertEqual(posixpath.normpath(b""), b".") |
| 267 | self.assertEqual(posixpath.normpath(b"/"), b"/") |
| 268 | self.assertEqual(posixpath.normpath(b"//"), b"//") |
| 269 | self.assertEqual(posixpath.normpath(b"///"), b"/") |
| 270 | self.assertEqual(posixpath.normpath(b"///foo/.//bar//"), b"/foo/bar") |
| 271 | self.assertEqual(posixpath.normpath(b"///foo/.//bar//.//..//.//baz"), |
| 272 | b"/foo/baz") |
| 273 | self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"), |
| 274 | b"/foo/bar") |
| 275 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 276 | @support.skip_unless_symlink |
| 277 | @skip_if_ABSTFN_contains_backslash |
| 278 | def test_realpath_basic(self): |
| 279 | # Basic operation. |
| 280 | try: |
| 281 | os.symlink(ABSTFN+"1", ABSTFN) |
| 282 | self.assertEqual(realpath(ABSTFN), ABSTFN+"1") |
| 283 | finally: |
| 284 | support.unlink(ABSTFN) |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 285 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 286 | @support.skip_unless_symlink |
| 287 | @skip_if_ABSTFN_contains_backslash |
| 288 | def test_realpath_symlink_loops(self): |
| 289 | # Bug #930024, return the path unchanged if we get into an infinite |
| 290 | # symlink loop. |
| 291 | try: |
| 292 | old_path = abspath('.') |
| 293 | os.symlink(ABSTFN, ABSTFN) |
| 294 | self.assertEqual(realpath(ABSTFN), ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 295 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 296 | os.symlink(ABSTFN+"1", ABSTFN+"2") |
| 297 | os.symlink(ABSTFN+"2", ABSTFN+"1") |
| 298 | self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1") |
| 299 | self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 300 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 301 | # Test using relative path as well. |
| 302 | os.chdir(dirname(ABSTFN)) |
| 303 | self.assertEqual(realpath(basename(ABSTFN)), ABSTFN) |
| 304 | finally: |
| 305 | os.chdir(old_path) |
| 306 | support.unlink(ABSTFN) |
| 307 | support.unlink(ABSTFN+"1") |
| 308 | support.unlink(ABSTFN+"2") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 309 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 310 | @support.skip_unless_symlink |
| 311 | @skip_if_ABSTFN_contains_backslash |
| 312 | def test_realpath_resolve_parents(self): |
| 313 | # We also need to resolve any symlinks in the parents of a relative |
| 314 | # path passed to realpath. E.g.: current working directory is |
| 315 | # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call |
| 316 | # realpath("a"). This should return /usr/share/doc/a/. |
| 317 | try: |
| 318 | old_path = abspath('.') |
| 319 | os.mkdir(ABSTFN) |
| 320 | os.mkdir(ABSTFN + "/y") |
| 321 | os.symlink(ABSTFN + "/y", ABSTFN + "/k") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 322 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 323 | os.chdir(ABSTFN + "/k") |
| 324 | self.assertEqual(realpath("a"), ABSTFN + "/y/a") |
| 325 | finally: |
| 326 | os.chdir(old_path) |
| 327 | support.unlink(ABSTFN + "/k") |
| 328 | safe_rmdir(ABSTFN + "/y") |
| 329 | safe_rmdir(ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 330 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 331 | @support.skip_unless_symlink |
| 332 | @skip_if_ABSTFN_contains_backslash |
| 333 | def test_realpath_resolve_before_normalizing(self): |
| 334 | # Bug #990669: Symbolic links should be resolved before we |
| 335 | # normalize the path. E.g.: if we have directories 'a', 'k' and 'y' |
| 336 | # in the following hierarchy: |
| 337 | # a/k/y |
| 338 | # |
| 339 | # and a symbolic link 'link-y' pointing to 'y' in directory 'a', |
| 340 | # then realpath("link-y/..") should return 'k', not 'a'. |
| 341 | try: |
| 342 | old_path = abspath('.') |
| 343 | os.mkdir(ABSTFN) |
| 344 | os.mkdir(ABSTFN + "/k") |
| 345 | os.mkdir(ABSTFN + "/k/y") |
| 346 | os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 347 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 348 | # Absolute path. |
| 349 | self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k") |
| 350 | # Relative path. |
| 351 | os.chdir(dirname(ABSTFN)) |
| 352 | self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), |
| 353 | ABSTFN + "/k") |
| 354 | finally: |
| 355 | os.chdir(old_path) |
| 356 | support.unlink(ABSTFN + "/link-y") |
| 357 | safe_rmdir(ABSTFN + "/k/y") |
| 358 | safe_rmdir(ABSTFN + "/k") |
| 359 | safe_rmdir(ABSTFN) |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 360 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 361 | @support.skip_unless_symlink |
| 362 | @skip_if_ABSTFN_contains_backslash |
| 363 | def test_realpath_resolve_first(self): |
| 364 | # Bug #1213894: The first component of the path, if not absolute, |
| 365 | # must be resolved too. |
Georg Brandl | 268e61c | 2005-06-03 14:28:50 +0000 | [diff] [blame] | 366 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 367 | try: |
| 368 | old_path = abspath('.') |
| 369 | os.mkdir(ABSTFN) |
| 370 | os.mkdir(ABSTFN + "/k") |
| 371 | os.symlink(ABSTFN, ABSTFN + "link") |
| 372 | os.chdir(dirname(ABSTFN)) |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 373 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 374 | base = basename(ABSTFN) |
| 375 | self.assertEqual(realpath(base + "link"), ABSTFN) |
| 376 | self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k") |
| 377 | finally: |
| 378 | os.chdir(old_path) |
| 379 | support.unlink(ABSTFN + "link") |
| 380 | safe_rmdir(ABSTFN + "/k") |
| 381 | safe_rmdir(ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 382 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 383 | def test_relpath(self): |
| 384 | (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar") |
| 385 | try: |
| 386 | curdir = os.path.split(os.getcwd())[-1] |
| 387 | self.assertRaises(ValueError, posixpath.relpath, "") |
| 388 | self.assertEqual(posixpath.relpath("a"), "a") |
| 389 | self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a") |
| 390 | self.assertEqual(posixpath.relpath("a/b"), "a/b") |
| 391 | self.assertEqual(posixpath.relpath("../a/b"), "../a/b") |
| 392 | self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 393 | self.assertEqual(posixpath.relpath("a/b", "../c"), |
| 394 | "../"+curdir+"/a/b") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 395 | self.assertEqual(posixpath.relpath("a", "b/c"), "../../a") |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 396 | self.assertEqual(posixpath.relpath("a", "a"), ".") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 397 | finally: |
| 398 | os.getcwd = real_getcwd |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 399 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 400 | def test_relpath_bytes(self): |
| 401 | (real_getcwdb, os.getcwdb) = (os.getcwdb, lambda: br"/home/user/bar") |
| 402 | try: |
| 403 | curdir = os.path.split(os.getcwdb())[-1] |
| 404 | self.assertRaises(ValueError, posixpath.relpath, b"") |
| 405 | self.assertEqual(posixpath.relpath(b"a"), b"a") |
| 406 | self.assertEqual(posixpath.relpath(posixpath.abspath(b"a")), b"a") |
| 407 | self.assertEqual(posixpath.relpath(b"a/b"), b"a/b") |
| 408 | self.assertEqual(posixpath.relpath(b"../a/b"), b"../a/b") |
| 409 | self.assertEqual(posixpath.relpath(b"a", b"../b"), |
| 410 | b"../"+curdir+b"/a") |
| 411 | self.assertEqual(posixpath.relpath(b"a/b", b"../c"), |
| 412 | b"../"+curdir+b"/a/b") |
| 413 | self.assertEqual(posixpath.relpath(b"a", b"b/c"), b"../../a") |
| 414 | self.assertEqual(posixpath.relpath(b"a", b"a"), b".") |
| 415 | |
| 416 | self.assertRaises(TypeError, posixpath.relpath, b"bytes", "str") |
| 417 | self.assertRaises(TypeError, posixpath.relpath, "str", b"bytes") |
| 418 | finally: |
| 419 | os.getcwdb = real_getcwdb |
| 420 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 421 | |
| 422 | class PosixCommonTest(test_genericpath.CommonTest): |
| 423 | pathmodule = posixpath |
| 424 | attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat'] |
| 425 | |
| 426 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 427 | def test_main(): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 428 | support.run_unittest(PosixPathTest, PosixCommonTest) |
| 429 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 430 | |
| 431 | if __name__=="__main__": |
| 432 | test_main() |