blob: 4ffc1cdd578ed7d771b43d25e5f856ed11b3e7b6 [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
9
Florent Xiclunac9c79782010-03-08 12:24:53 +000010
11def safe_rmdir(dirname):
12 try:
13 os.rmdir(dirname)
14 except OSError:
15 pass
16
17
18class GenericTest(unittest.TestCase):
19 # The path module to be tested
20 pathmodule = genericpath
21 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
147 def test_isdir(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000148 self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000149 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000151 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000152 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000153 self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000154 os.remove(support.TESTFN)
155 os.mkdir(support.TESTFN)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000156 self.assertIs(self.pathmodule.isdir(support.TESTFN), True)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000157 os.rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000158 finally:
159 if not f.close():
160 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000161 support.unlink(support.TESTFN)
162 safe_rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000163
164 def test_isfile(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000165 self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000166 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000167 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000168 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000169 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000170 self.assertIs(self.pathmodule.isfile(support.TESTFN), True)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000171 os.remove(support.TESTFN)
172 os.mkdir(support.TESTFN)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000173 self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000174 os.rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000175 finally:
176 if not f.close():
177 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000178 support.unlink(support.TESTFN)
179 safe_rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000180
Thomas Wouters89f507f2006-12-13 04:49:30 +0000181
Florent Xiclunac9c79782010-03-08 12:24:53 +0000182class CommonTest(GenericTest):
183 # The path module to be tested
184 pathmodule = None
185 common_attributes = GenericTest.common_attributes + [
186 # Properties
187 'curdir', 'pardir', 'extsep', 'sep',
188 'pathsep', 'defpath', 'altsep', 'devnull',
189 # Methods
190 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
191 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
192 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
193 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000194
Florent Xiclunac9c79782010-03-08 12:24:53 +0000195 def test_normcase(self):
196 # Check that normcase() is idempotent
197 p = "FoO/./BaR"
198 p = self.pathmodule.normcase(p)
199 self.assertEqual(p, self.pathmodule.normcase(p))
200
201 p = b"FoO/./BaR"
202 p = self.pathmodule.normcase(p)
203 self.assertEqual(p, self.pathmodule.normcase(p))
204
205 def test_splitdrive(self):
206 # splitdrive for non-NT paths
207 splitdrive = self.pathmodule.splitdrive
208 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
209 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
210 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
211
212 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
213 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
214 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
215
216 def test_expandvars(self):
217 if self.pathmodule.__name__ == 'macpath':
218 self.skipTest('macpath.expandvars is a stub')
219 expandvars = self.pathmodule.expandvars
220 with support.EnvironmentVarGuard() as env:
221 env.clear()
222 env["foo"] = "bar"
223 env["{foo"] = "baz1"
224 env["{foo}"] = "baz2"
225 self.assertEqual(expandvars("foo"), "foo")
226 self.assertEqual(expandvars("$foo bar"), "bar bar")
227 self.assertEqual(expandvars("${foo}bar"), "barbar")
228 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
229 self.assertEqual(expandvars("$bar bar"), "$bar bar")
230 self.assertEqual(expandvars("$?bar"), "$?bar")
231 self.assertEqual(expandvars("${foo}bar"), "barbar")
232 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
233 self.assertEqual(expandvars("${foo"), "${foo")
234 self.assertEqual(expandvars("${{foo}}"), "baz1}")
235 self.assertEqual(expandvars("$foo$foo"), "barbar")
236 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
237
238 self.assertEqual(expandvars(b"foo"), b"foo")
239 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
240 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
241 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
242 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
243 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
244 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
245 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
246 self.assertEqual(expandvars(b"${foo"), b"${foo")
247 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
248 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
249 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
250
251 def test_abspath(self):
252 self.assertIn("foo", self.pathmodule.abspath("foo"))
253 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
254
255 # Abspath returns bytes when the arg is bytes
256 for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'):
257 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
258
259 def test_realpath(self):
260 self.assertIn("foo", self.pathmodule.realpath("foo"))
261 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
262
263 def test_normpath_issue5827(self):
264 # Make sure normpath preserves unicode
265 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
266 self.assertIsInstance(self.pathmodule.normpath(path), str)
267
268 def test_abspath_issue3426(self):
269 # Check that abspath returns unicode when the arg is unicode
270 # with both ASCII and non-ASCII cwds.
271 abspath = self.pathmodule.abspath
272 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
273 self.assertIsInstance(abspath(path), str)
274
275 unicwd = '\xe7w\xf0'
276 try:
277 fsencoding = support.TESTFN_ENCODING or "ascii"
278 unicwd.encode(fsencoding)
279 except (AttributeError, UnicodeEncodeError):
280 # FS encoding is probably ASCII
281 pass
282 else:
283 with support.temp_cwd(unicwd):
284 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
285 self.assertIsInstance(abspath(path), str)
286
287 # Test non-ASCII, non-UTF8 bytes in the path.
Florent Xicluna81c867c2010-03-08 12:47:44 +0000288 with support.temp_cwd(b'\xe7w\xf0'):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000289 self.test_abspath()
290
Thomas Wouters89f507f2006-12-13 04:49:30 +0000291
292def test_main():
Florent Xiclunac9c79782010-03-08 12:24:53 +0000293 support.run_unittest(GenericTest)
294
Thomas Wouters89f507f2006-12-13 04:49:30 +0000295
296if __name__=="__main__":
297 test_main()