MCPcopy Index your code
hub / github.com/python/cpython / test_expanduser_pwd

Method test_expanduser_pwd

Lib/test/test_posixpath.py:322–359  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

320 @unittest.skipIf(sys.platform == "vxworks",
321 "no home directory on VxWorks")
322 def test_expanduser_pwd(self):
323 pwd = import_helper.import_module('pwd')
324
325 self.assertIsInstance(posixpath.expanduser("~/"), str)
326 self.assertIsInstance(posixpath.expanduser(b"~/"), bytes)
327
328 # if home directory == root directory, this test makes no sense
329 if posixpath.expanduser("~") != '/':
330 self.assertEqual(
331 posixpath.expanduser("~") + "/",
332 posixpath.expanduser("~/")
333 )
334 self.assertEqual(
335 posixpath.expanduser(b"~") + b"/",
336 posixpath.expanduser(b"~/")
337 )
338 self.assertIsInstance(posixpath.expanduser("~root/"), str)
339 self.assertIsInstance(posixpath.expanduser("~foo/"), str)
340 self.assertIsInstance(posixpath.expanduser(b"~root/"), bytes)
341 self.assertIsInstance(posixpath.expanduser(b"~foo/"), bytes)
342
343 with os_helper.EnvironmentVarGuard() as env:
344 # expanduser should fall back to using the password database
345 del env['HOME']
346
347 home = pwd.getpwuid(os.getuid()).pw_dir
348 # $HOME can end with a trailing /, so strip it (see #17809)
349 home = home.rstrip("/") or '/'
350 self.assertEqual(posixpath.expanduser("~"), home)
351
352 # bpo-10496: If the HOME environment variable is not set and the
353 # user (current identifier or name in the path) doesn't exist in
354 # the password database (pwd.getuid() or pwd.getpwnam() fail),
355 # expanduser() must return the path unchanged.
356 with mock.patch.object(pwd, 'getpwuid', side_effect=KeyError), \
357 mock.patch.object(pwd, 'getpwnam', side_effect=KeyError):
358 for path in ('~', '~/.local', '~vstinner/'):
359 self.assertEqual(posixpath.expanduser(path), path)
360
361 @unittest.skipIf(sys.platform == "vxworks",
362 "no home directory on VxWorks")

Callers

nothing calls this directly

Calls 5

assertIsInstanceMethod · 0.80
expanduserMethod · 0.80
import_moduleMethod · 0.45
assertEqualMethod · 0.45
rstripMethod · 0.45

Tested by

no test coverage detected