Pytest tests cannot load global config that definitely exists even though real use of the library can
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 1.7k
- 派生
- 408
- 平均合并
- 2 天 57 分钟
- 30 天内合并 PR
- 7
描述
While working on and writing tests for #1466, I discovered that Config.get_global_config raised an IOError in my test. Confused as to why that didn't break other tests, I searched for use of this in the tests and found this:
def test_global_config() -> None:
try:
assert Config.get_global_config() is not None
except IOError:
# There is no user config
pass
def test_system_config() -> None:
try:
assert Config.get_system_config() is not None
except IOError:
# There is no system config
pass
I changed these tests to this so that these situations result in an official "skip" with information about why it was skipped, instead of just silently fake-passing:
def test_global_config() -> None:
try:
assert Config.get_global_config() is not None
except IOError as e:
settings = Settings()
pytest.skip(f'Unavailable for testing with home dir = {settings.homedir}: {e}')
def test_system_config() -> None:
try:
assert Config.get_system_config() is not None
except IOError as e:
pytest.skip(f'Unavailable for testing: {e}')
But the resulting pytest output puzzled me:
test/test_config.py::test_global_config SKIPPED (Unavailable for testing with home dir = /Users/nicholas: the global file '.gitconfig' doesn't exist:)
test/test_config.py::test_system_config SKIPPED (Unavailable for testing: the system file 'gitconfig' doesn't exist:)
The first reason it puzzled me is that /Users/nicholas/.gitconfig definitely exists:
$ ls -al /Users/nicholas/.gitconfig
-rw-r--r-- 1 nicholas staff 366 Apr 9 17:39 /Users/nicholas/.gitconfig
The second reason it puzzled me is that I don't encounter this error if I simply use the library in a Python shell prompt:
>>> c = pygit2.Config.get_global_config()
>>> c["push.gpgSign"]
'if-asked'
Now, it's correct that my system config file does not exist:
>>> pygit2.Config.get_system_config()
Traceback (most recent call last):
File "<python-input-8>", line 1, in <module>
pygit2.Config.get_system_config()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/Users/nicholas/Development/OSS/libgit2/pygit2/.venv/lib/python3.14/site-packages/pygit2/config.py", line 309, in get_system_config
check_error(err)
~~~~^^^^^
File "/Users/nicholas/Development/OSS/libgit2/pygit2/.venv/lib/python3.14/site-packages/pygit2/config.py", line 300, in _from_found_config
File "/Users/nicholas/Development/OSS/libgit2/pygit2/.venv/lib/python3.14/site-packages/pygit2/errors.py", line 56, in check_error
raise IOError(message)
OSError: the system file 'gitconfig' doesn't exist: No such file or directory
But note that that error message is slightly different from the skip message of the test: "No such file or directory" is missing in the skip message. Weird?
If I now manually create /etc/gitconfig:
>>> c = pygit2.Config.get_system_config()
>>> c["bogus.foo"]
'bar'
But, low and behold, that test, also, still skips with the same partial message about not finding the system config file.
So, these two functions do work ... just not in tests. I've been unable to figure out why. My best guess is that mypy is somehow sandboxed so that it can't read these files? But that's just a wild guess. I can't confirm it.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 test/test_config.py 以及 pygit2/config.py 中所示的 Config.get_global_config 和 Config.get_system_config 路径开始。在 pytest 下重现被跳过的测试,并将它们对配置文件的可见性和错误消息与交互式 Python 示例进行比较。完成的标准是:测试不再默默地错误报告现有配置文件,或者明确记录并报告环境限制。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- git, python
- 领域
- testing-qa
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 48/100