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 | |
Hynek Schlawack | c5a4566 | 2012-07-17 13:05:43 +0200 | [diff] [blame] | 4 | import itertools |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5 | import posixpath |
| 6 | import os |
| 7 | import sys |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 8 | from posixpath import realpath, abspath, dirname, basename |
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 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 189 | @staticmethod |
| 190 | def _create_file(filename): |
| 191 | with open(filename, 'wb') as f: |
| 192 | f.write(b'foo') |
| 193 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 194 | def test_samefile(self): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 195 | test_fn = support.TESTFN + "1" |
| 196 | self._create_file(test_fn) |
| 197 | self.assertTrue(posixpath.samefile(test_fn, test_fn)) |
| 198 | self.assertRaises(TypeError, posixpath.samefile) |
| 199 | |
| 200 | @unittest.skipIf( |
| 201 | sys.platform.startswith('win'), |
| 202 | "posixpath.samefile does not work on links in Windows") |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 203 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 204 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 205 | def test_samefile_on_links(self): |
| 206 | test_fn1 = support.TESTFN + "1" |
| 207 | test_fn2 = support.TESTFN + "2" |
| 208 | self._create_file(test_fn1) |
| 209 | |
| 210 | os.symlink(test_fn1, test_fn2) |
| 211 | self.assertTrue(posixpath.samefile(test_fn1, test_fn2)) |
| 212 | os.remove(test_fn2) |
| 213 | |
| 214 | self._create_file(test_fn2) |
| 215 | self.assertFalse(posixpath.samefile(test_fn1, test_fn2)) |
| 216 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 217 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 218 | def test_samestat(self): |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 219 | test_fn = support.TESTFN + "1" |
| 220 | self._create_file(test_fn) |
| 221 | test_fns = [test_fn]*2 |
| 222 | stats = map(os.stat, test_fns) |
| 223 | self.assertTrue(posixpath.samestat(*stats)) |
| 224 | |
| 225 | @unittest.skipIf( |
| 226 | sys.platform.startswith('win'), |
| 227 | "posixpath.samestat does not work on links in Windows") |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 228 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 229 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 230 | def test_samestat_on_links(self): |
| 231 | test_fn1 = support.TESTFN + "1" |
| 232 | test_fn2 = support.TESTFN + "2" |
Brian Curtin | 16633fa | 2010-07-09 13:54:27 +0000 | [diff] [blame] | 233 | self._create_file(test_fn1) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 234 | test_fns = (test_fn1, test_fn2) |
| 235 | os.symlink(*test_fns) |
| 236 | stats = map(os.stat, test_fns) |
| 237 | self.assertTrue(posixpath.samestat(*stats)) |
| 238 | os.remove(test_fn2) |
| 239 | |
| 240 | self._create_file(test_fn2) |
| 241 | stats = map(os.stat, test_fns) |
| 242 | self.assertFalse(posixpath.samestat(*stats)) |
| 243 | |
| 244 | self.assertRaises(TypeError, posixpath.samestat) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 245 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 246 | def test_ismount(self): |
| 247 | self.assertIs(posixpath.ismount("/"), True) |
Michael Foord | 07926f0 | 2011-03-16 17:19:16 -0400 | [diff] [blame] | 248 | self.assertIs(posixpath.ismount(b"/"), True) |
| 249 | |
| 250 | def test_ismount_non_existent(self): |
| 251 | # Non-existent mountpoint. |
| 252 | self.assertIs(posixpath.ismount(ABSTFN), False) |
| 253 | try: |
| 254 | os.mkdir(ABSTFN) |
| 255 | self.assertIs(posixpath.ismount(ABSTFN), False) |
| 256 | finally: |
| 257 | safe_rmdir(ABSTFN) |
| 258 | |
| 259 | @unittest.skipUnless(support.can_symlink(), |
| 260 | "Test requires symlink support") |
| 261 | def test_ismount_symlinks(self): |
| 262 | # Symlinks are never mountpoints. |
| 263 | try: |
| 264 | os.symlink("/", ABSTFN) |
| 265 | self.assertIs(posixpath.ismount(ABSTFN), False) |
| 266 | finally: |
| 267 | os.unlink(ABSTFN) |
| 268 | |
| 269 | @unittest.skipIf(posix is None, "Test requires posix module") |
| 270 | def test_ismount_different_device(self): |
| 271 | # Simulate the path being on a different device from its parent by |
| 272 | # mocking out st_dev. |
| 273 | save_lstat = os.lstat |
| 274 | def fake_lstat(path): |
| 275 | st_ino = 0 |
| 276 | st_dev = 0 |
| 277 | if path == ABSTFN: |
| 278 | st_dev = 1 |
| 279 | st_ino = 1 |
| 280 | return posix.stat_result((0, st_ino, st_dev, 0, 0, 0, 0, 0, 0, 0)) |
| 281 | try: |
| 282 | os.lstat = fake_lstat |
| 283 | self.assertIs(posixpath.ismount(ABSTFN), True) |
| 284 | finally: |
| 285 | os.lstat = save_lstat |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 286 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 287 | def test_expanduser(self): |
| 288 | self.assertEqual(posixpath.expanduser("foo"), "foo") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 289 | self.assertEqual(posixpath.expanduser(b"foo"), b"foo") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 290 | try: |
| 291 | import pwd |
| 292 | except ImportError: |
| 293 | pass |
| 294 | else: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 295 | self.assertIsInstance(posixpath.expanduser("~/"), str) |
| 296 | self.assertIsInstance(posixpath.expanduser(b"~/"), bytes) |
Neal Norwitz | 168e73d | 2003-07-01 03:33:31 +0000 | [diff] [blame] | 297 | # if home directory == root directory, this test makes no sense |
| 298 | if posixpath.expanduser("~") != '/': |
| 299 | self.assertEqual( |
| 300 | posixpath.expanduser("~") + "/", |
| 301 | posixpath.expanduser("~/") |
| 302 | ) |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 303 | self.assertEqual( |
| 304 | posixpath.expanduser(b"~") + b"/", |
| 305 | posixpath.expanduser(b"~/") |
| 306 | ) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 307 | self.assertIsInstance(posixpath.expanduser("~root/"), str) |
| 308 | self.assertIsInstance(posixpath.expanduser("~foo/"), str) |
| 309 | self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes) |
| 310 | self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 311 | |
Hirokazu Yamamoto | 7195963 | 2009-04-27 01:44:28 +0000 | [diff] [blame] | 312 | with support.EnvironmentVarGuard() as env: |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 313 | env['HOME'] = '/' |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 314 | self.assertEqual(posixpath.expanduser("~"), "/") |
Jesus Cea | 7f0d888 | 2012-05-10 05:10:50 +0200 | [diff] [blame] | 315 | self.assertEqual(posixpath.expanduser("~/foo"), "/foo") |
Michael Foord | 07926f0 | 2011-03-16 17:19:16 -0400 | [diff] [blame] | 316 | # expanduser should fall back to using the password database |
| 317 | del env['HOME'] |
| 318 | home = pwd.getpwuid(os.getuid()).pw_dir |
| 319 | self.assertEqual(posixpath.expanduser("~"), home) |
Benjamin Peterson | ef3e4c2 | 2009-04-11 19:48:14 +0000 | [diff] [blame] | 320 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 321 | def test_normpath(self): |
| 322 | self.assertEqual(posixpath.normpath(""), ".") |
| 323 | self.assertEqual(posixpath.normpath("/"), "/") |
| 324 | self.assertEqual(posixpath.normpath("//"), "//") |
| 325 | self.assertEqual(posixpath.normpath("///"), "/") |
| 326 | self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 327 | self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), |
| 328 | "/foo/baz") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 329 | self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar") |
| 330 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 331 | self.assertEqual(posixpath.normpath(b""), b".") |
| 332 | self.assertEqual(posixpath.normpath(b"/"), b"/") |
| 333 | self.assertEqual(posixpath.normpath(b"//"), b"//") |
| 334 | self.assertEqual(posixpath.normpath(b"///"), b"/") |
| 335 | self.assertEqual(posixpath.normpath(b"///foo/.//bar//"), b"/foo/bar") |
| 336 | self.assertEqual(posixpath.normpath(b"///foo/.//bar//.//..//.//baz"), |
| 337 | b"/foo/baz") |
| 338 | self.assertEqual(posixpath.normpath(b"///..//./foo/.//bar"), |
| 339 | b"/foo/bar") |
| 340 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 341 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 342 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 343 | @skip_if_ABSTFN_contains_backslash |
| 344 | def test_realpath_basic(self): |
| 345 | # Basic operation. |
| 346 | try: |
| 347 | os.symlink(ABSTFN+"1", ABSTFN) |
| 348 | self.assertEqual(realpath(ABSTFN), ABSTFN+"1") |
| 349 | finally: |
| 350 | support.unlink(ABSTFN) |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 351 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 352 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 353 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 354 | @skip_if_ABSTFN_contains_backslash |
Michael Foord | 07926f0 | 2011-03-16 17:19:16 -0400 | [diff] [blame] | 355 | def test_realpath_relative(self): |
| 356 | try: |
| 357 | os.symlink(posixpath.relpath(ABSTFN+"1"), ABSTFN) |
| 358 | self.assertEqual(realpath(ABSTFN), ABSTFN+"1") |
| 359 | finally: |
| 360 | support.unlink(ABSTFN) |
| 361 | |
| 362 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 363 | "Missing symlink implementation") |
| 364 | @skip_if_ABSTFN_contains_backslash |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 365 | def test_realpath_symlink_loops(self): |
| 366 | # Bug #930024, return the path unchanged if we get into an infinite |
| 367 | # symlink loop. |
| 368 | try: |
| 369 | old_path = abspath('.') |
| 370 | os.symlink(ABSTFN, ABSTFN) |
| 371 | self.assertEqual(realpath(ABSTFN), ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 372 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 373 | os.symlink(ABSTFN+"1", ABSTFN+"2") |
| 374 | os.symlink(ABSTFN+"2", ABSTFN+"1") |
| 375 | self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1") |
| 376 | self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 377 | |
Serhiy Storchaka | df32691 | 2013-02-10 12:22:07 +0200 | [diff] [blame] | 378 | self.assertEqual(realpath(ABSTFN+"1/x"), ABSTFN+"1/x") |
| 379 | self.assertEqual(realpath(ABSTFN+"1/.."), dirname(ABSTFN)) |
| 380 | self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x") |
| 381 | os.symlink(ABSTFN+"x", ABSTFN+"y") |
| 382 | self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"), |
| 383 | ABSTFN + "y") |
| 384 | self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"), |
| 385 | ABSTFN + "1") |
| 386 | |
| 387 | os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a") |
| 388 | self.assertEqual(realpath(ABSTFN+"a"), ABSTFN+"a/b") |
| 389 | |
| 390 | os.symlink("../" + basename(dirname(ABSTFN)) + "/" + |
| 391 | basename(ABSTFN) + "c", ABSTFN+"c") |
| 392 | self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c") |
| 393 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 394 | # Test using relative path as well. |
| 395 | os.chdir(dirname(ABSTFN)) |
| 396 | self.assertEqual(realpath(basename(ABSTFN)), ABSTFN) |
| 397 | finally: |
| 398 | os.chdir(old_path) |
| 399 | support.unlink(ABSTFN) |
| 400 | support.unlink(ABSTFN+"1") |
| 401 | support.unlink(ABSTFN+"2") |
Serhiy Storchaka | df32691 | 2013-02-10 12:22:07 +0200 | [diff] [blame] | 402 | support.unlink(ABSTFN+"y") |
| 403 | support.unlink(ABSTFN+"c") |
| 404 | |
| 405 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 406 | "Missing symlink implementation") |
| 407 | @skip_if_ABSTFN_contains_backslash |
| 408 | def test_realpath_repeated_indirect_symlinks(self): |
| 409 | # Issue #6975. |
| 410 | try: |
| 411 | os.mkdir(ABSTFN) |
| 412 | os.symlink('../' + basename(ABSTFN), ABSTFN + '/self') |
| 413 | os.symlink('self/self/self', ABSTFN + '/link') |
| 414 | self.assertEqual(realpath(ABSTFN + '/link'), ABSTFN) |
| 415 | finally: |
| 416 | support.unlink(ABSTFN + '/self') |
| 417 | support.unlink(ABSTFN + '/link') |
| 418 | safe_rmdir(ABSTFN) |
| 419 | |
| 420 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 421 | "Missing symlink implementation") |
| 422 | @skip_if_ABSTFN_contains_backslash |
| 423 | def test_realpath_deep_recursion(self): |
| 424 | depth = 10 |
| 425 | old_path = abspath('.') |
| 426 | try: |
| 427 | os.mkdir(ABSTFN) |
| 428 | for i in range(depth): |
| 429 | os.symlink('/'.join(['%d' % i] * 10), ABSTFN + '/%d' % (i + 1)) |
| 430 | os.symlink('.', ABSTFN + '/0') |
| 431 | self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN) |
| 432 | |
| 433 | # Test using relative path as well. |
| 434 | os.chdir(ABSTFN) |
| 435 | self.assertEqual(realpath('%d' % depth), ABSTFN) |
| 436 | finally: |
| 437 | os.chdir(old_path) |
| 438 | for i in range(depth + 1): |
| 439 | support.unlink(ABSTFN + '/%d' % i) |
| 440 | safe_rmdir(ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 441 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 442 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 443 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 444 | @skip_if_ABSTFN_contains_backslash |
| 445 | def test_realpath_resolve_parents(self): |
| 446 | # We also need to resolve any symlinks in the parents of a relative |
| 447 | # path passed to realpath. E.g.: current working directory is |
| 448 | # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call |
| 449 | # realpath("a"). This should return /usr/share/doc/a/. |
| 450 | try: |
| 451 | old_path = abspath('.') |
| 452 | os.mkdir(ABSTFN) |
| 453 | os.mkdir(ABSTFN + "/y") |
| 454 | os.symlink(ABSTFN + "/y", ABSTFN + "/k") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 455 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 456 | os.chdir(ABSTFN + "/k") |
| 457 | self.assertEqual(realpath("a"), ABSTFN + "/y/a") |
| 458 | finally: |
| 459 | os.chdir(old_path) |
| 460 | support.unlink(ABSTFN + "/k") |
| 461 | safe_rmdir(ABSTFN + "/y") |
| 462 | safe_rmdir(ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 463 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 464 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 465 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 466 | @skip_if_ABSTFN_contains_backslash |
| 467 | def test_realpath_resolve_before_normalizing(self): |
| 468 | # Bug #990669: Symbolic links should be resolved before we |
| 469 | # normalize the path. E.g.: if we have directories 'a', 'k' and 'y' |
| 470 | # in the following hierarchy: |
| 471 | # a/k/y |
| 472 | # |
| 473 | # and a symbolic link 'link-y' pointing to 'y' in directory 'a', |
| 474 | # then realpath("link-y/..") should return 'k', not 'a'. |
| 475 | try: |
| 476 | old_path = abspath('.') |
| 477 | os.mkdir(ABSTFN) |
| 478 | os.mkdir(ABSTFN + "/k") |
| 479 | os.mkdir(ABSTFN + "/k/y") |
| 480 | os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 481 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 482 | # Absolute path. |
| 483 | self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k") |
| 484 | # Relative path. |
| 485 | os.chdir(dirname(ABSTFN)) |
| 486 | self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), |
| 487 | ABSTFN + "/k") |
| 488 | finally: |
| 489 | os.chdir(old_path) |
| 490 | support.unlink(ABSTFN + "/link-y") |
| 491 | safe_rmdir(ABSTFN + "/k/y") |
| 492 | safe_rmdir(ABSTFN + "/k") |
| 493 | safe_rmdir(ABSTFN) |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 494 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 495 | @unittest.skipUnless(hasattr(os, "symlink"), |
| 496 | "Missing symlink implementation") |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 497 | @skip_if_ABSTFN_contains_backslash |
| 498 | def test_realpath_resolve_first(self): |
| 499 | # Bug #1213894: The first component of the path, if not absolute, |
| 500 | # must be resolved too. |
Georg Brandl | 268e61c | 2005-06-03 14:28:50 +0000 | [diff] [blame] | 501 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 502 | try: |
| 503 | old_path = abspath('.') |
| 504 | os.mkdir(ABSTFN) |
| 505 | os.mkdir(ABSTFN + "/k") |
| 506 | os.symlink(ABSTFN, ABSTFN + "link") |
| 507 | os.chdir(dirname(ABSTFN)) |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 508 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 509 | base = basename(ABSTFN) |
| 510 | self.assertEqual(realpath(base + "link"), ABSTFN) |
| 511 | self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k") |
| 512 | finally: |
| 513 | os.chdir(old_path) |
| 514 | support.unlink(ABSTFN + "link") |
| 515 | safe_rmdir(ABSTFN + "/k") |
| 516 | safe_rmdir(ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 517 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 518 | def test_relpath(self): |
| 519 | (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar") |
| 520 | try: |
| 521 | curdir = os.path.split(os.getcwd())[-1] |
| 522 | self.assertRaises(ValueError, posixpath.relpath, "") |
| 523 | self.assertEqual(posixpath.relpath("a"), "a") |
| 524 | self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a") |
| 525 | self.assertEqual(posixpath.relpath("a/b"), "a/b") |
| 526 | self.assertEqual(posixpath.relpath("../a/b"), "../a/b") |
| 527 | self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a") |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 528 | self.assertEqual(posixpath.relpath("a/b", "../c"), |
| 529 | "../"+curdir+"/a/b") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 530 | self.assertEqual(posixpath.relpath("a", "b/c"), "../../a") |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 531 | self.assertEqual(posixpath.relpath("a", "a"), ".") |
Hirokazu Yamamoto | b08820a | 2010-10-18 12:13:18 +0000 | [diff] [blame] | 532 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat') |
| 533 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/foo/bar"), 'bat') |
| 534 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/"), 'foo/bar/bat') |
| 535 | self.assertEqual(posixpath.relpath("/", "/foo/bar/bat"), '../../..') |
| 536 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x"), '../foo/bar/bat') |
| 537 | self.assertEqual(posixpath.relpath("/x", "/foo/bar/bat"), '../../../x') |
| 538 | self.assertEqual(posixpath.relpath("/", "/"), '.') |
| 539 | self.assertEqual(posixpath.relpath("/a", "/a"), '.') |
| 540 | self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 541 | finally: |
| 542 | os.getcwd = real_getcwd |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 543 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 544 | def test_relpath_bytes(self): |
| 545 | (real_getcwdb, os.getcwdb) = (os.getcwdb, lambda: br"/home/user/bar") |
| 546 | try: |
| 547 | curdir = os.path.split(os.getcwdb())[-1] |
| 548 | self.assertRaises(ValueError, posixpath.relpath, b"") |
| 549 | self.assertEqual(posixpath.relpath(b"a"), b"a") |
| 550 | self.assertEqual(posixpath.relpath(posixpath.abspath(b"a")), b"a") |
| 551 | self.assertEqual(posixpath.relpath(b"a/b"), b"a/b") |
| 552 | self.assertEqual(posixpath.relpath(b"../a/b"), b"../a/b") |
| 553 | self.assertEqual(posixpath.relpath(b"a", b"../b"), |
| 554 | b"../"+curdir+b"/a") |
| 555 | self.assertEqual(posixpath.relpath(b"a/b", b"../c"), |
| 556 | b"../"+curdir+b"/a/b") |
| 557 | self.assertEqual(posixpath.relpath(b"a", b"b/c"), b"../../a") |
| 558 | self.assertEqual(posixpath.relpath(b"a", b"a"), b".") |
Hirokazu Yamamoto | b08820a | 2010-10-18 12:13:18 +0000 | [diff] [blame] | 559 | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x/y/z"), b'../../../foo/bar/bat') |
| 560 | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/foo/bar"), b'bat') |
| 561 | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/"), b'foo/bar/bat') |
| 562 | self.assertEqual(posixpath.relpath(b"/", b"/foo/bar/bat"), b'../../..') |
| 563 | self.assertEqual(posixpath.relpath(b"/foo/bar/bat", b"/x"), b'../foo/bar/bat') |
| 564 | self.assertEqual(posixpath.relpath(b"/x", b"/foo/bar/bat"), b'../../../x') |
| 565 | self.assertEqual(posixpath.relpath(b"/", b"/"), b'.') |
| 566 | self.assertEqual(posixpath.relpath(b"/a", b"/a"), b'.') |
| 567 | self.assertEqual(posixpath.relpath(b"/a/b", b"/a/b"), b'.') |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 568 | |
| 569 | self.assertRaises(TypeError, posixpath.relpath, b"bytes", "str") |
| 570 | self.assertRaises(TypeError, posixpath.relpath, "str", b"bytes") |
| 571 | finally: |
| 572 | os.getcwdb = real_getcwdb |
| 573 | |
Michael Foord | 07926f0 | 2011-03-16 17:19:16 -0400 | [diff] [blame] | 574 | def test_sameopenfile(self): |
| 575 | fname = support.TESTFN + "1" |
| 576 | with open(fname, "wb") as a, open(fname, "wb") as b: |
| 577 | self.assertTrue(posixpath.sameopenfile(a.fileno(), b.fileno())) |
| 578 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 579 | |
| 580 | class PosixCommonTest(test_genericpath.CommonTest): |
| 581 | pathmodule = posixpath |
| 582 | attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat'] |
| 583 | |
| 584 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 585 | def test_main(): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 586 | support.run_unittest(PosixPathTest, PosixCommonTest) |
| 587 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 588 | |
| 589 | if __name__=="__main__": |
| 590 | test_main() |