blob: ae5dd6a5f7cd06bbec0e1fcd7f5dd99b75cafe31 [file] [log] [blame]
Florent Xiclunac9c79782010-03-08 12:24:53 +00001"""
2Tests common to genericpath, macpath, ntpath and posixpath
3"""
4
Thomas Wouters89f507f2006-12-13 04:49:30 +00005import genericpath
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006import os
Victor Stinner1efde672010-04-18 08:23:42 +00007import sys
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01008import unittest
9import warnings
10from test import support
Thomas Wouters89f507f2006-12-13 04:49:30 +000011
Florent Xiclunac9c79782010-03-08 12:24:53 +000012
Victor Stinnere3212742016-03-24 13:44:19 +010013def create_file(filename, data=b'foo'):
14 with open(filename, 'xb', 0) as fp:
15 fp.write(data)
Florent Xiclunac9c79782010-03-08 12:24:53 +000016
17
Ezio Melottid0dfe9a2013-01-10 03:12:50 +020018class GenericTest:
Florent Xiclunac9c79782010-03-08 12:24:53 +000019 common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime',
20 'getmtime', 'exists', 'isdir', 'isfile']
21 attributes = []
22
23 def test_no_argument(self):
24 for attr in self.common_attributes + self.attributes:
25 with self.assertRaises(TypeError):
26 getattr(self.pathmodule, attr)()
27 raise self.fail("{}.{}() did not raise a TypeError"
28 .format(self.pathmodule.__name__, attr))
Thomas Wouters89f507f2006-12-13 04:49:30 +000029
Thomas Wouters89f507f2006-12-13 04:49:30 +000030 def test_commonprefix(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +000031 commonprefix = self.pathmodule.commonprefix
Thomas Wouters89f507f2006-12-13 04:49:30 +000032 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000033 commonprefix([]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000034 ""
35 )
36 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000037 commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000038 "/home/swen"
39 )
40 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000041 commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000042 "/home/swen/"
43 )
44 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000045 commonprefix(["/home/swen/spam", "/home/swen/spam"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000046 "/home/swen/spam"
47 )
Florent Xiclunae3ed2e02010-03-08 12:42:20 +000048 self.assertEqual(
49 commonprefix(["home:swenson:spam", "home:swen:spam"]),
50 "home:swen"
51 )
52 self.assertEqual(
53 commonprefix([":home:swen:spam", ":home:swen:eggs"]),
54 ":home:swen:"
55 )
56 self.assertEqual(
57 commonprefix([":home:swen:spam", ":home:swen:spam"]),
58 ":home:swen:spam"
59 )
Thomas Wouters89f507f2006-12-13 04:49:30 +000060
Florent Xiclunac9c79782010-03-08 12:24:53 +000061 self.assertEqual(
62 commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]),
63 b"/home/swen"
64 )
65 self.assertEqual(
66 commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]),
67 b"/home/swen/"
68 )
69 self.assertEqual(
70 commonprefix([b"/home/swen/spam", b"/home/swen/spam"]),
71 b"/home/swen/spam"
72 )
Florent Xiclunae3ed2e02010-03-08 12:42:20 +000073 self.assertEqual(
74 commonprefix([b"home:swenson:spam", b"home:swen:spam"]),
75 b"home:swen"
76 )
77 self.assertEqual(
78 commonprefix([b":home:swen:spam", b":home:swen:eggs"]),
79 b":home:swen:"
80 )
81 self.assertEqual(
82 commonprefix([b":home:swen:spam", b":home:swen:spam"]),
83 b":home:swen:spam"
84 )
Florent Xiclunac9c79782010-03-08 12:24:53 +000085
86 testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
87 'aXc', 'abd', 'ab', 'aX', 'abcX']
88 for s1 in testlist:
89 for s2 in testlist:
90 p = commonprefix([s1, s2])
91 self.assertTrue(s1.startswith(p))
92 self.assertTrue(s2.startswith(p))
93 if s1 != s2:
94 n = len(p)
95 self.assertNotEqual(s1[n:n+1], s2[n:n+1])
96
Thomas Wouters89f507f2006-12-13 04:49:30 +000097 def test_getsize(self):
Victor Stinnere3212742016-03-24 13:44:19 +010098 filename = support.TESTFN
99 self.addCleanup(support.unlink, filename)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000100
Victor Stinnere3212742016-03-24 13:44:19 +0100101 create_file(filename, b'Hello')
102 self.assertEqual(self.pathmodule.getsize(filename), 5)
103 os.remove(filename)
104
105 create_file(filename, b'Hello World!')
106 self.assertEqual(self.pathmodule.getsize(filename), 12)
107
108 def test_filetime(self):
109 filename = support.TESTFN
110 self.addCleanup(support.unlink, filename)
111
112 create_file(filename, b'foo')
113
114 with open(filename, "ab", 0) as f:
Guido van Rossum199fc752007-08-27 23:38:12 +0000115 f.write(b"bar")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000116
Victor Stinnere3212742016-03-24 13:44:19 +0100117 with open(filename, "rb", 0) as f:
118 data = f.read()
119 self.assertEqual(data, b"foobar")
120
121 self.assertLessEqual(
122 self.pathmodule.getctime(filename),
123 self.pathmodule.getmtime(filename)
124 )
Thomas Wouters89f507f2006-12-13 04:49:30 +0000125
126 def test_exists(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100127 filename = support.TESTFN
128 self.addCleanup(support.unlink, filename)
129
130 self.assertIs(self.pathmodule.exists(filename), False)
131
132 with open(filename, "xb") as f:
Guido van Rossum199fc752007-08-27 23:38:12 +0000133 f.write(b"foo")
Victor Stinnere3212742016-03-24 13:44:19 +0100134
135 self.assertIs(self.pathmodule.exists(filename), True)
136
137 if not self.pathmodule == genericpath:
138 self.assertIs(self.pathmodule.lexists(filename), True)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000139
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100140 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
141 def test_exists_fd(self):
142 r, w = os.pipe()
143 try:
144 self.assertTrue(self.pathmodule.exists(r))
145 finally:
146 os.close(r)
147 os.close(w)
148 self.assertFalse(self.pathmodule.exists(r))
149
Victor Stinnere3212742016-03-24 13:44:19 +0100150 def test_isdir_file(self):
151 filename = support.TESTFN
152 self.addCleanup(support.unlink, filename)
153 self.assertIs(self.pathmodule.isdir(filename), False)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000154
Victor Stinnere3212742016-03-24 13:44:19 +0100155 create_file(filename)
156 self.assertIs(self.pathmodule.isdir(filename), False)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000157
Victor Stinnere3212742016-03-24 13:44:19 +0100158 def test_isdir_dir(self):
159 filename = support.TESTFN
160 self.addCleanup(support.rmdir, filename)
161 self.assertIs(self.pathmodule.isdir(filename), False)
162
163 os.mkdir(filename)
164 self.assertIs(self.pathmodule.isdir(filename), True)
165
166 def test_isfile_file(self):
167 filename = support.TESTFN
168 self.addCleanup(support.unlink, filename)
169 self.assertIs(self.pathmodule.isfile(filename), False)
170
171 create_file(filename)
172 self.assertIs(self.pathmodule.isfile(filename), True)
173
174 def test_isfile_dir(self):
175 filename = support.TESTFN
176 self.addCleanup(support.rmdir, filename)
177 self.assertIs(self.pathmodule.isfile(filename), False)
178
179 os.mkdir(filename)
180 self.assertIs(self.pathmodule.isfile(filename), False)
Brian Curtin490b32a2012-12-26 07:03:03 -0600181
182 def test_samefile(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100183 file1 = support.TESTFN
184 file2 = support.TESTFN + "2"
185 self.addCleanup(support.unlink, file1)
186 self.addCleanup(support.unlink, file2)
187
188 create_file(file1)
189 self.assertTrue(self.pathmodule.samefile(file1, file1))
190
191 create_file(file2)
192 self.assertFalse(self.pathmodule.samefile(file1, file2))
193
194 self.assertRaises(TypeError, self.pathmodule.samefile)
195
196 def _test_samefile_on_link_func(self, func):
197 test_fn1 = support.TESTFN
198 test_fn2 = support.TESTFN + "2"
199 self.addCleanup(support.unlink, test_fn1)
200 self.addCleanup(support.unlink, test_fn2)
201
202 create_file(test_fn1)
203
204 func(test_fn1, test_fn2)
205 self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2))
206 os.remove(test_fn2)
207
208 create_file(test_fn2)
209 self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2))
Brian Curtin490b32a2012-12-26 07:03:03 -0600210
211 @support.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600212 def test_samefile_on_symlink(self):
213 self._test_samefile_on_link_func(os.symlink)
214
215 def test_samefile_on_link(self):
216 self._test_samefile_on_link_func(os.link)
217
Brian Curtin490b32a2012-12-26 07:03:03 -0600218 def test_samestat(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100219 test_fn1 = support.TESTFN
220 test_fn2 = support.TESTFN + "2"
221 self.addCleanup(support.unlink, test_fn1)
222 self.addCleanup(support.unlink, test_fn2)
223
224 create_file(test_fn1)
225 stat1 = os.stat(test_fn1)
226 self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1)))
227
228 create_file(test_fn2)
229 stat2 = os.stat(test_fn2)
230 self.assertFalse(self.pathmodule.samestat(stat1, stat2))
231
232 self.assertRaises(TypeError, self.pathmodule.samestat)
233
234 def _test_samestat_on_link_func(self, func):
235 test_fn1 = support.TESTFN + "1"
236 test_fn2 = support.TESTFN + "2"
237 self.addCleanup(support.unlink, test_fn1)
238 self.addCleanup(support.unlink, test_fn2)
239
240 create_file(test_fn1)
241 func(test_fn1, test_fn2)
242 self.assertTrue(self.pathmodule.samestat(os.stat(test_fn1),
243 os.stat(test_fn2)))
244 os.remove(test_fn2)
245
246 create_file(test_fn2)
247 self.assertFalse(self.pathmodule.samestat(os.stat(test_fn1),
248 os.stat(test_fn2)))
Brian Curtin490b32a2012-12-26 07:03:03 -0600249
250 @support.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600251 def test_samestat_on_symlink(self):
252 self._test_samestat_on_link_func(os.symlink)
253
254 def test_samestat_on_link(self):
255 self._test_samestat_on_link_func(os.link)
256
Brian Curtin490b32a2012-12-26 07:03:03 -0600257 def test_sameopenfile(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100258 filename = support.TESTFN
259 self.addCleanup(support.unlink, filename)
260 create_file(filename)
261
262 with open(filename, "rb", 0) as fp1:
263 fd1 = fp1.fileno()
264 with open(filename, "rb", 0) as fp2:
265 fd2 = fp2.fileno()
266 self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2))
267
Brian Curtin490b32a2012-12-26 07:03:03 -0600268
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200269class TestGenericTest(GenericTest, unittest.TestCase):
270 # Issue 16852: GenericTest can't inherit from unittest.TestCase
271 # for test discovery purposes; CommonTest inherits from GenericTest
272 # and is only meant to be inherited by others.
273 pathmodule = genericpath
Thomas Wouters89f507f2006-12-13 04:49:30 +0000274
Berker Peksag9adc1a32016-07-23 07:31:47 +0300275 def test_null_bytes(self):
276 for attr in GenericTest.common_attributes:
277 # os.path.commonprefix doesn't raise ValueError
278 if attr == 'commonprefix':
279 continue
280 with self.subTest(attr=attr):
281 with self.assertRaises(ValueError) as cm:
282 getattr(self.pathmodule, attr)('/tmp\x00abcds')
Berker Peksag5f804e32016-07-23 08:42:41 +0300283 self.assertIn('embedded null', str(cm.exception))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000284
Florent Xicluna87082ee2010-08-09 17:18:05 +0000285# Following TestCase is not supposed to be run from test_genericpath.
286# It is inherited by other test modules (macpath, ntpath, posixpath).
287
Florent Xiclunac9c79782010-03-08 12:24:53 +0000288class CommonTest(GenericTest):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000289 common_attributes = GenericTest.common_attributes + [
290 # Properties
291 'curdir', 'pardir', 'extsep', 'sep',
292 'pathsep', 'defpath', 'altsep', 'devnull',
293 # Methods
294 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
295 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
296 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
297 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000298
Florent Xiclunac9c79782010-03-08 12:24:53 +0000299 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000300 normcase = self.pathmodule.normcase
301 # check that normcase() is idempotent
302 for p in ["FoO/./BaR", b"FoO/./BaR"]:
303 p = normcase(p)
304 self.assertEqual(p, normcase(p))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000305
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000306 self.assertEqual(normcase(''), '')
307 self.assertEqual(normcase(b''), b'')
308
309 # check that normcase raises a TypeError for invalid types
310 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
311 self.assertRaises(TypeError, normcase, path)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000312
313 def test_splitdrive(self):
314 # splitdrive for non-NT paths
315 splitdrive = self.pathmodule.splitdrive
316 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
317 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
318 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
319
320 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
321 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
322 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
323
324 def test_expandvars(self):
325 if self.pathmodule.__name__ == 'macpath':
326 self.skipTest('macpath.expandvars is a stub')
327 expandvars = self.pathmodule.expandvars
328 with support.EnvironmentVarGuard() as env:
329 env.clear()
330 env["foo"] = "bar"
331 env["{foo"] = "baz1"
332 env["{foo}"] = "baz2"
333 self.assertEqual(expandvars("foo"), "foo")
334 self.assertEqual(expandvars("$foo bar"), "bar bar")
335 self.assertEqual(expandvars("${foo}bar"), "barbar")
336 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
337 self.assertEqual(expandvars("$bar bar"), "$bar bar")
338 self.assertEqual(expandvars("$?bar"), "$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000339 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
340 self.assertEqual(expandvars("${foo"), "${foo")
341 self.assertEqual(expandvars("${{foo}}"), "baz1}")
342 self.assertEqual(expandvars("$foo$foo"), "barbar")
343 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
344
345 self.assertEqual(expandvars(b"foo"), b"foo")
346 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
347 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
348 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
349 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
350 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000351 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
352 self.assertEqual(expandvars(b"${foo"), b"${foo")
353 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
354 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
355 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
356
Serhiy Storchakadbb10192014-02-13 10:13:53 +0200357 @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
358 def test_expandvars_nonascii(self):
359 if self.pathmodule.__name__ == 'macpath':
360 self.skipTest('macpath.expandvars is a stub')
361 expandvars = self.pathmodule.expandvars
362 def check(value, expected):
363 self.assertEqual(expandvars(value), expected)
364 with support.EnvironmentVarGuard() as env:
365 env.clear()
366 nonascii = support.FS_NONASCII
367 env['spam'] = nonascii
368 env[nonascii] = 'ham' + nonascii
369 check(nonascii, nonascii)
370 check('$spam bar', '%s bar' % nonascii)
371 check('${spam}bar', '%sbar' % nonascii)
372 check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
373 check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii)
374 check('$spam}bar', '%s}bar' % nonascii)
375
376 check(os.fsencode(nonascii), os.fsencode(nonascii))
377 check(b'$spam bar', os.fsencode('%s bar' % nonascii))
378 check(b'${spam}bar', os.fsencode('%sbar' % nonascii))
379 check(os.fsencode('${%s}bar' % nonascii),
380 os.fsencode('ham%sbar' % nonascii))
381 check(os.fsencode('$bar%s bar' % nonascii),
382 os.fsencode('$bar%s bar' % nonascii))
383 check(b'$spam}bar', os.fsencode('%s}bar' % nonascii))
384
Florent Xiclunac9c79782010-03-08 12:24:53 +0000385 def test_abspath(self):
386 self.assertIn("foo", self.pathmodule.abspath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100387 with warnings.catch_warnings():
388 warnings.simplefilter("ignore", DeprecationWarning)
389 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000390
Steve Dowere58571b2016-09-08 11:11:13 -0700391 # avoid UnicodeDecodeError on Windows
392 undecodable_path = b'' if sys.platform == 'win32' else b'f\xf2\xf2'
393
Florent Xiclunac9c79782010-03-08 12:24:53 +0000394 # Abspath returns bytes when the arg is bytes
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100395 with warnings.catch_warnings():
396 warnings.simplefilter("ignore", DeprecationWarning)
Steve Dowere58571b2016-09-08 11:11:13 -0700397 for path in (b'', b'foo', undecodable_path, b'/foo', b'C:\\'):
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100398 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000399
400 def test_realpath(self):
401 self.assertIn("foo", self.pathmodule.realpath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100402 with warnings.catch_warnings():
403 warnings.simplefilter("ignore", DeprecationWarning)
404 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000405
406 def test_normpath_issue5827(self):
407 # Make sure normpath preserves unicode
408 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
409 self.assertIsInstance(self.pathmodule.normpath(path), str)
410
411 def test_abspath_issue3426(self):
412 # Check that abspath returns unicode when the arg is unicode
413 # with both ASCII and non-ASCII cwds.
414 abspath = self.pathmodule.abspath
415 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
416 self.assertIsInstance(abspath(path), str)
417
418 unicwd = '\xe7w\xf0'
419 try:
Victor Stinner7c7ea622012-08-01 20:03:49 +0200420 os.fsencode(unicwd)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000421 except (AttributeError, UnicodeEncodeError):
422 # FS encoding is probably ASCII
423 pass
424 else:
425 with support.temp_cwd(unicwd):
426 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
427 self.assertIsInstance(abspath(path), str)
428
Victor Stinner1efde672010-04-18 08:23:42 +0000429 def test_nonascii_abspath(self):
Victor Stinnerff3d5152012-11-10 12:07:39 +0100430 if (support.TESTFN_UNDECODABLE
431 # Mac OS X denies the creation of a directory with an invalid
Martin Panterc04fb562016-02-10 05:44:01 +0000432 # UTF-8 name. Windows allows creating a directory with an
Victor Stinnerff3d5152012-11-10 12:07:39 +0100433 # arbitrary bytes name, but fails to enter this directory
434 # (when the bytes name is used).
435 and sys.platform not in ('win32', 'darwin')):
436 name = support.TESTFN_UNDECODABLE
437 elif support.TESTFN_NONASCII:
438 name = support.TESTFN_NONASCII
Victor Stinnerfce2a6e2012-10-31 23:01:30 +0100439 else:
Victor Stinnerff3d5152012-11-10 12:07:39 +0100440 self.skipTest("need support.TESTFN_NONASCII")
Victor Stinner7c7ea622012-08-01 20:03:49 +0200441
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100442 with warnings.catch_warnings():
443 warnings.simplefilter("ignore", DeprecationWarning)
Victor Stinner7c7ea622012-08-01 20:03:49 +0200444 with support.temp_cwd(name):
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100445 self.test_abspath()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000446
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300447 def test_join_errors(self):
448 # Check join() raises friendly TypeErrors.
449 with support.check_warnings(('', BytesWarning), quiet=True):
450 errmsg = "Can't mix strings and bytes in path components"
451 with self.assertRaisesRegex(TypeError, errmsg):
452 self.pathmodule.join(b'bytes', 'str')
453 with self.assertRaisesRegex(TypeError, errmsg):
454 self.pathmodule.join('str', b'bytes')
455 # regression, see #15377
Brett Cannon3f9183b2016-08-26 14:44:48 -0700456 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300457 self.pathmodule.join(42, 'str')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700458 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300459 self.pathmodule.join('str', 42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700460 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300461 self.pathmodule.join(42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700462 with self.assertRaisesRegex(TypeError, 'list'):
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300463 self.pathmodule.join([])
Brett Cannon3f9183b2016-08-26 14:44:48 -0700464 with self.assertRaisesRegex(TypeError, 'bytearray'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300465 self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar'))
466
467 def test_relpath_errors(self):
468 # Check relpath() raises friendly TypeErrors.
Serhiy Storchakae4f47082014-10-04 16:09:02 +0300469 with support.check_warnings(('', (BytesWarning, DeprecationWarning)),
470 quiet=True):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300471 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')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700476 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300477 self.pathmodule.relpath(42, 'str')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700478 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300479 self.pathmodule.relpath('str', 42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700480 with self.assertRaisesRegex(TypeError, 'bytearray'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300481 self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar'))
482
Thomas Wouters89f507f2006-12-13 04:49:30 +0000483
Brett Cannon3f9183b2016-08-26 14:44:48 -0700484class PathLikeTests(unittest.TestCase):
485
486 class PathLike:
487 def __init__(self, path=''):
488 self.path = path
489 def __fspath__(self):
490 if isinstance(self.path, BaseException):
491 raise self.path
492 else:
493 return self.path
494
495 def setUp(self):
496 self.file_name = support.TESTFN.lower()
497 self.file_path = self.PathLike(support.TESTFN)
498 self.addCleanup(support.unlink, self.file_name)
499 create_file(self.file_name, b"test_genericpath.PathLikeTests")
500
501 def assertPathEqual(self, func):
502 self.assertEqual(func(self.file_path), func(self.file_name))
503
504 def test_path_exists(self):
505 self.assertPathEqual(os.path.exists)
506
507 def test_path_isfile(self):
508 self.assertPathEqual(os.path.isfile)
509
510 def test_path_isdir(self):
511 self.assertPathEqual(os.path.isdir)
512
513 def test_path_commonprefix(self):
514 self.assertEqual(os.path.commonprefix([self.file_path, self.file_name]),
515 self.file_name)
516
517 def test_path_getsize(self):
518 self.assertPathEqual(os.path.getsize)
519
520 def test_path_getmtime(self):
521 self.assertPathEqual(os.path.getatime)
522
523 def test_path_getctime(self):
524 self.assertPathEqual(os.path.getctime)
525
526 def test_path_samefile(self):
527 self.assertTrue(os.path.samefile(self.file_path, self.file_name))
528
529
Thomas Wouters89f507f2006-12-13 04:49:30 +0000530if __name__=="__main__":
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200531 unittest.main()