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 |
| 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 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): |
| 33 | self.assertEqual( |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 34 | self.path.commonprefix([]), |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 35 | "" |
| 36 | ) |
| 37 | self.assertEqual( |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 38 | self.path.commonprefix(["/home/swenson/spam", "/home/swen/spam"]), |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 39 | "/home/swen" |
| 40 | ) |
| 41 | self.assertEqual( |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 42 | self.path.commonprefix(["/home/swen/spam", "/home/swen/eggs"]), |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 43 | "/home/swen/" |
| 44 | ) |
| 45 | self.assertEqual( |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 46 | self.path.commonprefix(["/home/swen/spam", "/home/swen/spam"]), |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 47 | "/home/swen/spam" |
| 48 | ) |
| 49 | |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 50 | 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 Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 60 | def test_getsize(self): |
| 61 | f = open(test_support.TESTFN, "wb") |
| 62 | try: |
| 63 | f.write("foo") |
| 64 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 65 | self.assertEqual(self.path.getsize(test_support.TESTFN), 3) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 66 | finally: |
| 67 | if not f.closed: |
| 68 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 69 | test_support.unlink(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 70 | |
| 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 Melotti | e3467d5 | 2010-02-20 09:40:07 +0000 | [diff] [blame] | 84 | self.assertLessEqual( |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 85 | self.path.getctime(test_support.TESTFN), |
| 86 | self.path.getmtime(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 87 | ) |
| 88 | finally: |
| 89 | if not f.closed: |
| 90 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 91 | test_support.unlink(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 92 | |
| 93 | def test_exists(self): |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 94 | self.assertIs(self.path.exists(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 95 | f = open(test_support.TESTFN, "wb") |
| 96 | try: |
| 97 | f.write("foo") |
| 98 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 99 | 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 Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 102 | finally: |
| 103 | if not f.close(): |
| 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_isdir(self): |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 108 | self.assertIs(self.path.isdir(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 | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 113 | self.assertIs(self.path.isdir(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 114 | os.remove(test_support.TESTFN) |
| 115 | os.mkdir(test_support.TESTFN) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 116 | self.assertIs(self.path.isdir(test_support.TESTFN), True) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 117 | os.rmdir(test_support.TESTFN) |
| 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) |
| 122 | safe_rmdir(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 123 | |
| 124 | def test_isfile(self): |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 125 | self.assertIs(self.path.isfile(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 126 | f = open(test_support.TESTFN, "wb") |
| 127 | try: |
| 128 | f.write("foo") |
| 129 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 130 | self.assertIs(self.path.isfile(test_support.TESTFN), True) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 131 | os.remove(test_support.TESTFN) |
| 132 | os.mkdir(test_support.TESTFN) |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 133 | self.assertIs(self.path.isfile(test_support.TESTFN), False) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 134 | os.rmdir(test_support.TESTFN) |
| 135 | finally: |
| 136 | if not f.close(): |
| 137 | f.close() |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 138 | test_support.unlink(test_support.TESTFN) |
| 139 | safe_rmdir(test_support.TESTFN) |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 140 | |
Ezio Melotti | 9e9af21 | 2010-02-20 22:34:21 +0000 | [diff] [blame] | 141 | |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 142 | class 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 Melotti | 9e9af21 | 2010-02-20 22:34:21 +0000 | [diff] [blame] | 192 | for path in ('', 'foo', 'f\xf2\xf2', '/foo', 'C:\\'): |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 193 | 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 Melotti | 9e9af21 | 2010-02-20 22:34:21 +0000 | [diff] [blame] | 224 | |
| 225 | |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 226 | def test_main(): |
Florent Xicluna | dc1531c | 2010-03-06 18:07:18 +0000 | [diff] [blame^] | 227 | test_support.run_unittest(GenericTest) |
| 228 | |
Jack Diederich | 7b60464 | 2006-08-26 18:42:06 +0000 | [diff] [blame] | 229 | |
| 230 | if __name__=="__main__": |
| 231 | test_main() |