blob: 1ff7f75ad3e6147936b14c86160c6d999debfe3d [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
Hai Shi46605972020-08-04 00:49:18 +080010from test.support import os_helper
11from test.support import warnings_helper
Serhiy Storchaka34601982018-01-07 17:54:31 +020012from test.support.script_helper import assert_python_ok
Hai Shi46605972020-08-04 00:49:18 +080013from test.support.os_helper import FakePath
Thomas Wouters89f507f2006-12-13 04:49:30 +000014
Florent Xiclunac9c79782010-03-08 12:24:53 +000015
Victor Stinnere3212742016-03-24 13:44:19 +010016def create_file(filename, data=b'foo'):
17 with open(filename, 'xb', 0) as fp:
18 fp.write(data)
Florent Xiclunac9c79782010-03-08 12:24:53 +000019
20
Ezio Melottid0dfe9a2013-01-10 03:12:50 +020021class GenericTest:
Florent Xiclunac9c79782010-03-08 12:24:53 +000022 common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime',
23 'getmtime', 'exists', 'isdir', 'isfile']
24 attributes = []
25
26 def test_no_argument(self):
27 for attr in self.common_attributes + self.attributes:
28 with self.assertRaises(TypeError):
29 getattr(self.pathmodule, attr)()
30 raise self.fail("{}.{}() did not raise a TypeError"
31 .format(self.pathmodule.__name__, attr))
Thomas Wouters89f507f2006-12-13 04:49:30 +000032
Thomas Wouters89f507f2006-12-13 04:49:30 +000033 def test_commonprefix(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +000034 commonprefix = self.pathmodule.commonprefix
Thomas Wouters89f507f2006-12-13 04:49:30 +000035 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000036 commonprefix([]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000037 ""
38 )
39 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000040 commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000041 "/home/swen"
42 )
43 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000044 commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000045 "/home/swen/"
46 )
47 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000048 commonprefix(["/home/swen/spam", "/home/swen/spam"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000049 "/home/swen/spam"
50 )
Florent Xiclunae3ed2e02010-03-08 12:42:20 +000051 self.assertEqual(
52 commonprefix(["home:swenson:spam", "home:swen:spam"]),
53 "home:swen"
54 )
55 self.assertEqual(
56 commonprefix([":home:swen:spam", ":home:swen:eggs"]),
57 ":home:swen:"
58 )
59 self.assertEqual(
60 commonprefix([":home:swen:spam", ":home:swen:spam"]),
61 ":home:swen:spam"
62 )
Thomas Wouters89f507f2006-12-13 04:49:30 +000063
Florent Xiclunac9c79782010-03-08 12:24:53 +000064 self.assertEqual(
65 commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]),
66 b"/home/swen"
67 )
68 self.assertEqual(
69 commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]),
70 b"/home/swen/"
71 )
72 self.assertEqual(
73 commonprefix([b"/home/swen/spam", b"/home/swen/spam"]),
74 b"/home/swen/spam"
75 )
Florent Xiclunae3ed2e02010-03-08 12:42:20 +000076 self.assertEqual(
77 commonprefix([b"home:swenson:spam", b"home:swen:spam"]),
78 b"home:swen"
79 )
80 self.assertEqual(
81 commonprefix([b":home:swen:spam", b":home:swen:eggs"]),
82 b":home:swen:"
83 )
84 self.assertEqual(
85 commonprefix([b":home:swen:spam", b":home:swen:spam"]),
86 b":home:swen:spam"
87 )
Florent Xiclunac9c79782010-03-08 12:24:53 +000088
89 testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
90 'aXc', 'abd', 'ab', 'aX', 'abcX']
91 for s1 in testlist:
92 for s2 in testlist:
93 p = commonprefix([s1, s2])
94 self.assertTrue(s1.startswith(p))
95 self.assertTrue(s2.startswith(p))
96 if s1 != s2:
97 n = len(p)
98 self.assertNotEqual(s1[n:n+1], s2[n:n+1])
99
Thomas Wouters89f507f2006-12-13 04:49:30 +0000100 def test_getsize(self):
Hai Shi46605972020-08-04 00:49:18 +0800101 filename = os_helper.TESTFN
102 self.addCleanup(os_helper.unlink, filename)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000103
Victor Stinnere3212742016-03-24 13:44:19 +0100104 create_file(filename, b'Hello')
105 self.assertEqual(self.pathmodule.getsize(filename), 5)
106 os.remove(filename)
107
108 create_file(filename, b'Hello World!')
109 self.assertEqual(self.pathmodule.getsize(filename), 12)
110
111 def test_filetime(self):
Hai Shi46605972020-08-04 00:49:18 +0800112 filename = os_helper.TESTFN
113 self.addCleanup(os_helper.unlink, filename)
Victor Stinnere3212742016-03-24 13:44:19 +0100114
115 create_file(filename, b'foo')
116
117 with open(filename, "ab", 0) as f:
Guido van Rossum199fc752007-08-27 23:38:12 +0000118 f.write(b"bar")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119
Victor Stinnere3212742016-03-24 13:44:19 +0100120 with open(filename, "rb", 0) as f:
121 data = f.read()
122 self.assertEqual(data, b"foobar")
123
124 self.assertLessEqual(
125 self.pathmodule.getctime(filename),
126 self.pathmodule.getmtime(filename)
127 )
Thomas Wouters89f507f2006-12-13 04:49:30 +0000128
129 def test_exists(self):
Hai Shi46605972020-08-04 00:49:18 +0800130 filename = os_helper.TESTFN
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300131 bfilename = os.fsencode(filename)
Hai Shi46605972020-08-04 00:49:18 +0800132 self.addCleanup(os_helper.unlink, filename)
Victor Stinnere3212742016-03-24 13:44:19 +0100133
134 self.assertIs(self.pathmodule.exists(filename), False)
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300135 self.assertIs(self.pathmodule.exists(bfilename), False)
Victor Stinnere3212742016-03-24 13:44:19 +0100136
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300137 create_file(filename)
Victor Stinnere3212742016-03-24 13:44:19 +0100138
139 self.assertIs(self.pathmodule.exists(filename), True)
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300140 self.assertIs(self.pathmodule.exists(bfilename), True)
Victor Stinnere3212742016-03-24 13:44:19 +0100141
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300142 self.assertIs(self.pathmodule.exists(filename + '\udfff'), False)
143 self.assertIs(self.pathmodule.exists(bfilename + b'\xff'), False)
144 self.assertIs(self.pathmodule.exists(filename + '\x00'), False)
145 self.assertIs(self.pathmodule.exists(bfilename + b'\x00'), False)
146
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300147 if self.pathmodule is not genericpath:
Victor Stinnere3212742016-03-24 13:44:19 +0100148 self.assertIs(self.pathmodule.lexists(filename), True)
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300149 self.assertIs(self.pathmodule.lexists(bfilename), True)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300151 self.assertIs(self.pathmodule.lexists(filename + '\udfff'), False)
152 self.assertIs(self.pathmodule.lexists(bfilename + b'\xff'), False)
153 self.assertIs(self.pathmodule.lexists(filename + '\x00'), False)
154 self.assertIs(self.pathmodule.lexists(bfilename + b'\x00'), False)
155
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100156 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
157 def test_exists_fd(self):
158 r, w = os.pipe()
159 try:
160 self.assertTrue(self.pathmodule.exists(r))
161 finally:
162 os.close(r)
163 os.close(w)
164 self.assertFalse(self.pathmodule.exists(r))
165
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300166 def test_isdir(self):
Hai Shi46605972020-08-04 00:49:18 +0800167 filename = os_helper.TESTFN
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300168 bfilename = os.fsencode(filename)
Victor Stinnere3212742016-03-24 13:44:19 +0100169 self.assertIs(self.pathmodule.isdir(filename), False)
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300170 self.assertIs(self.pathmodule.isdir(bfilename), False)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000171
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300172 self.assertIs(self.pathmodule.isdir(filename + '\udfff'), False)
173 self.assertIs(self.pathmodule.isdir(bfilename + b'\xff'), False)
174 self.assertIs(self.pathmodule.isdir(filename + '\x00'), False)
175 self.assertIs(self.pathmodule.isdir(bfilename + b'\x00'), False)
176
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300177 try:
178 create_file(filename)
179 self.assertIs(self.pathmodule.isdir(filename), False)
180 self.assertIs(self.pathmodule.isdir(bfilename), False)
181 finally:
Hai Shi46605972020-08-04 00:49:18 +0800182 os_helper.unlink(filename)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000183
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300184 try:
185 os.mkdir(filename)
186 self.assertIs(self.pathmodule.isdir(filename), True)
187 self.assertIs(self.pathmodule.isdir(bfilename), True)
188 finally:
Hai Shi46605972020-08-04 00:49:18 +0800189 os_helper.rmdir(filename)
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300190
191 def test_isfile(self):
Hai Shi46605972020-08-04 00:49:18 +0800192 filename = os_helper.TESTFN
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300193 bfilename = os.fsencode(filename)
Victor Stinnere3212742016-03-24 13:44:19 +0100194 self.assertIs(self.pathmodule.isfile(filename), False)
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300195 self.assertIs(self.pathmodule.isfile(bfilename), False)
Victor Stinnere3212742016-03-24 13:44:19 +0100196
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300197 self.assertIs(self.pathmodule.isfile(filename + '\udfff'), False)
198 self.assertIs(self.pathmodule.isfile(bfilename + b'\xff'), False)
199 self.assertIs(self.pathmodule.isfile(filename + '\x00'), False)
200 self.assertIs(self.pathmodule.isfile(bfilename + b'\x00'), False)
201
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300202 try:
203 create_file(filename)
204 self.assertIs(self.pathmodule.isfile(filename), True)
205 self.assertIs(self.pathmodule.isfile(bfilename), True)
206 finally:
Hai Shi46605972020-08-04 00:49:18 +0800207 os_helper.unlink(filename)
Victor Stinnere3212742016-03-24 13:44:19 +0100208
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300209 try:
210 os.mkdir(filename)
211 self.assertIs(self.pathmodule.isfile(filename), False)
212 self.assertIs(self.pathmodule.isfile(bfilename), False)
213 finally:
Hai Shi46605972020-08-04 00:49:18 +0800214 os_helper.rmdir(filename)
Brian Curtin490b32a2012-12-26 07:03:03 -0600215
216 def test_samefile(self):
Hai Shi46605972020-08-04 00:49:18 +0800217 file1 = os_helper.TESTFN
218 file2 = os_helper.TESTFN + "2"
219 self.addCleanup(os_helper.unlink, file1)
220 self.addCleanup(os_helper.unlink, file2)
Victor Stinnere3212742016-03-24 13:44:19 +0100221
222 create_file(file1)
223 self.assertTrue(self.pathmodule.samefile(file1, file1))
224
225 create_file(file2)
226 self.assertFalse(self.pathmodule.samefile(file1, file2))
227
228 self.assertRaises(TypeError, self.pathmodule.samefile)
229
230 def _test_samefile_on_link_func(self, func):
Hai Shi46605972020-08-04 00:49:18 +0800231 test_fn1 = os_helper.TESTFN
232 test_fn2 = os_helper.TESTFN + "2"
233 self.addCleanup(os_helper.unlink, test_fn1)
234 self.addCleanup(os_helper.unlink, test_fn2)
Victor Stinnere3212742016-03-24 13:44:19 +0100235
236 create_file(test_fn1)
237
238 func(test_fn1, test_fn2)
239 self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2))
240 os.remove(test_fn2)
241
242 create_file(test_fn2)
243 self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2))
Brian Curtin490b32a2012-12-26 07:03:03 -0600244
Hai Shi46605972020-08-04 00:49:18 +0800245 @os_helper.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600246 def test_samefile_on_symlink(self):
247 self._test_samefile_on_link_func(os.symlink)
248
249 def test_samefile_on_link(self):
xdegaye92c2ca72017-11-12 17:31:07 +0100250 try:
251 self._test_samefile_on_link_func(os.link)
252 except PermissionError as e:
253 self.skipTest('os.link(): %s' % e)
Brian Curtine701ec52012-12-26 07:36:16 -0600254
Brian Curtin490b32a2012-12-26 07:03:03 -0600255 def test_samestat(self):
Hai Shi46605972020-08-04 00:49:18 +0800256 test_fn1 = os_helper.TESTFN
257 test_fn2 = os_helper.TESTFN + "2"
258 self.addCleanup(os_helper.unlink, test_fn1)
259 self.addCleanup(os_helper.unlink, test_fn2)
Victor Stinnere3212742016-03-24 13:44:19 +0100260
261 create_file(test_fn1)
262 stat1 = os.stat(test_fn1)
263 self.assertTrue(self.pathmodule.samestat(stat1, os.stat(test_fn1)))
264
265 create_file(test_fn2)
266 stat2 = os.stat(test_fn2)
267 self.assertFalse(self.pathmodule.samestat(stat1, stat2))
268
269 self.assertRaises(TypeError, self.pathmodule.samestat)
270
271 def _test_samestat_on_link_func(self, func):
Hai Shi46605972020-08-04 00:49:18 +0800272 test_fn1 = os_helper.TESTFN + "1"
273 test_fn2 = os_helper.TESTFN + "2"
274 self.addCleanup(os_helper.unlink, test_fn1)
275 self.addCleanup(os_helper.unlink, test_fn2)
Victor Stinnere3212742016-03-24 13:44:19 +0100276
277 create_file(test_fn1)
278 func(test_fn1, test_fn2)
279 self.assertTrue(self.pathmodule.samestat(os.stat(test_fn1),
280 os.stat(test_fn2)))
281 os.remove(test_fn2)
282
283 create_file(test_fn2)
284 self.assertFalse(self.pathmodule.samestat(os.stat(test_fn1),
285 os.stat(test_fn2)))
Brian Curtin490b32a2012-12-26 07:03:03 -0600286
Hai Shi46605972020-08-04 00:49:18 +0800287 @os_helper.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600288 def test_samestat_on_symlink(self):
289 self._test_samestat_on_link_func(os.symlink)
290
291 def test_samestat_on_link(self):
xdegaye92c2ca72017-11-12 17:31:07 +0100292 try:
293 self._test_samestat_on_link_func(os.link)
294 except PermissionError as e:
295 self.skipTest('os.link(): %s' % e)
Brian Curtine701ec52012-12-26 07:36:16 -0600296
Brian Curtin490b32a2012-12-26 07:03:03 -0600297 def test_sameopenfile(self):
Hai Shi46605972020-08-04 00:49:18 +0800298 filename = os_helper.TESTFN
299 self.addCleanup(os_helper.unlink, filename)
Victor Stinnere3212742016-03-24 13:44:19 +0100300 create_file(filename)
301
302 with open(filename, "rb", 0) as fp1:
303 fd1 = fp1.fileno()
304 with open(filename, "rb", 0) as fp2:
305 fd2 = fp2.fileno()
306 self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2))
307
Brian Curtin490b32a2012-12-26 07:03:03 -0600308
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200309class TestGenericTest(GenericTest, unittest.TestCase):
310 # Issue 16852: GenericTest can't inherit from unittest.TestCase
311 # for test discovery purposes; CommonTest inherits from GenericTest
312 # and is only meant to be inherited by others.
313 pathmodule = genericpath
Thomas Wouters89f507f2006-12-13 04:49:30 +0000314
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300315 def test_invalid_paths(self):
Berker Peksag9adc1a32016-07-23 07:31:47 +0300316 for attr in GenericTest.common_attributes:
317 # os.path.commonprefix doesn't raise ValueError
318 if attr == 'commonprefix':
319 continue
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300320 func = getattr(self.pathmodule, attr)
Berker Peksag9adc1a32016-07-23 07:31:47 +0300321 with self.subTest(attr=attr):
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300322 if attr in ('exists', 'isdir', 'isfile'):
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300323 func('/tmp\udfffabcds')
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300324 func(b'/tmp\xffabcds')
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300325 func('/tmp\x00abcds')
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300326 func(b'/tmp\x00abcds')
Serhiy Storchaka0185f342018-09-18 11:28:51 +0300327 else:
328 with self.assertRaises((OSError, UnicodeEncodeError)):
329 func('/tmp\udfffabcds')
330 with self.assertRaises((OSError, UnicodeDecodeError)):
331 func(b'/tmp\xffabcds')
332 with self.assertRaisesRegex(ValueError, 'embedded null'):
333 func('/tmp\x00abcds')
334 with self.assertRaisesRegex(ValueError, 'embedded null'):
335 func(b'/tmp\x00abcds')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000336
Florent Xicluna87082ee2010-08-09 17:18:05 +0000337# Following TestCase is not supposed to be run from test_genericpath.
Victor Stinnerd7538dd2018-12-14 13:37:26 +0100338# It is inherited by other test modules (ntpath, posixpath).
Florent Xicluna87082ee2010-08-09 17:18:05 +0000339
Florent Xiclunac9c79782010-03-08 12:24:53 +0000340class CommonTest(GenericTest):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000341 common_attributes = GenericTest.common_attributes + [
342 # Properties
343 'curdir', 'pardir', 'extsep', 'sep',
344 'pathsep', 'defpath', 'altsep', 'devnull',
345 # Methods
346 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
347 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
348 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
349 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000350
Florent Xiclunac9c79782010-03-08 12:24:53 +0000351 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000352 normcase = self.pathmodule.normcase
353 # check that normcase() is idempotent
354 for p in ["FoO/./BaR", b"FoO/./BaR"]:
355 p = normcase(p)
356 self.assertEqual(p, normcase(p))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000357
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000358 self.assertEqual(normcase(''), '')
359 self.assertEqual(normcase(b''), b'')
360
361 # check that normcase raises a TypeError for invalid types
362 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
363 self.assertRaises(TypeError, normcase, path)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000364
365 def test_splitdrive(self):
366 # splitdrive for non-NT paths
367 splitdrive = self.pathmodule.splitdrive
368 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
369 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
370 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
371
372 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
373 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
374 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
375
376 def test_expandvars(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000377 expandvars = self.pathmodule.expandvars
Hai Shi46605972020-08-04 00:49:18 +0800378 with os_helper.EnvironmentVarGuard() as env:
Florent Xiclunac9c79782010-03-08 12:24:53 +0000379 env.clear()
380 env["foo"] = "bar"
381 env["{foo"] = "baz1"
382 env["{foo}"] = "baz2"
383 self.assertEqual(expandvars("foo"), "foo")
384 self.assertEqual(expandvars("$foo bar"), "bar bar")
385 self.assertEqual(expandvars("${foo}bar"), "barbar")
386 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
387 self.assertEqual(expandvars("$bar bar"), "$bar bar")
388 self.assertEqual(expandvars("$?bar"), "$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000389 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
390 self.assertEqual(expandvars("${foo"), "${foo")
391 self.assertEqual(expandvars("${{foo}}"), "baz1}")
392 self.assertEqual(expandvars("$foo$foo"), "barbar")
393 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
394
395 self.assertEqual(expandvars(b"foo"), b"foo")
396 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
397 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
398 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
399 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
400 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
Florent Xiclunac9c79782010-03-08 12:24:53 +0000401 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
402 self.assertEqual(expandvars(b"${foo"), b"${foo")
403 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
404 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
405 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
406
Hai Shi46605972020-08-04 00:49:18 +0800407 @unittest.skipUnless(os_helper.FS_NONASCII, 'need os_helper.FS_NONASCII')
Serhiy Storchakadbb10192014-02-13 10:13:53 +0200408 def test_expandvars_nonascii(self):
Serhiy Storchakadbb10192014-02-13 10:13:53 +0200409 expandvars = self.pathmodule.expandvars
410 def check(value, expected):
411 self.assertEqual(expandvars(value), expected)
Hai Shi46605972020-08-04 00:49:18 +0800412 with os_helper.EnvironmentVarGuard() as env:
Serhiy Storchakadbb10192014-02-13 10:13:53 +0200413 env.clear()
Hai Shi46605972020-08-04 00:49:18 +0800414 nonascii = os_helper.FS_NONASCII
Serhiy Storchakadbb10192014-02-13 10:13:53 +0200415 env['spam'] = nonascii
416 env[nonascii] = 'ham' + nonascii
417 check(nonascii, nonascii)
418 check('$spam bar', '%s bar' % nonascii)
419 check('${spam}bar', '%sbar' % nonascii)
420 check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
421 check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii)
422 check('$spam}bar', '%s}bar' % nonascii)
423
424 check(os.fsencode(nonascii), os.fsencode(nonascii))
425 check(b'$spam bar', os.fsencode('%s bar' % nonascii))
426 check(b'${spam}bar', os.fsencode('%sbar' % nonascii))
427 check(os.fsencode('${%s}bar' % nonascii),
428 os.fsencode('ham%sbar' % nonascii))
429 check(os.fsencode('$bar%s bar' % nonascii),
430 os.fsencode('$bar%s bar' % nonascii))
431 check(b'$spam}bar', os.fsencode('%s}bar' % nonascii))
432
Florent Xiclunac9c79782010-03-08 12:24:53 +0000433 def test_abspath(self):
434 self.assertIn("foo", self.pathmodule.abspath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100435 with warnings.catch_warnings():
436 warnings.simplefilter("ignore", DeprecationWarning)
437 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000438
Steve Dowere58571b2016-09-08 11:11:13 -0700439 # avoid UnicodeDecodeError on Windows
440 undecodable_path = b'' if sys.platform == 'win32' else b'f\xf2\xf2'
441
Florent Xiclunac9c79782010-03-08 12:24:53 +0000442 # Abspath returns bytes when the arg is bytes
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100443 with warnings.catch_warnings():
444 warnings.simplefilter("ignore", DeprecationWarning)
Steve Dowere58571b2016-09-08 11:11:13 -0700445 for path in (b'', b'foo', undecodable_path, b'/foo', b'C:\\'):
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100446 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000447
448 def test_realpath(self):
449 self.assertIn("foo", self.pathmodule.realpath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100450 with warnings.catch_warnings():
451 warnings.simplefilter("ignore", DeprecationWarning)
452 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000453
454 def test_normpath_issue5827(self):
455 # Make sure normpath preserves unicode
456 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
457 self.assertIsInstance(self.pathmodule.normpath(path), str)
458
459 def test_abspath_issue3426(self):
460 # Check that abspath returns unicode when the arg is unicode
461 # with both ASCII and non-ASCII cwds.
462 abspath = self.pathmodule.abspath
463 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
464 self.assertIsInstance(abspath(path), str)
465
466 unicwd = '\xe7w\xf0'
467 try:
Victor Stinner7c7ea622012-08-01 20:03:49 +0200468 os.fsencode(unicwd)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000469 except (AttributeError, UnicodeEncodeError):
470 # FS encoding is probably ASCII
471 pass
472 else:
Hai Shi46605972020-08-04 00:49:18 +0800473 with os_helper.temp_cwd(unicwd):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000474 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
475 self.assertIsInstance(abspath(path), str)
476
Victor Stinner1efde672010-04-18 08:23:42 +0000477 def test_nonascii_abspath(self):
Hai Shi46605972020-08-04 00:49:18 +0800478 if (os_helper.TESTFN_UNDECODABLE
Victor Stinnerff3d5152012-11-10 12:07:39 +0100479 # Mac OS X denies the creation of a directory with an invalid
Martin Panterc04fb562016-02-10 05:44:01 +0000480 # UTF-8 name. Windows allows creating a directory with an
Victor Stinnerff3d5152012-11-10 12:07:39 +0100481 # arbitrary bytes name, but fails to enter this directory
482 # (when the bytes name is used).
483 and sys.platform not in ('win32', 'darwin')):
Hai Shi46605972020-08-04 00:49:18 +0800484 name = os_helper.TESTFN_UNDECODABLE
485 elif os_helper.TESTFN_NONASCII:
486 name = os_helper.TESTFN_NONASCII
Victor Stinnerfce2a6e2012-10-31 23:01:30 +0100487 else:
Hai Shi46605972020-08-04 00:49:18 +0800488 self.skipTest("need os_helper.TESTFN_NONASCII")
Victor Stinner7c7ea622012-08-01 20:03:49 +0200489
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100490 with warnings.catch_warnings():
491 warnings.simplefilter("ignore", DeprecationWarning)
Hai Shi46605972020-08-04 00:49:18 +0800492 with os_helper.temp_cwd(name):
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100493 self.test_abspath()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000494
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300495 def test_join_errors(self):
496 # Check join() raises friendly TypeErrors.
Hai Shi46605972020-08-04 00:49:18 +0800497 with warnings_helper.check_warnings(('', BytesWarning), quiet=True):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300498 errmsg = "Can't mix strings and bytes in path components"
499 with self.assertRaisesRegex(TypeError, errmsg):
500 self.pathmodule.join(b'bytes', 'str')
501 with self.assertRaisesRegex(TypeError, errmsg):
502 self.pathmodule.join('str', b'bytes')
503 # regression, see #15377
Brett Cannon3f9183b2016-08-26 14:44:48 -0700504 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300505 self.pathmodule.join(42, 'str')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700506 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300507 self.pathmodule.join('str', 42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700508 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300509 self.pathmodule.join(42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700510 with self.assertRaisesRegex(TypeError, 'list'):
Serhiy Storchaka5bfc03f2015-05-19 11:00:07 +0300511 self.pathmodule.join([])
Brett Cannon3f9183b2016-08-26 14:44:48 -0700512 with self.assertRaisesRegex(TypeError, 'bytearray'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300513 self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar'))
514
515 def test_relpath_errors(self):
516 # Check relpath() raises friendly TypeErrors.
Hai Shi46605972020-08-04 00:49:18 +0800517 with warnings_helper.check_warnings(
518 ('', (BytesWarning, DeprecationWarning)), quiet=True):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300519 errmsg = "Can't mix strings and bytes in path components"
520 with self.assertRaisesRegex(TypeError, errmsg):
521 self.pathmodule.relpath(b'bytes', 'str')
522 with self.assertRaisesRegex(TypeError, errmsg):
523 self.pathmodule.relpath('str', b'bytes')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700524 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300525 self.pathmodule.relpath(42, 'str')
Brett Cannon3f9183b2016-08-26 14:44:48 -0700526 with self.assertRaisesRegex(TypeError, 'int'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300527 self.pathmodule.relpath('str', 42)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700528 with self.assertRaisesRegex(TypeError, 'bytearray'):
Serhiy Storchaka3deeeb02014-10-04 14:58:43 +0300529 self.pathmodule.relpath(bytearray(b'foo'), bytearray(b'bar'))
530
Serhiy Storchaka34601982018-01-07 17:54:31 +0200531 def test_import(self):
532 assert_python_ok('-S', '-c', 'import ' + self.pathmodule.__name__)
533
Thomas Wouters89f507f2006-12-13 04:49:30 +0000534
Brett Cannon3f9183b2016-08-26 14:44:48 -0700535class PathLikeTests(unittest.TestCase):
536
Brett Cannon3f9183b2016-08-26 14:44:48 -0700537 def setUp(self):
Hai Shi46605972020-08-04 00:49:18 +0800538 self.file_name = os_helper.TESTFN
539 self.file_path = FakePath(os_helper.TESTFN)
540 self.addCleanup(os_helper.unlink, self.file_name)
Brett Cannon3f9183b2016-08-26 14:44:48 -0700541 create_file(self.file_name, b"test_genericpath.PathLikeTests")
542
543 def assertPathEqual(self, func):
544 self.assertEqual(func(self.file_path), func(self.file_name))
545
546 def test_path_exists(self):
547 self.assertPathEqual(os.path.exists)
548
549 def test_path_isfile(self):
550 self.assertPathEqual(os.path.isfile)
551
552 def test_path_isdir(self):
553 self.assertPathEqual(os.path.isdir)
554
555 def test_path_commonprefix(self):
556 self.assertEqual(os.path.commonprefix([self.file_path, self.file_name]),
557 self.file_name)
558
559 def test_path_getsize(self):
560 self.assertPathEqual(os.path.getsize)
561
562 def test_path_getmtime(self):
563 self.assertPathEqual(os.path.getatime)
564
565 def test_path_getctime(self):
566 self.assertPathEqual(os.path.getctime)
567
568 def test_path_samefile(self):
569 self.assertTrue(os.path.samefile(self.file_path, self.file_name))
570
571
Serhiy Storchaka17a00882018-06-16 13:25:55 +0300572if __name__ == "__main__":
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200573 unittest.main()