Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 1 | """ |
| 2 | Tests common to genericpath, macpath, ntpath and posixpath |
| 3 | """ |
| 4 | |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 5 | import unittest |
| 6 | from test import test_support |
| 7 | import os |
| 8 | import genericpath |
Victor Stinner | 20c3258 | 2010-05-13 16:18:14 +0000 | [diff] [blame] | 9 | import sys |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 10 | |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 11 | |
| 12 | def safe_rmdir(dirname): |
| 13 | try: |
| 14 | os.rmdir(dirname) |
| 15 | except OSError: |
| 16 | pass |
| 17 | |
| 18 | |
| 19 | class GenericTest(unittest.TestCase): |
| 20 | # The path module to be tested |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 21 | pathmodule = genericpath |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 22 | common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime', |
| 23 | 'getmtime', 'exists', 'isdir', 'isfile'] |
| 24 | attributes = [] |
| 25 | |
| 26 | def test_no_argument(self): |
| 27 | for attr in self.common_attributes + self.attributes: |
| 28 | with self.assertRaises(TypeError): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 29 | getattr(self.pathmodule, attr)() |
| 30 | raise self.fail("{}.{}() did not raise a TypeError" |
| 31 | .format(self.pathmodule.__name__, attr)) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 32 | |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 33 | def test_commonprefix(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 34 | commonprefix = self.pathmodule.commonprefix |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 35 | self.assertEqual( |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 36 | commonprefix([]), |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 37 | "" |
| 38 | ) |
| 39 | self.assertEqual( |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 40 | commonprefix(["/home/swenson/spam", "/home/swen/spam"]), |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 41 | "/home/swen" |
| 42 | ) |
| 43 | self.assertEqual( |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 44 | commonprefix(["/home/swen/spam", "/home/swen/eggs"]), |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 45 | "/home/swen/" |
| 46 | ) |
| 47 | self.assertEqual( |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 48 | commonprefix(["/home/swen/spam", "/home/swen/spam"]), |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 49 | "/home/swen/spam" |
| 50 | ) |
Florent Xicluna | 6f682be | 2010-03-08 12:39:35 +0000 | [diff] [blame] | 51 | self.assertEqual( |
| 52 | commonprefix(["home:swenson:spam", "home:swen:spam"]), |
| 53 | "home:swen" |
| 54 | ) |
| 55 | self.assertEqual( |
| 56 | commonprefix([":home:swen:spam", ":home:swen:eggs"]), |
| 57 | ":home:swen:" |
| 58 | ) |
| 59 | self.assertEqual( |
| 60 | commonprefix([":home:swen:spam", ":home:swen:spam"]), |
| 61 | ":home:swen:spam" |
| 62 | ) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 63 | |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 64 | testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', |
| 65 | 'aXc', 'abd', 'ab', 'aX', 'abcX'] |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 66 | for s1 in testlist: |
| 67 | for s2 in testlist: |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 68 | p = commonprefix([s1, s2]) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 69 | self.assertTrue(s1.startswith(p)) |
| 70 | self.assertTrue(s2.startswith(p)) |
| 71 | if s1 != s2: |
| 72 | n = len(p) |
| 73 | self.assertNotEqual(s1[n:n+1], s2[n:n+1]) |
| 74 | |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 75 | def test_getsize(self): |
| 76 | f = open(test_support.TESTFN, "wb") |
| 77 | try: |
| 78 | f.write("foo") |
| 79 | f.close() |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 80 | self.assertEqual(self.pathmodule.getsize(test_support.TESTFN), 3) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 81 | finally: |
| 82 | if not f.closed: |
| 83 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 84 | test_support.unlink(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 85 | |
| 86 | def test_time(self): |
| 87 | f = open(test_support.TESTFN, "wb") |
| 88 | try: |
| 89 | f.write("foo") |
| 90 | f.close() |
| 91 | f = open(test_support.TESTFN, "ab") |
| 92 | f.write("bar") |
| 93 | f.close() |
| 94 | f = open(test_support.TESTFN, "rb") |
| 95 | d = f.read() |
| 96 | f.close() |
| 97 | self.assertEqual(d, "foobar") |
| 98 | |
Ezio Melotti | e3467d5 | 2010-02-20 09:40:07 +0000 | [diff] [blame] | 99 | self.assertLessEqual( |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 100 | self.pathmodule.getctime(test_support.TESTFN), |
| 101 | self.pathmodule.getmtime(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 102 | ) |
| 103 | finally: |
| 104 | if not f.closed: |
| 105 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 106 | test_support.unlink(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 107 | |
| 108 | def test_exists(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 109 | self.assertIs(self.pathmodule.exists(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 110 | f = open(test_support.TESTFN, "wb") |
| 111 | try: |
| 112 | f.write("foo") |
| 113 | f.close() |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 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), |
| 117 | True) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 118 | finally: |
| 119 | if not f.close(): |
| 120 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 121 | test_support.unlink(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 122 | |
| 123 | def test_isdir(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 124 | self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 125 | f = open(test_support.TESTFN, "wb") |
| 126 | try: |
| 127 | f.write("foo") |
| 128 | f.close() |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 129 | self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 130 | os.remove(test_support.TESTFN) |
| 131 | os.mkdir(test_support.TESTFN) |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 132 | self.assertIs(self.pathmodule.isdir(test_support.TESTFN), True) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 133 | os.rmdir(test_support.TESTFN) |
| 134 | finally: |
| 135 | if not f.close(): |
| 136 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 137 | test_support.unlink(test_support.TESTFN) |
| 138 | safe_rmdir(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 139 | |
| 140 | def test_isfile(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 141 | self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 142 | f = open(test_support.TESTFN, "wb") |
| 143 | try: |
| 144 | f.write("foo") |
| 145 | f.close() |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 146 | self.assertIs(self.pathmodule.isfile(test_support.TESTFN), True) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 147 | os.remove(test_support.TESTFN) |
| 148 | os.mkdir(test_support.TESTFN) |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 149 | self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 150 | os.rmdir(test_support.TESTFN) |
| 151 | finally: |
| 152 | if not f.close(): |
| 153 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 154 | test_support.unlink(test_support.TESTFN) |
| 155 | safe_rmdir(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 156 | |
Ezio Melotti | 9e9af21 | 2010-02-20 22:34:21 +0000 | [diff] [blame] | 157 | |
Florent Xicluna | a37b7af | 2010-06-17 20:30:56 +0000 | [diff] [blame] | 158 | # Following TestCase is not supposed to be run from test_genericpath. |
| 159 | # It is inherited by other test modules (macpath, ntpath, posixpath). |
| 160 | |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 161 | class CommonTest(GenericTest): |
| 162 | # The path module to be tested |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 163 | pathmodule = None |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 164 | common_attributes = GenericTest.common_attributes + [ |
| 165 | # Properties |
| 166 | 'curdir', 'pardir', 'extsep', 'sep', |
| 167 | 'pathsep', 'defpath', 'altsep', 'devnull', |
| 168 | # Methods |
| 169 | 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath', |
| 170 | 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname', |
| 171 | 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath', |
| 172 | ] |
| 173 | |
| 174 | def test_normcase(self): |
| 175 | # Check that normcase() is idempotent |
| 176 | p = "FoO/./BaR" |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 177 | p = self.pathmodule.normcase(p) |
| 178 | self.assertEqual(p, self.pathmodule.normcase(p)) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 179 | |
| 180 | def test_splitdrive(self): |
| 181 | # splitdrive for non-NT paths |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 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")) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 186 | |
| 187 | def test_expandvars(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 188 | if self.pathmodule.__name__ == 'macpath': |
| 189 | self.skipTest('macpath.expandvars is a stub') |
| 190 | expandvars = self.pathmodule.expandvars |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 191 | with test_support.EnvironmentVarGuard() as env: |
| 192 | env.clear() |
| 193 | env["foo"] = "bar" |
| 194 | env["{foo"] = "baz1" |
| 195 | env["{foo}"] = "baz2" |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 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") |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 202 | self.assertEqual(expandvars("$foo}bar"), "bar}bar") |
| 203 | self.assertEqual(expandvars("${foo"), "${foo") |
| 204 | self.assertEqual(expandvars("${{foo}}"), "baz1}") |
| 205 | self.assertEqual(expandvars("$foo$foo"), "barbar") |
| 206 | self.assertEqual(expandvars("$bar$bar"), "$bar$bar") |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 207 | |
Serhiy Storchaka | 2ac9d31 | 2014-02-19 23:27:37 +0200 | [diff] [blame] | 208 | @unittest.skipUnless(test_support.FS_NONASCII, 'need test_support.FS_NONASCII') |
| 209 | def test_expandvars_nonascii(self): |
| 210 | if self.pathmodule.__name__ == 'macpath': |
| 211 | self.skipTest('macpath.expandvars is a stub') |
| 212 | expandvars = self.pathmodule.expandvars |
| 213 | def check(value, expected): |
| 214 | self.assertEqual(expandvars(value), expected) |
| 215 | encoding = sys.getfilesystemencoding() |
| 216 | with test_support.EnvironmentVarGuard() as env: |
| 217 | env.clear() |
| 218 | unonascii = test_support.FS_NONASCII |
| 219 | snonascii = unonascii.encode(encoding) |
| 220 | env['spam'] = snonascii |
| 221 | env[snonascii] = 'ham' + snonascii |
| 222 | check(snonascii, snonascii) |
| 223 | check('$spam bar', '%s bar' % snonascii) |
| 224 | check('${spam}bar', '%sbar' % snonascii) |
| 225 | check('${%s}bar' % snonascii, 'ham%sbar' % snonascii) |
| 226 | check('$bar%s bar' % snonascii, '$bar%s bar' % snonascii) |
| 227 | check('$spam}bar', '%s}bar' % snonascii) |
| 228 | |
| 229 | check(unonascii, unonascii) |
| 230 | check(u'$spam bar', u'%s bar' % unonascii) |
| 231 | check(u'${spam}bar', u'%sbar' % unonascii) |
| 232 | check(u'${%s}bar' % unonascii, u'ham%sbar' % unonascii) |
| 233 | check(u'$bar%s bar' % unonascii, u'$bar%s bar' % unonascii) |
| 234 | check(u'$spam}bar', u'%s}bar' % unonascii) |
| 235 | |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 236 | def test_abspath(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 237 | self.assertIn("foo", self.pathmodule.abspath("foo")) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 238 | |
| 239 | # Abspath returns bytes when the arg is bytes |
Ezio Melotti | 9e9af21 | 2010-02-20 22:34:21 +0000 | [diff] [blame] | 240 | for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 241 | self.assertIsInstance(self.pathmodule.abspath(path), str) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 242 | |
| 243 | def test_realpath(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 244 | self.assertIn("foo", self.pathmodule.realpath("foo")) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 245 | |
Serhiy Storchaka | 2bd8b22 | 2015-02-13 12:02:05 +0200 | [diff] [blame] | 246 | @test_support.requires_unicode |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 247 | def test_normpath_issue5827(self): |
| 248 | # Make sure normpath preserves unicode |
| 249 | for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 250 | self.assertIsInstance(self.pathmodule.normpath(path), unicode) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 251 | |
Serhiy Storchaka | 2bd8b22 | 2015-02-13 12:02:05 +0200 | [diff] [blame] | 252 | @test_support.requires_unicode |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 253 | def test_abspath_issue3426(self): |
| 254 | # Check that abspath returns unicode when the arg is unicode |
| 255 | # with both ASCII and non-ASCII cwds. |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 256 | abspath = self.pathmodule.abspath |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 257 | for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 258 | self.assertIsInstance(abspath(path), unicode) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 259 | |
| 260 | unicwd = u'\xe7w\xf0' |
| 261 | try: |
| 262 | fsencoding = test_support.TESTFN_ENCODING or "ascii" |
| 263 | unicwd.encode(fsencoding) |
| 264 | except (AttributeError, UnicodeEncodeError): |
| 265 | # FS encoding is probably ASCII |
| 266 | pass |
| 267 | else: |
| 268 | with test_support.temp_cwd(unicwd): |
| 269 | for path in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\'): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 270 | self.assertIsInstance(abspath(path), unicode) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 271 | |
Victor Stinner | 20c3258 | 2010-05-13 16:18:14 +0000 | [diff] [blame] | 272 | @unittest.skipIf(sys.platform == 'darwin', |
Victor Stinner | 3736816 | 2010-05-13 16:22:15 +0000 | [diff] [blame] | 273 | "Mac OS X denies the creation of a directory with an invalid utf8 name") |
Victor Stinner | 20c3258 | 2010-05-13 16:18:14 +0000 | [diff] [blame] | 274 | def test_nonascii_abspath(self): |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 275 | # Test non-ASCII, non-UTF8 bytes in the path. |
| 276 | with test_support.temp_cwd('\xe7w\xf0'): |
| 277 | self.test_abspath() |
Ezio Melotti | 9e9af21 | 2010-02-20 22:34:21 +0000 | [diff] [blame] | 278 | |
| 279 | |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 280 | def test_main(): |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 281 | test_support.run_unittest(GenericTest) |
| 282 | |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 283 | |
| 284 | if __name__=="__main__": |
| 285 | test_main() |