2 Tests common to genericpath, macpath, ntpath and posixpath
6 from test
import test_support
12 def safe_rmdir(dirname
):
19 class GenericTest(unittest
.TestCase
):
20 # The path module to be tested
21 pathmodule
= genericpath
22 common_attributes
= ['commonprefix', 'getsize', 'getatime', 'getctime',
23 'getmtime', 'exists', 'isdir', 'isfile']
26 def test_no_argument(self
):
27 for attr
in self
.common_attributes
+ self
.attributes
:
28 with self
.assertRaises(TypeError):
29 getattr(self
.pathmodule
, attr
)()
30 raise self
.fail("{}.{}() did not raise a TypeError"
31 .format(self
.pathmodule
.__name
__, attr
))
33 def test_commonprefix(self
):
34 commonprefix
= self
.pathmodule
.commonprefix
40 commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
44 commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
48 commonprefix(["/home/swen/spam", "/home/swen/spam"]),
52 commonprefix(["home:swenson:spam", "home:swen:spam"]),
56 commonprefix([":home:swen:spam", ":home:swen:eggs"]),
60 commonprefix([":home:swen:spam", ":home:swen:spam"]),
64 testlist
= ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd',
65 'aXc', 'abd', 'ab', 'aX', 'abcX']
68 p
= commonprefix([s1
, s2
])
69 self
.assertTrue(s1
.startswith(p
))
70 self
.assertTrue(s2
.startswith(p
))
73 self
.assertNotEqual(s1
[n
:n
+1], s2
[n
:n
+1])
75 def test_getsize(self
):
76 f
= open(test_support
.TESTFN
, "wb")
80 self
.assertEqual(self
.pathmodule
.getsize(test_support
.TESTFN
), 3)
84 test_support
.unlink(test_support
.TESTFN
)
87 f
= open(test_support
.TESTFN
, "wb")
91 f
= open(test_support
.TESTFN
, "ab")
94 f
= open(test_support
.TESTFN
, "rb")
97 self
.assertEqual(d
, "foobar")
100 self
.pathmodule
.getctime(test_support
.TESTFN
),
101 self
.pathmodule
.getmtime(test_support
.TESTFN
)
106 test_support
.unlink(test_support
.TESTFN
)
108 def test_exists(self
):
109 self
.assertIs(self
.pathmodule
.exists(test_support
.TESTFN
), False)
110 f
= open(test_support
.TESTFN
, "wb")
114 self
.assertIs(self
.pathmodule
.exists(test_support
.TESTFN
), True)
115 if not self
.pathmodule
== genericpath
:
116 self
.assertIs(self
.pathmodule
.lexists(test_support
.TESTFN
),
121 test_support
.unlink(test_support
.TESTFN
)
123 def test_isdir(self
):
124 self
.assertIs(self
.pathmodule
.isdir(test_support
.TESTFN
), False)
125 f
= open(test_support
.TESTFN
, "wb")
129 self
.assertIs(self
.pathmodule
.isdir(test_support
.TESTFN
), False)
130 os
.remove(test_support
.TESTFN
)
131 os
.mkdir(test_support
.TESTFN
)
132 self
.assertIs(self
.pathmodule
.isdir(test_support
.TESTFN
), True)
133 os
.rmdir(test_support
.TESTFN
)
137 test_support
.unlink(test_support
.TESTFN
)
138 safe_rmdir(test_support
.TESTFN
)
140 def test_isfile(self
):
141 self
.assertIs(self
.pathmodule
.isfile(test_support
.TESTFN
), False)
142 f
= open(test_support
.TESTFN
, "wb")
146 self
.assertIs(self
.pathmodule
.isfile(test_support
.TESTFN
), True)
147 os
.remove(test_support
.TESTFN
)
148 os
.mkdir(test_support
.TESTFN
)
149 self
.assertIs(self
.pathmodule
.isfile(test_support
.TESTFN
), False)
150 os
.rmdir(test_support
.TESTFN
)
154 test_support
.unlink(test_support
.TESTFN
)
155 safe_rmdir(test_support
.TESTFN
)
158 # Following TestCase is not supposed to be run from test_genericpath.
159 # It is inherited by other test modules (macpath, ntpath, posixpath).
161 class CommonTest(GenericTest
):
162 # The path module to be tested
164 common_attributes
= GenericTest
.common_attributes
+ [
166 'curdir', 'pardir', 'extsep', 'sep',
167 'pathsep', 'defpath', 'altsep', 'devnull',
169 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath',
170 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname',
171 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath',
174 def test_normcase(self
):
175 # Check that normcase() is idempotent
177 p
= self
.pathmodule
.normcase(p
)
178 self
.assertEqual(p
, self
.pathmodule
.normcase(p
))
180 def test_splitdrive(self
):
181 # splitdrive for non-NT paths
182 splitdrive
= self
.pathmodule
.splitdrive
183 self
.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar"))
184 self
.assertEqual(splitdrive("foo:bar"), ("", "foo:bar"))
185 self
.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar"))
187 def test_expandvars(self
):
188 if self
.pathmodule
.__name
__ == 'macpath':
189 self
.skipTest('macpath.expandvars is a stub')
190 expandvars
= self
.pathmodule
.expandvars
191 with test_support
.EnvironmentVarGuard() as env
:
195 env
["{foo}"] = "baz2"
196 self
.assertEqual(expandvars("foo"), "foo")
197 self
.assertEqual(expandvars("$foo bar"), "bar bar")
198 self
.assertEqual(expandvars("${foo}bar"), "barbar")
199 self
.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
200 self
.assertEqual(expandvars("$bar bar"), "$bar bar")
201 self
.assertEqual(expandvars("$?bar"), "$?bar")
202 self
.assertEqual(expandvars("${foo}bar"), "barbar")
203 self
.assertEqual(expandvars("$foo}bar"), "bar}bar")
204 self
.assertEqual(expandvars("${foo"), "${foo")
205 self
.assertEqual(expandvars("${{foo}}"), "baz1}")
206 self
.assertEqual(expandvars("$foo$foo"), "barbar")
207 self
.assertEqual(expandvars("$bar$bar"), "$bar$bar")
209 def test_abspath(self
):
210 self
.assertIn("foo", self
.pathmodule
.abspath("foo"))
212 # Abspath returns bytes when the arg is bytes
213 for path
in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'):
214 self
.assertIsInstance(self
.pathmodule
.abspath(path
), str)
216 def test_realpath(self
):
217 self
.assertIn("foo", self
.pathmodule
.realpath("foo"))
219 def test_normpath_issue5827(self
):
220 # Make sure normpath preserves unicode
221 for path
in (u
'', u
'.', u
'/', u
'\\', u
'///foo/.//bar//'):
222 self
.assertIsInstance(self
.pathmodule
.normpath(path
), unicode)
224 def test_abspath_issue3426(self
):
225 # Check that abspath returns unicode when the arg is unicode
226 # with both ASCII and non-ASCII cwds.
227 abspath
= self
.pathmodule
.abspath
228 for path
in (u
'', u
'fuu', u
'f\xf9\xf9', u
'/fuu', u
'U:\\'):
229 self
.assertIsInstance(abspath(path
), unicode)
231 unicwd
= u
'\xe7w\xf0'
233 fsencoding
= test_support
.TESTFN_ENCODING
or "ascii"
234 unicwd
.encode(fsencoding
)
235 except (AttributeError, UnicodeEncodeError):
236 # FS encoding is probably ASCII
239 with test_support
.temp_cwd(unicwd
):
240 for path
in (u
'', u
'fuu', u
'f\xf9\xf9', u
'/fuu', u
'U:\\'):
241 self
.assertIsInstance(abspath(path
), unicode)
243 @unittest.skipIf(sys
.platform
== 'darwin',
244 "Mac OS X denies the creation of a directory with an invalid utf8 name")
245 def test_nonascii_abspath(self
):
246 # Test non-ASCII, non-UTF8 bytes in the path.
247 with test_support
.temp_cwd('\xe7w\xf0'):
252 test_support
.run_unittest(GenericTest
)
255 if __name__
=="__main__":