|
5 | 5 | from posixpath import realpath, abspath, dirname, basename |
6 | 6 | from test import support, test_genericpath |
7 | 7 | from test.support import FakePath |
| 8 | +from unittest import mock |
8 | 9 |
|
9 | 10 | try: |
10 | 11 | import posix |
@@ -242,42 +243,61 @@ def fake_lstat(path): |
242 | 243 | def test_expanduser(self): |
243 | 244 | self.assertEqual(posixpath.expanduser("foo"), "foo") |
244 | 245 | self.assertEqual(posixpath.expanduser(b"foo"), b"foo") |
| 246 | + |
| 247 | + def test_expanduser_home_envvar(self): |
245 | 248 | with support.EnvironmentVarGuard() as env: |
| 249 | + env['HOME'] = '/home/victor' |
| 250 | + self.assertEqual(posixpath.expanduser("~"), "/home/victor") |
| 251 | + |
| 252 | + # expanduser() strips trailing slash |
| 253 | + env['HOME'] = '/home/victor/' |
| 254 | + self.assertEqual(posixpath.expanduser("~"), "/home/victor") |
| 255 | + |
246 | 256 | for home in '/', '', '//', '///': |
247 | 257 | with self.subTest(home=home): |
248 | 258 | env['HOME'] = home |
249 | 259 | self.assertEqual(posixpath.expanduser("~"), "/") |
250 | 260 | self.assertEqual(posixpath.expanduser("~/"), "/") |
251 | 261 | self.assertEqual(posixpath.expanduser("~/foo"), "/foo") |
252 | | - try: |
253 | | - import pwd |
254 | | - except ImportError: |
255 | | - pass |
256 | | - else: |
257 | | - self.assertIsInstance(posixpath.expanduser("~/"), str) |
258 | | - self.assertIsInstance(posixpath.expanduser(b"~/"), bytes) |
259 | | - # if home directory == root directory, this test makes no sense |
260 | | - if posixpath.expanduser("~") != '/': |
261 | | - self.assertEqual( |
262 | | - posixpath.expanduser("~") + "/", |
263 | | - posixpath.expanduser("~/") |
264 | | - ) |
265 | | - self.assertEqual( |
266 | | - posixpath.expanduser(b"~") + b"/", |
267 | | - posixpath.expanduser(b"~/") |
268 | | - ) |
269 | | - self.assertIsInstance(posixpath.expanduser("~root/"), str) |
270 | | - self.assertIsInstance(posixpath.expanduser("~foo/"), str) |
271 | | - self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes) |
272 | | - self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes) |
273 | | - |
274 | | - with support.EnvironmentVarGuard() as env: |
275 | | - # expanduser should fall back to using the password database |
276 | | - del env['HOME'] |
277 | | - home = pwd.getpwuid(os.getuid()).pw_dir |
278 | | - # $HOME can end with a trailing /, so strip it (see #17809) |
279 | | - home = home.rstrip("/") or '/' |
280 | | - self.assertEqual(posixpath.expanduser("~"), home) |
| 262 | + |
| 263 | + def test_expanduser_pwd(self): |
| 264 | + pwd = support.import_module('pwd') |
| 265 | + |
| 266 | + self.assertIsInstance(posixpath.expanduser("~/"), str) |
| 267 | + self.assertIsInstance(posixpath.expanduser(b"~/"), bytes) |
| 268 | + |
| 269 | + # if home directory == root directory, this test makes no sense |
| 270 | + if posixpath.expanduser("~") != '/': |
| 271 | + self.assertEqual( |
| 272 | + posixpath.expanduser("~") + "/", |
| 273 | + posixpath.expanduser("~/") |
| 274 | + ) |
| 275 | + self.assertEqual( |
| 276 | + posixpath.expanduser(b"~") + b"/", |
| 277 | + posixpath.expanduser(b"~/") |
| 278 | + ) |
| 279 | + self.assertIsInstance(posixpath.expanduser("~root/"), str) |
| 280 | + self.assertIsInstance(posixpath.expanduser("~foo/"), str) |
| 281 | + self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes) |
| 282 | + self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes) |
| 283 | + |
| 284 | + with support.EnvironmentVarGuard() as env: |
| 285 | + # expanduser should fall back to using the password database |
| 286 | + del env['HOME'] |
| 287 | + |
| 288 | + home = pwd.getpwuid(os.getuid()).pw_dir |
| 289 | + # $HOME can end with a trailing /, so strip it (see #17809) |
| 290 | + home = home.rstrip("/") or '/' |
| 291 | + self.assertEqual(posixpath.expanduser("~"), home) |
| 292 | + |
| 293 | + # bpo-10496: If the HOME environment variable is not set and the |
| 294 | + # user (current identifier or name in the path) doesn't exist in |
| 295 | + # the password database (pwd.getuid() or pwd.getpwnam() fail), |
| 296 | + # expanduser() must return the path unchanged. |
| 297 | + with mock.patch.object(pwd, 'getpwuid', side_effect=KeyError), \ |
| 298 | + mock.patch.object(pwd, 'getpwnam', side_effect=KeyError): |
| 299 | + for path in ('~', '~/.local', '~vstinner/'): |
| 300 | + self.assertEqual(posixpath.expanduser(path), path) |
281 | 301 |
|
282 | 302 | def test_normpath(self): |
283 | 303 | self.assertEqual(posixpath.normpath(""), ".") |
|
0 commit comments