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