blob: 9ed53902dc12bd108b305db6d3dc14ce7115e8c6 [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
Miss Islington (bot)a13b6542018-03-02 02:17:51 -080012from test.support import FakePath
Thomas Wouters89f507f2006-12-13 04:49:30 +000013
Florent Xiclunac9c79782010-03-08 12:24:53 +000014
Victor Stinnere3212742016-03-24 13:44:19 +010015def create_file(filename, data=b'foo'):
16 with open(filename, 'xb', 0) as fp:
17 fp.write(data)
Florent Xiclunac9c79782010-03-08 12:24:53 +000018
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):
Victor Stinnere3212742016-03-24 13:44:19 +0100100 filename = support.TESTFN
101 self.addCleanup(support.unlink, filename)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000102
Victor Stinnere3212742016-03-24 13:44:19 +0100103 create_file(filename, b'Hello')
104 self.assertEqual(self.pathmodule.getsize(filename), 5)
105 os.remove(filename)
106
107 create_file(filename, b'Hello World!')
108 self.assertEqual(self.pathmodule.getsize(filename), 12)
109
110 def test_filetime(self):
111 filename = support.TESTFN
112 self.addCleanup(support.unlink, filename)
113
114 create_file(filename, b'foo')
115
116 with open(filename, "ab", 0) as f:
Guido van Rossum199fc752007-08-27 23:38:12 +0000117 f.write(b"bar")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118
Victor Stinnere3212742016-03-24 13:44:19 +0100119 with open(filename, "rb", 0) as f:
120 data = f.read()
121 self.assertEqual(data, b"foobar")
122
123 self.assertLessEqual(
124 self.pathmodule.getctime(filename),
125 self.pathmodule.getmtime(filename)
126 )
Thomas Wouters89f507f2006-12-13 04:49:30 +0000127
128 def test_exists(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100129 filename = support.TESTFN
130 self.addCleanup(support.unlink, filename)
131
132 self.assertIs(self.pathmodule.exists(filename), False)
133
134 with open(filename, "xb") as f:
Guido van Rossum199fc752007-08-27 23:38:12 +0000135 f.write(b"foo")
Victor Stinnere3212742016-03-24 13:44:19 +0100136
137 self.assertIs(self.pathmodule.exists(filename), True)
138
139 if not self.pathmodule == genericpath:
140 self.assertIs(self.pathmodule.lexists(filename), True)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000141
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100142 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
143 def test_exists_fd(self):
144 r, w = os.pipe()
145 try:
146 self.assertTrue(self.pathmodule.exists(r))
147 finally:
148 os.close(r)
149 os.close(w)
150 self.assertFalse(self.pathmodule.exists(r))
151
Victor Stinnere3212742016-03-24 13:44:19 +0100152 def test_isdir_file(self):
153 filename = support.TESTFN
154 self.addCleanup(support.unlink, filename)
155 self.assertIs(self.pathmodule.isdir(filename), False)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000156
Victor Stinnere3212742016-03-24 13:44:19 +0100157 create_file(filename)
158 self.assertIs(self.pathmodule.isdir(filename), False)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000159
Victor Stinnere3212742016-03-24 13:44:19 +0100160 def test_isdir_dir(self):
161 filename = support.TESTFN
162 self.addCleanup(support.rmdir, filename)
163 self.assertIs(self.pathmodule.isdir(filename), False)
164
165 os.mkdir(filename)
166 self.assertIs(self.pathmodule.isdir(filename), True)
167
168 def test_isfile_file(self):
169 filename = support.TESTFN
170 self.addCleanup(support.unlink, filename)
171 self.assertIs(self.pathmodule.isfile(filename), False)
172
173 create_file(filename)
174 self.assertIs(self.pathmodule.isfile(filename), True)
175
176 def test_isfile_dir(self):
177 filename = support.TESTFN
178 self.addCleanup(support.rmdir, filename)
179 self.assertIs(self.pathmodule.isfile(filename), False)
180
181 os.mkdir(filename)
182 self.assertIs(self.pathmodule.isfile(filename), False)
Brian Curtin490b32a2012-12-26 07:03:03 -0600183
184 def test_samefile(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100185 file1 = support.TESTFN
186 file2 = support.TESTFN + "2"
187 self.addCleanup(support.unlink, file1)
188 self.addCleanup(support.unlink, file2)
189
190 create_file(file1)
191 self.assertTrue(self.pathmodule.samefile(file1, file1))
192
193 create_file(file2)
194 self.assertFalse(self.pathmodule.samefile(file1, file2))
195
196 self.assertRaises(TypeError, self.pathmodule.samefile)
197
198 def _test_samefile_on_link_func(self, func):
199 test_fn1 = support.TESTFN
200 test_fn2 = support.TESTFN + "2"
201 self.addCleanup(support.unlink, test_fn1)
202 self.addCleanup(support.unlink, test_fn2)
203
204 create_file(test_fn1)
205
206 func(test_fn1, test_fn2)
207 self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2))
208 os.remove(test_fn2)
209
210 create_file(test_fn2)
211 self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2))
Brian Curtin490b32a2012-12-26 07:03:03 -0600212
213 @support.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600214 def test_samefile_on_symlink(self):
215 self._test_samefile_on_link_func(os.symlink)
216
217 def test_samefile_on_link(self):
xdegaye92c2ca72017-11-12 17:31:07 +0100218 try:
219 self._test_samefile_on_link_func(os.link)
220 except PermissionError as e:
221 self.skipTest('os.link(): %s' % e)
Brian Curtine701ec52012-12-26 07:36:16 -0600222
Brian Curtin490b32a2012-12-26 07:03:03 -0600223 def test_samestat(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100224 test_fn1 = support.TESTFN
225 test_fn2 = support.TESTFN + "2"
226 self.addCleanup(support.unlink, test_fn1)
227 self.addCleanup(support.unlink, test_fn2)
228
229 create_file(test_fn1)
230 stat1 = os.stat(test_fn1)
231 self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1)))
232
233 create_file(test_fn2)
234 stat2 = os.stat(test_fn2)
235 self.assertFalse(self.pathmodule.samestat(stat1, stat2))
236
237 self.assertRaises(TypeError, self.pathmodule.samestat)
238
239 def _test_samestat_on_link_func(self, func):
240 test_fn1 = support.TESTFN + "1"
241 test_fn2 = support.TESTFN + "2"
242 self.addCleanup(support.unlink, test_fn1)
243 self.addCleanup(support.unlink, test_fn2)
244
245 create_file(test_fn1)
246 func(test_fn1, test_fn2)
247 self.assertTrue(self.pathmodule.samestat(os.stat(test_fn1),
248 os.stat(test_fn2)))
249 os.remove(test_fn2)
250
251 create_file(test_fn2)
252 self.assertFalse(self.pathmodule.samestat(os.stat(test_fn1),
253 os.stat(test_fn2)))
Brian Curtin490b32a2012-12-26 07:03:03 -0600254
255 @support.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600256 def test_samestat_on_symlink(self):
257 self._test_samestat_on_link_func(os.symlink)
258
259 def test_samestat_on_link(self):
xdegaye92c2ca72017-11-12 17:31:07 +0100260 try:
261 self._test_samestat_on_link_func(os.link)
262 except PermissionError as e:
263 self.skipTest('os.link(): %s' % e)
Brian Curtine701ec52012-12-26 07:36:16 -0600264
Brian Curtin490b32a2012-12-26 07:03:03 -0600265 def test_sameopenfile(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100266 filename = support.TESTFN
267 self.addCleanup(support.unlink, filename)
268 create_file(filename)
269
270 with open(filename, "rb", 0) as fp1:
271 fd1 = fp1.fileno()
272 with open(filename, "rb", 0) as fp2:
273 fd2 = fp2.fileno()
274 self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2))
275
Brian Curtin490b32a2012-12-26 07:03:03 -0600276
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200277class TestGenericTest(GenericTest, unittest.TestCase):
278 # Issue 16852: GenericTest can't inherit from unittest.TestCase
279 # for test discovery purposes; CommonTest inherits from GenericTest
280 # and is only meant to be inherited by others.
281 pathmodule = genericpath
Thomas Wouters89f507f2006-12-13 04:49:30 +0000282
Berker Peksag9adc1a32016-07-23 07:31:47 +0300283 def test_null_bytes(self):
284 for attr in GenericTest.common_attributes:
285 # os.path.commonprefix doesn't raise ValueError
286 if attr == 'commonprefix':
287 continue
288 with self.subTest(attr=attr):
289 with self.assertRaises(ValueError) as cm:
290 getattr(self.pathmodule, attr)('/tmp\x00abcds')
Berker Peksag5f804e32016-07-23 08:42:41 +0300291 self.assertIn('embedded null', str(cm.exception))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000292
Florent Xicluna87082ee2010-08-09 17:18:05 +0000293# Following TestCase is not supposed to be run from test_genericpath.
294# It is inherited by other test modules (macpath, ntpath, posixpath).
295
Florent Xiclunac9c79782010-03-08 12:24:53 +0000296class CommonTest(GenericTest):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000297 common_attributes = GenericTest.common_attributes + [
298 # Properties
299 'curdir', 'pardir', 'extsep', 'sep',
300 'pathsep', 'defpath', 'altsep', 'devnull',
301 # Methods
302 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
303 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
304 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
305 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000306
Florent Xiclunac9c79782010-03-08 12:24:53 +0000307 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000308 normcase = self.pathmodule.normcase
309 # check that normcase() is idempotent
310 for p in ["FoO/./BaR", b"FoO/./BaR"]:
311 p = normcase(p)
312 self.assertEqual(p, normcase(p))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000313
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000314 self.assertEqual(normcase(''), '')
315 self.assertEqual(normcase(b''), b'')
316
317 # check that normcase raises a TypeError for invalid types
318 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
319 self.assertRaises(TypeError, normcase, path)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000320
321 def test_splitdrive(self):
322 # splitdrive for non-NT paths
323 splitdrive = self.pathmodule.splitdrive
324 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
325 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
326 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
327
328 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
329 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
330 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
331
332 def test_expandvars(self):
333 if self.pathmodule.__name__ == 'macpath':
334 self.skipTest('macpath.expandvars is a stub')
335 expandvars = self.pathmodule.expandvars
336 with support.EnvironmentVarGuard() as env:
337 env.clear()
338 env["foo"] = "bar"
339 env["{foo"] = "baz1"
340 env["{foo}"] = "baz2"
341 self.assertEqual(expandvars("foo"), "foo")
342 self.assertEqual(expandvars("$foo bar"), "bar bar")
343 self.assertEqual(expandvars("${foo}bar"), "barbar")
344 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
345 self.assertEqual(expandvars("$bar bar"), "$bar bar")
346 self.assertEqual(expandvars("$?bar"), "$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000347 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
348 self.assertEqual(expandvars("${foo"), "${foo")
349 self.assertEqual(expandvars("${{foo}}"), "baz1}")
350 self.assertEqual(expandvars("$foo$foo"), "barbar")
351 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
352
353 self.assertEqual(expandvars(b"foo"), b"foo")
354 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
355 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
356 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
357 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
358 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000359 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
360 self.assertEqual(expandvars(b"${foo"), b"${foo")
361 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
362 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
363 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
364
Serhiy Storchakadbb10192014-02-13 10:13:53 +0200365 @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
366 def test_expandvars_nonascii(self):
367 if self.pathmodule.__name__ == 'macpath':
368 self.skipTest('macpath.expandvars is a stub')
369 expandvars = self.pathmodule.expandvars
370 def check(value, expected):
371 self.assertEqual(expandvars(value), expected)
372 with support.EnvironmentVarGuard() as env:
373 env.clear()
374 nonascii = support.FS_NONASCII
375 env['spam'] = nonascii
376 env[nonascii] = 'ham' + nonascii
377 check(nonascii, nonascii)
378 check('$spam bar', '%s bar' % nonascii)
379 check('${spam}bar', '%sbar' % nonascii)
380 check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
381 check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii)
382 check('$spam}bar', '%s}bar' % nonascii)
383
384 check(os.fsencode(nonascii), os.fsencode(nonascii))
385 check(b'$spam bar', os.fsencode('%s bar' % nonascii))
386 check(b'${spam}bar', os.fsencode('%sbar' % nonascii))
387 check(os.fsencode('${%s}bar' % nonascii),
388 os.fsencode('ham%sbar' % nonascii))
389 check(os.fsencode('$bar%s bar' % nonascii),
390 os.fsencode('$bar%s bar' % nonascii))
391 check(b'$spam}bar', os.fsencode('%s}bar' % nonascii))
392
Florent Xiclunac9c79782010-03-08 12:24:53 +0000393 def test_abspath(self):
394 self.assertIn("foo", self.pathmodule.abspath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100395 with warnings.catch_warnings():
396 warnings.simplefilter("ignore", DeprecationWarning)
397 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000398
Steve Dowere58571b2016-09-08 11:11:13 -0700399 # avoid UnicodeDecodeError on Windows
400 undecodable_path = b'' if sys.platform == 'win32' else b'f\xf2\xf2'
401
Florent Xiclunac9c79782010-03-08 12:24:53 +0000402 # Abspath returns bytes when the arg is bytes
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100403 with warnings.catch_warnings():
404 warnings.simplefilter("ignore", DeprecationWarning)
Steve Dowere58571b2016-09-08 11:11:13 -0700405 for path in (b'', b'foo', undecodable_path, b'/foo', b'C:\\'):
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100406 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000407
408 def test_realpath(self):
409 self.assertIn("foo", self.pathmodule.realpath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100410 with warnings.catch_warnings():
411 warnings.simplefilter("ignore", DeprecationWarning)
412 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000413
414 def test_normpath_issue5827(self):
415 # Make sure normpath preserves unicode
416 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
417 self.assertIsInstance(self.pathmodule.normpath(path), str)
418
419 def test_abspath_issue3426(self):
420 # Check that abspath returns unicode when the arg is unicode
421 # with both ASCII and non-ASCII cwds.
422 abspath = self.pathmodule.abspath
423 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
424 self.assertIsInstance(abspath(path), str)
425
426 unicwd = '\xe7w\xf0'
427 try:
Victor Stinner7c7ea622012-08-01 20:03:49 +0200428 os.fsencode(unicwd)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000429 except (AttributeError, UnicodeEncodeError):
430 # FS encoding is probably ASCII
431 pass
432 else:
433 with support.temp_cwd(unicwd):
434 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
435 self.assertIsInstance(abspath(path), str)
436
Victor Stinner1efde672010-04-18 08:23:42 +0000437 def test_nonascii_abspath(self):
Victor Stinnerff3d5152012-11-10 12:07:39 +0100438 if (support.TESTFN_UNDECODABLE
439 # Mac OS X denies the creation of a directory with an invalid
Martin Panterc04fb562016-02-10 05:44:01 +0000440 # UTF-8 name. Windows allows creating a directory with an
Victor Stinnerff3d5152012-11-10 12:07:39 +0100441 # arbitrary bytes name, but fails to enter this directory
442 # (when the bytes name is used).
443 and sys.platform not in ('win32', 'darwin')):
444 name = support.TESTFN_UNDECODABLE
445 elif support.TESTFN_NONASCII:
446 name = support.TESTFN_NONASCII
Victor Stinnerfce2a6e2012-10-31 23:01:30 +0100447 else:
Victor Stinnerff3d5152012-11-10 12:07:39 +0100448 self.skipTest("need support.TESTFN_NONASCII")
Victor Stinner7c7ea622012-08-01 20:03:49 +0200449
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100450 with warnings.catch_warnings():
451 warnings.simplefilter("ignore", DeprecationWarning)
Victor Stinner7c7ea622012-08-01 20:03:49 +0200452 with support.temp_cwd(name):
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100453 self.test_abspath()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000454
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300455 def test_join_errors(self):
456 # Check join() raises friendly TypeErrors.
457 with support.check_warnings(('', BytesWarning), quiet=True):
458 errmsg = "Can't mix strings and bytes in path components"
459 with self.assertRaisesRegex(TypeError, errmsg):
460 self.pathmodule.join(b'bytes', 'str')
461 with self.assertRaisesRegex(TypeError, errmsg):
462 self.pathmodule.join('str', b'bytes')
463 # regression, see #15377
Brett Cannon3f9183b2016-08-26 14:44:48 -0700464 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300465 self.pathmodule.join(42, 'str')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700466 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300467 self.pathmodule.join('str', 42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700468 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300469 self.pathmodule.join(42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700470 with self.assertRaisesRegex(TypeError, 'list'):
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300471 self.pathmodule.join([])
Brett Cannon3f9183b2016-08-26 14:44:48 -0700472 with self.assertRaisesRegex(TypeError, 'bytearray'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300473 self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar'))
474
475 def test_relpath_errors(self):
476 # Check relpath() raises friendly TypeErrors.
Serhiy Storchakae4f47082014-10-04 16:09:02 +0300477 with support.check_warnings(('', (BytesWarning, DeprecationWarning)),
478 quiet=True):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300479 errmsg = "Can't mix strings and bytes in path components"
480 with self.assertRaisesRegex(TypeError, errmsg):
481 self.pathmodule.relpath(b'bytes', 'str')
482 with self.assertRaisesRegex(TypeError, errmsg):
483 self.pathmodule.relpath('str', b'bytes')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700484 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300485 self.pathmodule.relpath(42, 'str')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700486 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300487 self.pathmodule.relpath('str', 42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700488 with self.assertRaisesRegex(TypeError, 'bytearray'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300489 self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar'))
490
Serhiy Storchaka34601982018-01-07 17:54:31 +0200491 def test_import(self):
492 assert_python_ok('-S', '-c', 'import ' + self.pathmodule.__name__)
493
Thomas Wouters89f507f2006-12-13 04:49:30 +0000494
Brett Cannon3f9183b2016-08-26 14:44:48 -0700495class PathLikeTests(unittest.TestCase):
496
Brett Cannon3f9183b2016-08-26 14:44:48 -0700497 def setUp(self):
498 self.file_name = support.TESTFN.lower()
Miss Islington (bot)a13b6542018-03-02 02:17:51 -0800499 self.file_path = FakePath(support.TESTFN)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700500 self.addCleanup(support.unlink, self.file_name)
501 create_file(self.file_name, b"test_genericpath.PathLikeTests")
502
503 def assertPathEqual(self, func):
504 self.assertEqual(func(self.file_path), func(self.file_name))
505
506 def test_path_exists(self):
507 self.assertPathEqual(os.path.exists)
508
509 def test_path_isfile(self):
510 self.assertPathEqual(os.path.isfile)
511
512 def test_path_isdir(self):
513 self.assertPathEqual(os.path.isdir)
514
515 def test_path_commonprefix(self):
516 self.assertEqual(os.path.commonprefix([self.file_path, self.file_name]),
517 self.file_name)
518
519 def test_path_getsize(self):
520 self.assertPathEqual(os.path.getsize)
521
522 def test_path_getmtime(self):
523 self.assertPathEqual(os.path.getatime)
524
525 def test_path_getctime(self):
526 self.assertPathEqual(os.path.getctime)
527
528 def test_path_samefile(self):
529 self.assertTrue(os.path.samefile(self.file_path, self.file_name))
530
531
Thomas Wouters89f507f2006-12-13 04:49:30 +0000532if __name__=="__main__":
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200533 unittest.main()