blob: fc3d44c589e40c8bd28c0fbc77ce840301b03bff [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
20class GenericTest(unittest.TestCase):
21 # The path module to be tested
22 pathmodule = genericpath
23 common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime',
24 'getmtime', 'exists', 'isdir', 'isfile']
25 attributes = []
26
27 def test_no_argument(self):
28 for attr in self.common_attributes + self.attributes:
29 with self.assertRaises(TypeError):
30 getattr(self.pathmodule, attr)()
31 raise self.fail("{}.{}() did not raise a TypeError"
32 .format(self.pathmodule.__name__, attr))
Thomas Wouters89f507f2006-12-13 04:49:30 +000033
Thomas Wouters89f507f2006-12-13 04:49:30 +000034 def test_commonprefix(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +000035 commonprefix = self.pathmodule.commonprefix
Thomas Wouters89f507f2006-12-13 04:49:30 +000036 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000037 commonprefix([]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000038 ""
39 )
40 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000041 commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000042 "/home/swen"
43 )
44 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000045 commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000046 "/home/swen/"
47 )
48 self.assertEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +000049 commonprefix(["/home/swen/spam", "/home/swen/spam"]),
Thomas Wouters89f507f2006-12-13 04:49:30 +000050 "/home/swen/spam"
51 )
Florent Xiclunae3ed2e02010-03-08 12:42:20 +000052 self.assertEqual(
53 commonprefix(["home:swenson:spam", "home:swen:spam"]),
54 "home:swen"
55 )
56 self.assertEqual(
57 commonprefix([":home:swen:spam", ":home:swen:eggs"]),
58 ":home:swen:"
59 )
60 self.assertEqual(
61 commonprefix([":home:swen:spam", ":home:swen:spam"]),
62 ":home:swen:spam"
63 )
Thomas Wouters89f507f2006-12-13 04:49:30 +000064
Florent Xiclunac9c79782010-03-08 12:24:53 +000065 self.assertEqual(
66 commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]),
67 b"/home/swen"
68 )
69 self.assertEqual(
70 commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]),
71 b"/home/swen/"
72 )
73 self.assertEqual(
74 commonprefix([b"/home/swen/spam", b"/home/swen/spam"]),
75 b"/home/swen/spam"
76 )
Florent Xiclunae3ed2e02010-03-08 12:42:20 +000077 self.assertEqual(
78 commonprefix([b"home:swenson:spam", b"home:swen:spam"]),
79 b"home:swen"
80 )
81 self.assertEqual(
82 commonprefix([b":home:swen:spam", b":home:swen:eggs"]),
83 b":home:swen:"
84 )
85 self.assertEqual(
86 commonprefix([b":home:swen:spam", b":home:swen:spam"]),
87 b":home:swen:spam"
88 )
Florent Xiclunac9c79782010-03-08 12:24:53 +000089
90 testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
91 'aXc', 'abd', 'ab', 'aX', 'abcX']
92 for s1 in testlist:
93 for s2 in testlist:
94 p = commonprefix([s1, s2])
95 self.assertTrue(s1.startswith(p))
96 self.assertTrue(s2.startswith(p))
97 if s1 != s2:
98 n = len(p)
99 self.assertNotEqual(s1[n:n+1], s2[n:n+1])
100
Thomas Wouters89f507f2006-12-13 04:49:30 +0000101 def test_getsize(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000102 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000103 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000104 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000106 self.assertEqual(self.pathmodule.getsize(support.TESTFN), 3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000107 finally:
108 if not f.closed:
109 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000110 support.unlink(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000111
112 def test_time(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000113 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000115 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000116 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000117 f = open(support.TESTFN, "ab")
Guido van Rossum199fc752007-08-27 23:38:12 +0000118 f.write(b"bar")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000120 f = open(support.TESTFN, "rb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000121 d = f.read()
122 f.close()
Guido van Rossumdc122882007-07-10 12:09:13 +0000123 self.assertEqual(d, b"foobar")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124
Ezio Melottia11865b2010-02-20 09:47:55 +0000125 self.assertLessEqual(
Florent Xiclunac9c79782010-03-08 12:24:53 +0000126 self.pathmodule.getctime(support.TESTFN),
127 self.pathmodule.getmtime(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000128 )
129 finally:
130 if not f.closed:
131 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000132 support.unlink(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000133
134 def test_exists(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000135 self.assertIs(self.pathmodule.exists(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000136 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000137 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000138 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000139 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000140 self.assertIs(self.pathmodule.exists(support.TESTFN), True)
141 if not self.pathmodule == genericpath:
142 self.assertIs(self.pathmodule.lexists(support.TESTFN),
143 True)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000144 finally:
145 if not f.close():
146 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000147 support.unlink(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000148
Richard Oudkerk2240ac12012-07-06 12:05:32 +0100149 @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
150 def test_exists_fd(self):
151 r, w = os.pipe()
152 try:
153 self.assertTrue(self.pathmodule.exists(r))
154 finally:
155 os.close(r)
156 os.close(w)
157 self.assertFalse(self.pathmodule.exists(r))
158
Thomas Wouters89f507f2006-12-13 04:49:30 +0000159 def test_isdir(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000160 self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000161 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000162 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000163 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000164 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000165 self.assertIs(self.pathmodule.isdir(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000166 os.remove(support.TESTFN)
167 os.mkdir(support.TESTFN)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000168 self.assertIs(self.pathmodule.isdir(support.TESTFN), True)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000169 os.rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000170 finally:
171 if not f.close():
172 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000173 support.unlink(support.TESTFN)
174 safe_rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000175
176 def test_isfile(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000177 self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000178 f = open(support.TESTFN, "wb")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000179 try:
Guido van Rossum199fc752007-08-27 23:38:12 +0000180 f.write(b"foo")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000181 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000182 self.assertIs(self.pathmodule.isfile(support.TESTFN), True)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000183 os.remove(support.TESTFN)
184 os.mkdir(support.TESTFN)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000185 self.assertIs(self.pathmodule.isfile(support.TESTFN), False)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000186 os.rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000187 finally:
188 if not f.close():
189 f.close()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000190 support.unlink(support.TESTFN)
191 safe_rmdir(support.TESTFN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000192
Thomas Wouters89f507f2006-12-13 04:49:30 +0000193
Florent Xicluna87082ee2010-08-09 17:18:05 +0000194# Following TestCase is not supposed to be run from test_genericpath.
195# It is inherited by other test modules (macpath, ntpath, posixpath).
196
Florent Xiclunac9c79782010-03-08 12:24:53 +0000197class CommonTest(GenericTest):
198 # The path module to be tested
199 pathmodule = None
200 common_attributes = GenericTest.common_attributes + [
201 # Properties
202 'curdir', 'pardir', 'extsep', 'sep',
203 'pathsep', 'defpath', 'altsep', 'devnull',
204 # Methods
205 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
206 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
207 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
208 ]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000209
Florent Xiclunac9c79782010-03-08 12:24:53 +0000210 def test_normcase(self):
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000211 normcase = self.pathmodule.normcase
212 # check that normcase() is idempotent
213 for p in ["FoO/./BaR", b"FoO/./BaR"]:
214 p = normcase(p)
215 self.assertEqual(p, normcase(p))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000216
Ezio Melotti5a3ef5b2010-06-25 10:56:11 +0000217 self.assertEqual(normcase(''), '')
218 self.assertEqual(normcase(b''), b'')
219
220 # check that normcase raises a TypeError for invalid types
221 for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}):
222 self.assertRaises(TypeError, normcase, path)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000223
224 def test_splitdrive(self):
225 # splitdrive for non-NT paths
226 splitdrive = self.pathmodule.splitdrive
227 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
228 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
229 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
230
231 self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar"))
232 self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar"))
233 self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar"))
234
235 def test_expandvars(self):
236 if self.pathmodule.__name__ == 'macpath':
237 self.skipTest('macpath.expandvars is a stub')
238 expandvars = self.pathmodule.expandvars
239 with support.EnvironmentVarGuard() as env:
240 env.clear()
241 env["foo"] = "bar"
242 env["{foo"] = "baz1"
243 env["{foo}"] = "baz2"
244 self.assertEqual(expandvars("foo"), "foo")
245 self.assertEqual(expandvars("$foo bar"), "bar bar")
246 self.assertEqual(expandvars("${foo}bar"), "barbar")
247 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
248 self.assertEqual(expandvars("$bar bar"), "$bar bar")
249 self.assertEqual(expandvars("$?bar"), "$?bar")
250 self.assertEqual(expandvars("${foo}bar"), "barbar")
251 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
252 self.assertEqual(expandvars("${foo"), "${foo")
253 self.assertEqual(expandvars("${{foo}}"), "baz1}")
254 self.assertEqual(expandvars("$foo$foo"), "barbar")
255 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
256
257 self.assertEqual(expandvars(b"foo"), b"foo")
258 self.assertEqual(expandvars(b"$foo bar"), b"bar bar")
259 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
260 self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
261 self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
262 self.assertEqual(expandvars(b"$?bar"), b"$?bar")
263 self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
264 self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
265 self.assertEqual(expandvars(b"${foo"), b"${foo")
266 self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
267 self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
268 self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
269
270 def test_abspath(self):
271 self.assertIn("foo", self.pathmodule.abspath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100272 with warnings.catch_warnings():
273 warnings.simplefilter("ignore", DeprecationWarning)
274 self.assertIn(b"foo", self.pathmodule.abspath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000275
276 # Abspath returns bytes when the arg is bytes
Victor Stinnerf7c5ae22011-11-16 23:43:07 +0100277 with warnings.catch_warnings():
278 warnings.simplefilter("ignore", DeprecationWarning)
279 for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'):
280 self.assertIsInstance(self.pathmodule.abspath(path), bytes)
Florent Xiclunac9c79782010-03-08 12:24:53 +0000281
282 def test_realpath(self):
283 self.assertIn("foo", self.pathmodule.realpath("foo"))
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100284 with warnings.catch_warnings():
285 warnings.simplefilter("ignore", DeprecationWarning)
286 self.assertIn(b"foo", self.pathmodule.realpath(b"foo"))
Florent Xiclunac9c79782010-03-08 12:24:53 +0000287
288 def test_normpath_issue5827(self):
289 # Make sure normpath preserves unicode
290 for path in ('', '.', '/', '\\', '///foo/.//bar//'):
291 self.assertIsInstance(self.pathmodule.normpath(path), str)
292
293 def test_abspath_issue3426(self):
294 # Check that abspath returns unicode when the arg is unicode
295 # with both ASCII and non-ASCII cwds.
296 abspath = self.pathmodule.abspath
297 for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'):
298 self.assertIsInstance(abspath(path), str)
299
300 unicwd = '\xe7w\xf0'
301 try:
302 fsencoding = support.TESTFN_ENCODING or "ascii"
303 unicwd.encode(fsencoding)
304 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 @unittest.skipIf(sys.platform == 'darwin',
R. David Murrayb6f916d2010-04-22 01:49:37 +0000313 "Mac OS X denies the creation of a directory with an invalid utf8 name")
Victor Stinner1efde672010-04-18 08:23:42 +0000314 def test_nonascii_abspath(self):
Florent Xiclunac9c79782010-03-08 12:24:53 +0000315 # Test non-ASCII, non-UTF8 bytes in the path.
Victor Stinner1ab6c2d2011-11-15 22:27:41 +0100316 with warnings.catch_warnings():
317 warnings.simplefilter("ignore", DeprecationWarning)
318 with support.temp_cwd(b'\xe7w\xf0'):
319 self.test_abspath()
Florent Xiclunac9c79782010-03-08 12:24:53 +0000320
Thomas Wouters89f507f2006-12-13 04:49:30 +0000321
322def test_main():
Florent Xiclunac9c79782010-03-08 12:24:53 +0000323 support.run_unittest(GenericTest)
324
Thomas Wouters89f507f2006-12-13 04:49:30 +0000325
326if __name__=="__main__":
327 test_main()