blob: b77d1d79a1e88f6c73d38cc4909eb792e815d027 [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
13def safe_rmdir(dirname):
14 try:
15 os.rmdir(dirname)
16 except OSError:
17 pass
18
19
Ezio Melottid0dfe9a2013-01-10 03:12:50 +020020class GenericTest:
Florent Xiclunac9c79782010-03-08 12:24:53 +000021 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 Wouters89f507f2006-12-13 04:49:30 +000031
Thomas Wouters89f507f2006-12-13 04:49:30 +000032 def test_commonprefix(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +000033 commonprefix = self.pathmodule.commonprefix
Thomas Wouters89f507f2006-12-13 04:49:30 +000034 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000035 commonprefix([]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000036 ""
37 )
38 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000039 commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000040 "/home/swen"
41 )
42 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000043 commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000044 "/home/swen/"
45 )
46 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000047 commonprefix(["/home/swen/spam", "/home/swen/spam"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000048 "/home/swen/spam"
49 )
Florent Xiclunae3ed2e02010-03-08 12:42:20 +000050 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 Wouters89f507f2006-12-13 04:49:30 +000062
Florent Xiclunac9c79782010-03-08 12:24:53 +000063 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 Xiclunae3ed2e02010-03-08 12:42:20 +000075 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 Xiclunac9c79782010-03-08 12:24:53 +000087
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 Wouters89f507f2006-12-13 04:49:30 +000099 def test_getsize(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000100 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000101 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000102 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000103 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000104 self.assertEqual(self.pathmodule.getsize(support.TESTFN), 3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 finally:
106 if not f.closed:
107 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000108 support.unlink(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000109
110 def test_time(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000111 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000113 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000115 f = open(support.TESTFN, "ab")
Guido van Rossum199fc752007-08-27 23:38:12 +0000116 f.write(b"bar")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000117 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000118 f = open(support.TESTFN, "rb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119 d = f.read()
120 f.close()
Guido van Rossumdc122882007-07-10 12:09:13 +0000121 self.assertEqual(d, b"foobar")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000122
Ezio Melottia11865b2010-02-20 09:47:55 +0000123 self.assertLessEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +0000124 self.pathmodule.getctime(support.TESTFN),
125 self.pathmodule.getmtime(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000126 )
127 finally:
128 if not f.closed:
129 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000130 support.unlink(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000131
132 def test_exists(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000133 self.assertIs(self.pathmodule.exists(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000134 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000135 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000136 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000137 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000138 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 Wouters89f507f2006-12-13 04:49:30 +0000142 finally:
143 if not f.close():
144 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000145 support.unlink(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000146
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100147 @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 Wouters89f507f2006-12-13 04:49:30 +0000157 def test_isdir(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000158 self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000159 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000160 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000161 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000162 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000163 self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000164 os.remove(support.TESTFN)
165 os.mkdir(support.TESTFN)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000166 self.assertIs(self.pathmodule.isdir(support.TESTFN), True)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000167 os.rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000168 finally:
169 if not f.close():
170 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000171 support.unlink(support.TESTFN)
172 safe_rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000173
174 def test_isfile(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000175 self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000176 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000177 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000178 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000179 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000180 self.assertIs(self.pathmodule.isfile(support.TESTFN), True)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000181 os.remove(support.TESTFN)
182 os.mkdir(support.TESTFN)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000183 self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000184 os.rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000185 finally:
186 if not f.close():
187 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000188 support.unlink(support.TESTFN)
189 safe_rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190
Brian Curtin490b32a2012-12-26 07:03:03 -0600191 @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 Curtine701ec52012-12-26 07:36:16 -0600206 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 Curtin490b32a2012-12-26 07:03:03 -0600213 try:
214 test_fn1 = support.TESTFN + "1"
215 test_fn2 = support.TESTFN + "2"
216 self._create_file(test_fn1)
217
Brian Curtine701ec52012-12-26 07:36:16 -0600218 func(test_fn1, test_fn2)
Brian Curtin490b32a2012-12-26 07:03:03 -0600219 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 Curtine701ec52012-12-26 07:36:16 -0600239 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 Curtin490b32a2012-12-26 07:03:03 -0600246 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 Curtine701ec52012-12-26 07:36:16 -0600251 func(*test_fns)
Brian Curtin490b32a2012-12-26 07:03:03 -0600252 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 Melottid0dfe9a2013-01-10 03:12:50 +0200271class 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 Wouters89f507f2006-12-13 04:49:30 +0000276
Berker Peksag9adc1a32016-07-23 07:31:47 +0300277 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 Peksag5f804e32016-07-23 08:42:41 +0300285 self.assertIn('embedded null', str(cm.exception))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000286
Florent Xicluna87082ee2010-08-09 17:18:05 +0000287# Following TestCase is not supposed to be run from test_genericpath.
288# It is inherited by other test modules (macpath, ntpath, posixpath).
289
Florent Xiclunac9c79782010-03-08 12:24:53 +0000290class CommonTest(GenericTest):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000291 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 Wouters89f507f2006-12-13 04:49:30 +0000300
Florent Xiclunac9c79782010-03-08 12:24:53 +0000301 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000302 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 Xiclunac9c79782010-03-08 12:24:53 +0000307
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000308 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 Xiclunac9c79782010-03-08 12:24:53 +0000314
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 Xiclunac9c79782010-03-08 12:24:53 +0000341 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 Xiclunac9c79782010-03-08 12:24:53 +0000353 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 Storchakadbb10192014-02-13 10:13:53 +0200359 @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 Xiclunac9c79782010-03-08 12:24:53 +0000387 def test_abspath(self):
388 self.assertIn("foo", self.pathmodule.abspath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100389 with warnings.catch_warnings():
390 warnings.simplefilter("ignore", DeprecationWarning)
391 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000392
393 # Abspath returns bytes when the arg is bytes
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100394 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 Xiclunac9c79782010-03-08 12:24:53 +0000398
399 def test_realpath(self):
400 self.assertIn("foo", self.pathmodule.realpath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100401 with warnings.catch_warnings():
402 warnings.simplefilter("ignore", DeprecationWarning)
403 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000404
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 Stinner7c7ea622012-08-01 20:03:49 +0200419 os.fsencode(unicwd)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000420 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 Stinner1efde672010-04-18 08:23:42 +0000428 def test_nonascii_abspath(self):
Victor Stinnerff3d5152012-11-10 12:07:39 +0100429 if (support.TESTFN_UNDECODABLE
430 # Mac OS X denies the creation of a directory with an invalid
Martin Panterc04fb562016-02-10 05:44:01 +0000431 # UTF-8 name. Windows allows creating a directory with an
Victor Stinnerff3d5152012-11-10 12:07:39 +0100432 # 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 Stinnerfce2a6e2012-10-31 23:01:30 +0100438 else:
Victor Stinnerff3d5152012-11-10 12:07:39 +0100439 self.skipTest("need support.TESTFN_NONASCII")
Victor Stinner7c7ea622012-08-01 20:03:49 +0200440
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100441 with warnings.catch_warnings():
442 warnings.simplefilter("ignore", DeprecationWarning)
Victor Stinner7c7ea622012-08-01 20:03:49 +0200443 with support.temp_cwd(name):
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100444 self.test_abspath()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000445
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300446 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 Storchaka5bfc03f2015-05-19 11:00:07 +0300460 with self.assertRaisesRegex(TypeError, errmsg % 'int'):
461 self.pathmodule.join(42)
462 with self.assertRaisesRegex(TypeError, errmsg % 'list'):
463 self.pathmodule.join([])
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300464 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 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')
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 Wouters89f507f2006-12-13 04:49:30 +0000484
Thomas Wouters89f507f2006-12-13 04:49:30 +0000485if __name__=="__main__":
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200486 unittest.main()