python - 运行 pytest 作为预提交钩子 // 没有这样的文件或目录问题
问题描述
在我的 Python 项目中,我将pytest
其用作pre-commit
挂钩。一些测试创建和删除临时文件,当我运行pytest <test directory>
. 但是,当我运行git commit
并pre-commit
挂钩触发器时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
输出如下所示:
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
当我从控制台运行 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
关于在 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
有没有更好的解决方案?pylint
不支持无限深度的递归,因此我需要一个 bash 命令。
谢谢!
最好的,阿列克谢
解决方案
显示你的输出,否则我们只能猜测问题
也就是说,您可能想要使用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
免责声明:我是 pre-commit 的作者
推荐阅读
- vuejs2 - 如何将数据值放入链接
- django - 如何在 django 模型中自动赋值
- javascript - 位置:修复了不能使用 scrollIntoView()
- reactjs - 使用 React 在从 mongoDB 加载的 html 中插入 javascript
- unity3d - 为 Unity 制作动画和创建 3D 模型的软件
- javascript - 在我的 javascript 文件中插入 Google 分析脚本
- parallel-processing - 消息排序和并行处理apache骆驼
- reactjs - 使用 Material-UI-Next 反应 Dropbox 组件
- python - json到数据框:字典的嵌套列表
- google-apps-script - 如何让“来自表单”事件显示在触发器中?