Ale*_*mov 3 python pre-commit pre-commit-hook pytest pre-commit.com
在我的 Python 项目中,我pytest用作pre-commit挂钩。一些测试创建和删除临时文件,当我运行pytest <test directory>. 但是,当我运行git commit并pre-commit挂钩 triggers 时pytest,某些测试由于FileNotFoundError: [Errno 2] No such file or directory: '<file name>'. 我的印象是,当许多文件已更改且位于暂存区域时,会发生这种情况(对于 1-2 个文件,我没有观察到此问题)。这是我的pytest部分.pre-commit-config.yaml:
- repo: local
hooks:
- id: pytest-check
name: pytest-check
entry: bash -c 'pytest'
language: system
Run Code Online (Sandbox Code Playgroud)
输出如下所示:
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
Run Code Online (Sandbox Code Playgroud)
当我从控制台运行 pytest 时不会发生这种情况。
与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
Run Code Online (Sandbox Code Playgroud)
关于用 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
Run Code Online (Sandbox Code Playgroud)
有没有更好的解决方案?pylint不支持无限深度的递归,因此我需要一个 bash 命令。
谢谢!
最好的,阿列克谢
Ant*_*ile 25
显示您的输出,否则我们只能猜测问题
也就是说,您可能想要使用always_run: true并且pass_filenames: false- 而且您entry是伪造的,无需将内容包装在 bash 中,只需直接调用可执行文件即可。把所有这些放在一起:
- repo: local
hooks:
- id: pytest-check
name: pytest-check
entry: pytest
language: system
pass_filenames: false
always_run: true
Run Code Online (Sandbox Code Playgroud)
免责声明:我是 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
Run Code Online (Sandbox Code Playgroud)
即使没有运行测试或没有 Python 文件,添加该types参数也将允许您通过。pre-commit
| 归档时间: |
|
| 查看次数: |
4014 次 |
| 最近记录: |