blob: 01e11da09805e70dff41a1f285debce98cf73646 [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):
xdegaye92c2ca72017-11-12 17:31:07 +0100216 try:
217 self._test_samefile_on_link_func(os.link)
218 except PermissionError as e:
219 self.skipTest('os.link(): %s' % e)
Brian Curtine701ec52012-12-26 07:36:16 -0600220
Brian Curtin490b32a2012-12-26 07:03:03 -0600221 def test_samestat(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100222 test_fn1 = support.TESTFN
223 test_fn2 = support.TESTFN + "2"
224 self.addCleanup(support.unlink, test_fn1)
225 self.addCleanup(support.unlink, test_fn2)
226
227 create_file(test_fn1)
228 stat1 = os.stat(test_fn1)
229 self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1)))
230
231 create_file(test_fn2)
232 stat2 = os.stat(test_fn2)
233 self.assertFalse(self.pathmodule.samestat(stat1, stat2))
234
235 self.assertRaises(TypeError, self.pathmodule.samestat)
236
237 def _test_samestat_on_link_func(self, func):
238 test_fn1 = support.TESTFN + "1"
239 test_fn2 = support.TESTFN + "2"
240 self.addCleanup(support.unlink, test_fn1)
241 self.addCleanup(support.unlink, test_fn2)
242
243 create_file(test_fn1)
244 func(test_fn1, test_fn2)
245 self.assertTrue(self.pathmodule.samestat(os.stat(test_fn1),
246 os.stat(test_fn2)))
247 os.remove(test_fn2)
248
249 create_file(test_fn2)
250 self.assertFalse(self.pathmodule.samestat(os.stat(test_fn1),
251 os.stat(test_fn2)))
Brian Curtin490b32a2012-12-26 07:03:03 -0600252
253 @support.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600254 def test_samestat_on_symlink(self):
255 self._test_samestat_on_link_func(os.symlink)
256
257 def test_samestat_on_link(self):
xdegaye92c2ca72017-11-12 17:31:07 +0100258 try:
259 self._test_samestat_on_link_func(os.link)
260 except PermissionError as e:
261 self.skipTest('os.link(): %s' % e)
Brian Curtine701ec52012-12-26 07:36:16 -0600262
Brian Curtin490b32a2012-12-26 07:03:03 -0600263 def test_sameopenfile(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100264 filename = support.TESTFN
265 self.addCleanup(support.unlink, filename)
266 create_file(filename)
267
268 with open(filename, "rb", 0) as fp1:
269 fd1 = fp1.fileno()
270 with open(filename, "rb", 0) as fp2:
271 fd2 = fp2.fileno()
272 self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2))
273
Brian Curtin490b32a2012-12-26 07:03:03 -0600274
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200275class TestGenericTest(GenericTest, unittest.TestCase):
276 # Issue 16852: GenericTest can't inherit from unittest.TestCase
277 # for test discovery purposes; CommonTest inherits from GenericTest
278 # and is only meant to be inherited by others.
279 pathmodule = genericpath
Thomas Wouters89f507f2006-12-13 04:49:30 +0000280
Berker Peksag9adc1a32016-07-23 07:31:47 +0300281 def test_null_bytes(self):
282 for attr in GenericTest.common_attributes:
283 # os.path.commonprefix doesn't raise ValueError
284 if attr == 'commonprefix':
285 continue
286 with self.subTest(attr=attr):
287 with self.assertRaises(ValueError) as cm:
288 getattr(self.pathmodule, attr)('/tmp\x00abcds')
Berker Peksag5f804e32016-07-23 08:42:41 +0300289 self.assertIn('embedded null', str(cm.exception))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000290
Florent Xicluna87082ee2010-08-09 17:18:05 +0000291# Following TestCase is not supposed to be run from test_genericpath.
292# It is inherited by other test modules (macpath, ntpath, posixpath).
293
Florent Xiclunac9c79782010-03-08 12:24:53 +0000294class CommonTest(GenericTest):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000295 common_attributes = GenericTest.common_attributes + [
296 # Properties
297 'curdir', 'pardir', 'extsep', 'sep',
298 'pathsep', 'defpath', 'altsep', 'devnull',
299 # Methods
300 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
301 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
302 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
303 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000304
Florent Xiclunac9c79782010-03-08 12:24:53 +0000305 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000306 normcase = self.pathmodule.normcase
307 # check that normcase() is idempotent
308 for p in ["FoO/./BaR", b"FoO/./BaR"]:
309 p = normcase(p)
310 self.assertEqual(p, normcase(p))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000311
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000312 self.assertEqual(normcase(''), '')
313 self.assertEqual(normcase(b''), b'')
314
315 # check that normcase raises a TypeError for invalid types
316 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
317 self.assertRaises(TypeError, normcase, path)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000318
319 def test_splitdrive(self):
320 # splitdrive for non-NT paths
321 splitdrive = self.pathmodule.splitdrive
322 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
323 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
324 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
325
326 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
327 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
328 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
329
330 def test_expandvars(self):
331 if self.pathmodule.__name__ == 'macpath':
332 self.skipTest('macpath.expandvars is a stub')
333 expandvars = self.pathmodule.expandvars
334 with support.EnvironmentVarGuard() as env:
335 env.clear()
336 env["foo"] = "bar"
337 env["{foo"] = "baz1"
338 env["{foo}"] = "baz2"
339 self.assertEqual(expandvars("foo"), "foo")
340 self.assertEqual(expandvars("$foo bar"), "bar bar")
341 self.assertEqual(expandvars("${foo}bar"), "barbar")
342 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
343 self.assertEqual(expandvars("$bar bar"), "$bar bar")
344 self.assertEqual(expandvars("$?bar"), "$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000345 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
346 self.assertEqual(expandvars("${foo"), "${foo")
347 self.assertEqual(expandvars("${{foo}}"), "baz1}")
348 self.assertEqual(expandvars("$foo$foo"), "barbar")
349 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
350
351 self.assertEqual(expandvars(b"foo"), b"foo")
352 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
353 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
354 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
355 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
356 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000357 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
358 self.assertEqual(expandvars(b"${foo"), b"${foo")
359 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
360 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
361 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
362
Serhiy Storchakadbb10192014-02-13 10:13:53 +0200363 @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
364 def test_expandvars_nonascii(self):
365 if self.pathmodule.__name__ == 'macpath':
366 self.skipTest('macpath.expandvars is a stub')
367 expandvars = self.pathmodule.expandvars
368 def check(value, expected):
369 self.assertEqual(expandvars(value), expected)
370 with support.EnvironmentVarGuard() as env:
371 env.clear()
372 nonascii = support.FS_NONASCII
373 env['spam'] = nonascii
374 env[nonascii] = 'ham' + nonascii
375 check(nonascii, nonascii)
376 check('$spam bar', '%s bar' % nonascii)
377 check('${spam}bar', '%sbar' % nonascii)
378 check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
379 check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii)
380 check('$spam}bar', '%s}bar' % nonascii)
381
382 check(os.fsencode(nonascii), os.fsencode(nonascii))
383 check(b'$spam bar', os.fsencode('%s bar' % nonascii))
384 check(b'${spam}bar', os.fsencode('%sbar' % nonascii))
385 check(os.fsencode('${%s}bar' % nonascii),
386 os.fsencode('ham%sbar' % nonascii))
387 check(os.fsencode('$bar%s bar' % nonascii),
388 os.fsencode('$bar%s bar' % nonascii))
389 check(b'$spam}bar', os.fsencode('%s}bar' % nonascii))
390
Florent Xiclunac9c79782010-03-08 12:24:53 +0000391 def test_abspath(self):
392 self.assertIn("foo", self.pathmodule.abspath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100393 with warnings.catch_warnings():
394 warnings.simplefilter("ignore", DeprecationWarning)
395 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000396
Steve Dowere58571b2016-09-08 11:11:13 -0700397 # avoid UnicodeDecodeError on Windows
398 undecodable_path = b'' if sys.platform == 'win32' else b'f\xf2\xf2'
399
Florent Xiclunac9c79782010-03-08 12:24:53 +0000400 # Abspath returns bytes when the arg is bytes
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100401 with warnings.catch_warnings():
402 warnings.simplefilter("ignore", DeprecationWarning)
Steve Dowere58571b2016-09-08 11:11:13 -0700403 for path in (b'', b'foo', undecodable_path, b'/foo', b'C:\\'):
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100404 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000405
406 def test_realpath(self):
407 self.assertIn("foo", self.pathmodule.realpath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100408 with warnings.catch_warnings():
409 warnings.simplefilter("ignore", DeprecationWarning)
410 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000411
412 def test_normpath_issue5827(self):
413 # Make sure normpath preserves unicode
414 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
415 self.assertIsInstance(self.pathmodule.normpath(path), str)
416
417 def test_abspath_issue3426(self):
418 # Check that abspath returns unicode when the arg is unicode
419 # with both ASCII and non-ASCII cwds.
420 abspath = self.pathmodule.abspath
421 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
422 self.assertIsInstance(abspath(path), str)
423
424 unicwd = '\xe7w\xf0'
425 try:
Victor Stinner7c7ea622012-08-01 20:03:49 +0200426 os.fsencode(unicwd)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000427 except (AttributeError, UnicodeEncodeError):
428 # FS encoding is probably ASCII
429 pass
430 else:
431 with support.temp_cwd(unicwd):
432 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
433 self.assertIsInstance(abspath(path), str)
434
Victor Stinner1efde672010-04-18 08:23:42 +0000435 def test_nonascii_abspath(self):
Victor Stinnerff3d5152012-11-10 12:07:39 +0100436 if (support.TESTFN_UNDECODABLE
437 # Mac OS X denies the creation of a directory with an invalid
Martin Panterc04fb562016-02-10 05:44:01 +0000438 # UTF-8 name. Windows allows creating a directory with an
Victor Stinnerff3d5152012-11-10 12:07:39 +0100439 # arbitrary bytes name, but fails to enter this directory
440 # (when the bytes name is used).
441 and sys.platform not in ('win32', 'darwin')):
442 name = support.TESTFN_UNDECODABLE
443 elif support.TESTFN_NONASCII:
444 name = support.TESTFN_NONASCII
Victor Stinnerfce2a6e2012-10-31 23:01:30 +0100445 else:
Victor Stinnerff3d5152012-11-10 12:07:39 +0100446 self.skipTest("need support.TESTFN_NONASCII")
Victor Stinner7c7ea622012-08-01 20:03:49 +0200447
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100448 with warnings.catch_warnings():
449 warnings.simplefilter("ignore", DeprecationWarning)
Victor Stinner7c7ea622012-08-01 20:03:49 +0200450 with support.temp_cwd(name):
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100451 self.test_abspath()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000452
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300453 def test_join_errors(self):
454 # Check join() raises friendly TypeErrors.
455 with support.check_warnings(('', BytesWarning), quiet=True):
456 errmsg = "Can't mix strings and bytes in path components"
457 with self.assertRaisesRegex(TypeError, errmsg):
458 self.pathmodule.join(b'bytes', 'str')
459 with self.assertRaisesRegex(TypeError, errmsg):
460 self.pathmodule.join('str', b'bytes')
461 # regression, see #15377
Brett Cannon3f9183b2016-08-26 14:44:48 -0700462 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300463 self.pathmodule.join(42, 'str')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700464 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300465 self.pathmodule.join('str', 42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700466 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300467 self.pathmodule.join(42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700468 with self.assertRaisesRegex(TypeError, 'list'):
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300469 self.pathmodule.join([])
Brett Cannon3f9183b2016-08-26 14:44:48 -0700470 with self.assertRaisesRegex(TypeError, 'bytearray'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300471 self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar'))
472
473 def test_relpath_errors(self):
474 # Check relpath() raises friendly TypeErrors.
Serhiy Storchakae4f47082014-10-04 16:09:02 +0300475 with support.check_warnings(('', (BytesWarning, DeprecationWarning)),
476 quiet=True):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300477 errmsg = "Can't mix strings and bytes in path components"
478 with self.assertRaisesRegex(TypeError, errmsg):
479 self.pathmodule.relpath(b'bytes', 'str')
480 with self.assertRaisesRegex(TypeError, errmsg):
481 self.pathmodule.relpath('str', b'bytes')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700482 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300483 self.pathmodule.relpath(42, 'str')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700484 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300485 self.pathmodule.relpath('str', 42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700486 with self.assertRaisesRegex(TypeError, 'bytearray'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300487 self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar'))
488
Thomas Wouters89f507f2006-12-13 04:49:30 +0000489
Brett Cannon3f9183b2016-08-26 14:44:48 -0700490class PathLikeTests(unittest.TestCase):
491
492 class PathLike:
493 def __init__(self, path=''):
494 self.path = path
495 def __fspath__(self):
496 if isinstance(self.path, BaseException):
497 raise self.path
498 else:
499 return self.path
500
501 def setUp(self):
502 self.file_name = support.TESTFN.lower()
503 self.file_path = self.PathLike(support.TESTFN)
504 self.addCleanup(support.unlink, self.file_name)
505 create_file(self.file_name, b"test_genericpath.PathLikeTests")
506
507 def assertPathEqual(self, func):
508 self.assertEqual(func(self.file_path), func(self.file_name))
509
510 def test_path_exists(self):
511 self.assertPathEqual(os.path.exists)
512
513 def test_path_isfile(self):
514 self.assertPathEqual(os.path.isfile)
515
516 def test_path_isdir(self):
517 self.assertPathEqual(os.path.isdir)
518
519 def test_path_commonprefix(self):
520 self.assertEqual(os.path.commonprefix([self.file_path, self.file_name]),
521 self.file_name)
522
523 def test_path_getsize(self):
524 self.assertPathEqual(os.path.getsize)
525
526 def test_path_getmtime(self):
527 self.assertPathEqual(os.path.getatime)
528
529 def test_path_getctime(self):
530 self.assertPathEqual(os.path.getctime)
531
532 def test_path_samefile(self):
533 self.assertTrue(os.path.samefile(self.file_path, self.file_name))
534
535
Thomas Wouters89f507f2006-12-13 04:49:30 +0000536if __name__=="__main__":
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200537 unittest.main()