test_posixpath.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:python2-tracer 作者: extremecoders-re 项目源码 文件源码
def test_expanduser(self):
        self.assertEqual(posixpath.expanduser("foo"), "foo")
        with test_support.EnvironmentVarGuard() as env:
            for home in '/', '', '//', '///':
                env['HOME'] = home
                self.assertEqual(posixpath.expanduser("~"), "/")
                self.assertEqual(posixpath.expanduser("~/"), "/")
                self.assertEqual(posixpath.expanduser("~/foo"), "/foo")
        try:
            import pwd
        except ImportError:
            pass
        else:
            self.assertIsInstance(posixpath.expanduser("~/"), basestring)
            # if home directory == root directory, this test makes no sense
            if posixpath.expanduser("~") != '/':
                self.assertEqual(
                    posixpath.expanduser("~") + "/",
                    posixpath.expanduser("~/")
                )
            self.assertIsInstance(posixpath.expanduser("~root/"), basestring)
            self.assertIsInstance(posixpath.expanduser("~foo/"), basestring)

            with test_support.EnvironmentVarGuard() as env:
                # expanduser should fall back to using the password database
                del env['HOME']
                home = pwd.getpwuid(os.getuid()).pw_dir
                # $HOME can end with a trailing /, so strip it (see #17809)
                home = home.rstrip("/") or '/'
                self.assertEqual(posixpath.expanduser("~"), home)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号