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