Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 1 | import unittest |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 2 | from test import test_support, test_genericpath |
Serhiy Storchaka | 7c7b4b5 | 2015-09-06 14:16:18 +0300 | [diff] [blame] | 3 | from test import test_support as support |
Ezio Melotti | 9e9af21 | 2010-02-20 22:34:21 +0000 | [diff] [blame] | 4 | |
Serhiy Storchaka | 2bd8b22 | 2015-02-13 12:02:05 +0200 | [diff] [blame] | 5 | import posixpath |
| 6 | import os |
| 7 | import sys |
Georg Brandl | b86d3fa | 2010-02-07 12:55:12 +0000 | [diff] [blame] | 8 | from posixpath import realpath, abspath, dirname, basename |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 9 | |
| 10 | # An absolute path to a temporary filename for testing. We can't rely on TESTFN |
| 11 | # being an absolute path, so we need this. |
| 12 | |
| 13 | ABSTFN = abspath(test_support.TESTFN) |
Skip Montanaro | e809b00 | 2000-07-12 00:20:08 +0000 | [diff] [blame] | 14 | |
Serhiy Storchaka | c8e75ba | 2013-02-18 13:32:06 +0200 | [diff] [blame] | 15 | def skip_if_ABSTFN_contains_backslash(test): |
| 16 | """ |
| 17 | On Windows, posixpath.abspath still returns paths with backslashes |
| 18 | instead of posix forward slashes. If this is the case, several tests |
| 19 | fail, so skip them. |
| 20 | """ |
| 21 | found_backslash = '\\' in ABSTFN |
| 22 | msg = "ABSTFN is not a posix path - tests fail" |
| 23 | return [test, unittest.skip(msg)(test)][found_backslash] |
| 24 | |
Collin Winter | dbead56 | 2007-03-10 02:23:40 +0000 | [diff] [blame] | 25 | def safe_rmdir(dirname): |
| 26 | try: |
| 27 | os.rmdir(dirname) |
| 28 | except OSError: |
| 29 | pass |
| 30 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 31 | class PosixPathTest(unittest.TestCase): |
Skip Montanaro | e809b00 | 2000-07-12 00:20:08 +0000 | [diff] [blame] | 32 | |
Collin Winter | dbead56 | 2007-03-10 02:23:40 +0000 | [diff] [blame] | 33 | def setUp(self): |
| 34 | self.tearDown() |
| 35 | |
| 36 | def tearDown(self): |
| 37 | for suffix in ["", "1", "2"]: |
| 38 | test_support.unlink(test_support.TESTFN + suffix) |
| 39 | safe_rmdir(test_support.TESTFN + suffix) |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 40 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 41 | def test_join(self): |
| 42 | self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), "/bar/baz") |
| 43 | self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz") |
| 44 | self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), "/foo/bar/baz/") |
Skip Montanaro | e809b00 | 2000-07-12 00:20:08 +0000 | [diff] [blame] | 45 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 46 | def test_split(self): |
| 47 | self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar")) |
| 48 | self.assertEqual(posixpath.split("/"), ("/", "")) |
| 49 | self.assertEqual(posixpath.split("foo"), ("", "foo")) |
| 50 | self.assertEqual(posixpath.split("////foo"), ("////", "foo")) |
| 51 | self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar")) |
| 52 | |
Martin v. Löwis | 05c075d | 2007-03-07 11:04:33 +0000 | [diff] [blame] | 53 | def splitextTest(self, path, filename, ext): |
| 54 | self.assertEqual(posixpath.splitext(path), (filename, ext)) |
| 55 | self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext)) |
| 56 | self.assertEqual(posixpath.splitext("abc/" + path), ("abc/" + filename, ext)) |
| 57 | self.assertEqual(posixpath.splitext("abc.def/" + path), ("abc.def/" + filename, ext)) |
| 58 | self.assertEqual(posixpath.splitext("/abc.def/" + path), ("/abc.def/" + filename, ext)) |
| 59 | self.assertEqual(posixpath.splitext(path + "/"), (filename + ext + "/", "")) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 60 | |
Martin v. Löwis | 05c075d | 2007-03-07 11:04:33 +0000 | [diff] [blame] | 61 | def test_splitext(self): |
| 62 | self.splitextTest("foo.bar", "foo", ".bar") |
| 63 | self.splitextTest("foo.boo.bar", "foo.boo", ".bar") |
| 64 | self.splitextTest("foo.boo.biff.bar", "foo.boo.biff", ".bar") |
| 65 | self.splitextTest(".csh.rc", ".csh", ".rc") |
| 66 | self.splitextTest("nodots", "nodots", "") |
| 67 | self.splitextTest(".cshrc", ".cshrc", "") |
| 68 | self.splitextTest("...manydots", "...manydots", "") |
| 69 | self.splitextTest("...manydots.ext", "...manydots", ".ext") |
| 70 | self.splitextTest(".", ".", "") |
| 71 | self.splitextTest("..", "..", "") |
| 72 | self.splitextTest("........", "........", "") |
| 73 | self.splitextTest("", "", "") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 74 | |
| 75 | def test_isabs(self): |
| 76 | self.assertIs(posixpath.isabs(""), False) |
| 77 | self.assertIs(posixpath.isabs("/"), True) |
| 78 | self.assertIs(posixpath.isabs("/foo"), True) |
| 79 | self.assertIs(posixpath.isabs("/foo/bar"), True) |
| 80 | self.assertIs(posixpath.isabs("foo/bar"), False) |
| 81 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 82 | def test_basename(self): |
| 83 | self.assertEqual(posixpath.basename("/foo/bar"), "bar") |
| 84 | self.assertEqual(posixpath.basename("/"), "") |
| 85 | self.assertEqual(posixpath.basename("foo"), "foo") |
| 86 | self.assertEqual(posixpath.basename("////foo"), "foo") |
| 87 | self.assertEqual(posixpath.basename("//foo//bar"), "bar") |
| 88 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 89 | def test_dirname(self): |
| 90 | self.assertEqual(posixpath.dirname("/foo/bar"), "/foo") |
| 91 | self.assertEqual(posixpath.dirname("/"), "/") |
| 92 | self.assertEqual(posixpath.dirname("foo"), "") |
| 93 | self.assertEqual(posixpath.dirname("////foo"), "////") |
| 94 | self.assertEqual(posixpath.dirname("//foo//bar"), "//foo") |
| 95 | |
Tim Peters | ea5962f | 2007-03-12 18:07:52 +0000 | [diff] [blame] | 96 | def test_islink(self): |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 97 | self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False) |
| 98 | f = open(test_support.TESTFN + "1", "wb") |
| 99 | try: |
| 100 | f.write("foo") |
| 101 | f.close() |
| 102 | self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False) |
| 103 | if hasattr(os, "symlink"): |
| 104 | os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2") |
| 105 | self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True) |
| 106 | os.remove(test_support.TESTFN + "1") |
| 107 | self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True) |
| 108 | self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False) |
Johannes Gijsbers | ae882f7 | 2004-08-30 10:19:56 +0000 | [diff] [blame] | 109 | self.assertIs(posixpath.lexists(test_support.TESTFN + "2"), True) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 110 | finally: |
| 111 | if not f.close(): |
| 112 | f.close() |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 113 | |
Collin Winter | dbead56 | 2007-03-10 02:23:40 +0000 | [diff] [blame] | 114 | def test_samefile(self): |
| 115 | f = open(test_support.TESTFN + "1", "wb") |
| 116 | try: |
| 117 | f.write("foo") |
| 118 | f.close() |
| 119 | self.assertIs( |
| 120 | posixpath.samefile( |
| 121 | test_support.TESTFN + "1", |
| 122 | test_support.TESTFN + "1" |
| 123 | ), |
| 124 | True |
| 125 | ) |
Benjamin Peterson | 8a1a17b | 2012-11-30 16:12:15 -0500 | [diff] [blame] | 126 | |
| 127 | # If we don't have links, assume that os.stat doesn't return |
| 128 | # reasonable inode information and thus, that samefile() doesn't |
| 129 | # work. |
Collin Winter | dbead56 | 2007-03-10 02:23:40 +0000 | [diff] [blame] | 130 | if hasattr(os, "symlink"): |
| 131 | os.symlink( |
| 132 | test_support.TESTFN + "1", |
| 133 | test_support.TESTFN + "2" |
| 134 | ) |
| 135 | self.assertIs( |
| 136 | posixpath.samefile( |
| 137 | test_support.TESTFN + "1", |
| 138 | test_support.TESTFN + "2" |
| 139 | ), |
| 140 | True |
| 141 | ) |
| 142 | os.remove(test_support.TESTFN + "2") |
| 143 | f = open(test_support.TESTFN + "2", "wb") |
| 144 | f.write("bar") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 145 | f.close() |
| 146 | self.assertIs( |
| 147 | posixpath.samefile( |
| 148 | test_support.TESTFN + "1", |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 149 | test_support.TESTFN + "2" |
Collin Winter | dbead56 | 2007-03-10 02:23:40 +0000 | [diff] [blame] | 150 | ), |
| 151 | False |
| 152 | ) |
| 153 | finally: |
| 154 | if not f.close(): |
| 155 | f.close() |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 156 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 157 | def test_samestat(self): |
| 158 | f = open(test_support.TESTFN + "1", "wb") |
| 159 | try: |
| 160 | f.write("foo") |
| 161 | f.close() |
| 162 | self.assertIs( |
| 163 | posixpath.samestat( |
| 164 | os.stat(test_support.TESTFN + "1"), |
| 165 | os.stat(test_support.TESTFN + "1") |
| 166 | ), |
| 167 | True |
| 168 | ) |
Benjamin Peterson | 7196605 | 2012-11-30 16:13:14 -0500 | [diff] [blame] | 169 | # If we don't have links, assume that os.stat() doesn't return |
| 170 | # reasonable inode information and thus, that samestat() doesn't |
| 171 | # work. |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 172 | if hasattr(os, "symlink"): |
Benjamin Peterson | 8a1a17b | 2012-11-30 16:12:15 -0500 | [diff] [blame] | 173 | os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2") |
| 174 | self.assertIs( |
| 175 | posixpath.samestat( |
| 176 | os.stat(test_support.TESTFN + "1"), |
| 177 | os.stat(test_support.TESTFN + "2") |
| 178 | ), |
| 179 | True |
| 180 | ) |
| 181 | os.remove(test_support.TESTFN + "2") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 182 | f = open(test_support.TESTFN + "2", "wb") |
| 183 | f.write("bar") |
| 184 | f.close() |
| 185 | self.assertIs( |
| 186 | posixpath.samestat( |
| 187 | os.stat(test_support.TESTFN + "1"), |
| 188 | os.stat(test_support.TESTFN + "2") |
| 189 | ), |
| 190 | False |
| 191 | ) |
| 192 | finally: |
| 193 | if not f.close(): |
| 194 | f.close() |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 195 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 196 | def test_ismount(self): |
| 197 | self.assertIs(posixpath.ismount("/"), True) |
| 198 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 199 | def test_expanduser(self): |
| 200 | self.assertEqual(posixpath.expanduser("foo"), "foo") |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 201 | try: |
| 202 | import pwd |
| 203 | except ImportError: |
| 204 | pass |
| 205 | else: |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 206 | self.assertIsInstance(posixpath.expanduser("~/"), basestring) |
Neal Norwitz | 168e73d | 2003-07-01 03:33:31 +0000 | [diff] [blame] | 207 | # if home directory == root directory, this test makes no sense |
| 208 | if posixpath.expanduser("~") != '/': |
| 209 | self.assertEqual( |
| 210 | posixpath.expanduser("~") + "/", |
| 211 | posixpath.expanduser("~/") |
| 212 | ) |
Ezio Melotti | b0f5adc | 2010-01-24 16:58:36 +0000 | [diff] [blame] | 213 | self.assertIsInstance(posixpath.expanduser("~root/"), basestring) |
| 214 | self.assertIsInstance(posixpath.expanduser("~foo/"), basestring) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 215 | |
Walter Dörwald | 4b965f6 | 2009-04-26 20:51:44 +0000 | [diff] [blame] | 216 | with test_support.EnvironmentVarGuard() as env: |
Walter Dörwald | 6733bed | 2009-05-01 17:35:37 +0000 | [diff] [blame] | 217 | env['HOME'] = '/' |
Walter Dörwald | 4b965f6 | 2009-04-26 20:51:44 +0000 | [diff] [blame] | 218 | self.assertEqual(posixpath.expanduser("~"), "/") |
Jesus Cea | f2011e3 | 2012-05-10 05:01:11 +0200 | [diff] [blame] | 219 | self.assertEqual(posixpath.expanduser("~/foo"), "/foo") |
Georg Brandl | 3f0ef20 | 2009-04-05 14:48:49 +0000 | [diff] [blame] | 220 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 221 | def test_normpath(self): |
| 222 | self.assertEqual(posixpath.normpath(""), ".") |
| 223 | self.assertEqual(posixpath.normpath("/"), "/") |
| 224 | self.assertEqual(posixpath.normpath("//"), "//") |
| 225 | self.assertEqual(posixpath.normpath("///"), "/") |
| 226 | self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar") |
| 227 | self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz") |
| 228 | self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar") |
| 229 | |
Serhiy Storchaka | c8e75ba | 2013-02-18 13:32:06 +0200 | [diff] [blame] | 230 | @skip_if_ABSTFN_contains_backslash |
Serhiy Storchaka | 142d2bc | 2013-02-18 12:20:44 +0200 | [diff] [blame] | 231 | def test_realpath_curdir(self): |
| 232 | self.assertEqual(realpath('.'), os.getcwd()) |
| 233 | self.assertEqual(realpath('./.'), os.getcwd()) |
| 234 | self.assertEqual(realpath('/'.join(['.'] * 100)), os.getcwd()) |
| 235 | |
Serhiy Storchaka | c8e75ba | 2013-02-18 13:32:06 +0200 | [diff] [blame] | 236 | @skip_if_ABSTFN_contains_backslash |
Serhiy Storchaka | 142d2bc | 2013-02-18 12:20:44 +0200 | [diff] [blame] | 237 | def test_realpath_pardir(self): |
| 238 | self.assertEqual(realpath('..'), dirname(os.getcwd())) |
| 239 | self.assertEqual(realpath('../..'), dirname(dirname(os.getcwd()))) |
| 240 | self.assertEqual(realpath('/'.join(['..'] * 100)), '/') |
| 241 | |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 242 | if hasattr(os, "symlink"): |
| 243 | def test_realpath_basic(self): |
| 244 | # Basic operation. |
| 245 | try: |
| 246 | os.symlink(ABSTFN+"1", ABSTFN) |
| 247 | self.assertEqual(realpath(ABSTFN), ABSTFN+"1") |
| 248 | finally: |
Collin Winter | dbead56 | 2007-03-10 02:23:40 +0000 | [diff] [blame] | 249 | test_support.unlink(ABSTFN) |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 250 | |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 251 | def test_realpath_symlink_loops(self): |
| 252 | # Bug #930024, return the path unchanged if we get into an infinite |
| 253 | # symlink loop. |
| 254 | try: |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 255 | os.symlink(ABSTFN, ABSTFN) |
| 256 | self.assertEqual(realpath(ABSTFN), ABSTFN) |
| 257 | |
| 258 | os.symlink(ABSTFN+"1", ABSTFN+"2") |
| 259 | os.symlink(ABSTFN+"2", ABSTFN+"1") |
| 260 | self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1") |
| 261 | self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2") |
| 262 | |
Serhiy Storchaka | 0dd3d30 | 2013-02-10 12:21:49 +0200 | [diff] [blame] | 263 | self.assertEqual(realpath(ABSTFN+"1/x"), ABSTFN+"1/x") |
| 264 | self.assertEqual(realpath(ABSTFN+"1/.."), dirname(ABSTFN)) |
| 265 | self.assertEqual(realpath(ABSTFN+"1/../x"), dirname(ABSTFN) + "/x") |
| 266 | os.symlink(ABSTFN+"x", ABSTFN+"y") |
| 267 | self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "y"), |
| 268 | ABSTFN + "y") |
| 269 | self.assertEqual(realpath(ABSTFN+"1/../" + basename(ABSTFN) + "1"), |
| 270 | ABSTFN + "1") |
| 271 | |
| 272 | os.symlink(basename(ABSTFN) + "a/b", ABSTFN+"a") |
| 273 | self.assertEqual(realpath(ABSTFN+"a"), ABSTFN+"a/b") |
| 274 | |
| 275 | os.symlink("../" + basename(dirname(ABSTFN)) + "/" + |
| 276 | basename(ABSTFN) + "c", ABSTFN+"c") |
| 277 | self.assertEqual(realpath(ABSTFN+"c"), ABSTFN+"c") |
| 278 | |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 279 | # Test using relative path as well. |
Serhiy Storchaka | 7c7b4b5 | 2015-09-06 14:16:18 +0300 | [diff] [blame] | 280 | with support.change_cwd(dirname(ABSTFN)): |
| 281 | self.assertEqual(realpath(basename(ABSTFN)), ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 282 | finally: |
Collin Winter | dbead56 | 2007-03-10 02:23:40 +0000 | [diff] [blame] | 283 | test_support.unlink(ABSTFN) |
| 284 | test_support.unlink(ABSTFN+"1") |
| 285 | test_support.unlink(ABSTFN+"2") |
Serhiy Storchaka | 0dd3d30 | 2013-02-10 12:21:49 +0200 | [diff] [blame] | 286 | test_support.unlink(ABSTFN+"y") |
| 287 | test_support.unlink(ABSTFN+"c") |
Ezio Melotti | c86e866 | 2013-03-01 20:56:13 +0200 | [diff] [blame] | 288 | test_support.unlink(ABSTFN+"a") |
Serhiy Storchaka | 0dd3d30 | 2013-02-10 12:21:49 +0200 | [diff] [blame] | 289 | |
| 290 | def test_realpath_repeated_indirect_symlinks(self): |
| 291 | # Issue #6975. |
| 292 | try: |
| 293 | os.mkdir(ABSTFN) |
| 294 | os.symlink('../' + basename(ABSTFN), ABSTFN + '/self') |
| 295 | os.symlink('self/self/self', ABSTFN + '/link') |
| 296 | self.assertEqual(realpath(ABSTFN + '/link'), ABSTFN) |
| 297 | finally: |
| 298 | test_support.unlink(ABSTFN + '/self') |
| 299 | test_support.unlink(ABSTFN + '/link') |
| 300 | safe_rmdir(ABSTFN) |
| 301 | |
| 302 | def test_realpath_deep_recursion(self): |
| 303 | depth = 10 |
Serhiy Storchaka | 0dd3d30 | 2013-02-10 12:21:49 +0200 | [diff] [blame] | 304 | try: |
| 305 | os.mkdir(ABSTFN) |
| 306 | for i in range(depth): |
| 307 | os.symlink('/'.join(['%d' % i] * 10), ABSTFN + '/%d' % (i + 1)) |
| 308 | os.symlink('.', ABSTFN + '/0') |
| 309 | self.assertEqual(realpath(ABSTFN + '/%d' % depth), ABSTFN) |
| 310 | |
| 311 | # Test using relative path as well. |
Serhiy Storchaka | 7c7b4b5 | 2015-09-06 14:16:18 +0300 | [diff] [blame] | 312 | with support.change_cwd(ABSTFN): |
| 313 | self.assertEqual(realpath('%d' % depth), ABSTFN) |
Serhiy Storchaka | 0dd3d30 | 2013-02-10 12:21:49 +0200 | [diff] [blame] | 314 | finally: |
Serhiy Storchaka | 0dd3d30 | 2013-02-10 12:21:49 +0200 | [diff] [blame] | 315 | for i in range(depth + 1): |
| 316 | test_support.unlink(ABSTFN + '/%d' % i) |
| 317 | safe_rmdir(ABSTFN) |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 318 | |
Tim Peters | a45cacf | 2004-08-20 03:47:14 +0000 | [diff] [blame] | 319 | def test_realpath_resolve_parents(self): |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 320 | # We also need to resolve any symlinks in the parents of a relative |
| 321 | # path passed to realpath. E.g.: current working directory is |
| 322 | # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call |
| 323 | # realpath("a"). This should return /usr/share/doc/a/. |
| 324 | try: |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 325 | os.mkdir(ABSTFN) |
| 326 | os.mkdir(ABSTFN + "/y") |
| 327 | os.symlink(ABSTFN + "/y", ABSTFN + "/k") |
| 328 | |
Serhiy Storchaka | 7c7b4b5 | 2015-09-06 14:16:18 +0300 | [diff] [blame] | 329 | with support.change_cwd(ABSTFN + "/k"): |
| 330 | self.assertEqual(realpath("a"), ABSTFN + "/y/a") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 331 | finally: |
Collin Winter | dbead56 | 2007-03-10 02:23:40 +0000 | [diff] [blame] | 332 | test_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 | |
| 336 | def test_realpath_resolve_before_normalizing(self): |
| 337 | # Bug #990669: Symbolic links should be resolved before we |
| 338 | # normalize the path. E.g.: if we have directories 'a', 'k' and 'y' |
| 339 | # in the following hierarchy: |
| 340 | # a/k/y |
| 341 | # |
| 342 | # and a symbolic link 'link-y' pointing to 'y' in directory 'a', |
| 343 | # then realpath("link-y/..") should return 'k', not 'a'. |
| 344 | try: |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 345 | os.mkdir(ABSTFN) |
| 346 | os.mkdir(ABSTFN + "/k") |
| 347 | os.mkdir(ABSTFN + "/k/y") |
| 348 | os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y") |
| 349 | |
| 350 | # Absolute path. |
| 351 | self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k") |
| 352 | # Relative path. |
Serhiy Storchaka | 7c7b4b5 | 2015-09-06 14:16:18 +0300 | [diff] [blame] | 353 | with support.change_cwd(dirname(ABSTFN)): |
| 354 | self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), |
| 355 | ABSTFN + "/k") |
Johannes Gijsbers | 4ec4064 | 2004-08-14 15:01:53 +0000 | [diff] [blame] | 356 | finally: |
Collin Winter | dbead56 | 2007-03-10 02:23:40 +0000 | [diff] [blame] | 357 | test_support.unlink(ABSTFN + "/link-y") |
| 358 | safe_rmdir(ABSTFN + "/k/y") |
| 359 | safe_rmdir(ABSTFN + "/k") |
| 360 | safe_rmdir(ABSTFN) |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 361 | |
Georg Brandl | 268e61c | 2005-06-03 14:28:50 +0000 | [diff] [blame] | 362 | def test_realpath_resolve_first(self): |
| 363 | # Bug #1213894: The first component of the path, if not absolute, |
| 364 | # must be resolved too. |
| 365 | |
| 366 | try: |
Georg Brandl | 268e61c | 2005-06-03 14:28:50 +0000 | [diff] [blame] | 367 | os.mkdir(ABSTFN) |
| 368 | os.mkdir(ABSTFN + "/k") |
| 369 | os.symlink(ABSTFN, ABSTFN + "link") |
Serhiy Storchaka | 7c7b4b5 | 2015-09-06 14:16:18 +0300 | [diff] [blame] | 370 | with support.change_cwd(dirname(ABSTFN)): |
| 371 | base = basename(ABSTFN) |
| 372 | self.assertEqual(realpath(base + "link"), ABSTFN) |
| 373 | self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k") |
Georg Brandl | 268e61c | 2005-06-03 14:28:50 +0000 | [diff] [blame] | 374 | finally: |
Collin Winter | dbead56 | 2007-03-10 02:23:40 +0000 | [diff] [blame] | 375 | test_support.unlink(ABSTFN + "link") |
| 376 | safe_rmdir(ABSTFN + "/k") |
| 377 | safe_rmdir(ABSTFN) |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 378 | |
Collin Winter | 6f18774 | 2007-03-16 22:16:08 +0000 | [diff] [blame] | 379 | def test_relpath(self): |
Collin Winter | 75c7eb4 | 2007-03-23 22:24:39 +0000 | [diff] [blame] | 380 | (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar") |
| 381 | try: |
| 382 | curdir = os.path.split(os.getcwd())[-1] |
| 383 | self.assertRaises(ValueError, posixpath.relpath, "") |
| 384 | self.assertEqual(posixpath.relpath("a"), "a") |
| 385 | self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a") |
| 386 | self.assertEqual(posixpath.relpath("a/b"), "a/b") |
| 387 | self.assertEqual(posixpath.relpath("../a/b"), "../a/b") |
| 388 | self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a") |
| 389 | self.assertEqual(posixpath.relpath("a/b", "../c"), "../"+curdir+"/a/b") |
| 390 | self.assertEqual(posixpath.relpath("a", "b/c"), "../../a") |
Georg Brandl | 183a084 | 2008-01-06 14:27:15 +0000 | [diff] [blame] | 391 | self.assertEqual(posixpath.relpath("a", "a"), ".") |
Hirokazu Yamamoto | 50f7d7e | 2010-10-18 13:55:29 +0000 | [diff] [blame] | 392 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x/y/z"), '../../../foo/bar/bat') |
| 393 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/foo/bar"), 'bat') |
| 394 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/"), 'foo/bar/bat') |
| 395 | self.assertEqual(posixpath.relpath("/", "/foo/bar/bat"), '../../..') |
| 396 | self.assertEqual(posixpath.relpath("/foo/bar/bat", "/x"), '../foo/bar/bat') |
| 397 | self.assertEqual(posixpath.relpath("/x", "/foo/bar/bat"), '../../../x') |
| 398 | self.assertEqual(posixpath.relpath("/", "/"), '.') |
| 399 | self.assertEqual(posixpath.relpath("/a", "/a"), '.') |
| 400 | self.assertEqual(posixpath.relpath("/a/b", "/a/b"), '.') |
Collin Winter | 75c7eb4 | 2007-03-23 22:24:39 +0000 | [diff] [blame] | 401 | finally: |
| 402 | os.getcwd = real_getcwd |
Collin Winter | 6f18774 | 2007-03-16 22:16:08 +0000 | [diff] [blame] | 403 | |
Serhiy Storchaka | 2bd8b22 | 2015-02-13 12:02:05 +0200 | [diff] [blame] | 404 | @test_support.requires_unicode |
| 405 | def test_expandvars_nonascii_word(self): |
| 406 | encoding = sys.getfilesystemencoding() |
| 407 | # Non-ASCII word characters |
| 408 | letters = test_support.u(r'\xe6\u0130\u0141\u03c6\u041a\u05d0\u062a\u0e01') |
| 409 | uwnonascii = letters.encode(encoding, 'ignore').decode(encoding)[:3] |
| 410 | swnonascii = uwnonascii.encode(encoding) |
| 411 | if not swnonascii: |
Serhiy Storchaka | 3be0d0e | 2015-02-13 12:47:08 +0200 | [diff] [blame] | 412 | self.skipTest('Needs non-ASCII word characters') |
Serhiy Storchaka | 2bd8b22 | 2015-02-13 12:02:05 +0200 | [diff] [blame] | 413 | with test_support.EnvironmentVarGuard() as env: |
| 414 | env.clear() |
| 415 | env[swnonascii] = 'baz' + swnonascii |
| 416 | self.assertEqual(posixpath.expandvars(u'$%s bar' % uwnonascii), |
| 417 | u'baz%s bar' % uwnonascii) |
| 418 | |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 419 | |
| 420 | class PosixCommonTest(test_genericpath.CommonTest): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 421 | pathmodule = posixpath |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 422 | attributes = ['relpath', 'samefile', 'sameopenfile', 'samestat'] |
| 423 | |
| 424 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 425 | def test_main(): |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 426 | test_support.run_unittest(PosixPathTest, PosixCommonTest) |
| 427 | |
Brett Cannon | b47243a | 2003-06-16 21:54:50 +0000 | [diff] [blame] | 428 | |
| 429 | if __name__=="__main__": |
| 430 | test_main() |