blob: ad5a59c44ed7bdbd9f6b716a1377dc389a98e70d [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
Serhiy Storchaka34601982018-01-07 17:54:31 +020011from test.support.script_helper import assert_python_ok
Thomas Wouters89f507f2006-12-13 04:49:30 +000012
Florent Xiclunac9c79782010-03-08 12:24:53 +000013
Victor Stinnere3212742016-03-24 13:44:19 +010014def create_file(filename, data=b'foo'):
15 with open(filename, 'xb', 0) as fp:
16 fp.write(data)
Florent Xiclunac9c79782010-03-08 12:24:53 +000017
18
Ezio Melottid0dfe9a2013-01-10 03:12:50 +020019class GenericTest:
Florent Xiclunac9c79782010-03-08 12:24:53 +000020 common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime',
21 'getmtime', 'exists', 'isdir', 'isfile']
22 attributes = []
23
24 def test_no_argument(self):
25 for attr in self.common_attributes + self.attributes:
26 with self.assertRaises(TypeError):
27 getattr(self.pathmodule, attr)()
28 raise self.fail("{}.{}() did not raise a TypeError"
29 .format(self.pathmodule.__name__, attr))
Thomas Wouters89f507f2006-12-13 04:49:30 +000030
Thomas Wouters89f507f2006-12-13 04:49:30 +000031 def test_commonprefix(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +000032 commonprefix = self.pathmodule.commonprefix
Thomas Wouters89f507f2006-12-13 04:49:30 +000033 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000034 commonprefix([]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000035 ""
36 )
37 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000038 commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000039 "/home/swen"
40 )
41 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000042 commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000043 "/home/swen/"
44 )
45 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000046 commonprefix(["/home/swen/spam", "/home/swen/spam"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000047 "/home/swen/spam"
48 )
Florent Xiclunae3ed2e02010-03-08 12:42:20 +000049 self.assertEqual(
50 commonprefix(["home:swenson:spam", "home:swen:spam"]),
51 "home:swen"
52 )
53 self.assertEqual(
54 commonprefix([":home:swen:spam", ":home:swen:eggs"]),
55 ":home:swen:"
56 )
57 self.assertEqual(
58 commonprefix([":home:swen:spam", ":home:swen:spam"]),
59 ":home:swen:spam"
60 )
Thomas Wouters89f507f2006-12-13 04:49:30 +000061
Florent Xiclunac9c79782010-03-08 12:24:53 +000062 self.assertEqual(
63 commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]),
64 b"/home/swen"
65 )
66 self.assertEqual(
67 commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]),
68 b"/home/swen/"
69 )
70 self.assertEqual(
71 commonprefix([b"/home/swen/spam", b"/home/swen/spam"]),
72 b"/home/swen/spam"
73 )
Florent Xiclunae3ed2e02010-03-08 12:42:20 +000074 self.assertEqual(
75 commonprefix([b"home:swenson:spam", b"home:swen:spam"]),
76 b"home:swen"
77 )
78 self.assertEqual(
79 commonprefix([b":home:swen:spam", b":home:swen:eggs"]),
80 b":home:swen:"
81 )
82 self.assertEqual(
83 commonprefix([b":home:swen:spam", b":home:swen:spam"]),
84 b":home:swen:spam"
85 )
Florent Xiclunac9c79782010-03-08 12:24:53 +000086
87 testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
88 'aXc', 'abd', 'ab', 'aX', 'abcX']
89 for s1 in testlist:
90 for s2 in testlist:
91 p = commonprefix([s1, s2])
92 self.assertTrue(s1.startswith(p))
93 self.assertTrue(s2.startswith(p))
94 if s1 != s2:
95 n = len(p)
96 self.assertNotEqual(s1[n:n+1], s2[n:n+1])
97
Thomas Wouters89f507f2006-12-13 04:49:30 +000098 def test_getsize(self):
Victor Stinnere3212742016-03-24 13:44:19 +010099 filename = support.TESTFN
100 self.addCleanup(support.unlink, filename)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000101
Victor Stinnere3212742016-03-24 13:44:19 +0100102 create_file(filename, b'Hello')
103 self.assertEqual(self.pathmodule.getsize(filename), 5)
104 os.remove(filename)
105
106 create_file(filename, b'Hello World!')
107 self.assertEqual(self.pathmodule.getsize(filename), 12)
108
109 def test_filetime(self):
110 filename = support.TESTFN
111 self.addCleanup(support.unlink, filename)
112
113 create_file(filename, b'foo')
114
115 with open(filename, "ab", 0) as f:
Guido van Rossum199fc752007-08-27 23:38:12 +0000116 f.write(b"bar")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000117
Victor Stinnere3212742016-03-24 13:44:19 +0100118 with open(filename, "rb", 0) as f:
119 data = f.read()
120 self.assertEqual(data, b"foobar")
121
122 self.assertLessEqual(
123 self.pathmodule.getctime(filename),
124 self.pathmodule.getmtime(filename)
125 )
Thomas Wouters89f507f2006-12-13 04:49:30 +0000126
127 def test_exists(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100128 filename = support.TESTFN
129 self.addCleanup(support.unlink, filename)
130
131 self.assertIs(self.pathmodule.exists(filename), False)
132
133 with open(filename, "xb") as f:
Guido van Rossum199fc752007-08-27 23:38:12 +0000134 f.write(b"foo")
Victor Stinnere3212742016-03-24 13:44:19 +0100135
136 self.assertIs(self.pathmodule.exists(filename), True)
137
138 if not self.pathmodule == genericpath:
139 self.assertIs(self.pathmodule.lexists(filename), True)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000140
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100141 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
142 def test_exists_fd(self):
143 r, w = os.pipe()
144 try:
145 self.assertTrue(self.pathmodule.exists(r))
146 finally:
147 os.close(r)
148 os.close(w)
149 self.assertFalse(self.pathmodule.exists(r))
150
Victor Stinnere3212742016-03-24 13:44:19 +0100151 def test_isdir_file(self):
152 filename = support.TESTFN
153 self.addCleanup(support.unlink, filename)
154 self.assertIs(self.pathmodule.isdir(filename), False)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000155
Victor Stinnere3212742016-03-24 13:44:19 +0100156 create_file(filename)
157 self.assertIs(self.pathmodule.isdir(filename), False)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000158
Victor Stinnere3212742016-03-24 13:44:19 +0100159 def test_isdir_dir(self):
160 filename = support.TESTFN
161 self.addCleanup(support.rmdir, filename)
162 self.assertIs(self.pathmodule.isdir(filename), False)
163
164 os.mkdir(filename)
165 self.assertIs(self.pathmodule.isdir(filename), True)
166
167 def test_isfile_file(self):
168 filename = support.TESTFN
169 self.addCleanup(support.unlink, filename)
170 self.assertIs(self.pathmodule.isfile(filename), False)
171
172 create_file(filename)
173 self.assertIs(self.pathmodule.isfile(filename), True)
174
175 def test_isfile_dir(self):
176 filename = support.TESTFN
177 self.addCleanup(support.rmdir, filename)
178 self.assertIs(self.pathmodule.isfile(filename), False)
179
180 os.mkdir(filename)
181 self.assertIs(self.pathmodule.isfile(filename), False)
Brian Curtin490b32a2012-12-26 07:03:03 -0600182
183 def test_samefile(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100184 file1 = support.TESTFN
185 file2 = support.TESTFN + "2"
186 self.addCleanup(support.unlink, file1)
187 self.addCleanup(support.unlink, file2)
188
189 create_file(file1)
190 self.assertTrue(self.pathmodule.samefile(file1, file1))
191
192 create_file(file2)
193 self.assertFalse(self.pathmodule.samefile(file1, file2))
194
195 self.assertRaises(TypeError, self.pathmodule.samefile)
196
197 def _test_samefile_on_link_func(self, func):
198 test_fn1 = support.TESTFN
199 test_fn2 = support.TESTFN + "2"
200 self.addCleanup(support.unlink, test_fn1)
201 self.addCleanup(support.unlink, test_fn2)
202
203 create_file(test_fn1)
204
205 func(test_fn1, test_fn2)
206 self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2))
207 os.remove(test_fn2)
208
209 create_file(test_fn2)
210 self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2))
Brian Curtin490b32a2012-12-26 07:03:03 -0600211
212 @support.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600213 def test_samefile_on_symlink(self):
214 self._test_samefile_on_link_func(os.symlink)
215
216 def test_samefile_on_link(self):
xdegaye92c2ca72017-11-12 17:31:07 +0100217 try:
218 self._test_samefile_on_link_func(os.link)
219 except PermissionError as e:
220 self.skipTest('os.link(): %s' % e)
Brian Curtine701ec52012-12-26 07:36:16 -0600221
Brian Curtin490b32a2012-12-26 07:03:03 -0600222 def test_samestat(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100223 test_fn1 = support.TESTFN
224 test_fn2 = support.TESTFN + "2"
225 self.addCleanup(support.unlink, test_fn1)
226 self.addCleanup(support.unlink, test_fn2)
227
228 create_file(test_fn1)
229 stat1 = os.stat(test_fn1)
230 self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1)))
231
232 create_file(test_fn2)
233 stat2 = os.stat(test_fn2)
234 self.assertFalse(self.pathmodule.samestat(stat1, stat2))
235
236 self.assertRaises(TypeError, self.pathmodule.samestat)
237
238 def _test_samestat_on_link_func(self, func):
239 test_fn1 = support.TESTFN + "1"
240 test_fn2 = support.TESTFN + "2"
241 self.addCleanup(support.unlink, test_fn1)
242 self.addCleanup(support.unlink, test_fn2)
243
244 create_file(test_fn1)
245 func(test_fn1, test_fn2)
246 self.assertTrue(self.pathmodule.samestat(os.stat(test_fn1),
247 os.stat(test_fn2)))
248 os.remove(test_fn2)
249
250 create_file(test_fn2)
251 self.assertFalse(self.pathmodule.samestat(os.stat(test_fn1),
252 os.stat(test_fn2)))
Brian Curtin490b32a2012-12-26 07:03:03 -0600253
254 @support.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600255 def test_samestat_on_symlink(self):
256 self._test_samestat_on_link_func(os.symlink)
257
258 def test_samestat_on_link(self):
xdegaye92c2ca72017-11-12 17:31:07 +0100259 try:
260 self._test_samestat_on_link_func(os.link)
261 except PermissionError as e:
262 self.skipTest('os.link(): %s' % e)
Brian Curtine701ec52012-12-26 07:36:16 -0600263
Brian Curtin490b32a2012-12-26 07:03:03 -0600264 def test_sameopenfile(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100265 filename = support.TESTFN
266 self.addCleanup(support.unlink, filename)
267 create_file(filename)
268
269 with open(filename, "rb", 0) as fp1:
270 fd1 = fp1.fileno()
271 with open(filename, "rb", 0) as fp2:
272 fd2 = fp2.fileno()
273 self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2))
274
Brian Curtin490b32a2012-12-26 07:03:03 -0600275
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200276class TestGenericTest(GenericTest, unittest.TestCase):
277 # Issue 16852: GenericTest can't inherit from unittest.TestCase
278 # for test discovery purposes; CommonTest inherits from GenericTest
279 # and is only meant to be inherited by others.
280 pathmodule = genericpath
Thomas Wouters89f507f2006-12-13 04:49:30 +0000281
Berker Peksag9adc1a32016-07-23 07:31:47 +0300282 def test_null_bytes(self):
283 for attr in GenericTest.common_attributes:
284 # os.path.commonprefix doesn't raise ValueError
285 if attr == 'commonprefix':
286 continue
287 with self.subTest(attr=attr):
288 with self.assertRaises(ValueError) as cm:
289 getattr(self.pathmodule, attr)('/tmp\x00abcds')
Berker Peksag5f804e32016-07-23 08:42:41 +0300290 self.assertIn('embedded null', str(cm.exception))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000291
Florent Xicluna87082ee2010-08-09 17:18:05 +0000292# Following TestCase is not supposed to be run from test_genericpath.
293# It is inherited by other test modules (macpath, ntpath, posixpath).
294
Florent Xiclunac9c79782010-03-08 12:24:53 +0000295class CommonTest(GenericTest):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000296 common_attributes = GenericTest.common_attributes + [
297 # Properties
298 'curdir', 'pardir', 'extsep', 'sep',
299 'pathsep', 'defpath', 'altsep', 'devnull',
300 # Methods
301 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
302 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
303 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
304 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000305
Florent Xiclunac9c79782010-03-08 12:24:53 +0000306 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000307 normcase = self.pathmodule.normcase
308 # check that normcase() is idempotent
309 for p in ["FoO/./BaR", b"FoO/./BaR"]:
310 p = normcase(p)
311 self.assertEqual(p, normcase(p))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000312
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000313 self.assertEqual(normcase(''), '')
314 self.assertEqual(normcase(b''), b'')
315
316 # check that normcase raises a TypeError for invalid types
317 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
318 self.assertRaises(TypeError, normcase, path)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000319
320 def test_splitdrive(self):
321 # splitdrive for non-NT paths
322 splitdrive = self.pathmodule.splitdrive
323 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
324 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
325 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
326
327 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
328 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
329 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
330
331 def test_expandvars(self):
332 if self.pathmodule.__name__ == 'macpath':
333 self.skipTest('macpath.expandvars is a stub')
334 expandvars = self.pathmodule.expandvars
335 with support.EnvironmentVarGuard() as env:
336 env.clear()
337 env["foo"] = "bar"
338 env["{foo"] = "baz1"
339 env["{foo}"] = "baz2"
340 self.assertEqual(expandvars("foo"), "foo")
341 self.assertEqual(expandvars("$foo bar"), "bar bar")
342 self.assertEqual(expandvars("${foo}bar"), "barbar")
343 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
344 self.assertEqual(expandvars("$bar bar"), "$bar bar")
345 self.assertEqual(expandvars("$?bar"), "$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000346 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
347 self.assertEqual(expandvars("${foo"), "${foo")
348 self.assertEqual(expandvars("${{foo}}"), "baz1}")
349 self.assertEqual(expandvars("$foo$foo"), "barbar")
350 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
351
352 self.assertEqual(expandvars(b"foo"), b"foo")
353 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
354 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
355 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
356 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
357 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000358 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
359 self.assertEqual(expandvars(b"${foo"), b"${foo")
360 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
361 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
362 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
363
Serhiy Storchakadbb10192014-02-13 10:13:53 +0200364 @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
365 def test_expandvars_nonascii(self):
366 if self.pathmodule.__name__ == 'macpath':
367 self.skipTest('macpath.expandvars is a stub')
368 expandvars = self.pathmodule.expandvars
369 def check(value, expected):
370 self.assertEqual(expandvars(value), expected)
371 with support.EnvironmentVarGuard() as env:
372 env.clear()
373 nonascii = support.FS_NONASCII
374 env['spam'] = nonascii
375 env[nonascii] = 'ham' + nonascii
376 check(nonascii, nonascii)
377 check('$spam bar', '%s bar' % nonascii)
378 check('${spam}bar', '%sbar' % nonascii)
379 check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
380 check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii)
381 check('$spam}bar', '%s}bar' % nonascii)
382
383 check(os.fsencode(nonascii), os.fsencode(nonascii))
384 check(b'$spam bar', os.fsencode('%s bar' % nonascii))
385 check(b'${spam}bar', os.fsencode('%sbar' % nonascii))
386 check(os.fsencode('${%s}bar' % nonascii),
387 os.fsencode('ham%sbar' % nonascii))
388 check(os.fsencode('$bar%s bar' % nonascii),
389 os.fsencode('$bar%s bar' % nonascii))
390 check(b'$spam}bar', os.fsencode('%s}bar' % nonascii))
391
Florent Xiclunac9c79782010-03-08 12:24:53 +0000392 def test_abspath(self):
393 self.assertIn("foo", self.pathmodule.abspath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100394 with warnings.catch_warnings():
395 warnings.simplefilter("ignore", DeprecationWarning)
396 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000397
Steve Dowere58571b2016-09-08 11:11:13 -0700398 # avoid UnicodeDecodeError on Windows
399 undecodable_path = b'' if sys.platform == 'win32' else b'f\xf2\xf2'
400
Florent Xiclunac9c79782010-03-08 12:24:53 +0000401 # Abspath returns bytes when the arg is bytes
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100402 with warnings.catch_warnings():
403 warnings.simplefilter("ignore", DeprecationWarning)
Steve Dowere58571b2016-09-08 11:11:13 -0700404 for path in (b'', b'foo', undecodable_path, b'/foo', b'C:\\'):
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100405 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000406
407 def test_realpath(self):
408 self.assertIn("foo", self.pathmodule.realpath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100409 with warnings.catch_warnings():
410 warnings.simplefilter("ignore", DeprecationWarning)
411 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000412
413 def test_normpath_issue5827(self):
414 # Make sure normpath preserves unicode
415 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
416 self.assertIsInstance(self.pathmodule.normpath(path), str)
417
418 def test_abspath_issue3426(self):
419 # Check that abspath returns unicode when the arg is unicode
420 # with both ASCII and non-ASCII cwds.
421 abspath = self.pathmodule.abspath
422 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
423 self.assertIsInstance(abspath(path), str)
424
425 unicwd = '\xe7w\xf0'
426 try:
Victor Stinner7c7ea622012-08-01 20:03:49 +0200427 os.fsencode(unicwd)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000428 except (AttributeError, UnicodeEncodeError):
429 # FS encoding is probably ASCII
430 pass
431 else:
432 with support.temp_cwd(unicwd):
433 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
434 self.assertIsInstance(abspath(path), str)
435
Victor Stinner1efde672010-04-18 08:23:42 +0000436 def test_nonascii_abspath(self):
Victor Stinnerff3d5152012-11-10 12:07:39 +0100437 if (support.TESTFN_UNDECODABLE
438 # Mac OS X denies the creation of a directory with an invalid
Martin Panterc04fb562016-02-10 05:44:01 +0000439 # UTF-8 name. Windows allows creating a directory with an
Victor Stinnerff3d5152012-11-10 12:07:39 +0100440 # arbitrary bytes name, but fails to enter this directory
441 # (when the bytes name is used).
442 and sys.platform not in ('win32', 'darwin')):
443 name = support.TESTFN_UNDECODABLE
444 elif support.TESTFN_NONASCII:
445 name = support.TESTFN_NONASCII
Victor Stinnerfce2a6e2012-10-31 23:01:30 +0100446 else:
Victor Stinnerff3d5152012-11-10 12:07:39 +0100447 self.skipTest("need support.TESTFN_NONASCII")
Victor Stinner7c7ea622012-08-01 20:03:49 +0200448
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100449 with warnings.catch_warnings():
450 warnings.simplefilter("ignore", DeprecationWarning)
Victor Stinner7c7ea622012-08-01 20:03:49 +0200451 with support.temp_cwd(name):
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100452 self.test_abspath()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000453
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300454 def test_join_errors(self):
455 # Check join() raises friendly TypeErrors.
456 with support.check_warnings(('', BytesWarning), quiet=True):
457 errmsg = "Can't mix strings and bytes in path components"
458 with self.assertRaisesRegex(TypeError, errmsg):
459 self.pathmodule.join(b'bytes', 'str')
460 with self.assertRaisesRegex(TypeError, errmsg):
461 self.pathmodule.join('str', b'bytes')
462 # regression, see #15377
Brett Cannon3f9183b2016-08-26 14:44:48 -0700463 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300464 self.pathmodule.join(42, 'str')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700465 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300466 self.pathmodule.join('str', 42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700467 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300468 self.pathmodule.join(42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700469 with self.assertRaisesRegex(TypeError, 'list'):
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300470 self.pathmodule.join([])
Brett Cannon3f9183b2016-08-26 14:44:48 -0700471 with self.assertRaisesRegex(TypeError, 'bytearray'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300472 self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar'))
473
474 def test_relpath_errors(self):
475 # Check relpath() raises friendly TypeErrors.
Serhiy Storchakae4f47082014-10-04 16:09:02 +0300476 with support.check_warnings(('', (BytesWarning, DeprecationWarning)),
477 quiet=True):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300478 errmsg = "Can't mix strings and bytes in path components"
479 with self.assertRaisesRegex(TypeError, errmsg):
480 self.pathmodule.relpath(b'bytes', 'str')
481 with self.assertRaisesRegex(TypeError, errmsg):
482 self.pathmodule.relpath('str', b'bytes')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700483 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300484 self.pathmodule.relpath(42, 'str')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700485 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300486 self.pathmodule.relpath('str', 42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700487 with self.assertRaisesRegex(TypeError, 'bytearray'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300488 self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar'))
489
Serhiy Storchaka34601982018-01-07 17:54:31 +0200490 def test_import(self):
491 assert_python_ok('-S', '-c', 'import ' + self.pathmodule.__name__)
492
Thomas Wouters89f507f2006-12-13 04:49:30 +0000493
Brett Cannon3f9183b2016-08-26 14:44:48 -0700494class PathLikeTests(unittest.TestCase):
495
496 class PathLike:
497 def __init__(self, path=''):
498 self.path = path
499 def __fspath__(self):
500 if isinstance(self.path, BaseException):
501 raise self.path
502 else:
503 return self.path
504
505 def setUp(self):
506 self.file_name = support.TESTFN.lower()
507 self.file_path = self.PathLike(support.TESTFN)
508 self.addCleanup(support.unlink, self.file_name)
509 create_file(self.file_name, b"test_genericpath.PathLikeTests")
510
511 def assertPathEqual(self, func):
512 self.assertEqual(func(self.file_path), func(self.file_name))
513
514 def test_path_exists(self):
515 self.assertPathEqual(os.path.exists)
516
517 def test_path_isfile(self):
518 self.assertPathEqual(os.path.isfile)
519
520 def test_path_isdir(self):
521 self.assertPathEqual(os.path.isdir)
522
523 def test_path_commonprefix(self):
524 self.assertEqual(os.path.commonprefix([self.file_path, self.file_name]),
525 self.file_name)
526
527 def test_path_getsize(self):
528 self.assertPathEqual(os.path.getsize)
529
530 def test_path_getmtime(self):
531 self.assertPathEqual(os.path.getatime)
532
533 def test_path_getctime(self):
534 self.assertPathEqual(os.path.getctime)
535
536 def test_path_samefile(self):
537 self.assertTrue(os.path.samefile(self.file_path, self.file_name))
538
539
Thomas Wouters89f507f2006-12-13 04:49:30 +0000540if __name__=="__main__":
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200541 unittest.main()