blob: 9c28a6872be894c8db27c2451c35de32f0edc006 [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
Thomas Wouters89f507f2006-12-13 04:49:30 +0000275
Florent Xicluna87082ee2010-08-09 17:18:05 +0000276# Following TestCase is not supposed to be run from test_genericpath.
277# It is inherited by other test modules (macpath, ntpath, posixpath).
278
Florent Xiclunac9c79782010-03-08 12:24:53 +0000279class CommonTest(GenericTest):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000280 common_attributes = GenericTest.common_attributes + [
281 # Properties
282 'curdir', 'pardir', 'extsep', 'sep',
283 'pathsep', 'defpath', 'altsep', 'devnull',
284 # Methods
285 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
286 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
287 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
288 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000289
Florent Xiclunac9c79782010-03-08 12:24:53 +0000290 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000291 normcase = self.pathmodule.normcase
292 # check that normcase() is idempotent
293 for p in ["FoO/./BaR", b"FoO/./BaR"]:
294 p = normcase(p)
295 self.assertEqual(p, normcase(p))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000296
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000297 self.assertEqual(normcase(''), '')
298 self.assertEqual(normcase(b''), b'')
299
300 # check that normcase raises a TypeError for invalid types
301 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
302 self.assertRaises(TypeError, normcase, path)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000303
304 def test_splitdrive(self):
305 # splitdrive for non-NT paths
306 splitdrive = self.pathmodule.splitdrive
307 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
308 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
309 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
310
311 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
312 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
313 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
314
315 def test_expandvars(self):
316 if self.pathmodule.__name__ == 'macpath':
317 self.skipTest('macpath.expandvars is a stub')
318 expandvars = self.pathmodule.expandvars
319 with support.EnvironmentVarGuard() as env:
320 env.clear()
321 env["foo"] = "bar"
322 env["{foo"] = "baz1"
323 env["{foo}"] = "baz2"
324 self.assertEqual(expandvars("foo"), "foo")
325 self.assertEqual(expandvars("$foo bar"), "bar bar")
326 self.assertEqual(expandvars("${foo}bar"), "barbar")
327 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
328 self.assertEqual(expandvars("$bar bar"), "$bar bar")
329 self.assertEqual(expandvars("$?bar"), "$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000330 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
331 self.assertEqual(expandvars("${foo"), "${foo")
332 self.assertEqual(expandvars("${{foo}}"), "baz1}")
333 self.assertEqual(expandvars("$foo$foo"), "barbar")
334 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
335
336 self.assertEqual(expandvars(b"foo"), b"foo")
337 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
338 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
339 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
340 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
341 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000342 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
343 self.assertEqual(expandvars(b"${foo"), b"${foo")
344 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
345 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
346 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
347
Serhiy Storchakadbb10192014-02-13 10:13:53 +0200348 @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
349 def test_expandvars_nonascii(self):
350 if self.pathmodule.__name__ == 'macpath':
351 self.skipTest('macpath.expandvars is a stub')
352 expandvars = self.pathmodule.expandvars
353 def check(value, expected):
354 self.assertEqual(expandvars(value), expected)
355 with support.EnvironmentVarGuard() as env:
356 env.clear()
357 nonascii = support.FS_NONASCII
358 env['spam'] = nonascii
359 env[nonascii] = 'ham' + nonascii
360 check(nonascii, nonascii)
361 check('$spam bar', '%s bar' % nonascii)
362 check('${spam}bar', '%sbar' % nonascii)
363 check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
364 check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii)
365 check('$spam}bar', '%s}bar' % nonascii)
366
367 check(os.fsencode(nonascii), os.fsencode(nonascii))
368 check(b'$spam bar', os.fsencode('%s bar' % nonascii))
369 check(b'${spam}bar', os.fsencode('%sbar' % nonascii))
370 check(os.fsencode('${%s}bar' % nonascii),
371 os.fsencode('ham%sbar' % nonascii))
372 check(os.fsencode('$bar%s bar' % nonascii),
373 os.fsencode('$bar%s bar' % nonascii))
374 check(b'$spam}bar', os.fsencode('%s}bar' % nonascii))
375
Florent Xiclunac9c79782010-03-08 12:24:53 +0000376 def test_abspath(self):
377 self.assertIn("foo", self.pathmodule.abspath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100378 with warnings.catch_warnings():
379 warnings.simplefilter("ignore", DeprecationWarning)
380 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000381
382 # Abspath returns bytes when the arg is bytes
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100383 with warnings.catch_warnings():
384 warnings.simplefilter("ignore", DeprecationWarning)
385 for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'):
386 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000387
388 def test_realpath(self):
389 self.assertIn("foo", self.pathmodule.realpath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100390 with warnings.catch_warnings():
391 warnings.simplefilter("ignore", DeprecationWarning)
392 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000393
394 def test_normpath_issue5827(self):
395 # Make sure normpath preserves unicode
396 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
397 self.assertIsInstance(self.pathmodule.normpath(path), str)
398
399 def test_abspath_issue3426(self):
400 # Check that abspath returns unicode when the arg is unicode
401 # with both ASCII and non-ASCII cwds.
402 abspath = self.pathmodule.abspath
403 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
404 self.assertIsInstance(abspath(path), str)
405
406 unicwd = '\xe7w\xf0'
407 try:
Victor Stinner7c7ea622012-08-01 20:03:49 +0200408 os.fsencode(unicwd)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000409 except (AttributeError, UnicodeEncodeError):
410 # FS encoding is probably ASCII
411 pass
412 else:
413 with support.temp_cwd(unicwd):
414 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
415 self.assertIsInstance(abspath(path), str)
416
Victor Stinner1efde672010-04-18 08:23:42 +0000417 def test_nonascii_abspath(self):
Victor Stinnerff3d5152012-11-10 12:07:39 +0100418 if (support.TESTFN_UNDECODABLE
419 # Mac OS X denies the creation of a directory with an invalid
Martin Panterc04fb562016-02-10 05:44:01 +0000420 # UTF-8 name. Windows allows creating a directory with an
Victor Stinnerff3d5152012-11-10 12:07:39 +0100421 # arbitrary bytes name, but fails to enter this directory
422 # (when the bytes name is used).
423 and sys.platform not in ('win32', 'darwin')):
424 name = support.TESTFN_UNDECODABLE
425 elif support.TESTFN_NONASCII:
426 name = support.TESTFN_NONASCII
Victor Stinnerfce2a6e2012-10-31 23:01:30 +0100427 else:
Victor Stinnerff3d5152012-11-10 12:07:39 +0100428 self.skipTest("need support.TESTFN_NONASCII")
Victor Stinner7c7ea622012-08-01 20:03:49 +0200429
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100430 with warnings.catch_warnings():
431 warnings.simplefilter("ignore", DeprecationWarning)
Victor Stinner7c7ea622012-08-01 20:03:49 +0200432 with support.temp_cwd(name):
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100433 self.test_abspath()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000434
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300435 def test_join_errors(self):
436 # Check join() raises friendly TypeErrors.
437 with support.check_warnings(('', BytesWarning), quiet=True):
438 errmsg = "Can't mix strings and bytes in path components"
439 with self.assertRaisesRegex(TypeError, errmsg):
440 self.pathmodule.join(b'bytes', 'str')
441 with self.assertRaisesRegex(TypeError, errmsg):
442 self.pathmodule.join('str', b'bytes')
443 # regression, see #15377
444 errmsg = r'join\(\) argument must be str or bytes, not %r'
445 with self.assertRaisesRegex(TypeError, errmsg % 'int'):
446 self.pathmodule.join(42, 'str')
447 with self.assertRaisesRegex(TypeError, errmsg % 'int'):
448 self.pathmodule.join('str', 42)
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300449 with self.assertRaisesRegex(TypeError, errmsg % 'int'):
450 self.pathmodule.join(42)
451 with self.assertRaisesRegex(TypeError, errmsg % 'list'):
452 self.pathmodule.join([])
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300453 with self.assertRaisesRegex(TypeError, errmsg % 'bytearray'):
454 self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar'))
455
456 def test_relpath_errors(self):
457 # Check relpath() raises friendly TypeErrors.
Serhiy Storchakae4f47082014-10-04 16:09:02 +0300458 with support.check_warnings(('', (BytesWarning, DeprecationWarning)),
459 quiet=True):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300460 errmsg = "Can't mix strings and bytes in path components"
461 with self.assertRaisesRegex(TypeError, errmsg):
462 self.pathmodule.relpath(b'bytes', 'str')
463 with self.assertRaisesRegex(TypeError, errmsg):
464 self.pathmodule.relpath('str', b'bytes')
465 errmsg = r'relpath\(\) argument must be str or bytes, not %r'
466 with self.assertRaisesRegex(TypeError, errmsg % 'int'):
467 self.pathmodule.relpath(42, 'str')
468 with self.assertRaisesRegex(TypeError, errmsg % 'int'):
469 self.pathmodule.relpath('str', 42)
470 with self.assertRaisesRegex(TypeError, errmsg % 'bytearray'):
471 self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar'))
472
Thomas Wouters89f507f2006-12-13 04:49:30 +0000473
Thomas Wouters89f507f2006-12-13 04:49:30 +0000474if __name__=="__main__":
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200475 unittest.main()