blob: 2955f494693c1d8f3cf8f8c1e0c9dcf17fb544f9 [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 unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Thomas Wouters89f507f2006-12-13 04:49:30 +00007import os
8import genericpath
Victor Stinner1efde672010-04-18 08:23:42 +00009import sys
Thomas Wouters89f507f2006-12-13 04:49:30 +000010
Florent Xiclunac9c79782010-03-08 12:24:53 +000011
12def safe_rmdir(dirname):
13 try:
14 os.rmdir(dirname)
15 except OSError:
16 pass
17
18
19class GenericTest(unittest.TestCase):
20 # The path module to be tested
21 pathmodule = genericpath
22 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):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000101 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000102 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000103 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000104 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000105 self.assertEqual(self.pathmodule.getsize(support.TESTFN), 3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000106 finally:
107 if not f.closed:
108 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000109 support.unlink(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110
111 def test_time(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000112 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000113 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000114 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000116 f = open(support.TESTFN, "ab")
Guido van Rossum199fc752007-08-27 23:38:12 +0000117 f.write(b"bar")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000119 f = open(support.TESTFN, "rb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000120 d = f.read()
121 f.close()
Guido van Rossumdc122882007-07-10 12:09:13 +0000122 self.assertEqual(d, b"foobar")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000123
Ezio Melottia11865b2010-02-20 09:47:55 +0000124 self.assertLessEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +0000125 self.pathmodule.getctime(support.TESTFN),
126 self.pathmodule.getmtime(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000127 )
128 finally:
129 if not f.closed:
130 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000131 support.unlink(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000132
133 def test_exists(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000134 self.assertIs(self.pathmodule.exists(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000135 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000136 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000137 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000138 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000139 self.assertIs(self.pathmodule.exists(support.TESTFN), True)
140 if not self.pathmodule == genericpath:
141 self.assertIs(self.pathmodule.lexists(support.TESTFN),
142 True)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000143 finally:
144 if not f.close():
145 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000146 support.unlink(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000147
148 def test_isdir(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000149 self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000150 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000151 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000152 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000153 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000154 self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000155 os.remove(support.TESTFN)
156 os.mkdir(support.TESTFN)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000157 self.assertIs(self.pathmodule.isdir(support.TESTFN), True)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000158 os.rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000159 finally:
160 if not f.close():
161 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000162 support.unlink(support.TESTFN)
163 safe_rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000164
165 def test_isfile(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000166 self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000167 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000168 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000169 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000170 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000171 self.assertIs(self.pathmodule.isfile(support.TESTFN), True)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000172 os.remove(support.TESTFN)
173 os.mkdir(support.TESTFN)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000174 self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000175 os.rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000176 finally:
177 if not f.close():
178 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000179 support.unlink(support.TESTFN)
180 safe_rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000181
Thomas Wouters89f507f2006-12-13 04:49:30 +0000182
Florent Xiclunac9c79782010-03-08 12:24:53 +0000183class CommonTest(GenericTest):
184 # The path module to be tested
185 pathmodule = None
186 common_attributes = GenericTest.common_attributes + [
187 # Properties
188 'curdir', 'pardir', 'extsep', 'sep',
189 'pathsep', 'defpath', 'altsep', 'devnull',
190 # Methods
191 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
192 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
193 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
194 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000195
Florent Xiclunac9c79782010-03-08 12:24:53 +0000196 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000197 normcase = self.pathmodule.normcase
198 # check that normcase() is idempotent
199 for p in ["FoO/./BaR", b"FoO/./BaR"]:
200 p = normcase(p)
201 self.assertEqual(p, normcase(p))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000202
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000203 self.assertEqual(normcase(''), '')
204 self.assertEqual(normcase(b''), b'')
205
206 # check that normcase raises a TypeError for invalid types
207 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
208 self.assertRaises(TypeError, normcase, path)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000209
210 def test_splitdrive(self):
211 # splitdrive for non-NT paths
212 splitdrive = self.pathmodule.splitdrive
213 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
214 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
215 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
216
217 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
218 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
219 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
220
221 def test_expandvars(self):
222 if self.pathmodule.__name__ == 'macpath':
223 self.skipTest('macpath.expandvars is a stub')
224 expandvars = self.pathmodule.expandvars
225 with support.EnvironmentVarGuard() as env:
226 env.clear()
227 env["foo"] = "bar"
228 env["{foo"] = "baz1"
229 env["{foo}"] = "baz2"
230 self.assertEqual(expandvars("foo"), "foo")
231 self.assertEqual(expandvars("$foo bar"), "bar bar")
232 self.assertEqual(expandvars("${foo}bar"), "barbar")
233 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
234 self.assertEqual(expandvars("$bar bar"), "$bar bar")
235 self.assertEqual(expandvars("$?bar"), "$?bar")
236 self.assertEqual(expandvars("${foo}bar"), "barbar")
237 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
238 self.assertEqual(expandvars("${foo"), "${foo")
239 self.assertEqual(expandvars("${{foo}}"), "baz1}")
240 self.assertEqual(expandvars("$foo$foo"), "barbar")
241 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
242
243 self.assertEqual(expandvars(b"foo"), b"foo")
244 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
245 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
246 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
247 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
248 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
249 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
250 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
251 self.assertEqual(expandvars(b"${foo"), b"${foo")
252 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
253 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
254 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
255
256 def test_abspath(self):
257 self.assertIn("foo", self.pathmodule.abspath("foo"))
258 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
259
260 # Abspath returns bytes when the arg is bytes
261 for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'):
262 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
263
264 def test_realpath(self):
265 self.assertIn("foo", self.pathmodule.realpath("foo"))
266 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
267
268 def test_normpath_issue5827(self):
269 # Make sure normpath preserves unicode
270 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
271 self.assertIsInstance(self.pathmodule.normpath(path), str)
272
273 def test_abspath_issue3426(self):
274 # Check that abspath returns unicode when the arg is unicode
275 # with both ASCII and non-ASCII cwds.
276 abspath = self.pathmodule.abspath
277 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
278 self.assertIsInstance(abspath(path), str)
279
280 unicwd = '\xe7w\xf0'
281 try:
282 fsencoding = support.TESTFN_ENCODING or "ascii"
283 unicwd.encode(fsencoding)
284 except (AttributeError, UnicodeEncodeError):
285 # FS encoding is probably ASCII
286 pass
287 else:
288 with support.temp_cwd(unicwd):
289 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
290 self.assertIsInstance(abspath(path), str)
291
Victor Stinner1efde672010-04-18 08:23:42 +0000292 @unittest.skipIf(sys.platform == 'darwin',
R. David Murrayb6f916d2010-04-22 01:49:37 +0000293 "Mac OS X denies the creation of a directory with an invalid utf8 name")
Victor Stinner1efde672010-04-18 08:23:42 +0000294 def test_nonascii_abspath(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000295 # Test non-ASCII, non-UTF8 bytes in the path.
Florent Xicluna81c867c2010-03-08 12:47:44 +0000296 with support.temp_cwd(b'\xe7w\xf0'):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000297 self.test_abspath()
298
Thomas Wouters89f507f2006-12-13 04:49:30 +0000299
300def test_main():
Florent Xiclunac9c79782010-03-08 12:24:53 +0000301 support.run_unittest(GenericTest)
302
Thomas Wouters89f507f2006-12-13 04:49:30 +0000303
304if __name__=="__main__":
305 test_main()