blob: fd8bc577ca85e836d85fd05e697a3c991f7c2a66 [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
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200191class TestGenericTest(GenericTest, unittest.TestCase):
192 # Issue 16852: GenericTest can't inherit from unittest.TestCase
193 # for test discovery purposes; CommonTest inherits from GenericTest
194 # and is only meant to be inherited by others.
195 pathmodule = genericpath
Thomas Wouters89f507f2006-12-13 04:49:30 +0000196
Florent Xicluna87082ee2010-08-09 17:18:05 +0000197# Following TestCase is not supposed to be run from test_genericpath.
198# It is inherited by other test modules (macpath, ntpath, posixpath).
199
Florent Xiclunac9c79782010-03-08 12:24:53 +0000200class CommonTest(GenericTest):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000201 common_attributes = GenericTest.common_attributes + [
202 # Properties
203 'curdir', 'pardir', 'extsep', 'sep',
204 'pathsep', 'defpath', 'altsep', 'devnull',
205 # Methods
206 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
207 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
208 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
209 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000210
Florent Xiclunac9c79782010-03-08 12:24:53 +0000211 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000212 normcase = self.pathmodule.normcase
213 # check that normcase() is idempotent
214 for p in ["FoO/./BaR", b"FoO/./BaR"]:
215 p = normcase(p)
216 self.assertEqual(p, normcase(p))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000217
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000218 self.assertEqual(normcase(''), '')
219 self.assertEqual(normcase(b''), b'')
220
221 # check that normcase raises a TypeError for invalid types
222 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
223 self.assertRaises(TypeError, normcase, path)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000224
225 def test_splitdrive(self):
226 # splitdrive for non-NT paths
227 splitdrive = self.pathmodule.splitdrive
228 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
229 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
230 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
231
232 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
233 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
234 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
235
236 def test_expandvars(self):
237 if self.pathmodule.__name__ == 'macpath':
238 self.skipTest('macpath.expandvars is a stub')
239 expandvars = self.pathmodule.expandvars
240 with support.EnvironmentVarGuard() as env:
241 env.clear()
242 env["foo"] = "bar"
243 env["{foo"] = "baz1"
244 env["{foo}"] = "baz2"
245 self.assertEqual(expandvars("foo"), "foo")
246 self.assertEqual(expandvars("$foo bar"), "bar bar")
247 self.assertEqual(expandvars("${foo}bar"), "barbar")
248 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
249 self.assertEqual(expandvars("$bar bar"), "$bar bar")
250 self.assertEqual(expandvars("$?bar"), "$?bar")
251 self.assertEqual(expandvars("${foo}bar"), "barbar")
252 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
253 self.assertEqual(expandvars("${foo"), "${foo")
254 self.assertEqual(expandvars("${{foo}}"), "baz1}")
255 self.assertEqual(expandvars("$foo$foo"), "barbar")
256 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
257
258 self.assertEqual(expandvars(b"foo"), b"foo")
259 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
260 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
261 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
262 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
263 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
264 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
265 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
266 self.assertEqual(expandvars(b"${foo"), b"${foo")
267 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
268 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
269 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
270
271 def test_abspath(self):
272 self.assertIn("foo", self.pathmodule.abspath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100273 with warnings.catch_warnings():
274 warnings.simplefilter("ignore", DeprecationWarning)
275 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000276
277 # Abspath returns bytes when the arg is bytes
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100278 with warnings.catch_warnings():
279 warnings.simplefilter("ignore", DeprecationWarning)
280 for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'):
281 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000282
283 def test_realpath(self):
284 self.assertIn("foo", self.pathmodule.realpath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100285 with warnings.catch_warnings():
286 warnings.simplefilter("ignore", DeprecationWarning)
287 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000288
289 def test_normpath_issue5827(self):
290 # Make sure normpath preserves unicode
291 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
292 self.assertIsInstance(self.pathmodule.normpath(path), str)
293
294 def test_abspath_issue3426(self):
295 # Check that abspath returns unicode when the arg is unicode
296 # with both ASCII and non-ASCII cwds.
297 abspath = self.pathmodule.abspath
298 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
299 self.assertIsInstance(abspath(path), str)
300
301 unicwd = '\xe7w\xf0'
302 try:
Victor Stinner7c7ea622012-08-01 20:03:49 +0200303 os.fsencode(unicwd)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000304 except (AttributeError, UnicodeEncodeError):
305 # FS encoding is probably ASCII
306 pass
307 else:
308 with support.temp_cwd(unicwd):
309 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
310 self.assertIsInstance(abspath(path), str)
311
Victor Stinner1efde672010-04-18 08:23:42 +0000312 def test_nonascii_abspath(self):
Victor Stinner0af71aa2013-01-03 01:50:30 +0100313 if (support.TESTFN_UNDECODABLE
314 # Mac OS X denies the creation of a directory with an invalid
315 # UTF-8 name. Windows allows to create a directory with an
316 # arbitrary bytes name, but fails to enter this directory
317 # (when the bytes name is used).
318 and sys.platform not in ('win32', 'darwin')):
319 name = support.TESTFN_UNDECODABLE
320 elif support.TESTFN_NONASCII:
321 name = support.TESTFN_NONASCII
322 else:
323 self.skipTest("need support.TESTFN_NONASCII")
Victor Stinner7c7ea622012-08-01 20:03:49 +0200324
Florent Xiclunac9c79782010-03-08 12:24:53 +0000325 # Test non-ASCII, non-UTF8 bytes in the path.
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100326 with warnings.catch_warnings():
327 warnings.simplefilter("ignore", DeprecationWarning)
Victor Stinner7c7ea622012-08-01 20:03:49 +0200328 with support.temp_cwd(name):
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100329 self.test_abspath()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000330
Thomas Wouters89f507f2006-12-13 04:49:30 +0000331
Thomas Wouters89f507f2006-12-13 04:49:30 +0000332if __name__=="__main__":
Ezio Melottid0dfe9a2013-01-10 03:12:50 +0200333 unittest.main()