blob: af501cf0c76a0fae2c3b06cc017bf5ecbd45eee5 [file] [log] [blame]
Florent Xiclunadc1531c2010-03-06 18:07:18 +00001"""
2Tests common to genericpath, macpath, ntpath and posixpath
3"""
4
Jack Diederich7b604642006-08-26 18:42:06 +00005import unittest
6from test import test_support
7import os
8import genericpath
9
Florent Xiclunadc1531c2010-03-06 18:07:18 +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
Florent Xicluna985478d2010-03-06 18:52:52 +000020 pathmodule = genericpath
Florent Xiclunadc1531c2010-03-06 18:07:18 +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):
Florent Xicluna985478d2010-03-06 18:52:52 +000028 getattr(self.pathmodule, attr)()
29 raise self.fail("{}.{}() did not raise a TypeError"
30 .format(self.pathmodule.__name__, attr))
Jack Diederich7b604642006-08-26 18:42:06 +000031
Jack Diederich7b604642006-08-26 18:42:06 +000032 def test_commonprefix(self):
Florent Xicluna985478d2010-03-06 18:52:52 +000033 commonprefix = self.pathmodule.commonprefix
Jack Diederich7b604642006-08-26 18:42:06 +000034 self.assertEqual(
Florent Xicluna985478d2010-03-06 18:52:52 +000035 commonprefix([]),
Jack Diederich7b604642006-08-26 18:42:06 +000036 ""
37 )
38 self.assertEqual(
Florent Xicluna985478d2010-03-06 18:52:52 +000039 commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
Jack Diederich7b604642006-08-26 18:42:06 +000040 "/home/swen"
41 )
42 self.assertEqual(
Florent Xicluna985478d2010-03-06 18:52:52 +000043 commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
Jack Diederich7b604642006-08-26 18:42:06 +000044 "/home/swen/"
45 )
46 self.assertEqual(
Florent Xicluna985478d2010-03-06 18:52:52 +000047 commonprefix(["/home/swen/spam", "/home/swen/spam"]),
Jack Diederich7b604642006-08-26 18:42:06 +000048 "/home/swen/spam"
49 )
Florent Xicluna6f682be2010-03-08 12:39:35 +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 )
Jack Diederich7b604642006-08-26 18:42:06 +000062
Florent Xicluna985478d2010-03-06 18:52:52 +000063 testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
64 'aXc', 'abd', 'ab', 'aX', 'abcX']
Florent Xiclunadc1531c2010-03-06 18:07:18 +000065 for s1 in testlist:
66 for s2 in testlist:
Florent Xicluna985478d2010-03-06 18:52:52 +000067 p = commonprefix([s1, s2])
Florent Xiclunadc1531c2010-03-06 18:07:18 +000068 self.assertTrue(s1.startswith(p))
69 self.assertTrue(s2.startswith(p))
70 if s1 != s2:
71 n = len(p)
72 self.assertNotEqual(s1[n:n+1], s2[n:n+1])
73
Jack Diederich7b604642006-08-26 18:42:06 +000074 def test_getsize(self):
75 f = open(test_support.TESTFN, "wb")
76 try:
77 f.write("foo")
78 f.close()
Florent Xicluna985478d2010-03-06 18:52:52 +000079 self.assertEqual(self.pathmodule.getsize(test_support.TESTFN), 3)
Jack Diederich7b604642006-08-26 18:42:06 +000080 finally:
81 if not f.closed:
82 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +000083 test_support.unlink(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +000084
85 def test_time(self):
86 f = open(test_support.TESTFN, "wb")
87 try:
88 f.write("foo")
89 f.close()
90 f = open(test_support.TESTFN, "ab")
91 f.write("bar")
92 f.close()
93 f = open(test_support.TESTFN, "rb")
94 d = f.read()
95 f.close()
96 self.assertEqual(d, "foobar")
97
Ezio Melottie3467d52010-02-20 09:40:07 +000098 self.assertLessEqual(
Florent Xicluna985478d2010-03-06 18:52:52 +000099 self.pathmodule.getctime(test_support.TESTFN),
100 self.pathmodule.getmtime(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +0000101 )
102 finally:
103 if not f.closed:
104 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000105 test_support.unlink(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +0000106
107 def test_exists(self):
Florent Xicluna985478d2010-03-06 18:52:52 +0000108 self.assertIs(self.pathmodule.exists(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +0000109 f = open(test_support.TESTFN, "wb")
110 try:
111 f.write("foo")
112 f.close()
Florent Xicluna985478d2010-03-06 18:52:52 +0000113 self.assertIs(self.pathmodule.exists(test_support.TESTFN), True)
114 if not self.pathmodule == genericpath:
115 self.assertIs(self.pathmodule.lexists(test_support.TESTFN),
116 True)
Jack Diederich7b604642006-08-26 18:42:06 +0000117 finally:
118 if not f.close():
119 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000120 test_support.unlink(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +0000121
122 def test_isdir(self):
Florent Xicluna985478d2010-03-06 18:52:52 +0000123 self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +0000124 f = open(test_support.TESTFN, "wb")
125 try:
126 f.write("foo")
127 f.close()
Florent Xicluna985478d2010-03-06 18:52:52 +0000128 self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +0000129 os.remove(test_support.TESTFN)
130 os.mkdir(test_support.TESTFN)
Florent Xicluna985478d2010-03-06 18:52:52 +0000131 self.assertIs(self.pathmodule.isdir(test_support.TESTFN), True)
Jack Diederich7b604642006-08-26 18:42:06 +0000132 os.rmdir(test_support.TESTFN)
133 finally:
134 if not f.close():
135 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000136 test_support.unlink(test_support.TESTFN)
137 safe_rmdir(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +0000138
139 def test_isfile(self):
Florent Xicluna985478d2010-03-06 18:52:52 +0000140 self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +0000141 f = open(test_support.TESTFN, "wb")
142 try:
143 f.write("foo")
144 f.close()
Florent Xicluna985478d2010-03-06 18:52:52 +0000145 self.assertIs(self.pathmodule.isfile(test_support.TESTFN), True)
Jack Diederich7b604642006-08-26 18:42:06 +0000146 os.remove(test_support.TESTFN)
147 os.mkdir(test_support.TESTFN)
Florent Xicluna985478d2010-03-06 18:52:52 +0000148 self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +0000149 os.rmdir(test_support.TESTFN)
150 finally:
151 if not f.close():
152 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000153 test_support.unlink(test_support.TESTFN)
154 safe_rmdir(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +0000155
Ezio Melotti9e9af212010-02-20 22:34:21 +0000156
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000157class CommonTest(GenericTest):
158 # The path module to be tested
Florent Xicluna985478d2010-03-06 18:52:52 +0000159 pathmodule = None
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000160 common_attributes = GenericTest.common_attributes + [
161 # Properties
162 'curdir', 'pardir', 'extsep', 'sep',
163 'pathsep', 'defpath', 'altsep', 'devnull',
164 # Methods
165 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
166 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
167 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
168 ]
169
170 def test_normcase(self):
171 # Check that normcase() is idempotent
172 p = "FoO/./BaR"
Florent Xicluna985478d2010-03-06 18:52:52 +0000173 p = self.pathmodule.normcase(p)
174 self.assertEqual(p, self.pathmodule.normcase(p))
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000175
176 def test_splitdrive(self):
177 # splitdrive for non-NT paths
Florent Xicluna985478d2010-03-06 18:52:52 +0000178 splitdrive = self.pathmodule.splitdrive
179 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
180 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
181 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000182
183 def test_expandvars(self):
Florent Xicluna985478d2010-03-06 18:52:52 +0000184 if self.pathmodule.__name__ == 'macpath':
185 self.skipTest('macpath.expandvars is a stub')
186 expandvars = self.pathmodule.expandvars
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000187 with test_support.EnvironmentVarGuard() as env:
188 env.clear()
189 env["foo"] = "bar"
190 env["{foo"] = "baz1"
191 env["{foo}"] = "baz2"
Florent Xicluna985478d2010-03-06 18:52:52 +0000192 self.assertEqual(expandvars("foo"), "foo")
193 self.assertEqual(expandvars("$foo bar"), "bar bar")
194 self.assertEqual(expandvars("${foo}bar"), "barbar")
195 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
196 self.assertEqual(expandvars("$bar bar"), "$bar bar")
197 self.assertEqual(expandvars("$?bar"), "$?bar")
198 self.assertEqual(expandvars("${foo}bar"), "barbar")
199 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
200 self.assertEqual(expandvars("${foo"), "${foo")
201 self.assertEqual(expandvars("${{foo}}"), "baz1}")
202 self.assertEqual(expandvars("$foo$foo"), "barbar")
203 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000204
205 def test_abspath(self):
Florent Xicluna985478d2010-03-06 18:52:52 +0000206 self.assertIn("foo", self.pathmodule.abspath("foo"))
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000207
208 # Abspath returns bytes when the arg is bytes
Ezio Melotti9e9af212010-02-20 22:34:21 +0000209 for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
Florent Xicluna985478d2010-03-06 18:52:52 +0000210 self.assertIsInstance(self.pathmodule.abspath(path), str)
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000211
212 def test_realpath(self):
Florent Xicluna985478d2010-03-06 18:52:52 +0000213 self.assertIn("foo", self.pathmodule.realpath("foo"))
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000214
215 def test_normpath_issue5827(self):
216 # Make sure normpath preserves unicode
217 for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'):
Florent Xicluna985478d2010-03-06 18:52:52 +0000218 self.assertIsInstance(self.pathmodule.normpath(path), unicode)
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000219
220 def test_abspath_issue3426(self):
221 # Check that abspath returns unicode when the arg is unicode
222 # with both ASCII and non-ASCII cwds.
Florent Xicluna985478d2010-03-06 18:52:52 +0000223 abspath = self.pathmodule.abspath
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000224 for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
Florent Xicluna985478d2010-03-06 18:52:52 +0000225 self.assertIsInstance(abspath(path), unicode)
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000226
227 unicwd = u'\xe7w\xf0'
228 try:
229 fsencoding = test_support.TESTFN_ENCODING or "ascii"
230 unicwd.encode(fsencoding)
231 except (AttributeError, UnicodeEncodeError):
232 # FS encoding is probably ASCII
233 pass
234 else:
235 with test_support.temp_cwd(unicwd):
236 for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
Florent Xicluna985478d2010-03-06 18:52:52 +0000237 self.assertIsInstance(abspath(path), unicode)
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000238
239 # Test non-ASCII, non-UTF8 bytes in the path.
240 with test_support.temp_cwd('\xe7w\xf0'):
241 self.test_abspath()
Ezio Melotti9e9af212010-02-20 22:34:21 +0000242
243
Jack Diederich7b604642006-08-26 18:42:06 +0000244def test_main():
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000245 test_support.run_unittest(GenericTest)
246
Jack Diederich7b604642006-08-26 18:42:06 +0000247
248if __name__=="__main__":
249 test_main()