blob: fc0c33d430f9840bbdd9a932f8193b72ff02c1a3 [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 )
50
Florent Xicluna985478d2010-03-06 18:52:52 +000051 testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
52 'aXc', 'abd', 'ab', 'aX', 'abcX']
Florent Xiclunadc1531c2010-03-06 18:07:18 +000053 for s1 in testlist:
54 for s2 in testlist:
Florent Xicluna985478d2010-03-06 18:52:52 +000055 p = commonprefix([s1, s2])
Florent Xiclunadc1531c2010-03-06 18:07:18 +000056 self.assertTrue(s1.startswith(p))
57 self.assertTrue(s2.startswith(p))
58 if s1 != s2:
59 n = len(p)
60 self.assertNotEqual(s1[n:n+1], s2[n:n+1])
61
Jack Diederich7b604642006-08-26 18:42:06 +000062 def test_getsize(self):
63 f = open(test_support.TESTFN, "wb")
64 try:
65 f.write("foo")
66 f.close()
Florent Xicluna985478d2010-03-06 18:52:52 +000067 self.assertEqual(self.pathmodule.getsize(test_support.TESTFN), 3)
Jack Diederich7b604642006-08-26 18:42:06 +000068 finally:
69 if not f.closed:
70 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +000071 test_support.unlink(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +000072
73 def test_time(self):
74 f = open(test_support.TESTFN, "wb")
75 try:
76 f.write("foo")
77 f.close()
78 f = open(test_support.TESTFN, "ab")
79 f.write("bar")
80 f.close()
81 f = open(test_support.TESTFN, "rb")
82 d = f.read()
83 f.close()
84 self.assertEqual(d, "foobar")
85
Ezio Melottie3467d52010-02-20 09:40:07 +000086 self.assertLessEqual(
Florent Xicluna985478d2010-03-06 18:52:52 +000087 self.pathmodule.getctime(test_support.TESTFN),
88 self.pathmodule.getmtime(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +000089 )
90 finally:
91 if not f.closed:
92 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +000093 test_support.unlink(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +000094
95 def test_exists(self):
Florent Xicluna985478d2010-03-06 18:52:52 +000096 self.assertIs(self.pathmodule.exists(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +000097 f = open(test_support.TESTFN, "wb")
98 try:
99 f.write("foo")
100 f.close()
Florent Xicluna985478d2010-03-06 18:52:52 +0000101 self.assertIs(self.pathmodule.exists(test_support.TESTFN), True)
102 if not self.pathmodule == genericpath:
103 self.assertIs(self.pathmodule.lexists(test_support.TESTFN),
104 True)
Jack Diederich7b604642006-08-26 18:42:06 +0000105 finally:
106 if not f.close():
107 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000108 test_support.unlink(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +0000109
110 def test_isdir(self):
Florent Xicluna985478d2010-03-06 18:52:52 +0000111 self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +0000112 f = open(test_support.TESTFN, "wb")
113 try:
114 f.write("foo")
115 f.close()
Florent Xicluna985478d2010-03-06 18:52:52 +0000116 self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +0000117 os.remove(test_support.TESTFN)
118 os.mkdir(test_support.TESTFN)
Florent Xicluna985478d2010-03-06 18:52:52 +0000119 self.assertIs(self.pathmodule.isdir(test_support.TESTFN), True)
Jack Diederich7b604642006-08-26 18:42:06 +0000120 os.rmdir(test_support.TESTFN)
121 finally:
122 if not f.close():
123 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000124 test_support.unlink(test_support.TESTFN)
125 safe_rmdir(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +0000126
127 def test_isfile(self):
Florent Xicluna985478d2010-03-06 18:52:52 +0000128 self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +0000129 f = open(test_support.TESTFN, "wb")
130 try:
131 f.write("foo")
132 f.close()
Florent Xicluna985478d2010-03-06 18:52:52 +0000133 self.assertIs(self.pathmodule.isfile(test_support.TESTFN), True)
Jack Diederich7b604642006-08-26 18:42:06 +0000134 os.remove(test_support.TESTFN)
135 os.mkdir(test_support.TESTFN)
Florent Xicluna985478d2010-03-06 18:52:52 +0000136 self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False)
Jack Diederich7b604642006-08-26 18:42:06 +0000137 os.rmdir(test_support.TESTFN)
138 finally:
139 if not f.close():
140 f.close()
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000141 test_support.unlink(test_support.TESTFN)
142 safe_rmdir(test_support.TESTFN)
Jack Diederich7b604642006-08-26 18:42:06 +0000143
Ezio Melotti9e9af212010-02-20 22:34:21 +0000144
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000145class CommonTest(GenericTest):
146 # The path module to be tested
Florent Xicluna985478d2010-03-06 18:52:52 +0000147 pathmodule = None
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000148 common_attributes = GenericTest.common_attributes + [
149 # Properties
150 'curdir', 'pardir', 'extsep', 'sep',
151 'pathsep', 'defpath', 'altsep', 'devnull',
152 # Methods
153 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
154 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
155 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
156 ]
157
158 def test_normcase(self):
159 # Check that normcase() is idempotent
160 p = "FoO/./BaR"
Florent Xicluna985478d2010-03-06 18:52:52 +0000161 p = self.pathmodule.normcase(p)
162 self.assertEqual(p, self.pathmodule.normcase(p))
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000163
164 def test_splitdrive(self):
165 # splitdrive for non-NT paths
Florent Xicluna985478d2010-03-06 18:52:52 +0000166 splitdrive = self.pathmodule.splitdrive
167 self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
168 self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
169 self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000170
171 def test_expandvars(self):
Florent Xicluna985478d2010-03-06 18:52:52 +0000172 if self.pathmodule.__name__ == 'macpath':
173 self.skipTest('macpath.expandvars is a stub')
174 expandvars = self.pathmodule.expandvars
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000175 with test_support.EnvironmentVarGuard() as env:
176 env.clear()
177 env["foo"] = "bar"
178 env["{foo"] = "baz1"
179 env["{foo}"] = "baz2"
Florent Xicluna985478d2010-03-06 18:52:52 +0000180 self.assertEqual(expandvars("foo"), "foo")
181 self.assertEqual(expandvars("$foo bar"), "bar bar")
182 self.assertEqual(expandvars("${foo}bar"), "barbar")
183 self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
184 self.assertEqual(expandvars("$bar bar"), "$bar bar")
185 self.assertEqual(expandvars("$?bar"), "$?bar")
186 self.assertEqual(expandvars("${foo}bar"), "barbar")
187 self.assertEqual(expandvars("$foo}bar"), "bar}bar")
188 self.assertEqual(expandvars("${foo"), "${foo")
189 self.assertEqual(expandvars("${{foo}}"), "baz1}")
190 self.assertEqual(expandvars("$foo$foo"), "barbar")
191 self.assertEqual(expandvars("$bar$bar"), "$bar$bar")
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000192
193 def test_abspath(self):
Florent Xicluna985478d2010-03-06 18:52:52 +0000194 self.assertIn("foo", self.pathmodule.abspath("foo"))
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000195
196 # Abspath returns bytes when the arg is bytes
Ezio Melotti9e9af212010-02-20 22:34:21 +0000197 for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
Florent Xicluna985478d2010-03-06 18:52:52 +0000198 self.assertIsInstance(self.pathmodule.abspath(path), str)
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000199
200 def test_realpath(self):
Florent Xicluna985478d2010-03-06 18:52:52 +0000201 self.assertIn("foo", self.pathmodule.realpath("foo"))
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000202
203 def test_normpath_issue5827(self):
204 # Make sure normpath preserves unicode
205 for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'):
Florent Xicluna985478d2010-03-06 18:52:52 +0000206 self.assertIsInstance(self.pathmodule.normpath(path), unicode)
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000207
208 def test_abspath_issue3426(self):
209 # Check that abspath returns unicode when the arg is unicode
210 # with both ASCII and non-ASCII cwds.
Florent Xicluna985478d2010-03-06 18:52:52 +0000211 abspath = self.pathmodule.abspath
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000212 for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'):
Florent Xicluna985478d2010-03-06 18:52:52 +0000213 self.assertIsInstance(abspath(path), unicode)
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000214
215 unicwd = u'\xe7w\xf0'
216 try:
217 fsencoding = test_support.TESTFN_ENCODING or "ascii"
218 unicwd.encode(fsencoding)
219 except (AttributeError, UnicodeEncodeError):
220 # FS encoding is probably ASCII
221 pass
222 else:
223 with test_support.temp_cwd(unicwd):
224 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 # Test non-ASCII, non-UTF8 bytes in the path.
228 with test_support.temp_cwd('\xe7w\xf0'):
229 self.test_abspath()
Ezio Melotti9e9af212010-02-20 22:34:21 +0000230
231
Jack Diederich7b604642006-08-26 18:42:06 +0000232def test_main():
Florent Xiclunadc1531c2010-03-06 18:07:18 +0000233 test_support.run_unittest(GenericTest)
234
Jack Diederich7b604642006-08-26 18:42:06 +0000235
236if __name__=="__main__":
237 test_main()