Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 1 | """ |
| 2 | Tests common to genericpath, macpath, ntpath and posixpath |
| 3 | """ |
| 4 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5 | import genericpath |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 6 | import os |
Victor Stinner | 1efde67 | 2010-04-18 08:23:42 +0000 | [diff] [blame] | 7 | import sys |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 8 | import unittest |
| 9 | import warnings |
| 10 | from test import support |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 11 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 12 | |
| 13 | def safe_rmdir(dirname): |
| 14 | try: |
| 15 | os.rmdir(dirname) |
| 16 | except OSError: |
| 17 | pass |
| 18 | |
| 19 | |
| 20 | class GenericTest(unittest.TestCase): |
| 21 | # The path module to be tested |
| 22 | pathmodule = genericpath |
| 23 | common_attributes = ['commonprefix', 'getsize', 'getatime', 'getctime', |
| 24 | 'getmtime', 'exists', 'isdir', 'isfile'] |
| 25 | attributes = [] |
| 26 | |
| 27 | def test_no_argument(self): |
| 28 | for attr in self.common_attributes + self.attributes: |
| 29 | with self.assertRaises(TypeError): |
| 30 | getattr(self.pathmodule, attr)() |
| 31 | raise self.fail("{}.{}() did not raise a TypeError" |
| 32 | .format(self.pathmodule.__name__, attr)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 33 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 34 | def test_commonprefix(self): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 35 | commonprefix = self.pathmodule.commonprefix |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 36 | self.assertEqual( |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 37 | commonprefix([]), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 38 | "" |
| 39 | ) |
| 40 | self.assertEqual( |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 41 | commonprefix(["/home/swenson/spam", "/home/swen/spam"]), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 42 | "/home/swen" |
| 43 | ) |
| 44 | self.assertEqual( |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 45 | commonprefix(["/home/swen/spam", "/home/swen/eggs"]), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 46 | "/home/swen/" |
| 47 | ) |
| 48 | self.assertEqual( |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 49 | commonprefix(["/home/swen/spam", "/home/swen/spam"]), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 50 | "/home/swen/spam" |
| 51 | ) |
Florent Xicluna | e3ed2e0 | 2010-03-08 12:42:20 +0000 | [diff] [blame] | 52 | self.assertEqual( |
| 53 | commonprefix(["home:swenson:spam", "home:swen:spam"]), |
| 54 | "home:swen" |
| 55 | ) |
| 56 | self.assertEqual( |
| 57 | commonprefix([":home:swen:spam", ":home:swen:eggs"]), |
| 58 | ":home:swen:" |
| 59 | ) |
| 60 | self.assertEqual( |
| 61 | commonprefix([":home:swen:spam", ":home:swen:spam"]), |
| 62 | ":home:swen:spam" |
| 63 | ) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 64 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 65 | self.assertEqual( |
| 66 | commonprefix([b"/home/swenson/spam", b"/home/swen/spam"]), |
| 67 | b"/home/swen" |
| 68 | ) |
| 69 | self.assertEqual( |
| 70 | commonprefix([b"/home/swen/spam", b"/home/swen/eggs"]), |
| 71 | b"/home/swen/" |
| 72 | ) |
| 73 | self.assertEqual( |
| 74 | commonprefix([b"/home/swen/spam", b"/home/swen/spam"]), |
| 75 | b"/home/swen/spam" |
| 76 | ) |
Florent Xicluna | e3ed2e0 | 2010-03-08 12:42:20 +0000 | [diff] [blame] | 77 | self.assertEqual( |
| 78 | commonprefix([b"home:swenson:spam", b"home:swen:spam"]), |
| 79 | b"home:swen" |
| 80 | ) |
| 81 | self.assertEqual( |
| 82 | commonprefix([b":home:swen:spam", b":home:swen:eggs"]), |
| 83 | b":home:swen:" |
| 84 | ) |
| 85 | self.assertEqual( |
| 86 | commonprefix([b":home:swen:spam", b":home:swen:spam"]), |
| 87 | b":home:swen:spam" |
| 88 | ) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 89 | |
| 90 | testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', |
| 91 | 'aXc', 'abd', 'ab', 'aX', 'abcX'] |
| 92 | for s1 in testlist: |
| 93 | for s2 in testlist: |
| 94 | p = commonprefix([s1, s2]) |
| 95 | self.assertTrue(s1.startswith(p)) |
| 96 | self.assertTrue(s2.startswith(p)) |
| 97 | if s1 != s2: |
| 98 | n = len(p) |
| 99 | self.assertNotEqual(s1[n:n+1], s2[n:n+1]) |
| 100 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 101 | def test_getsize(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 102 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 103 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 104 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 105 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 106 | self.assertEqual(self.pathmodule.getsize(support.TESTFN), 3) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 107 | finally: |
| 108 | if not f.closed: |
| 109 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 110 | support.unlink(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 111 | |
| 112 | def test_time(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 113 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 114 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 115 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 116 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 117 | f = open(support.TESTFN, "ab") |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 118 | f.write(b"bar") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 119 | f.close() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 120 | f = open(support.TESTFN, "rb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 121 | d = f.read() |
| 122 | f.close() |
Guido van Rossum | dc12288 | 2007-07-10 12:09:13 +0000 | [diff] [blame] | 123 | self.assertEqual(d, b"foobar") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 124 | |
Ezio Melotti | a11865b | 2010-02-20 09:47:55 +0000 | [diff] [blame] | 125 | self.assertLessEqual( |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 126 | self.pathmodule.getctime(support.TESTFN), |
| 127 | self.pathmodule.getmtime(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 128 | ) |
| 129 | finally: |
| 130 | if not f.closed: |
| 131 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 132 | support.unlink(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 133 | |
| 134 | def test_exists(self): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 135 | self.assertIs(self.pathmodule.exists(support.TESTFN), False) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 136 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 137 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 138 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 139 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 140 | self.assertIs(self.pathmodule.exists(support.TESTFN), True) |
| 141 | if not self.pathmodule == genericpath: |
| 142 | self.assertIs(self.pathmodule.lexists(support.TESTFN), |
| 143 | True) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 144 | finally: |
| 145 | if not f.close(): |
| 146 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 147 | support.unlink(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 148 | |
Richard Oudkerk | 2240ac1 | 2012-07-06 12:05:32 +0100 | [diff] [blame^] | 149 | @unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()") |
| 150 | def test_exists_fd(self): |
| 151 | r, w = os.pipe() |
| 152 | try: |
| 153 | self.assertTrue(self.pathmodule.exists(r)) |
| 154 | finally: |
| 155 | os.close(r) |
| 156 | os.close(w) |
| 157 | self.assertFalse(self.pathmodule.exists(r)) |
| 158 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 159 | def test_isdir(self): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 160 | self.assertIs(self.pathmodule.isdir(support.TESTFN), False) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 161 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 162 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 163 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 164 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 165 | self.assertIs(self.pathmodule.isdir(support.TESTFN), False) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 166 | os.remove(support.TESTFN) |
| 167 | os.mkdir(support.TESTFN) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 168 | self.assertIs(self.pathmodule.isdir(support.TESTFN), True) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 169 | os.rmdir(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 170 | finally: |
| 171 | if not f.close(): |
| 172 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 173 | support.unlink(support.TESTFN) |
| 174 | safe_rmdir(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 175 | |
| 176 | def test_isfile(self): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 177 | self.assertIs(self.pathmodule.isfile(support.TESTFN), False) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 178 | f = open(support.TESTFN, "wb") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 179 | try: |
Guido van Rossum | 199fc75 | 2007-08-27 23:38:12 +0000 | [diff] [blame] | 180 | f.write(b"foo") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 181 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 182 | self.assertIs(self.pathmodule.isfile(support.TESTFN), True) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 183 | os.remove(support.TESTFN) |
| 184 | os.mkdir(support.TESTFN) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 185 | self.assertIs(self.pathmodule.isfile(support.TESTFN), False) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 186 | os.rmdir(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 187 | finally: |
| 188 | if not f.close(): |
| 189 | f.close() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 190 | support.unlink(support.TESTFN) |
| 191 | safe_rmdir(support.TESTFN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 192 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 193 | |
Florent Xicluna | 87082ee | 2010-08-09 17:18:05 +0000 | [diff] [blame] | 194 | # Following TestCase is not supposed to be run from test_genericpath. |
| 195 | # It is inherited by other test modules (macpath, ntpath, posixpath). |
| 196 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 197 | class CommonTest(GenericTest): |
| 198 | # The path module to be tested |
| 199 | pathmodule = None |
| 200 | common_attributes = GenericTest.common_attributes + [ |
| 201 | # Properties |
| 202 | 'curdir', 'pardir', 'extsep', 'sep', |
| 203 | 'pathsep', 'defpath', 'altsep', 'devnull', |
| 204 | # Methods |
| 205 | 'normcase', 'splitdrive', 'expandvars', 'normpath', 'abspath', |
| 206 | 'join', 'split', 'splitext', 'isabs', 'basename', 'dirname', |
| 207 | 'lexists', 'islink', 'ismount', 'expanduser', 'normpath', 'realpath', |
| 208 | ] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 209 | |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 210 | def test_normcase(self): |
Ezio Melotti | 5a3ef5b | 2010-06-25 10:56:11 +0000 | [diff] [blame] | 211 | normcase = self.pathmodule.normcase |
| 212 | # check that normcase() is idempotent |
| 213 | for p in ["FoO/./BaR", b"FoO/./BaR"]: |
| 214 | p = normcase(p) |
| 215 | self.assertEqual(p, normcase(p)) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 216 | |
Ezio Melotti | 5a3ef5b | 2010-06-25 10:56:11 +0000 | [diff] [blame] | 217 | self.assertEqual(normcase(''), '') |
| 218 | self.assertEqual(normcase(b''), b'') |
| 219 | |
| 220 | # check that normcase raises a TypeError for invalid types |
| 221 | for path in (None, True, 0, 2.5, [], bytearray(b''), {'o','o'}): |
| 222 | self.assertRaises(TypeError, normcase, path) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 223 | |
| 224 | def test_splitdrive(self): |
| 225 | # splitdrive for non-NT paths |
| 226 | splitdrive = self.pathmodule.splitdrive |
| 227 | self.assertEqual(splitdrive("/foo/bar"), ("", "/foo/bar")) |
| 228 | self.assertEqual(splitdrive("foo:bar"), ("", "foo:bar")) |
| 229 | self.assertEqual(splitdrive(":foo:bar"), ("", ":foo:bar")) |
| 230 | |
| 231 | self.assertEqual(splitdrive(b"/foo/bar"), (b"", b"/foo/bar")) |
| 232 | self.assertEqual(splitdrive(b"foo:bar"), (b"", b"foo:bar")) |
| 233 | self.assertEqual(splitdrive(b":foo:bar"), (b"", b":foo:bar")) |
| 234 | |
| 235 | def test_expandvars(self): |
| 236 | if self.pathmodule.__name__ == 'macpath': |
| 237 | self.skipTest('macpath.expandvars is a stub') |
| 238 | expandvars = self.pathmodule.expandvars |
| 239 | with support.EnvironmentVarGuard() as env: |
| 240 | env.clear() |
| 241 | env["foo"] = "bar" |
| 242 | env["{foo"] = "baz1" |
| 243 | env["{foo}"] = "baz2" |
| 244 | self.assertEqual(expandvars("foo"), "foo") |
| 245 | self.assertEqual(expandvars("$foo bar"), "bar bar") |
| 246 | self.assertEqual(expandvars("${foo}bar"), "barbar") |
| 247 | self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar") |
| 248 | self.assertEqual(expandvars("$bar bar"), "$bar bar") |
| 249 | self.assertEqual(expandvars("$?bar"), "$?bar") |
| 250 | self.assertEqual(expandvars("${foo}bar"), "barbar") |
| 251 | self.assertEqual(expandvars("$foo}bar"), "bar}bar") |
| 252 | self.assertEqual(expandvars("${foo"), "${foo") |
| 253 | self.assertEqual(expandvars("${{foo}}"), "baz1}") |
| 254 | self.assertEqual(expandvars("$foo$foo"), "barbar") |
| 255 | self.assertEqual(expandvars("$bar$bar"), "$bar$bar") |
| 256 | |
| 257 | self.assertEqual(expandvars(b"foo"), b"foo") |
| 258 | self.assertEqual(expandvars(b"$foo bar"), b"bar bar") |
| 259 | self.assertEqual(expandvars(b"${foo}bar"), b"barbar") |
| 260 | self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar") |
| 261 | self.assertEqual(expandvars(b"$bar bar"), b"$bar bar") |
| 262 | self.assertEqual(expandvars(b"$?bar"), b"$?bar") |
| 263 | self.assertEqual(expandvars(b"${foo}bar"), b"barbar") |
| 264 | self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar") |
| 265 | self.assertEqual(expandvars(b"${foo"), b"${foo") |
| 266 | self.assertEqual(expandvars(b"${{foo}}"), b"baz1}") |
| 267 | self.assertEqual(expandvars(b"$foo$foo"), b"barbar") |
| 268 | self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar") |
| 269 | |
| 270 | def test_abspath(self): |
| 271 | self.assertIn("foo", self.pathmodule.abspath("foo")) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 272 | with warnings.catch_warnings(): |
| 273 | warnings.simplefilter("ignore", DeprecationWarning) |
| 274 | self.assertIn(b"foo", self.pathmodule.abspath(b"foo")) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 275 | |
| 276 | # Abspath returns bytes when the arg is bytes |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 277 | with warnings.catch_warnings(): |
| 278 | warnings.simplefilter("ignore", DeprecationWarning) |
| 279 | for path in (b'', b'foo', b'f\xf2\xf2', b'/foo', b'C:\\'): |
| 280 | self.assertIsInstance(self.pathmodule.abspath(path), bytes) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 281 | |
| 282 | def test_realpath(self): |
| 283 | self.assertIn("foo", self.pathmodule.realpath("foo")) |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 284 | with warnings.catch_warnings(): |
| 285 | warnings.simplefilter("ignore", DeprecationWarning) |
| 286 | self.assertIn(b"foo", self.pathmodule.realpath(b"foo")) |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 287 | |
| 288 | def test_normpath_issue5827(self): |
| 289 | # Make sure normpath preserves unicode |
| 290 | for path in ('', '.', '/', '\\', '///foo/.//bar//'): |
| 291 | self.assertIsInstance(self.pathmodule.normpath(path), str) |
| 292 | |
| 293 | def test_abspath_issue3426(self): |
| 294 | # Check that abspath returns unicode when the arg is unicode |
| 295 | # with both ASCII and non-ASCII cwds. |
| 296 | abspath = self.pathmodule.abspath |
| 297 | for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): |
| 298 | self.assertIsInstance(abspath(path), str) |
| 299 | |
| 300 | unicwd = '\xe7w\xf0' |
| 301 | try: |
| 302 | fsencoding = support.TESTFN_ENCODING or "ascii" |
| 303 | unicwd.encode(fsencoding) |
| 304 | except (AttributeError, UnicodeEncodeError): |
| 305 | # FS encoding is probably ASCII |
| 306 | pass |
| 307 | else: |
| 308 | with support.temp_cwd(unicwd): |
| 309 | for path in ('', 'fuu', 'f\xf9\xf9', '/fuu', 'U:\\'): |
| 310 | self.assertIsInstance(abspath(path), str) |
| 311 | |
Victor Stinner | 1efde67 | 2010-04-18 08:23:42 +0000 | [diff] [blame] | 312 | @unittest.skipIf(sys.platform == 'darwin', |
R. David Murray | b6f916d | 2010-04-22 01:49:37 +0000 | [diff] [blame] | 313 | "Mac OS X denies the creation of a directory with an invalid utf8 name") |
Victor Stinner | 1efde67 | 2010-04-18 08:23:42 +0000 | [diff] [blame] | 314 | def test_nonascii_abspath(self): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 315 | # Test non-ASCII, non-UTF8 bytes in the path. |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 316 | with warnings.catch_warnings(): |
| 317 | warnings.simplefilter("ignore", DeprecationWarning) |
| 318 | with support.temp_cwd(b'\xe7w\xf0'): |
| 319 | self.test_abspath() |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 320 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 321 | |
| 322 | def test_main(): |
Florent Xicluna | c9c7978 | 2010-03-08 12:24:53 +0000 | [diff] [blame] | 323 | support.run_unittest(GenericTest) |
| 324 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 325 | |
| 326 | if __name__=="__main__": |
| 327 | test_main() |