Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 1 | """ |
| 2 | Tests common to genericpath, macpath, ntpath and posixpath |
| 3 | """ |
| 4 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5 | import genericpath |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6 | import os |
Victor Stinner | 1efde67 | 2010-04-18 08:23:42 +0000 | [diff] [blame] | 7 | import sys |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 8 | import unittest |
| 9 | import warnings |
| 10 | from test import support |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 11 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 12 | |
| 13 | def safe_rmdir(dirname): |
| 14 | try: |
| 15 | os.rmdir(dirname) |
| 16 | except OSError: |
| 17 | pass |
| 18 | |
| 19 | |
Ezio Melotti | d0dfe9a | 2013-01-10 03:12:50 +0200 | [diff] [blame] | 20 | class GenericTest: |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 21 | common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime', |
| 22 | 'getmtime', 'exists', 'isdir', 'isfile'] |
| 23 | attributes = [] |
| 24 | |
| 25 | def test_no_argument(self): |
| 26 | for attr in self.common_attributes + self.attributes: |
| 27 | with self.assertRaises(TypeError): |
| 28 | getattr(self.pathmodule, attr)() |
| 29 | raise self.fail("{}.{}() did not raise a TypeError" |
| 30 | .format(self.pathmodule.__name__, attr)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 31 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 32 | def test_commonprefix(self): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 33 | commonprefix = self.pathmodule.commonprefix |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 34 | self.assertEqual( |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 35 | commonprefix([]), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 36 | "" |
| 37 | ) |
| 38 | self.assertEqual( |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 39 | commonprefix(["/home/swenson/spam", "/home/swen/spam"]), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 40 | "/home/swen" |
| 41 | ) |
| 42 | self.assertEqual( |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 43 | commonprefix(["/home/swen/spam", "/home/swen/eggs"]), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 44 | "/home/swen/" |
| 45 | ) |
| 46 | self.assertEqual( |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 47 | commonprefix(["/home/swen/spam", "/home/swen/spam"]), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 48 | "/home/swen/spam" |
| 49 | ) |
Florent Xicluna | e3ed2e0 | 2010-03-08 12:42:20 +0000 | [diff] [blame] | 50 | self.assertEqual( |
| 51 | commonprefix(["home:swenson:spam", "home:swen:spam"]), |
| 52 | "home:swen" |
| 53 | ) |
| 54 | self.assertEqual( |
| 55 | commonprefix([":home:swen:spam", ":home:swen:eggs"]), |
| 56 | ":home:swen:" |
| 57 | ) |
| 58 | self.assertEqual( |
| 59 | commonprefix([":home:swen:spam", ":home:swen:spam"]), |
| 60 | ":home:swen:spam" |
| 61 | ) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 62 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 63 | self.assertEqual( |
| 64 | commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]), |
| 65 | b"/home/swen" |
| 66 | ) |
| 67 | self.assertEqual( |
| 68 | commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]), |
| 69 | b"/home/swen/" |
| 70 | ) |
| 71 | self.assertEqual( |
| 72 | commonprefix([b"/home/swen/spam", b"/home/swen/spam"]), |
| 73 | b"/home/swen/spam" |
| 74 | ) |
Florent Xicluna | e3ed2e0 | 2010-03-08 12:42:20 +0000 | [diff] [blame] | 75 | self.assertEqual( |
| 76 | commonprefix([b"home:swenson:spam", b"home:swen:spam"]), |
| 77 | b"home:swen" |
| 78 | ) |
| 79 | self.assertEqual( |
| 80 | commonprefix([b":home:swen:spam", b":home:swen:eggs"]), |
| 81 | b":home:swen:" |
| 82 | ) |
| 83 | self.assertEqual( |
| 84 | commonprefix([b":home:swen:spam", b":home:swen:spam"]), |
| 85 | b":home:swen:spam" |
| 86 | ) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 87 | |
| 88 | testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', |
| 89 | 'aXc', 'abd', 'ab', 'aX', 'abcX'] |
| 90 | for s1 in testlist: |
| 91 | for s2 in testlist: |
| 92 | p = commonprefix([s1, s2]) |
| 93 | self.assertTrue(s1.startswith(p)) |
| 94 | self.assertTrue(s2.startswith(p)) |
| 95 | if s1 != s2: |
| 96 | n = len(p) |
| 97 | self.assertNotEqual(s1[n:n+1], s2[n:n+1]) |
| 98 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 99 | def test_getsize(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 100 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 101 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 102 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 103 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 104 | self.assertEqual(self.pathmodule.getsize(support.TESTFN), 3) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 105 | finally: |
| 106 | if not f.closed: |
| 107 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 108 | support.unlink(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 109 | |
| 110 | def test_time(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 111 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 112 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 113 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 114 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 115 | f = open(support.TESTFN, "ab") |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 116 | f.write(b"bar") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 117 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 118 | f = open(support.TESTFN, "rb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 119 | d = f.read() |
| 120 | f.close() |
Guido van Rossum | dc12288 | 2007-07-10 12:09:13 +0000 | [diff] [blame] | 121 | self.assertEqual(d, b"foobar") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 122 | |
Ezio Melotti | a11865b | 2010-02-20 09:47:55 +0000 | [diff] [blame] | 123 | self.assertLessEqual( |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 124 | self.pathmodule.getctime(support.TESTFN), |
| 125 | self.pathmodule.getmtime(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 126 | ) |
| 127 | finally: |
| 128 | if not f.closed: |
| 129 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 130 | support.unlink(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 131 | |
| 132 | def test_exists(self): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 133 | self.assertIs(self.pathmodule.exists(support.TESTFN), False) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 134 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 135 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 136 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 137 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 138 | self.assertIs(self.pathmodule.exists(support.TESTFN), True) |
| 139 | if not self.pathmodule == genericpath: |
| 140 | self.assertIs(self.pathmodule.lexists(support.TESTFN), |
| 141 | True) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 142 | finally: |
| 143 | if not f.close(): |
| 144 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 145 | support.unlink(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 146 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 147 | @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()") |
| 148 | def test_exists_fd(self): |
| 149 | r, w = os.pipe() |
| 150 | try: |
| 151 | self.assertTrue(self.pathmodule.exists(r)) |
| 152 | finally: |
| 153 | os.close(r) |
| 154 | os.close(w) |
| 155 | self.assertFalse(self.pathmodule.exists(r)) |
| 156 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 157 | def test_isdir(self): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 158 | self.assertIs(self.pathmodule.isdir(support.TESTFN), False) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 159 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 160 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 161 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 162 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 163 | self.assertIs(self.pathmodule.isdir(support.TESTFN), False) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 164 | os.remove(support.TESTFN) |
| 165 | os.mkdir(support.TESTFN) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 166 | self.assertIs(self.pathmodule.isdir(support.TESTFN), True) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 167 | os.rmdir(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 168 | finally: |
| 169 | if not f.close(): |
| 170 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 171 | support.unlink(support.TESTFN) |
| 172 | safe_rmdir(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 173 | |
| 174 | def test_isfile(self): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 175 | self.assertIs(self.pathmodule.isfile(support.TESTFN), False) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 176 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 177 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 178 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 179 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 180 | self.assertIs(self.pathmodule.isfile(support.TESTFN), True) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 181 | os.remove(support.TESTFN) |
| 182 | os.mkdir(support.TESTFN) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 183 | self.assertIs(self.pathmodule.isfile(support.TESTFN), False) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 184 | os.rmdir(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 185 | finally: |
| 186 | if not f.close(): |
| 187 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 188 | support.unlink(support.TESTFN) |
| 189 | safe_rmdir(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 190 | |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 191 | @staticmethod |
| 192 | def _create_file(filename): |
| 193 | with open(filename, 'wb') as f: |
| 194 | f.write(b'foo') |
| 195 | |
| 196 | def test_samefile(self): |
| 197 | try: |
| 198 | test_fn = support.TESTFN + "1" |
| 199 | self._create_file(test_fn) |
| 200 | self.assertTrue(self.pathmodule.samefile(test_fn, test_fn)) |
| 201 | self.assertRaises(TypeError, self.pathmodule.samefile) |
| 202 | finally: |
| 203 | os.remove(test_fn) |
| 204 | |
| 205 | @support.skip_unless_symlink |
Brian Curtin | e701ec5 | 2012-12-26 07:36:16 -0600 | [diff] [blame] | 206 | def test_samefile_on_symlink(self): |
| 207 | self._test_samefile_on_link_func(os.symlink) |
| 208 | |
| 209 | def test_samefile_on_link(self): |
| 210 | self._test_samefile_on_link_func(os.link) |
| 211 | |
| 212 | def _test_samefile_on_link_func(self, func): |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 213 | try: |
| 214 | test_fn1 = support.TESTFN + "1" |
| 215 | test_fn2 = support.TESTFN + "2" |
| 216 | self._create_file(test_fn1) |
| 217 | |
Brian Curtin | e701ec5 | 2012-12-26 07:36:16 -0600 | [diff] [blame] | 218 | func(test_fn1, test_fn2) |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 219 | self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2)) |
| 220 | os.remove(test_fn2) |
| 221 | |
| 222 | self._create_file(test_fn2) |
| 223 | self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2)) |
| 224 | finally: |
| 225 | os.remove(test_fn1) |
| 226 | os.remove(test_fn2) |
| 227 | |
| 228 | def test_samestat(self): |
| 229 | try: |
| 230 | test_fn = support.TESTFN + "1" |
| 231 | self._create_file(test_fn) |
| 232 | test_fns = [test_fn]*2 |
| 233 | stats = map(os.stat, test_fns) |
| 234 | self.assertTrue(self.pathmodule.samestat(*stats)) |
| 235 | finally: |
| 236 | os.remove(test_fn) |
| 237 | |
| 238 | @support.skip_unless_symlink |
Brian Curtin | e701ec5 | 2012-12-26 07:36:16 -0600 | [diff] [blame] | 239 | def test_samestat_on_symlink(self): |
| 240 | self._test_samestat_on_link_func(os.symlink) |
| 241 | |
| 242 | def test_samestat_on_link(self): |
| 243 | self._test_samestat_on_link_func(os.link) |
| 244 | |
| 245 | def _test_samestat_on_link_func(self, func): |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 246 | try: |
| 247 | test_fn1 = support.TESTFN + "1" |
| 248 | test_fn2 = support.TESTFN + "2" |
| 249 | self._create_file(test_fn1) |
| 250 | test_fns = (test_fn1, test_fn2) |
Brian Curtin | e701ec5 | 2012-12-26 07:36:16 -0600 | [diff] [blame] | 251 | func(*test_fns) |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 252 | stats = map(os.stat, test_fns) |
| 253 | self.assertTrue(self.pathmodule.samestat(*stats)) |
| 254 | os.remove(test_fn2) |
| 255 | |
| 256 | self._create_file(test_fn2) |
| 257 | stats = map(os.stat, test_fns) |
| 258 | self.assertFalse(self.pathmodule.samestat(*stats)) |
| 259 | |
| 260 | self.assertRaises(TypeError, self.pathmodule.samestat) |
| 261 | finally: |
| 262 | os.remove(test_fn1) |
| 263 | os.remove(test_fn2) |
| 264 | |
| 265 | def test_sameopenfile(self): |
| 266 | fname = support.TESTFN + "1" |
| 267 | with open(fname, "wb") as a, open(fname, "wb") as b: |
| 268 | self.assertTrue(self.pathmodule.sameopenfile( |
| 269 | a.fileno(), b.fileno())) |
| 270 | |
Ezio Melotti | d0dfe9a | 2013-01-10 03:12:50 +0200 | [diff] [blame] | 271 | class TestGenericTest(GenericTest, unittest.TestCase): |
| 272 | # Issue 16852: GenericTest can't inherit from unittest.TestCase |
| 273 | # for test discovery purposes; CommonTest inherits from GenericTest |
| 274 | # and is only meant to be inherited by others. |
| 275 | pathmodule = genericpath |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 276 | |
Berker Peksag | 9adc1a3 | 2016-07-23 07:31:47 +0300 | [diff] [blame] | 277 | def test_null_bytes(self): |
| 278 | for attr in GenericTest.common_attributes: |
| 279 | # os.path.commonprefix doesn't raise ValueError |
| 280 | if attr == 'commonprefix': |
| 281 | continue |
| 282 | with self.subTest(attr=attr): |
| 283 | with self.assertRaises(ValueError) as cm: |
| 284 | getattr(self.pathmodule, attr)('/tmp\x00abcds') |
Berker Peksag | 5f804e3 | 2016-07-23 08:42:41 +0300 | [diff] [blame] | 285 | self.assertIn('embedded null', str(cm.exception)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 286 | |
Florent Xicluna | 87082ee | 2010-08-09 17:18:05 +0000 | [diff] [blame] | 287 | # Following TestCase is not supposed to be run from test_genericpath. |
| 288 | # It is inherited by other test modules (macpath, ntpath, posixpath). |
| 289 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 290 | class CommonTest(GenericTest): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 291 | common_attributes = GenericTest.common_attributes + [ |
| 292 | # Properties |
| 293 | 'curdir', 'pardir', 'extsep', 'sep', |
| 294 | 'pathsep', 'defpath', 'altsep', 'devnull', |
| 295 | # Methods |
| 296 | 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath', |
| 297 | 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname', |
| 298 | 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath', |
| 299 | ] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 300 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 301 | def test_normcase(self): |
Ezio Melotti | 5a3ef5b | 2010-06-25 10:56:11 +0000 | [diff] [blame] | 302 | normcase = self.pathmodule.normcase |
| 303 | # check that normcase() is idempotent |
| 304 | for p in ["FoO/./BaR", b"FoO/./BaR"]: |
| 305 | p = normcase(p) |
| 306 | self.assertEqual(p, normcase(p)) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 307 | |
Ezio Melotti | 5a3ef5b | 2010-06-25 10:56:11 +0000 | [diff] [blame] | 308 | self.assertEqual(normcase(''), '') |
| 309 | self.assertEqual(normcase(b''), b'') |
| 310 | |
| 311 | # check that normcase raises a TypeError for invalid types |
| 312 | for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}): |
| 313 | self.assertRaises(TypeError, normcase, path) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 314 | |
| 315 | def test_splitdrive(self): |
| 316 | # splitdrive for non-NT paths |
| 317 | splitdrive = self.pathmodule.splitdrive |
| 318 | self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar")) |
| 319 | self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar")) |
| 320 | self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar")) |
| 321 | |
| 322 | self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar")) |
| 323 | self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar")) |
| 324 | self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar")) |
| 325 | |
| 326 | def test_expandvars(self): |
| 327 | if self.pathmodule.__name__ == 'macpath': |
| 328 | self.skipTest('macpath.expandvars is a stub') |
| 329 | expandvars = self.pathmodule.expandvars |
| 330 | with support.EnvironmentVarGuard() as env: |
| 331 | env.clear() |
| 332 | env["foo"] = "bar" |
| 333 | env["{foo"] = "baz1" |
| 334 | env["{foo}"] = "baz2" |
| 335 | self.assertEqual(expandvars("foo"), "foo") |
| 336 | self.assertEqual(expandvars("$foo bar"), "bar bar") |
| 337 | self.assertEqual(expandvars("${foo}bar"), "barbar") |
| 338 | self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar") |
| 339 | self.assertEqual(expandvars("$bar bar"), "$bar bar") |
| 340 | self.assertEqual(expandvars("$?bar"), "$?bar") |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 341 | self.assertEqual(expandvars("$foo}bar"), "bar}bar") |
| 342 | self.assertEqual(expandvars("${foo"), "${foo") |
| 343 | self.assertEqual(expandvars("${{foo}}"), "baz1}") |
| 344 | self.assertEqual(expandvars("$foo$foo"), "barbar") |
| 345 | self.assertEqual(expandvars("$bar$bar"), "$bar$bar") |
| 346 | |
| 347 | self.assertEqual(expandvars(b"foo"), b"foo") |
| 348 | self.assertEqual(expandvars(b"$foo bar"), b"bar bar") |
| 349 | self.assertEqual(expandvars(b"${foo}bar"), b"barbar") |
| 350 | self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar") |
| 351 | self.assertEqual(expandvars(b"$bar bar"), b"$bar bar") |
| 352 | self.assertEqual(expandvars(b"$?bar"), b"$?bar") |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 353 | self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar") |
| 354 | self.assertEqual(expandvars(b"${foo"), b"${foo") |
| 355 | self.assertEqual(expandvars(b"${{foo}}"), b"baz1}") |
| 356 | self.assertEqual(expandvars(b"$foo$foo"), b"barbar") |
| 357 | self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar") |
| 358 | |
Serhiy Storchaka | dbb1019 | 2014-02-13 10:13:53 +0200 | [diff] [blame] | 359 | @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII') |
| 360 | def test_expandvars_nonascii(self): |
| 361 | if self.pathmodule.__name__ == 'macpath': |
| 362 | self.skipTest('macpath.expandvars is a stub') |
| 363 | expandvars = self.pathmodule.expandvars |
| 364 | def check(value, expected): |
| 365 | self.assertEqual(expandvars(value), expected) |
| 366 | with support.EnvironmentVarGuard() as env: |
| 367 | env.clear() |
| 368 | nonascii = support.FS_NONASCII |
| 369 | env['spam'] = nonascii |
| 370 | env[nonascii] = 'ham' + nonascii |
| 371 | check(nonascii, nonascii) |
| 372 | check('$spam bar', '%s bar' % nonascii) |
| 373 | check('${spam}bar', '%sbar' % nonascii) |
| 374 | check('${%s}bar' % nonascii, 'ham%sbar' % nonascii) |
| 375 | check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii) |
| 376 | check('$spam}bar', '%s}bar' % nonascii) |
| 377 | |
| 378 | check(os.fsencode(nonascii), os.fsencode(nonascii)) |
| 379 | check(b'$spam bar', os.fsencode('%s bar' % nonascii)) |
| 380 | check(b'${spam}bar', os.fsencode('%sbar' % nonascii)) |
| 381 | check(os.fsencode('${%s}bar' % nonascii), |
| 382 | os.fsencode('ham%sbar' % nonascii)) |
| 383 | check(os.fsencode('$bar%s bar' % nonascii), |
| 384 | os.fsencode('$bar%s bar' % nonascii)) |
| 385 | check(b'$spam}bar', os.fsencode('%s}bar' % nonascii)) |
| 386 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 387 | def test_abspath(self): |
| 388 | self.assertIn("foo", self.pathmodule.abspath("foo")) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 389 | with warnings.catch_warnings(): |
| 390 | warnings.simplefilter("ignore", DeprecationWarning) |
| 391 | self.assertIn(b"foo", self.pathmodule.abspath(b"foo")) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 392 | |
| 393 | # Abspath returns bytes when the arg is bytes |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 394 | with warnings.catch_warnings(): |
| 395 | warnings.simplefilter("ignore", DeprecationWarning) |
| 396 | for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'): |
| 397 | self.assertIsInstance(self.pathmodule.abspath(path), bytes) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 398 | |
| 399 | def test_realpath(self): |
| 400 | self.assertIn("foo", self.pathmodule.realpath("foo")) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 401 | with warnings.catch_warnings(): |
| 402 | warnings.simplefilter("ignore", DeprecationWarning) |
| 403 | self.assertIn(b"foo", self.pathmodule.realpath(b"foo")) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 404 | |
| 405 | def test_normpath_issue5827(self): |
| 406 | # Make sure normpath preserves unicode |
| 407 | for path in ('', '.', '/', '\\', '///foo/.//bar//'): |
| 408 | self.assertIsInstance(self.pathmodule.normpath(path), str) |
| 409 | |
| 410 | def test_abspath_issue3426(self): |
| 411 | # Check that abspath returns unicode when the arg is unicode |
| 412 | # with both ASCII and non-ASCII cwds. |
| 413 | abspath = self.pathmodule.abspath |
| 414 | for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): |
| 415 | self.assertIsInstance(abspath(path), str) |
| 416 | |
| 417 | unicwd = '\xe7w\xf0' |
| 418 | try: |
Victor Stinner | 7c7ea62 | 2012-08-01 20:03:49 +0200 | [diff] [blame] | 419 | os.fsencode(unicwd) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 420 | except (AttributeError, UnicodeEncodeError): |
| 421 | # FS encoding is probably ASCII |
| 422 | pass |
| 423 | else: |
| 424 | with support.temp_cwd(unicwd): |
| 425 | for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): |
| 426 | self.assertIsInstance(abspath(path), str) |
| 427 | |
Victor Stinner | 1efde67 | 2010-04-18 08:23:42 +0000 | [diff] [blame] | 428 | def test_nonascii_abspath(self): |
Victor Stinner | ff3d515 | 2012-11-10 12:07:39 +0100 | [diff] [blame] | 429 | if (support.TESTFN_UNDECODABLE |
| 430 | # Mac OS X denies the creation of a directory with an invalid |
Martin Panter | c04fb56 | 2016-02-10 05:44:01 +0000 | [diff] [blame] | 431 | # UTF-8 name. Windows allows creating a directory with an |
Victor Stinner | ff3d515 | 2012-11-10 12:07:39 +0100 | [diff] [blame] | 432 | # arbitrary bytes name, but fails to enter this directory |
| 433 | # (when the bytes name is used). |
| 434 | and sys.platform not in ('win32', 'darwin')): |
| 435 | name = support.TESTFN_UNDECODABLE |
| 436 | elif support.TESTFN_NONASCII: |
| 437 | name = support.TESTFN_NONASCII |
Victor Stinner | fce2a6e | 2012-10-31 23:01:30 +0100 | [diff] [blame] | 438 | else: |
Victor Stinner | ff3d515 | 2012-11-10 12:07:39 +0100 | [diff] [blame] | 439 | self.skipTest("need support.TESTFN_NONASCII") |
Victor Stinner | 7c7ea62 | 2012-08-01 20:03:49 +0200 | [diff] [blame] | 440 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 441 | with warnings.catch_warnings(): |
| 442 | warnings.simplefilter("ignore", DeprecationWarning) |
Victor Stinner | 7c7ea62 | 2012-08-01 20:03:49 +0200 | [diff] [blame] | 443 | with support.temp_cwd(name): |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 444 | self.test_abspath() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 445 | |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 446 | def test_join_errors(self): |
| 447 | # Check join() raises friendly TypeErrors. |
| 448 | with support.check_warnings(('', BytesWarning), quiet=True): |
| 449 | errmsg = "Can't mix strings and bytes in path components" |
| 450 | with self.assertRaisesRegex(TypeError, errmsg): |
| 451 | self.pathmodule.join(b'bytes', 'str') |
| 452 | with self.assertRaisesRegex(TypeError, errmsg): |
| 453 | self.pathmodule.join('str', b'bytes') |
| 454 | # regression, see #15377 |
| 455 | errmsg = r'join\(\) argument must be str or bytes, not %r' |
| 456 | with self.assertRaisesRegex(TypeError, errmsg % 'int'): |
| 457 | self.pathmodule.join(42, 'str') |
| 458 | with self.assertRaisesRegex(TypeError, errmsg % 'int'): |
| 459 | self.pathmodule.join('str', 42) |
Serhiy Storchaka | 5bfc03f | 2015-05-19 11:00:07 +0300 | [diff] [blame] | 460 | with self.assertRaisesRegex(TypeError, errmsg % 'int'): |
| 461 | self.pathmodule.join(42) |
| 462 | with self.assertRaisesRegex(TypeError, errmsg % 'list'): |
| 463 | self.pathmodule.join([]) |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 464 | with self.assertRaisesRegex(TypeError, errmsg % 'bytearray'): |
| 465 | self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar')) |
| 466 | |
| 467 | def test_relpath_errors(self): |
| 468 | # Check relpath() raises friendly TypeErrors. |
Serhiy Storchaka | e4f4708 | 2014-10-04 16:09:02 +0300 | [diff] [blame] | 469 | with support.check_warnings(('', (BytesWarning, DeprecationWarning)), |
| 470 | quiet=True): |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 471 | errmsg = "Can't mix strings and bytes in path components" |
| 472 | with self.assertRaisesRegex(TypeError, errmsg): |
| 473 | self.pathmodule.relpath(b'bytes', 'str') |
| 474 | with self.assertRaisesRegex(TypeError, errmsg): |
| 475 | self.pathmodule.relpath('str', b'bytes') |
| 476 | errmsg = r'relpath\(\) argument must be str or bytes, not %r' |
| 477 | with self.assertRaisesRegex(TypeError, errmsg % 'int'): |
| 478 | self.pathmodule.relpath(42, 'str') |
| 479 | with self.assertRaisesRegex(TypeError, errmsg % 'int'): |
| 480 | self.pathmodule.relpath('str', 42) |
| 481 | with self.assertRaisesRegex(TypeError, errmsg % 'bytearray'): |
| 482 | self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar')) |
| 483 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 484 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 485 | if __name__=="__main__": |
Ezio Melotti | d0dfe9a | 2013-01-10 03:12:50 +0200 | [diff] [blame] | 486 | unittest.main() |