Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 1 | """ |
Victor Stinner | d7538dd | 2018-12-14 13:37:26 +0100 | [diff] [blame] | 2 | Tests common to genericpath, ntpath and posixpath |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 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 |
Serhiy Storchaka | 3460198 | 2018-01-07 17:54:31 +0200 | [diff] [blame] | 11 | from test.support.script_helper import assert_python_ok |
Serhiy Storchaka | b21d155 | 2018-03-02 11:53:51 +0200 | [diff] [blame] | 12 | from test.support import FakePath |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 13 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 14 | |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 15 | def create_file(filename, data=b'foo'): |
| 16 | with open(filename, 'xb', 0) as fp: |
| 17 | fp.write(data) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 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): |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 100 | filename = support.TESTFN |
| 101 | self.addCleanup(support.unlink, filename) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 102 | |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 103 | create_file(filename, b'Hello') |
| 104 | self.assertEqual(self.pathmodule.getsize(filename), 5) |
| 105 | os.remove(filename) |
| 106 | |
| 107 | create_file(filename, b'Hello World!') |
| 108 | self.assertEqual(self.pathmodule.getsize(filename), 12) |
| 109 | |
| 110 | def test_filetime(self): |
| 111 | filename = support.TESTFN |
| 112 | self.addCleanup(support.unlink, filename) |
| 113 | |
| 114 | create_file(filename, b'foo') |
| 115 | |
| 116 | with open(filename, "ab", 0) as f: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 117 | f.write(b"bar") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 118 | |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 119 | with open(filename, "rb", 0) as f: |
| 120 | data = f.read() |
| 121 | self.assertEqual(data, b"foobar") |
| 122 | |
| 123 | self.assertLessEqual( |
| 124 | self.pathmodule.getctime(filename), |
| 125 | self.pathmodule.getmtime(filename) |
| 126 | ) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 127 | |
| 128 | def test_exists(self): |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 129 | filename = support.TESTFN |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 130 | bfilename = os.fsencode(filename) |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 131 | self.addCleanup(support.unlink, filename) |
| 132 | |
| 133 | self.assertIs(self.pathmodule.exists(filename), False) |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 134 | self.assertIs(self.pathmodule.exists(bfilename), False) |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 135 | |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 136 | create_file(filename) |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 137 | |
| 138 | self.assertIs(self.pathmodule.exists(filename), True) |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 139 | self.assertIs(self.pathmodule.exists(bfilename), True) |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 140 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 141 | self.assertIs(self.pathmodule.exists(filename + '\udfff'), False) |
| 142 | self.assertIs(self.pathmodule.exists(bfilename + b'\xff'), False) |
| 143 | self.assertIs(self.pathmodule.exists(filename + '\x00'), False) |
| 144 | self.assertIs(self.pathmodule.exists(bfilename + b'\x00'), False) |
| 145 | |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 146 | if self.pathmodule is not genericpath: |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 147 | self.assertIs(self.pathmodule.lexists(filename), True) |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 148 | self.assertIs(self.pathmodule.lexists(bfilename), True) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 149 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 150 | self.assertIs(self.pathmodule.lexists(filename + '\udfff'), False) |
| 151 | self.assertIs(self.pathmodule.lexists(bfilename + b'\xff'), False) |
| 152 | self.assertIs(self.pathmodule.lexists(filename + '\x00'), False) |
| 153 | self.assertIs(self.pathmodule.lexists(bfilename + b'\x00'), False) |
| 154 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame] | 155 | @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()") |
| 156 | def test_exists_fd(self): |
| 157 | r, w = os.pipe() |
| 158 | try: |
| 159 | self.assertTrue(self.pathmodule.exists(r)) |
| 160 | finally: |
| 161 | os.close(r) |
| 162 | os.close(w) |
| 163 | self.assertFalse(self.pathmodule.exists(r)) |
| 164 | |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 165 | def test_isdir(self): |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 166 | filename = support.TESTFN |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 167 | bfilename = os.fsencode(filename) |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 168 | self.assertIs(self.pathmodule.isdir(filename), False) |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 169 | self.assertIs(self.pathmodule.isdir(bfilename), False) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 170 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 171 | self.assertIs(self.pathmodule.isdir(filename + '\udfff'), False) |
| 172 | self.assertIs(self.pathmodule.isdir(bfilename + b'\xff'), False) |
| 173 | self.assertIs(self.pathmodule.isdir(filename + '\x00'), False) |
| 174 | self.assertIs(self.pathmodule.isdir(bfilename + b'\x00'), False) |
| 175 | |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 176 | try: |
| 177 | create_file(filename) |
| 178 | self.assertIs(self.pathmodule.isdir(filename), False) |
| 179 | self.assertIs(self.pathmodule.isdir(bfilename), False) |
| 180 | finally: |
| 181 | support.unlink(filename) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 182 | |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 183 | try: |
| 184 | os.mkdir(filename) |
| 185 | self.assertIs(self.pathmodule.isdir(filename), True) |
| 186 | self.assertIs(self.pathmodule.isdir(bfilename), True) |
| 187 | finally: |
| 188 | support.rmdir(filename) |
| 189 | |
| 190 | def test_isfile(self): |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 191 | filename = support.TESTFN |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 192 | bfilename = os.fsencode(filename) |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 193 | self.assertIs(self.pathmodule.isfile(filename), False) |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 194 | self.assertIs(self.pathmodule.isfile(bfilename), False) |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 195 | |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 196 | self.assertIs(self.pathmodule.isfile(filename + '\udfff'), False) |
| 197 | self.assertIs(self.pathmodule.isfile(bfilename + b'\xff'), False) |
| 198 | self.assertIs(self.pathmodule.isfile(filename + '\x00'), False) |
| 199 | self.assertIs(self.pathmodule.isfile(bfilename + b'\x00'), False) |
| 200 | |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 201 | try: |
| 202 | create_file(filename) |
| 203 | self.assertIs(self.pathmodule.isfile(filename), True) |
| 204 | self.assertIs(self.pathmodule.isfile(bfilename), True) |
| 205 | finally: |
| 206 | support.unlink(filename) |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 207 | |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 208 | try: |
| 209 | os.mkdir(filename) |
| 210 | self.assertIs(self.pathmodule.isfile(filename), False) |
| 211 | self.assertIs(self.pathmodule.isfile(bfilename), False) |
| 212 | finally: |
| 213 | support.rmdir(filename) |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 214 | |
| 215 | def test_samefile(self): |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 216 | file1 = support.TESTFN |
| 217 | file2 = support.TESTFN + "2" |
| 218 | self.addCleanup(support.unlink, file1) |
| 219 | self.addCleanup(support.unlink, file2) |
| 220 | |
| 221 | create_file(file1) |
| 222 | self.assertTrue(self.pathmodule.samefile(file1, file1)) |
| 223 | |
| 224 | create_file(file2) |
| 225 | self.assertFalse(self.pathmodule.samefile(file1, file2)) |
| 226 | |
| 227 | self.assertRaises(TypeError, self.pathmodule.samefile) |
| 228 | |
| 229 | def _test_samefile_on_link_func(self, func): |
| 230 | test_fn1 = support.TESTFN |
| 231 | test_fn2 = support.TESTFN + "2" |
| 232 | self.addCleanup(support.unlink, test_fn1) |
| 233 | self.addCleanup(support.unlink, test_fn2) |
| 234 | |
| 235 | create_file(test_fn1) |
| 236 | |
| 237 | func(test_fn1, test_fn2) |
| 238 | self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2)) |
| 239 | os.remove(test_fn2) |
| 240 | |
| 241 | create_file(test_fn2) |
| 242 | self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2)) |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 243 | |
| 244 | @support.skip_unless_symlink |
Brian Curtin | e701ec5 | 2012-12-26 07:36:16 -0600 | [diff] [blame] | 245 | def test_samefile_on_symlink(self): |
| 246 | self._test_samefile_on_link_func(os.symlink) |
| 247 | |
| 248 | def test_samefile_on_link(self): |
xdegaye | 92c2ca7 | 2017-11-12 17:31:07 +0100 | [diff] [blame] | 249 | try: |
| 250 | self._test_samefile_on_link_func(os.link) |
| 251 | except PermissionError as e: |
| 252 | self.skipTest('os.link(): %s' % e) |
Brian Curtin | e701ec5 | 2012-12-26 07:36:16 -0600 | [diff] [blame] | 253 | |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 254 | def test_samestat(self): |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 255 | test_fn1 = support.TESTFN |
| 256 | test_fn2 = support.TESTFN + "2" |
| 257 | self.addCleanup(support.unlink, test_fn1) |
| 258 | self.addCleanup(support.unlink, test_fn2) |
| 259 | |
| 260 | create_file(test_fn1) |
| 261 | stat1 = os.stat(test_fn1) |
| 262 | self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1))) |
| 263 | |
| 264 | create_file(test_fn2) |
| 265 | stat2 = os.stat(test_fn2) |
| 266 | self.assertFalse(self.pathmodule.samestat(stat1, stat2)) |
| 267 | |
| 268 | self.assertRaises(TypeError, self.pathmodule.samestat) |
| 269 | |
| 270 | def _test_samestat_on_link_func(self, func): |
| 271 | test_fn1 = support.TESTFN + "1" |
| 272 | test_fn2 = support.TESTFN + "2" |
| 273 | self.addCleanup(support.unlink, test_fn1) |
| 274 | self.addCleanup(support.unlink, test_fn2) |
| 275 | |
| 276 | create_file(test_fn1) |
| 277 | func(test_fn1, test_fn2) |
| 278 | self.assertTrue(self.pathmodule.samestat(os.stat(test_fn1), |
| 279 | os.stat(test_fn2))) |
| 280 | os.remove(test_fn2) |
| 281 | |
| 282 | create_file(test_fn2) |
| 283 | self.assertFalse(self.pathmodule.samestat(os.stat(test_fn1), |
| 284 | os.stat(test_fn2))) |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 285 | |
| 286 | @support.skip_unless_symlink |
Brian Curtin | e701ec5 | 2012-12-26 07:36:16 -0600 | [diff] [blame] | 287 | def test_samestat_on_symlink(self): |
| 288 | self._test_samestat_on_link_func(os.symlink) |
| 289 | |
| 290 | def test_samestat_on_link(self): |
xdegaye | 92c2ca7 | 2017-11-12 17:31:07 +0100 | [diff] [blame] | 291 | try: |
| 292 | self._test_samestat_on_link_func(os.link) |
| 293 | except PermissionError as e: |
| 294 | self.skipTest('os.link(): %s' % e) |
Brian Curtin | e701ec5 | 2012-12-26 07:36:16 -0600 | [diff] [blame] | 295 | |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 296 | def test_sameopenfile(self): |
Victor Stinner | e321274 | 2016-03-24 13:44:19 +0100 | [diff] [blame] | 297 | filename = support.TESTFN |
| 298 | self.addCleanup(support.unlink, filename) |
| 299 | create_file(filename) |
| 300 | |
| 301 | with open(filename, "rb", 0) as fp1: |
| 302 | fd1 = fp1.fileno() |
| 303 | with open(filename, "rb", 0) as fp2: |
| 304 | fd2 = fp2.fileno() |
| 305 | self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2)) |
| 306 | |
Brian Curtin | 490b32a | 2012-12-26 07:03:03 -0600 | [diff] [blame] | 307 | |
Ezio Melotti | d0dfe9a | 2013-01-10 03:12:50 +0200 | [diff] [blame] | 308 | class TestGenericTest(GenericTest, unittest.TestCase): |
| 309 | # Issue 16852: GenericTest can't inherit from unittest.TestCase |
| 310 | # for test discovery purposes; CommonTest inherits from GenericTest |
| 311 | # and is only meant to be inherited by others. |
| 312 | pathmodule = genericpath |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 313 | |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 314 | def test_invalid_paths(self): |
Berker Peksag | 9adc1a3 | 2016-07-23 07:31:47 +0300 | [diff] [blame] | 315 | for attr in GenericTest.common_attributes: |
| 316 | # os.path.commonprefix doesn't raise ValueError |
| 317 | if attr == 'commonprefix': |
| 318 | continue |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 319 | func = getattr(self.pathmodule, attr) |
Berker Peksag | 9adc1a3 | 2016-07-23 07:31:47 +0300 | [diff] [blame] | 320 | with self.subTest(attr=attr): |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 321 | if attr in ('exists', 'isdir', 'isfile'): |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 322 | func('/tmp\udfffabcds') |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 323 | func(b'/tmp\xffabcds') |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 324 | func('/tmp\x00abcds') |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 325 | func(b'/tmp\x00abcds') |
Serhiy Storchaka | 0185f34 | 2018-09-18 11:28:51 +0300 | [diff] [blame] | 326 | else: |
| 327 | with self.assertRaises((OSError, UnicodeEncodeError)): |
| 328 | func('/tmp\udfffabcds') |
| 329 | with self.assertRaises((OSError, UnicodeDecodeError)): |
| 330 | func(b'/tmp\xffabcds') |
| 331 | with self.assertRaisesRegex(ValueError, 'embedded null'): |
| 332 | func('/tmp\x00abcds') |
| 333 | with self.assertRaisesRegex(ValueError, 'embedded null'): |
| 334 | func(b'/tmp\x00abcds') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 335 | |
Florent Xicluna | 87082ee | 2010-08-09 17:18:05 +0000 | [diff] [blame] | 336 | # Following TestCase is not supposed to be run from test_genericpath. |
Victor Stinner | d7538dd | 2018-12-14 13:37:26 +0100 | [diff] [blame] | 337 | # It is inherited by other test modules (ntpath, posixpath). |
Florent Xicluna | 87082ee | 2010-08-09 17:18:05 +0000 | [diff] [blame] | 338 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 339 | class CommonTest(GenericTest): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 340 | common_attributes = GenericTest.common_attributes + [ |
| 341 | # Properties |
| 342 | 'curdir', 'pardir', 'extsep', 'sep', |
| 343 | 'pathsep', 'defpath', 'altsep', 'devnull', |
| 344 | # Methods |
| 345 | 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath', |
| 346 | 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname', |
| 347 | 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath', |
| 348 | ] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 349 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 350 | def test_normcase(self): |
Ezio Melotti | 5a3ef5b | 2010-06-25 10:56:11 +0000 | [diff] [blame] | 351 | normcase = self.pathmodule.normcase |
| 352 | # check that normcase() is idempotent |
| 353 | for p in ["FoO/./BaR", b"FoO/./BaR"]: |
| 354 | p = normcase(p) |
| 355 | self.assertEqual(p, normcase(p)) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 356 | |
Ezio Melotti | 5a3ef5b | 2010-06-25 10:56:11 +0000 | [diff] [blame] | 357 | self.assertEqual(normcase(''), '') |
| 358 | self.assertEqual(normcase(b''), b'') |
| 359 | |
| 360 | # check that normcase raises a TypeError for invalid types |
| 361 | for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}): |
| 362 | self.assertRaises(TypeError, normcase, path) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 363 | |
| 364 | def test_splitdrive(self): |
| 365 | # splitdrive for non-NT paths |
| 366 | splitdrive = self.pathmodule.splitdrive |
| 367 | self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar")) |
| 368 | self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar")) |
| 369 | self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar")) |
| 370 | |
| 371 | self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar")) |
| 372 | self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar")) |
| 373 | self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar")) |
| 374 | |
| 375 | def test_expandvars(self): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 376 | expandvars = self.pathmodule.expandvars |
| 377 | with support.EnvironmentVarGuard() as env: |
| 378 | env.clear() |
| 379 | env["foo"] = "bar" |
| 380 | env["{foo"] = "baz1" |
| 381 | env["{foo}"] = "baz2" |
| 382 | self.assertEqual(expandvars("foo"), "foo") |
| 383 | self.assertEqual(expandvars("$foo bar"), "bar bar") |
| 384 | self.assertEqual(expandvars("${foo}bar"), "barbar") |
| 385 | self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar") |
| 386 | self.assertEqual(expandvars("$bar bar"), "$bar bar") |
| 387 | self.assertEqual(expandvars("$?bar"), "$?bar") |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 388 | self.assertEqual(expandvars("$foo}bar"), "bar}bar") |
| 389 | self.assertEqual(expandvars("${foo"), "${foo") |
| 390 | self.assertEqual(expandvars("${{foo}}"), "baz1}") |
| 391 | self.assertEqual(expandvars("$foo$foo"), "barbar") |
| 392 | self.assertEqual(expandvars("$bar$bar"), "$bar$bar") |
| 393 | |
| 394 | self.assertEqual(expandvars(b"foo"), b"foo") |
| 395 | self.assertEqual(expandvars(b"$foo bar"), b"bar bar") |
| 396 | self.assertEqual(expandvars(b"${foo}bar"), b"barbar") |
| 397 | self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar") |
| 398 | self.assertEqual(expandvars(b"$bar bar"), b"$bar bar") |
| 399 | self.assertEqual(expandvars(b"$?bar"), b"$?bar") |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 400 | self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar") |
| 401 | self.assertEqual(expandvars(b"${foo"), b"${foo") |
| 402 | self.assertEqual(expandvars(b"${{foo}}"), b"baz1}") |
| 403 | self.assertEqual(expandvars(b"$foo$foo"), b"barbar") |
| 404 | self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar") |
| 405 | |
Serhiy Storchaka | dbb1019 | 2014-02-13 10:13:53 +0200 | [diff] [blame] | 406 | @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII') |
| 407 | def test_expandvars_nonascii(self): |
Serhiy Storchaka | dbb1019 | 2014-02-13 10:13:53 +0200 | [diff] [blame] | 408 | expandvars = self.pathmodule.expandvars |
| 409 | def check(value, expected): |
| 410 | self.assertEqual(expandvars(value), expected) |
| 411 | with support.EnvironmentVarGuard() as env: |
| 412 | env.clear() |
| 413 | nonascii = support.FS_NONASCII |
| 414 | env['spam'] = nonascii |
| 415 | env[nonascii] = 'ham' + nonascii |
| 416 | check(nonascii, nonascii) |
| 417 | check('$spam bar', '%s bar' % nonascii) |
| 418 | check('${spam}bar', '%sbar' % nonascii) |
| 419 | check('${%s}bar' % nonascii, 'ham%sbar' % nonascii) |
| 420 | check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii) |
| 421 | check('$spam}bar', '%s}bar' % nonascii) |
| 422 | |
| 423 | check(os.fsencode(nonascii), os.fsencode(nonascii)) |
| 424 | check(b'$spam bar', os.fsencode('%s bar' % nonascii)) |
| 425 | check(b'${spam}bar', os.fsencode('%sbar' % nonascii)) |
| 426 | check(os.fsencode('${%s}bar' % nonascii), |
| 427 | os.fsencode('ham%sbar' % nonascii)) |
| 428 | check(os.fsencode('$bar%s bar' % nonascii), |
| 429 | os.fsencode('$bar%s bar' % nonascii)) |
| 430 | check(b'$spam}bar', os.fsencode('%s}bar' % nonascii)) |
| 431 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 432 | def test_abspath(self): |
| 433 | self.assertIn("foo", self.pathmodule.abspath("foo")) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 434 | with warnings.catch_warnings(): |
| 435 | warnings.simplefilter("ignore", DeprecationWarning) |
| 436 | self.assertIn(b"foo", self.pathmodule.abspath(b"foo")) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 437 | |
Steve Dower | e58571b | 2016-09-08 11:11:13 -0700 | [diff] [blame] | 438 | # avoid UnicodeDecodeError on Windows |
| 439 | undecodable_path = b'' if sys.platform == 'win32' else b'f\xf2\xf2' |
| 440 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 441 | # Abspath returns bytes when the arg is bytes |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 442 | with warnings.catch_warnings(): |
| 443 | warnings.simplefilter("ignore", DeprecationWarning) |
Steve Dower | e58571b | 2016-09-08 11:11:13 -0700 | [diff] [blame] | 444 | for path in (b'', b'foo', undecodable_path, b'/foo', b'C:\\'): |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 445 | self.assertIsInstance(self.pathmodule.abspath(path), bytes) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 446 | |
| 447 | def test_realpath(self): |
| 448 | self.assertIn("foo", self.pathmodule.realpath("foo")) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 449 | with warnings.catch_warnings(): |
| 450 | warnings.simplefilter("ignore", DeprecationWarning) |
| 451 | self.assertIn(b"foo", self.pathmodule.realpath(b"foo")) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 452 | |
| 453 | def test_normpath_issue5827(self): |
| 454 | # Make sure normpath preserves unicode |
| 455 | for path in ('', '.', '/', '\\', '///foo/.//bar//'): |
| 456 | self.assertIsInstance(self.pathmodule.normpath(path), str) |
| 457 | |
| 458 | def test_abspath_issue3426(self): |
| 459 | # Check that abspath returns unicode when the arg is unicode |
| 460 | # with both ASCII and non-ASCII cwds. |
| 461 | abspath = self.pathmodule.abspath |
| 462 | for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): |
| 463 | self.assertIsInstance(abspath(path), str) |
| 464 | |
| 465 | unicwd = '\xe7w\xf0' |
| 466 | try: |
Victor Stinner | 7c7ea62 | 2012-08-01 20:03:49 +0200 | [diff] [blame] | 467 | os.fsencode(unicwd) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 468 | except (AttributeError, UnicodeEncodeError): |
| 469 | # FS encoding is probably ASCII |
| 470 | pass |
| 471 | else: |
| 472 | with support.temp_cwd(unicwd): |
| 473 | for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): |
| 474 | self.assertIsInstance(abspath(path), str) |
| 475 | |
Victor Stinner | 1efde67 | 2010-04-18 08:23:42 +0000 | [diff] [blame] | 476 | def test_nonascii_abspath(self): |
Victor Stinner | ff3d515 | 2012-11-10 12:07:39 +0100 | [diff] [blame] | 477 | if (support.TESTFN_UNDECODABLE |
| 478 | # Mac OS X denies the creation of a directory with an invalid |
Martin Panter | c04fb56 | 2016-02-10 05:44:01 +0000 | [diff] [blame] | 479 | # UTF-8 name. Windows allows creating a directory with an |
Victor Stinner | ff3d515 | 2012-11-10 12:07:39 +0100 | [diff] [blame] | 480 | # arbitrary bytes name, but fails to enter this directory |
| 481 | # (when the bytes name is used). |
| 482 | and sys.platform not in ('win32', 'darwin')): |
| 483 | name = support.TESTFN_UNDECODABLE |
| 484 | elif support.TESTFN_NONASCII: |
| 485 | name = support.TESTFN_NONASCII |
Victor Stinner | fce2a6e | 2012-10-31 23:01:30 +0100 | [diff] [blame] | 486 | else: |
Victor Stinner | ff3d515 | 2012-11-10 12:07:39 +0100 | [diff] [blame] | 487 | self.skipTest("need support.TESTFN_NONASCII") |
Victor Stinner | 7c7ea62 | 2012-08-01 20:03:49 +0200 | [diff] [blame] | 488 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 489 | with warnings.catch_warnings(): |
| 490 | warnings.simplefilter("ignore", DeprecationWarning) |
Victor Stinner | 7c7ea62 | 2012-08-01 20:03:49 +0200 | [diff] [blame] | 491 | with support.temp_cwd(name): |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 492 | self.test_abspath() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 493 | |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 494 | def test_join_errors(self): |
| 495 | # Check join() raises friendly TypeErrors. |
| 496 | with support.check_warnings(('', BytesWarning), quiet=True): |
| 497 | errmsg = "Can't mix strings and bytes in path components" |
| 498 | with self.assertRaisesRegex(TypeError, errmsg): |
| 499 | self.pathmodule.join(b'bytes', 'str') |
| 500 | with self.assertRaisesRegex(TypeError, errmsg): |
| 501 | self.pathmodule.join('str', b'bytes') |
| 502 | # regression, see #15377 |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 503 | with self.assertRaisesRegex(TypeError, 'int'): |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 504 | self.pathmodule.join(42, 'str') |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 505 | with self.assertRaisesRegex(TypeError, 'int'): |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 506 | self.pathmodule.join('str', 42) |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 507 | with self.assertRaisesRegex(TypeError, 'int'): |
Serhiy Storchaka | 5bfc03f | 2015-05-19 11:00:07 +0300 | [diff] [blame] | 508 | self.pathmodule.join(42) |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 509 | with self.assertRaisesRegex(TypeError, 'list'): |
Serhiy Storchaka | 5bfc03f | 2015-05-19 11:00:07 +0300 | [diff] [blame] | 510 | self.pathmodule.join([]) |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 511 | with self.assertRaisesRegex(TypeError, 'bytearray'): |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 512 | self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar')) |
| 513 | |
| 514 | def test_relpath_errors(self): |
| 515 | # Check relpath() raises friendly TypeErrors. |
Serhiy Storchaka | e4f4708 | 2014-10-04 16:09:02 +0300 | [diff] [blame] | 516 | with support.check_warnings(('', (BytesWarning, DeprecationWarning)), |
| 517 | quiet=True): |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 518 | errmsg = "Can't mix strings and bytes in path components" |
| 519 | with self.assertRaisesRegex(TypeError, errmsg): |
| 520 | self.pathmodule.relpath(b'bytes', 'str') |
| 521 | with self.assertRaisesRegex(TypeError, errmsg): |
| 522 | self.pathmodule.relpath('str', b'bytes') |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 523 | with self.assertRaisesRegex(TypeError, 'int'): |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 524 | self.pathmodule.relpath(42, 'str') |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 525 | with self.assertRaisesRegex(TypeError, 'int'): |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 526 | self.pathmodule.relpath('str', 42) |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 527 | with self.assertRaisesRegex(TypeError, 'bytearray'): |
Serhiy Storchaka | 3deeeb0 | 2014-10-04 14:58:43 +0300 | [diff] [blame] | 528 | self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar')) |
| 529 | |
Serhiy Storchaka | 3460198 | 2018-01-07 17:54:31 +0200 | [diff] [blame] | 530 | def test_import(self): |
| 531 | assert_python_ok('-S', '-c', 'import ' + self.pathmodule.__name__) |
| 532 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 533 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 534 | class PathLikeTests(unittest.TestCase): |
| 535 | |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 536 | def setUp(self): |
| 537 | self.file_name = support.TESTFN.lower() |
Serhiy Storchaka | b21d155 | 2018-03-02 11:53:51 +0200 | [diff] [blame] | 538 | self.file_path = FakePath(support.TESTFN) |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 539 | self.addCleanup(support.unlink, self.file_name) |
| 540 | create_file(self.file_name, b"test_genericpath.PathLikeTests") |
| 541 | |
| 542 | def assertPathEqual(self, func): |
| 543 | self.assertEqual(func(self.file_path), func(self.file_name)) |
| 544 | |
| 545 | def test_path_exists(self): |
| 546 | self.assertPathEqual(os.path.exists) |
| 547 | |
| 548 | def test_path_isfile(self): |
| 549 | self.assertPathEqual(os.path.isfile) |
| 550 | |
| 551 | def test_path_isdir(self): |
| 552 | self.assertPathEqual(os.path.isdir) |
| 553 | |
| 554 | def test_path_commonprefix(self): |
| 555 | self.assertEqual(os.path.commonprefix([self.file_path, self.file_name]), |
| 556 | self.file_name) |
| 557 | |
| 558 | def test_path_getsize(self): |
| 559 | self.assertPathEqual(os.path.getsize) |
| 560 | |
| 561 | def test_path_getmtime(self): |
| 562 | self.assertPathEqual(os.path.getatime) |
| 563 | |
| 564 | def test_path_getctime(self): |
| 565 | self.assertPathEqual(os.path.getctime) |
| 566 | |
| 567 | def test_path_samefile(self): |
| 568 | self.assertTrue(os.path.samefile(self.file_path, self.file_name)) |
| 569 | |
| 570 | |
Serhiy Storchaka | 17a0088 | 2018-06-16 13:25:55 +0300 | [diff] [blame] | 571 | if __name__ == "__main__": |
Ezio Melotti | d0dfe9a | 2013-01-10 03:12:50 +0200 | [diff] [blame] | 572 | unittest.main() |