[英]running pytest as a pre-commit hook // no such file or directory issue
in my Python project I use pytest
as a pre-commit
hook.在我的 Python 项目中,我使用
pytest
作为pre-commit
钩子。 Some tests create and delete temporary files, everything works fine when I run pytest <test directory>
.一些测试创建和删除临时文件,当我运行
pytest <test directory>
时一切正常。 However, when I run git commit
and pre-commit
hook triggers pytest
, some tests fail because of FileNotFoundError: [Errno 2] No such file or directory: '<file name>'
.但是,当我运行
git commit
并且pre-commit
hook 触发pytest
,一些测试会因为FileNotFoundError: [Errno 2] No such file or directory: '<file name>'
失败。 I have an impression this happens when many files have been changed and are in the staging area (with 1-2 files I do not observe this issue).我的印象是,当许多文件已更改且位于暂存区域时,会发生这种情况(对于 1-2 个文件,我没有观察到此问题)。 Here is my
pytest
section from .pre-commit-config.yaml
:这是我来自
.pre-commit-config.yaml
pytest
部分:
- repo: local
hooks:
- id: pytest-check
name: pytest-check
entry: bash -c 'pytest'
language: system
the output looks as follows:输出如下所示:
pytest-check.............................................................Failed
- hook id: pytest-check
- exit code: 1
tests/utils/test_application.py F [ 91%]
tests/utils/test_image_io.py .FFF......... [100%]
==================================== ERRORS ====================================
_ ERROR at teardown of test_calling_with_nonexisting_parameter[--non_existing 1337-hm] _
def teardown_module() -> None:
> os.remove(OUTPUT_FILE)
E FileNotFoundError: [Errno 2] No such file or directory: 'output.png'
tests/bdd/step_defs/test_runner_steps.py:98: FileNotFoundError
this does not happen when I run pytest from the console.当我从控制台运行 pytest 时不会发生这种情况。
with pass_filenames: false
and always_run: true
the error does not show up any more:使用
pass_filenames: false
和always_run: true
错误不再出现:
- repo: local
hooks:
- id: pytest-check
name: pytest-check
entry: pytest
language: system
pass_filenames: false
always_run: true
with respect to wrapping things in bash, I'm still doing this for pylint
:关于在 bash 中包装东西,我仍在为
pylint
这样做:
- repo: local
hooks:
- id: pylint-check
name: pylint-check
entry: bash -c 'find . -name "*.py" | xargs pylint'
language: system
types: [python]
pass_filenames: false
always_run: true
is there any better solution for this?有没有更好的解决方案?
pylint
does not support recursion of an unlimited depth, thus I need a bash command there. pylint
不支持无限深度的递归,因此我需要一个 bash 命令。
Thanks!谢谢!
Best, Alexey最好的,阿列克谢
show your output, we can only guess at the problem otherwise显示您的输出,否则我们只能猜测问题
that said, you probably want to use always_run: true
and pass_filenames: false
-- also your entry
is bogus, no need to wrap things in bash, just call the executable directly.也就是说,您可能想使用
always_run: true
和pass_filenames: false
—— 而且您的entry
也是假的,无需将东西包装在 bash 中,只需直接调用可执行文件即可。 Putting all that together:把所有这些放在一起:
- repo: local
hooks:
- id: pytest-check
name: pytest-check
entry: pytest
language: system
pass_filenames: false
always_run: true
disclaimer: I am the author of pre-commit免责声明:我是 pre-commit 的作者
If you are running various kinds of files through pre-commit
or using hooks before you commit, you'll need to add another parameter to the .pre-commit-config.yaml
file or it will fail with an exit code of 5 (no tests ran):如果您在
pre-commit
之前通过pre-commit
或使用钩子运行各种文件,则需要向.pre-commit-config.yaml
文件添加另一个参数,否则它将失败并显示退出代码 5(不测试运行):
- repo: local
hooks:
- id: pytest-check
name: pytest-check
stages: [commit]
types: [python]
entry: pytest
language: system
pass_filenames: false
always_run: true
The addition of the types
parameter will allow you to pass pre-commit
even if no tests ran or there are no Python files.即使没有运行测试或没有 Python 文件,添加
types
参数也将允许您通过pre-commit
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.