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 |
| 9 | |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 10 | |
| 11 | def safe_rmdir(dirname): |
| 12 | try: |
| 13 | os.rmdir(dirname) |
| 14 | except OSError: |
| 15 | pass |
| 16 | |
| 17 | |
| 18 | class GenericTest(unittest.TestCase): |
| 19 | # The path module to be tested |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 20 | pathmodule = genericpath |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 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): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 28 | getattr(self.pathmodule, attr)() |
| 29 | raise self.fail("{}.{}() did not raise a TypeError" |
| 30 | .format(self.pathmodule.__name__, attr)) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 31 | |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 32 | def test_commonprefix(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 33 | commonprefix = self.pathmodule.commonprefix |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 34 | self.assertEqual( |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 35 | commonprefix([]), |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 36 | "" |
| 37 | ) |
| 38 | self.assertEqual( |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 39 | commonprefix(["/home/swenson/spam", "/home/swen/spam"]), |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 40 | "/home/swen" |
| 41 | ) |
| 42 | self.assertEqual( |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 43 | commonprefix(["/home/swen/spam", "/home/swen/eggs"]), |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 44 | "/home/swen/" |
| 45 | ) |
| 46 | self.assertEqual( |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 47 | commonprefix(["/home/swen/spam", "/home/swen/spam"]), |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 48 | "/home/swen/spam" |
| 49 | ) |
Florent Xicluna | 6f682be | 2010-03-08 12:39:35 +0000 | [diff] [blame] | 50 | 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 Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 62 | |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 63 | testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', |
| 64 | 'aXc', 'abd', 'ab', 'aX', 'abcX'] |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 65 | for s1 in testlist: |
| 66 | for s2 in testlist: |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 67 | p = commonprefix([s1, s2]) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 68 | 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 Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 74 | def test_getsize(self): |
| 75 | f = open(test_support.TESTFN, "wb") |
| 76 | try: |
| 77 | f.write("foo") |
| 78 | f.close() |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 79 | self.assertEqual(self.pathmodule.getsize(test_support.TESTFN), 3) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 80 | finally: |
| 81 | if not f.closed: |
| 82 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 83 | test_support.unlink(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 84 | |
| 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 Melotti | e3467d5 | 2010-02-20 09:40:07 +0000 | [diff] [blame] | 98 | self.assertLessEqual( |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 99 | self.pathmodule.getctime(test_support.TESTFN), |
| 100 | self.pathmodule.getmtime(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 101 | ) |
| 102 | finally: |
| 103 | if not f.closed: |
| 104 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 105 | test_support.unlink(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 106 | |
| 107 | def test_exists(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 108 | self.assertIs(self.pathmodule.exists(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 109 | f = open(test_support.TESTFN, "wb") |
| 110 | try: |
| 111 | f.write("foo") |
| 112 | f.close() |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 113 | 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 Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 117 | finally: |
| 118 | if not f.close(): |
| 119 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 120 | test_support.unlink(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 121 | |
| 122 | def test_isdir(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 123 | self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 124 | f = open(test_support.TESTFN, "wb") |
| 125 | try: |
| 126 | f.write("foo") |
| 127 | f.close() |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 128 | self.assertIs(self.pathmodule.isdir(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 129 | os.remove(test_support.TESTFN) |
| 130 | os.mkdir(test_support.TESTFN) |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 131 | self.assertIs(self.pathmodule.isdir(test_support.TESTFN), True) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 132 | os.rmdir(test_support.TESTFN) |
| 133 | finally: |
| 134 | if not f.close(): |
| 135 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 136 | test_support.unlink(test_support.TESTFN) |
| 137 | safe_rmdir(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 138 | |
| 139 | def test_isfile(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 140 | self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 141 | f = open(test_support.TESTFN, "wb") |
| 142 | try: |
| 143 | f.write("foo") |
| 144 | f.close() |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 145 | self.assertIs(self.pathmodule.isfile(test_support.TESTFN), True) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 146 | os.remove(test_support.TESTFN) |
| 147 | os.mkdir(test_support.TESTFN) |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 148 | self.assertIs(self.pathmodule.isfile(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 149 | os.rmdir(test_support.TESTFN) |
| 150 | finally: |
| 151 | if not f.close(): |
| 152 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 153 | test_support.unlink(test_support.TESTFN) |
| 154 | safe_rmdir(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 155 | |
Ezio Melotti | 9e9af21 | 2010-02-20 22:34:21 +0000 | [diff] [blame] | 156 | |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 157 | class CommonTest(GenericTest): |
| 158 | # The path module to be tested |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 159 | pathmodule = None |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 160 | 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 Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 173 | p = self.pathmodule.normcase(p) |
| 174 | self.assertEqual(p, self.pathmodule.normcase(p)) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 175 | |
| 176 | def test_splitdrive(self): |
| 177 | # splitdrive for non-NT paths |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 178 | 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 Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 182 | |
| 183 | def test_expandvars(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 184 | if self.pathmodule.__name__ == 'macpath': |
| 185 | self.skipTest('macpath.expandvars is a stub') |
| 186 | expandvars = self.pathmodule.expandvars |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 187 | with test_support.EnvironmentVarGuard() as env: |
| 188 | env.clear() |
| 189 | env["foo"] = "bar" |
| 190 | env["{foo"] = "baz1" |
| 191 | env["{foo}"] = "baz2" |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 192 | 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 Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 204 | |
| 205 | def test_abspath(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 206 | self.assertIn("foo", self.pathmodule.abspath("foo")) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 207 | |
| 208 | # Abspath returns bytes when the arg is bytes |
Ezio Melotti | 9e9af21 | 2010-02-20 22:34:21 +0000 | [diff] [blame] | 209 | for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 210 | self.assertIsInstance(self.pathmodule.abspath(path), str) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 211 | |
| 212 | def test_realpath(self): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 213 | self.assertIn("foo", self.pathmodule.realpath("foo")) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 214 | |
| 215 | def test_normpath_issue5827(self): |
| 216 | # Make sure normpath preserves unicode |
| 217 | for path in (u'', u'.', u'/', u'\\', u'///foo/.//bar//'): |
Florent Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 218 | self.assertIsInstance(self.pathmodule.normpath(path), unicode) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 219 | |
| 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 Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 223 | abspath = self.pathmodule.abspath |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 224 | 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] | 225 | self.assertIsInstance(abspath(path), unicode) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 226 | |
| 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 Xicluna | 985478d | 2010-03-06 18:52:52 +0000 | [diff] [blame] | 237 | self.assertIsInstance(abspath(path), unicode) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 238 | |
| 239 | # Test non-ASCII, non-UTF8 bytes in the path. |
| 240 | with test_support.temp_cwd('\xe7w\xf0'): |
| 241 | self.test_abspath() |
Ezio Melotti | 9e9af21 | 2010-02-20 22:34:21 +0000 | [diff] [blame] | 242 | |
| 243 | |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 244 | def test_main(): |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame] | 245 | test_support.run_unittest(GenericTest) |
| 246 | |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 247 | |
| 248 | if __name__=="__main__": |
| 249 | test_main() |