blob: 9d5ac44b6d06a7f0cbb14644e3adfe7c312e7754 [file] [log] [blame]
Florent Xiclunac9c79782010-03-08 12:24:53 +00001"""
Victor Stinnerd7538dd2018-12-14 13:37:26 +01002Tests common to genericpath, ntpath and posixpath
Florent Xiclunac9c79782010-03-08 12:24:53 +00003"""
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
Serhiy Storchakab21d1552018-03-02 11:53:51 +020012from 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
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300130 bfilename = os.fsencode(filename)
Victor Stinnere3212742016-03-24 13:44:19 +0100131 self.addCleanup(support.unlink, filename)
132
133 self.assertIs(self.pathmodule.exists(filename), False)
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300134 self.assertIs(self.pathmodule.exists(bfilename), False)
Victor Stinnere3212742016-03-24 13:44:19 +0100135
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300136 create_file(filename)
Victor Stinnere3212742016-03-24 13:44:19 +0100137
138 self.assertIs(self.pathmodule.exists(filename), True)
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300139 self.assertIs(self.pathmodule.exists(bfilename), True)
Victor Stinnere3212742016-03-24 13:44:19 +0100140
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300141 self.assertIs(self.pathmodule.exists(filename + '\udfff'), False)
142 self.assertIs(self.pathmodule.exists(bfilename + b'\xff'), False)
143 self.assertIs(self.pathmodule.exists(filename + '\x00'), False)
144 self.assertIs(self.pathmodule.exists(bfilename + b'\x00'), False)
145
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300146 if self.pathmodule is not genericpath:
Victor Stinnere3212742016-03-24 13:44:19 +0100147 self.assertIs(self.pathmodule.lexists(filename), True)
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300148 self.assertIs(self.pathmodule.lexists(bfilename), True)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000149
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300150 self.assertIs(self.pathmodule.lexists(filename + '\udfff'), False)
151 self.assertIs(self.pathmodule.lexists(bfilename + b'\xff'), False)
152 self.assertIs(self.pathmodule.lexists(filename + '\x00'), False)
153 self.assertIs(self.pathmodule.lexists(bfilename + b'\x00'), False)
154
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100155 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
156 def test_exists_fd(self):
157 r, w = os.pipe()
158 try:
159 self.assertTrue(self.pathmodule.exists(r))
160 finally:
161 os.close(r)
162 os.close(w)
163 self.assertFalse(self.pathmodule.exists(r))
164
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300165 def test_isdir(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100166 filename = support.TESTFN
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300167 bfilename = os.fsencode(filename)
Victor Stinnere3212742016-03-24 13:44:19 +0100168 self.assertIs(self.pathmodule.isdir(filename), False)
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300169 self.assertIs(self.pathmodule.isdir(bfilename), False)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000170
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300171 self.assertIs(self.pathmodule.isdir(filename + '\udfff'), False)
172 self.assertIs(self.pathmodule.isdir(bfilename + b'\xff'), False)
173 self.assertIs(self.pathmodule.isdir(filename + '\x00'), False)
174 self.assertIs(self.pathmodule.isdir(bfilename + b'\x00'), False)
175
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300176 try:
177 create_file(filename)
178 self.assertIs(self.pathmodule.isdir(filename), False)
179 self.assertIs(self.pathmodule.isdir(bfilename), False)
180 finally:
181 support.unlink(filename)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000182
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300183 try:
184 os.mkdir(filename)
185 self.assertIs(self.pathmodule.isdir(filename), True)
186 self.assertIs(self.pathmodule.isdir(bfilename), True)
187 finally:
188 support.rmdir(filename)
189
190 def test_isfile(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100191 filename = support.TESTFN
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300192 bfilename = os.fsencode(filename)
Victor Stinnere3212742016-03-24 13:44:19 +0100193 self.assertIs(self.pathmodule.isfile(filename), False)
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300194 self.assertIs(self.pathmodule.isfile(bfilename), False)
Victor Stinnere3212742016-03-24 13:44:19 +0100195
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300196 self.assertIs(self.pathmodule.isfile(filename + '\udfff'), False)
197 self.assertIs(self.pathmodule.isfile(bfilename + b'\xff'), False)
198 self.assertIs(self.pathmodule.isfile(filename + '\x00'), False)
199 self.assertIs(self.pathmodule.isfile(bfilename + b'\x00'), False)
200
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300201 try:
202 create_file(filename)
203 self.assertIs(self.pathmodule.isfile(filename), True)
204 self.assertIs(self.pathmodule.isfile(bfilename), True)
205 finally:
206 support.unlink(filename)
Victor Stinnere3212742016-03-24 13:44:19 +0100207
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300208 try:
209 os.mkdir(filename)
210 self.assertIs(self.pathmodule.isfile(filename), False)
211 self.assertIs(self.pathmodule.isfile(bfilename), False)
212 finally:
213 support.rmdir(filename)
Brian Curtin490b32a2012-12-26 07:03:03 -0600214
215 def test_samefile(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100216 file1 = support.TESTFN
217 file2 = support.TESTFN + "2"
218 self.addCleanup(support.unlink, file1)
219 self.addCleanup(support.unlink, file2)
220
221 create_file(file1)
222 self.assertTrue(self.pathmodule.samefile(file1, file1))
223
224 create_file(file2)
225 self.assertFalse(self.pathmodule.samefile(file1, file2))
226
227 self.assertRaises(TypeError, self.pathmodule.samefile)
228
229 def _test_samefile_on_link_func(self, func):
230 test_fn1 = support.TESTFN
231 test_fn2 = support.TESTFN + "2"
232 self.addCleanup(support.unlink, test_fn1)
233 self.addCleanup(support.unlink, test_fn2)
234
235 create_file(test_fn1)
236
237 func(test_fn1, test_fn2)
238 self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2))
239 os.remove(test_fn2)
240
241 create_file(test_fn2)
242 self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2))
Brian Curtin490b32a2012-12-26 07:03:03 -0600243
244 @support.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600245 def test_samefile_on_symlink(self):
246 self._test_samefile_on_link_func(os.symlink)
247
248 def test_samefile_on_link(self):
xdegaye92c2ca72017-11-12 17:31:07 +0100249 try:
250 self._test_samefile_on_link_func(os.link)
251 except PermissionError as e:
252 self.skipTest('os.link(): %s' % e)
Brian Curtine701ec52012-12-26 07:36:16 -0600253
Brian Curtin490b32a2012-12-26 07:03:03 -0600254 def test_samestat(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100255 test_fn1 = support.TESTFN
256 test_fn2 = support.TESTFN + "2"
257 self.addCleanup(support.unlink, test_fn1)
258 self.addCleanup(support.unlink, test_fn2)
259
260 create_file(test_fn1)
261 stat1 = os.stat(test_fn1)
262 self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1)))
263
264 create_file(test_fn2)
265 stat2 = os.stat(test_fn2)
266 self.assertFalse(self.pathmodule.samestat(stat1, stat2))
267
268 self.assertRaises(TypeError, self.pathmodule.samestat)
269
270 def _test_samestat_on_link_func(self, func):
271 test_fn1 = support.TESTFN + "1"
272 test_fn2 = support.TESTFN + "2"
273 self.addCleanup(support.unlink, test_fn1)
274 self.addCleanup(support.unlink, test_fn2)
275
276 create_file(test_fn1)
277 func(test_fn1, test_fn2)
278 self.assertTrue(self.pathmodule.samestat(os.stat(test_fn1),
279 os.stat(test_fn2)))
280 os.remove(test_fn2)
281
282 create_file(test_fn2)
283 self.assertFalse(self.pathmodule.samestat(os.stat(test_fn1),
284 os.stat(test_fn2)))
Brian Curtin490b32a2012-12-26 07:03:03 -0600285
286 @support.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600287 def test_samestat_on_symlink(self):
288 self._test_samestat_on_link_func(os.symlink)
289
290 def test_samestat_on_link(self):
xdegaye92c2ca72017-11-12 17:31:07 +0100291 try:
292 self._test_samestat_on_link_func(os.link)
293 except PermissionError as e:
294 self.skipTest('os.link(): %s' % e)
Brian Curtine701ec52012-12-26 07:36:16 -0600295
Brian Curtin490b32a2012-12-26 07:03:03 -0600296 def test_sameopenfile(self):
Victor Stinnere3212742016-03-24 13:44:19 +0100297 filename = support.TESTFN
298 self.addCleanup(support.unlink, filename)
299 create_file(filename)
300
301 with open(filename, "rb", 0) as fp1:
302 fd1 = fp1.fileno()
303 with open(filename, "rb", 0) as fp2:
304 fd2 = fp2.fileno()
305 self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2))
306
Brian Curtin490b32a2012-12-26 07:03:03 -0600307
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200308class TestGenericTest(GenericTest, unittest.TestCase):
309 # Issue 16852: GenericTest can't inherit from unittest.TestCase
310 # for test discovery purposes; CommonTest inherits from GenericTest
311 # and is only meant to be inherited by others.
312 pathmodule = genericpath
Thomas Wouters89f507f2006-12-13 04:49:30 +0000313
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300314 def test_invalid_paths(self):
Berker Peksag9adc1a32016-07-23 07:31:47 +0300315 for attr in GenericTest.common_attributes:
316 # os.path.commonprefix doesn't raise ValueError
317 if attr == 'commonprefix':
318 continue
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300319 func = getattr(self.pathmodule, attr)
Berker Peksag9adc1a32016-07-23 07:31:47 +0300320 with self.subTest(attr=attr):
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300321 if attr in ('exists', 'isdir', 'isfile'):
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300322 func('/tmp\udfffabcds')
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300323 func(b'/tmp\xffabcds')
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300324 func('/tmp\x00abcds')
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300325 func(b'/tmp\x00abcds')
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300326 else:
327 with self.assertRaises((OSError, UnicodeEncodeError)):
328 func('/tmp\udfffabcds')
329 with self.assertRaises((OSError, UnicodeDecodeError)):
330 func(b'/tmp\xffabcds')
331 with self.assertRaisesRegex(ValueError, 'embedded null'):
332 func('/tmp\x00abcds')
333 with self.assertRaisesRegex(ValueError, 'embedded null'):
334 func(b'/tmp\x00abcds')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000335
Florent Xicluna87082ee2010-08-09 17:18:05 +0000336# Following TestCase is not supposed to be run from test_genericpath.
Victor Stinnerd7538dd2018-12-14 13:37:26 +0100337# It is inherited by other test modules (ntpath, posixpath).
Florent Xicluna87082ee2010-08-09 17:18:05 +0000338
Florent Xiclunac9c79782010-03-08 12:24:53 +0000339class CommonTest(GenericTest):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000340 common_attributes = GenericTest.common_attributes + [
341 # Properties
342 'curdir', 'pardir', 'extsep', 'sep',
343 'pathsep', 'defpath', 'altsep', 'devnull',
344 # Methods
345 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
346 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
347 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
348 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000349
Florent Xiclunac9c79782010-03-08 12:24:53 +0000350 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000351 normcase = self.pathmodule.normcase
352 # check that normcase() is idempotent
353 for p in ["FoO/./BaR", b"FoO/./BaR"]:
354 p = normcase(p)
355 self.assertEqual(p, normcase(p))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000356
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000357 self.assertEqual(normcase(''), '')
358 self.assertEqual(normcase(b''), b'')
359
360 # check that normcase raises a TypeError for invalid types
361 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
362 self.assertRaises(TypeError, normcase, path)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000363
364 def test_splitdrive(self):
365 # splitdrive for non-NT paths
366 splitdrive = self.pathmodule.splitdrive
367 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
368 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
369 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
370
371 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
372 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
373 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
374
375 def test_expandvars(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000376 expandvars = self.pathmodule.expandvars
377 with support.EnvironmentVarGuard() as env:
378 env.clear()
379 env["foo"] = "bar"
380 env["{foo"] = "baz1"
381 env["{foo}"] = "baz2"
382 self.assertEqual(expandvars("foo"), "foo")
383 self.assertEqual(expandvars("$foo bar"), "bar bar")
384 self.assertEqual(expandvars("${foo}bar"), "barbar")
385 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
386 self.assertEqual(expandvars("$bar bar"), "$bar bar")
387 self.assertEqual(expandvars("$?bar"), "$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000388 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
389 self.assertEqual(expandvars("${foo"), "${foo")
390 self.assertEqual(expandvars("${{foo}}"), "baz1}")
391 self.assertEqual(expandvars("$foo$foo"), "barbar")
392 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
393
394 self.assertEqual(expandvars(b"foo"), b"foo")
395 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
396 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
397 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
398 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
399 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000400 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
401 self.assertEqual(expandvars(b"${foo"), b"${foo")
402 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
403 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
404 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
405
Serhiy Storchakadbb10192014-02-13 10:13:53 +0200406 @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
407 def test_expandvars_nonascii(self):
Serhiy Storchakadbb10192014-02-13 10:13:53 +0200408 expandvars = self.pathmodule.expandvars
409 def check(value, expected):
410 self.assertEqual(expandvars(value), expected)
411 with support.EnvironmentVarGuard() as env:
412 env.clear()
413 nonascii = support.FS_NONASCII
414 env['spam'] = nonascii
415 env[nonascii] = 'ham' + nonascii
416 check(nonascii, nonascii)
417 check('$spam bar', '%s bar' % nonascii)
418 check('${spam}bar', '%sbar' % nonascii)
419 check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
420 check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii)
421 check('$spam}bar', '%s}bar' % nonascii)
422
423 check(os.fsencode(nonascii), os.fsencode(nonascii))
424 check(b'$spam bar', os.fsencode('%s bar' % nonascii))
425 check(b'${spam}bar', os.fsencode('%sbar' % nonascii))
426 check(os.fsencode('${%s}bar' % nonascii),
427 os.fsencode('ham%sbar' % nonascii))
428 check(os.fsencode('$bar%s bar' % nonascii),
429 os.fsencode('$bar%s bar' % nonascii))
430 check(b'$spam}bar', os.fsencode('%s}bar' % nonascii))
431
Florent Xiclunac9c79782010-03-08 12:24:53 +0000432 def test_abspath(self):
433 self.assertIn("foo", self.pathmodule.abspath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100434 with warnings.catch_warnings():
435 warnings.simplefilter("ignore", DeprecationWarning)
436 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000437
Steve Dowere58571b2016-09-08 11:11:13 -0700438 # avoid UnicodeDecodeError on Windows
439 undecodable_path = b'' if sys.platform == 'win32' else b'f\xf2\xf2'
440
Florent Xiclunac9c79782010-03-08 12:24:53 +0000441 # Abspath returns bytes when the arg is bytes
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100442 with warnings.catch_warnings():
443 warnings.simplefilter("ignore", DeprecationWarning)
Steve Dowere58571b2016-09-08 11:11:13 -0700444 for path in (b'', b'foo', undecodable_path, b'/foo', b'C:\\'):
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100445 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000446
447 def test_realpath(self):
448 self.assertIn("foo", self.pathmodule.realpath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100449 with warnings.catch_warnings():
450 warnings.simplefilter("ignore", DeprecationWarning)
451 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000452
453 def test_normpath_issue5827(self):
454 # Make sure normpath preserves unicode
455 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
456 self.assertIsInstance(self.pathmodule.normpath(path), str)
457
458 def test_abspath_issue3426(self):
459 # Check that abspath returns unicode when the arg is unicode
460 # with both ASCII and non-ASCII cwds.
461 abspath = self.pathmodule.abspath
462 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
463 self.assertIsInstance(abspath(path), str)
464
465 unicwd = '\xe7w\xf0'
466 try:
Victor Stinner7c7ea622012-08-01 20:03:49 +0200467 os.fsencode(unicwd)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000468 except (AttributeError, UnicodeEncodeError):
469 # FS encoding is probably ASCII
470 pass
471 else:
472 with support.temp_cwd(unicwd):
473 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
474 self.assertIsInstance(abspath(path), str)
475
Victor Stinner1efde672010-04-18 08:23:42 +0000476 def test_nonascii_abspath(self):
Victor Stinnerff3d5152012-11-10 12:07:39 +0100477 if (support.TESTFN_UNDECODABLE
478 # Mac OS X denies the creation of a directory with an invalid
Martin Panterc04fb562016-02-10 05:44:01 +0000479 # UTF-8 name. Windows allows creating a directory with an
Victor Stinnerff3d5152012-11-10 12:07:39 +0100480 # arbitrary bytes name, but fails to enter this directory
481 # (when the bytes name is used).
482 and sys.platform not in ('win32', 'darwin')):
483 name = support.TESTFN_UNDECODABLE
484 elif support.TESTFN_NONASCII:
485 name = support.TESTFN_NONASCII
Victor Stinnerfce2a6e2012-10-31 23:01:30 +0100486 else:
Victor Stinnerff3d5152012-11-10 12:07:39 +0100487 self.skipTest("need support.TESTFN_NONASCII")
Victor Stinner7c7ea622012-08-01 20:03:49 +0200488
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100489 with warnings.catch_warnings():
490 warnings.simplefilter("ignore", DeprecationWarning)
Victor Stinner7c7ea622012-08-01 20:03:49 +0200491 with support.temp_cwd(name):
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100492 self.test_abspath()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000493
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300494 def test_join_errors(self):
495 # Check join() raises friendly TypeErrors.
496 with support.check_warnings(('', BytesWarning), quiet=True):
497 errmsg = "Can't mix strings and bytes in path components"
498 with self.assertRaisesRegex(TypeError, errmsg):
499 self.pathmodule.join(b'bytes', 'str')
500 with self.assertRaisesRegex(TypeError, errmsg):
501 self.pathmodule.join('str', b'bytes')
502 # regression, see #15377
Brett Cannon3f9183b2016-08-26 14:44:48 -0700503 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300504 self.pathmodule.join(42, 'str')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700505 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300506 self.pathmodule.join('str', 42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700507 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300508 self.pathmodule.join(42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700509 with self.assertRaisesRegex(TypeError, 'list'):
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300510 self.pathmodule.join([])
Brett Cannon3f9183b2016-08-26 14:44:48 -0700511 with self.assertRaisesRegex(TypeError, 'bytearray'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300512 self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar'))
513
514 def test_relpath_errors(self):
515 # Check relpath() raises friendly TypeErrors.
Serhiy Storchakae4f47082014-10-04 16:09:02 +0300516 with support.check_warnings(('', (BytesWarning, DeprecationWarning)),
517 quiet=True):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300518 errmsg = "Can't mix strings and bytes in path components"
519 with self.assertRaisesRegex(TypeError, errmsg):
520 self.pathmodule.relpath(b'bytes', 'str')
521 with self.assertRaisesRegex(TypeError, errmsg):
522 self.pathmodule.relpath('str', b'bytes')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700523 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300524 self.pathmodule.relpath(42, 'str')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700525 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300526 self.pathmodule.relpath('str', 42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700527 with self.assertRaisesRegex(TypeError, 'bytearray'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300528 self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar'))
529
Serhiy Storchaka34601982018-01-07 17:54:31 +0200530 def test_import(self):
531 assert_python_ok('-S', '-c', 'import ' + self.pathmodule.__name__)
532
Thomas Wouters89f507f2006-12-13 04:49:30 +0000533
Brett Cannon3f9183b2016-08-26 14:44:48 -0700534class PathLikeTests(unittest.TestCase):
535
Brett Cannon3f9183b2016-08-26 14:44:48 -0700536 def setUp(self):
537 self.file_name = support.TESTFN.lower()
Serhiy Storchakab21d1552018-03-02 11:53:51 +0200538 self.file_path = FakePath(support.TESTFN)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700539 self.addCleanup(support.unlink, self.file_name)
540 create_file(self.file_name, b"test_genericpath.PathLikeTests")
541
542 def assertPathEqual(self, func):
543 self.assertEqual(func(self.file_path), func(self.file_name))
544
545 def test_path_exists(self):
546 self.assertPathEqual(os.path.exists)
547
548 def test_path_isfile(self):
549 self.assertPathEqual(os.path.isfile)
550
551 def test_path_isdir(self):
552 self.assertPathEqual(os.path.isdir)
553
554 def test_path_commonprefix(self):
555 self.assertEqual(os.path.commonprefix([self.file_path, self.file_name]),
556 self.file_name)
557
558 def test_path_getsize(self):
559 self.assertPathEqual(os.path.getsize)
560
561 def test_path_getmtime(self):
562 self.assertPathEqual(os.path.getatime)
563
564 def test_path_getctime(self):
565 self.assertPathEqual(os.path.getctime)
566
567 def test_path_samefile(self):
568 self.assertTrue(os.path.samefile(self.file_path, self.file_name))
569
570
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300571if __name__ == "__main__":
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200572 unittest.main()