blob: e967897d75774a091686234471c22e93d925172e [file] [log] [blame]
Florent Xiclunac9c79782010-03-08 12:24:53 +00001"""
2Tests common to genericpath, macpath, ntpath and posixpath
3"""
4
Thomas Wouters89f507f2006-12-13 04:49:30 +00005import genericpath
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01006import os
Victor Stinner1efde672010-04-18 08:23:42 +00007import sys
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01008import unittest
9import warnings
10from test import support
Thomas Wouters89f507f2006-12-13 04:49:30 +000011
Florent Xiclunac9c79782010-03-08 12:24:53 +000012
13def safe_rmdir(dirname):
14 try:
15 os.rmdir(dirname)
16 except OSError:
17 pass
18
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):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000100 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000101 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000102 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000103 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000104 self.assertEqual(self.pathmodule.getsize(support.TESTFN), 3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 finally:
106 if not f.closed:
107 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000108 support.unlink(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000109
110 def test_time(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000111 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000113 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000115 f = open(support.TESTFN, "ab")
Guido van Rossum199fc752007-08-27 23:38:12 +0000116 f.write(b"bar")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000117 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000118 f = open(support.TESTFN, "rb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119 d = f.read()
120 f.close()
Guido van Rossumdc122882007-07-10 12:09:13 +0000121 self.assertEqual(d, b"foobar")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000122
Ezio Melottia11865b2010-02-20 09:47:55 +0000123 self.assertLessEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +0000124 self.pathmodule.getctime(support.TESTFN),
125 self.pathmodule.getmtime(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000126 )
127 finally:
128 if not f.closed:
129 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000130 support.unlink(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000131
132 def test_exists(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000133 self.assertIs(self.pathmodule.exists(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000134 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000135 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000136 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000137 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000138 self.assertIs(self.pathmodule.exists(support.TESTFN), True)
139 if not self.pathmodule == genericpath:
140 self.assertIs(self.pathmodule.lexists(support.TESTFN),
141 True)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000142 finally:
143 if not f.close():
144 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000145 support.unlink(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000146
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100147 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
148 def test_exists_fd(self):
149 r, w = os.pipe()
150 try:
151 self.assertTrue(self.pathmodule.exists(r))
152 finally:
153 os.close(r)
154 os.close(w)
155 self.assertFalse(self.pathmodule.exists(r))
156
Thomas Wouters89f507f2006-12-13 04:49:30 +0000157 def test_isdir(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000158 self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000159 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000160 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000161 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000162 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000163 self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000164 os.remove(support.TESTFN)
165 os.mkdir(support.TESTFN)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000166 self.assertIs(self.pathmodule.isdir(support.TESTFN), True)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000167 os.rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000168 finally:
169 if not f.close():
170 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000171 support.unlink(support.TESTFN)
172 safe_rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000173
174 def test_isfile(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000175 self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000176 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000177 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000178 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000179 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000180 self.assertIs(self.pathmodule.isfile(support.TESTFN), True)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000181 os.remove(support.TESTFN)
182 os.mkdir(support.TESTFN)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000183 self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000184 os.rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000185 finally:
186 if not f.close():
187 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000188 support.unlink(support.TESTFN)
189 safe_rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190
Brian Curtin490b32a2012-12-26 07:03:03 -0600191 @staticmethod
192 def _create_file(filename):
193 with open(filename, 'wb') as f:
194 f.write(b'foo')
195
196 def test_samefile(self):
197 try:
198 test_fn = support.TESTFN + "1"
199 self._create_file(test_fn)
200 self.assertTrue(self.pathmodule.samefile(test_fn, test_fn))
201 self.assertRaises(TypeError, self.pathmodule.samefile)
202 finally:
203 os.remove(test_fn)
204
205 @support.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600206 def test_samefile_on_symlink(self):
207 self._test_samefile_on_link_func(os.symlink)
208
209 def test_samefile_on_link(self):
210 self._test_samefile_on_link_func(os.link)
211
212 def _test_samefile_on_link_func(self, func):
Brian Curtin490b32a2012-12-26 07:03:03 -0600213 try:
214 test_fn1 = support.TESTFN + "1"
215 test_fn2 = support.TESTFN + "2"
216 self._create_file(test_fn1)
217
Brian Curtine701ec52012-12-26 07:36:16 -0600218 func(test_fn1, test_fn2)
Brian Curtin490b32a2012-12-26 07:03:03 -0600219 self.assertTrue(self.pathmodule.samefile(test_fn1, test_fn2))
220 os.remove(test_fn2)
221
222 self._create_file(test_fn2)
223 self.assertFalse(self.pathmodule.samefile(test_fn1, test_fn2))
224 finally:
225 os.remove(test_fn1)
226 os.remove(test_fn2)
227
228 def test_samestat(self):
229 try:
230 test_fn = support.TESTFN + "1"
231 self._create_file(test_fn)
232 test_fns = [test_fn]*2
233 stats = map(os.stat, test_fns)
234 self.assertTrue(self.pathmodule.samestat(*stats))
235 finally:
236 os.remove(test_fn)
237
238 @support.skip_unless_symlink
Brian Curtine701ec52012-12-26 07:36:16 -0600239 def test_samestat_on_symlink(self):
240 self._test_samestat_on_link_func(os.symlink)
241
242 def test_samestat_on_link(self):
243 self._test_samestat_on_link_func(os.link)
244
245 def _test_samestat_on_link_func(self, func):
Brian Curtin490b32a2012-12-26 07:03:03 -0600246 try:
247 test_fn1 = support.TESTFN + "1"
248 test_fn2 = support.TESTFN + "2"
249 self._create_file(test_fn1)
250 test_fns = (test_fn1, test_fn2)
Brian Curtine701ec52012-12-26 07:36:16 -0600251 func(*test_fns)
Brian Curtin490b32a2012-12-26 07:03:03 -0600252 stats = map(os.stat, test_fns)
253 self.assertTrue(self.pathmodule.samestat(*stats))
254 os.remove(test_fn2)
255
256 self._create_file(test_fn2)
257 stats = map(os.stat, test_fns)
258 self.assertFalse(self.pathmodule.samestat(*stats))
259
260 self.assertRaises(TypeError, self.pathmodule.samestat)
261 finally:
262 os.remove(test_fn1)
263 os.remove(test_fn2)
264
265 def test_sameopenfile(self):
266 fname = support.TESTFN + "1"
267 with open(fname, "wb") as a, open(fname, "wb") as b:
268 self.assertTrue(self.pathmodule.sameopenfile(
269 a.fileno(), b.fileno()))
270
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200271class TestGenericTest(GenericTest, unittest.TestCase):
272 # Issue 16852: GenericTest can't inherit from unittest.TestCase
273 # for test discovery purposes; CommonTest inherits from GenericTest
274 # and is only meant to be inherited by others.
275 pathmodule = genericpath
Thomas Wouters89f507f2006-12-13 04:49:30 +0000276
Thomas Wouters89f507f2006-12-13 04:49:30 +0000277
Florent Xicluna87082ee2010-08-09 17:18:05 +0000278# Following TestCase is not supposed to be run from test_genericpath.
279# It is inherited by other test modules (macpath, ntpath, posixpath).
280
Florent Xiclunac9c79782010-03-08 12:24:53 +0000281class CommonTest(GenericTest):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000282 common_attributes = GenericTest.common_attributes + [
283 # Properties
284 'curdir', 'pardir', 'extsep', 'sep',
285 'pathsep', 'defpath', 'altsep', 'devnull',
286 # Methods
287 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
288 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
289 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
290 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000291
Florent Xiclunac9c79782010-03-08 12:24:53 +0000292 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000293 normcase = self.pathmodule.normcase
294 # check that normcase() is idempotent
295 for p in ["FoO/./BaR", b"FoO/./BaR"]:
296 p = normcase(p)
297 self.assertEqual(p, normcase(p))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000298
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000299 self.assertEqual(normcase(''), '')
300 self.assertEqual(normcase(b''), b'')
301
302 # check that normcase raises a TypeError for invalid types
303 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
304 self.assertRaises(TypeError, normcase, path)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000305
306 def test_splitdrive(self):
307 # splitdrive for non-NT paths
308 splitdrive = self.pathmodule.splitdrive
309 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
310 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
311 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
312
313 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
314 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
315 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
316
317 def test_expandvars(self):
318 if self.pathmodule.__name__ == 'macpath':
319 self.skipTest('macpath.expandvars is a stub')
320 expandvars = self.pathmodule.expandvars
321 with support.EnvironmentVarGuard() as env:
322 env.clear()
323 env["foo"] = "bar"
324 env["{foo"] = "baz1"
325 env["{foo}"] = "baz2"
326 self.assertEqual(expandvars("foo"), "foo")
327 self.assertEqual(expandvars("$foo bar"), "bar bar")
328 self.assertEqual(expandvars("${foo}bar"), "barbar")
329 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
330 self.assertEqual(expandvars("$bar bar"), "$bar bar")
331 self.assertEqual(expandvars("$?bar"), "$?bar")
332 self.assertEqual(expandvars("${foo}bar"), "barbar")
333 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
334 self.assertEqual(expandvars("${foo"), "${foo")
335 self.assertEqual(expandvars("${{foo}}"), "baz1}")
336 self.assertEqual(expandvars("$foo$foo"), "barbar")
337 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
338
339 self.assertEqual(expandvars(b"foo"), b"foo")
340 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
341 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
342 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
343 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
344 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
345 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
346 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
347 self.assertEqual(expandvars(b"${foo"), b"${foo")
348 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
349 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
350 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
351
352 def test_abspath(self):
353 self.assertIn("foo", self.pathmodule.abspath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100354 with warnings.catch_warnings():
355 warnings.simplefilter("ignore", DeprecationWarning)
356 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000357
358 # Abspath returns bytes when the arg is bytes
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100359 with warnings.catch_warnings():
360 warnings.simplefilter("ignore", DeprecationWarning)
361 for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'):
362 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000363
364 def test_realpath(self):
365 self.assertIn("foo", self.pathmodule.realpath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100366 with warnings.catch_warnings():
367 warnings.simplefilter("ignore", DeprecationWarning)
368 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000369
370 def test_normpath_issue5827(self):
371 # Make sure normpath preserves unicode
372 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
373 self.assertIsInstance(self.pathmodule.normpath(path), str)
374
375 def test_abspath_issue3426(self):
376 # Check that abspath returns unicode when the arg is unicode
377 # with both ASCII and non-ASCII cwds.
378 abspath = self.pathmodule.abspath
379 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
380 self.assertIsInstance(abspath(path), str)
381
382 unicwd = '\xe7w\xf0'
383 try:
Victor Stinner7c7ea622012-08-01 20:03:49 +0200384 os.fsencode(unicwd)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000385 except (AttributeError, UnicodeEncodeError):
386 # FS encoding is probably ASCII
387 pass
388 else:
389 with support.temp_cwd(unicwd):
390 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
391 self.assertIsInstance(abspath(path), str)
392
Victor Stinner1efde672010-04-18 08:23:42 +0000393 def test_nonascii_abspath(self):
Victor Stinnerff3d5152012-11-10 12:07:39 +0100394 if (support.TESTFN_UNDECODABLE
395 # Mac OS X denies the creation of a directory with an invalid
396 # UTF-8 name. Windows allows to create a directory with an
397 # arbitrary bytes name, but fails to enter this directory
398 # (when the bytes name is used).
399 and sys.platform not in ('win32', 'darwin')):
400 name = support.TESTFN_UNDECODABLE
401 elif support.TESTFN_NONASCII:
402 name = support.TESTFN_NONASCII
Victor Stinnerfce2a6e2012-10-31 23:01:30 +0100403 else:
Victor Stinnerff3d5152012-11-10 12:07:39 +0100404 self.skipTest("need support.TESTFN_NONASCII")
Victor Stinner7c7ea622012-08-01 20:03:49 +0200405
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100406 with warnings.catch_warnings():
407 warnings.simplefilter("ignore", DeprecationWarning)
Victor Stinner7c7ea622012-08-01 20:03:49 +0200408 with support.temp_cwd(name):
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100409 self.test_abspath()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000410
Thomas Wouters89f507f2006-12-13 04:49:30 +0000411
Thomas Wouters89f507f2006-12-13 04:49:30 +0000412if __name__=="__main__":
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200413 unittest.main()