blob: e54dd102dbe1c22981344ad5f4f2168155ef1207 [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
20 path = 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.path, attr)()
29 raise self.fail("{}.{}() do not raise a TypeError"
30 .format(self.path.__name__, attr))
Jack Diederich7b604642006-08-26 18:42:06 +000031
Jack Diederich7b604642006-08-26 18:42:06 +000032 def test_commonprefix(self):
33 self.assertEqual(
Florent Xiclunadc1531c2010-03-06 18:07:18 +000034 self.path.commonprefix([]),
Jack Diederich7b604642006-08-26 18:42:06 +000035 ""
36 )
37 self.assertEqual(
Florent Xiclunadc1531c2010-03-06 18:07:18 +000038 self.path.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
Jack Diederich7b604642006-08-26 18:42:06 +000039 "/home/swen"
40 )
41 self.assertEqual(
Florent Xiclunadc1531c2010-03-06 18:07:18 +000042 self.path.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
Jack Diederich7b604642006-08-26 18:42:06 +000043 "/home/swen/"
44 )
45 self.assertEqual(
Florent Xiclunadc1531c2010-03-06 18:07:18 +000046 self.path.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
Jack Diederich7b604642006-08-26 18:42:06 +000047 "/home/swen/spam"
48 )
49
Florent Xiclunadc1531c2010-03-06 18:07:18 +000050 testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', 'aXc', 'abd', 'ab', 'aX', 'abcX']
51 for s1 in testlist:
52 for s2 in testlist:
53 p = self.path.commonprefix([s1, s2])
54 self.assertTrue(s1.startswith(p))
55 self.assertTrue(s2.startswith(p))
56 if s1 != s2:
57 n = len(p)
58 self.assertNotEqual(s1[n:n+1], s2[n:n+1])
59
Jack Diederich7b604642006-08-26 18:42:06 +000060 def test_getsize(self):
61 f = open(test_support.TESTFN, "wb")
62 try:
63 f.write("foo")
64 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +000065 self.assertEqual(self.path.getsize(test_support.TESTFN), 3)
Jack Diederich7b604642006-08-26 18:42:06 +000066 finally:
67 if not f.closed:
68 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +000069 test_support.unlink(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +000070
71 def test_time(self):
72 f = open(test_support.TESTFN, "wb")
73 try:
74 f.write("foo")
75 f.close()
76 f = open(test_support.TESTFN, "ab")
77 f.write("bar")
78 f.close()
79 f = open(test_support.TESTFN, "rb")
80 d = f.read()
81 f.close()
82 self.assertEqual(d, "foobar")
83
Ezio Melottie3467d52010-02-20 09:40:07 +000084 self.assertLessEqual(
Florent Xiclunadc1531c2010-03-06 18:07:18 +000085 self.path.getctime(test_support.TESTFN),
86 self.path.getmtime(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +000087 )
88 finally:
89 if not f.closed:
90 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +000091 test_support.unlink(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +000092
93 def test_exists(self):
Florent Xiclunadc1531c2010-03-06 18:07:18 +000094 self.assertIs(self.path.exists(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +000095 f = open(test_support.TESTFN, "wb")
96 try:
97 f.write("foo")
98 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +000099 self.assertIs(self.path.exists(test_support.TESTFN), True)
100 if not self.path == genericpath:
101 self.assertIs(self.path.lexists(test_support.TESTFN), True)
Jack Diederich7b604642006-08-26 18:42:06 +0000102 finally:
103 if not f.close():
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_isdir(self):
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000108 self.assertIs(self.path.isdir(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 Xiclunadc1531c2010-03-06 18:07:18 +0000113 self.assertIs(self.path.isdir(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +0000114 os.remove(test_support.TESTFN)
115 os.mkdir(test_support.TESTFN)
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000116 self.assertIs(self.path.isdir(test_support.TESTFN), True)
Jack Diederich7b604642006-08-26 18:42:06 +0000117 os.rmdir(test_support.TESTFN)
118 finally:
119 if not f.close():
120 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000121 test_support.unlink(test_support.TESTFN)
122 safe_rmdir(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +0000123
124 def test_isfile(self):
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000125 self.assertIs(self.path.isfile(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +0000126 f = open(test_support.TESTFN, "wb")
127 try:
128 f.write("foo")
129 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000130 self.assertIs(self.path.isfile(test_support.TESTFN), True)
Jack Diederich7b604642006-08-26 18:42:06 +0000131 os.remove(test_support.TESTFN)
132 os.mkdir(test_support.TESTFN)
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000133 self.assertIs(self.path.isfile(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +0000134 os.rmdir(test_support.TESTFN)
135 finally:
136 if not f.close():
137 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000138 test_support.unlink(test_support.TESTFN)
139 safe_rmdir(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +0000140
Ezio Melotti9e9af212010-02-20 22:34:21 +0000141
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000142class CommonTest(GenericTest):
143 # The path module to be tested
144 path = None
145 common_attributes = GenericTest.common_attributes + [
146 # Properties
147 'curdir', 'pardir', 'extsep', 'sep',
148 'pathsep', 'defpath', 'altsep', 'devnull',
149 # Methods
150 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
151 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
152 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
153 ]
154
155 def test_normcase(self):
156 # Check that normcase() is idempotent
157 p = "FoO/./BaR"
158 p = self.path.normcase(p)
159 self.assertEqual(p, self.path.normcase(p))
160
161 def test_splitdrive(self):
162 # splitdrive for non-NT paths
163 self.assertEqual(self.path.splitdrive("/foo/bar"), ("", "/foo/bar"))
164 self.assertEqual(self.path.splitdrive("foo:bar"), ('', 'foo:bar'))
165 self.assertEqual(self.path.splitdrive(":foo:bar"), ('', ':foo:bar'))
166
167 def test_expandvars(self):
168 if self.path.__name__ == 'macpath':
169 raise unittest.SkipTest('macpath.expandvars is a stub')
170 with test_support.EnvironmentVarGuard() as env:
171 env.clear()
172 env["foo"] = "bar"
173 env["{foo"] = "baz1"
174 env["{foo}"] = "baz2"
175 self.assertEqual(self.path.expandvars("foo"), "foo")
176 self.assertEqual(self.path.expandvars("$foo bar"), "bar bar")
177 self.assertEqual(self.path.expandvars("${foo}bar"), "barbar")
178 self.assertEqual(self.path.expandvars("$[foo]bar"), "$[foo]bar")
179 self.assertEqual(self.path.expandvars("$bar bar"), "$bar bar")
180 self.assertEqual(self.path.expandvars("$?bar"), "$?bar")
181 self.assertEqual(self.path.expandvars("${foo}bar"), "barbar")
182 self.assertEqual(self.path.expandvars("$foo}bar"), "bar}bar")
183 self.assertEqual(self.path.expandvars("${foo"), "${foo")
184 self.assertEqual(self.path.expandvars("${{foo}}"), "baz1}")
185 self.assertEqual(self.path.expandvars("$foo$foo"), "barbar")
186 self.assertEqual(self.path.expandvars("$bar$bar"), "$bar$bar")
187
188 def test_abspath(self):
189 self.assertIn("foo", self.path.abspath("foo"))
190
191 # Abspath returns bytes when the arg is bytes
Ezio Melotti9e9af212010-02-20 22:34:21 +0000192 for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000193 self.assertIsInstance(self.path.abspath(path), str)
194
195 def test_realpath(self):
196 self.assertIn("foo", self.path.realpath("foo"))
197
198 def test_normpath_issue5827(self):
199 # Make sure normpath preserves unicode
200 for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'):
201 self.assertIsInstance(self.path.normpath(path), unicode)
202
203 def test_abspath_issue3426(self):
204 # Check that abspath returns unicode when the arg is unicode
205 # with both ASCII and non-ASCII cwds.
206 for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
207 self.assertIsInstance(self.path.abspath(path), unicode)
208
209 unicwd = u'\xe7w\xf0'
210 try:
211 fsencoding = test_support.TESTFN_ENCODING or "ascii"
212 unicwd.encode(fsencoding)
213 except (AttributeError, UnicodeEncodeError):
214 # FS encoding is probably ASCII
215 pass
216 else:
217 with test_support.temp_cwd(unicwd):
218 for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
219 self.assertIsInstance(self.path.abspath(path), unicode)
220
221 # Test non-ASCII, non-UTF8 bytes in the path.
222 with test_support.temp_cwd('\xe7w\xf0'):
223 self.test_abspath()
Ezio Melotti9e9af212010-02-20 22:34:21 +0000224
225
Jack Diederich7b604642006-08-26 18:42:06 +0000226def test_main():
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000227 test_support.run_unittest(GenericTest)
228
Jack Diederich7b604642006-08-26 18:42:06 +0000229
230if __name__=="__main__":
231 test_main()