blob: 50638a1cf0f14cf84e469145766fd7d0fe92aa64 [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 Xicluna87082ee2010-08-09 17:18:05 +0000183# Following TestCase is not supposed to be run from test_genericpath.
184# It is inherited by other test modules (macpath, ntpath, posixpath).
185
Florent Xiclunac9c79782010-03-08 12:24:53 +0000186class CommonTest(GenericTest):
187 # The path module to be tested
188 pathmodule = None
189 common_attributes = GenericTest.common_attributes + [
190 # Properties
191 'curdir', 'pardir', 'extsep', 'sep',
192 'pathsep', 'defpath', 'altsep', 'devnull',
193 # Methods
194 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
195 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
196 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
197 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000198
Florent Xiclunac9c79782010-03-08 12:24:53 +0000199 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000200 normcase = self.pathmodule.normcase
201 # check that normcase() is idempotent
202 for p in ["FoO/./BaR", b"FoO/./BaR"]:
203 p = normcase(p)
204 self.assertEqual(p, normcase(p))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000205
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000206 self.assertEqual(normcase(''), '')
207 self.assertEqual(normcase(b''), b'')
208
209 # check that normcase raises a TypeError for invalid types
210 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
211 self.assertRaises(TypeError, normcase, path)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000212
213 def test_splitdrive(self):
214 # splitdrive for non-NT paths
215 splitdrive = self.pathmodule.splitdrive
216 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
217 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
218 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
219
220 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
221 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
222 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
223
224 def test_expandvars(self):
225 if self.pathmodule.__name__ == 'macpath':
226 self.skipTest('macpath.expandvars is a stub')
227 expandvars = self.pathmodule.expandvars
228 with support.EnvironmentVarGuard() as env:
229 env.clear()
230 env["foo"] = "bar"
231 env["{foo"] = "baz1"
232 env["{foo}"] = "baz2"
233 self.assertEqual(expandvars("foo"), "foo")
234 self.assertEqual(expandvars("$foo bar"), "bar bar")
235 self.assertEqual(expandvars("${foo}bar"), "barbar")
236 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
237 self.assertEqual(expandvars("$bar bar"), "$bar bar")
238 self.assertEqual(expandvars("$?bar"), "$?bar")
239 self.assertEqual(expandvars("${foo}bar"), "barbar")
240 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
241 self.assertEqual(expandvars("${foo"), "${foo")
242 self.assertEqual(expandvars("${{foo}}"), "baz1}")
243 self.assertEqual(expandvars("$foo$foo"), "barbar")
244 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
245
246 self.assertEqual(expandvars(b"foo"), b"foo")
247 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
248 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
249 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
250 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
251 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
252 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
253 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
254 self.assertEqual(expandvars(b"${foo"), b"${foo")
255 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
256 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
257 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
258
259 def test_abspath(self):
260 self.assertIn("foo", self.pathmodule.abspath("foo"))
261 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
262
263 # Abspath returns bytes when the arg is bytes
264 for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'):
265 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
266
267 def test_realpath(self):
268 self.assertIn("foo", self.pathmodule.realpath("foo"))
269 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
270
271 def test_normpath_issue5827(self):
272 # Make sure normpath preserves unicode
273 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
274 self.assertIsInstance(self.pathmodule.normpath(path), str)
275
276 def test_abspath_issue3426(self):
277 # Check that abspath returns unicode when the arg is unicode
278 # with both ASCII and non-ASCII cwds.
279 abspath = self.pathmodule.abspath
280 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
281 self.assertIsInstance(abspath(path), str)
282
283 unicwd = '\xe7w\xf0'
284 try:
285 fsencoding = support.TESTFN_ENCODING or "ascii"
286 unicwd.encode(fsencoding)
287 except (AttributeError, UnicodeEncodeError):
288 # FS encoding is probably ASCII
289 pass
290 else:
291 with support.temp_cwd(unicwd):
292 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
293 self.assertIsInstance(abspath(path), str)
294
Victor Stinner1efde672010-04-18 08:23:42 +0000295 @unittest.skipIf(sys.platform == 'darwin',
R. David Murrayb6f916d2010-04-22 01:49:37 +0000296 "Mac OS X denies the creation of a directory with an invalid utf8 name")
Victor Stinner1efde672010-04-18 08:23:42 +0000297 def test_nonascii_abspath(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000298 # Test non-ASCII, non-UTF8 bytes in the path.
Florent Xicluna81c867c2010-03-08 12:47:44 +0000299 with support.temp_cwd(b'\xe7w\xf0'):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000300 self.test_abspath()
301
Thomas Wouters89f507f2006-12-13 04:49:30 +0000302
303def test_main():
Florent Xiclunac9c79782010-03-08 12:24:53 +0000304 support.run_unittest(GenericTest)
305
Thomas Wouters89f507f2006-12-13 04:49:30 +0000306
307if __name__=="__main__":
308 test_main()