Python 将pytest作为预提交挂钩运行//没有此类文件或目录问题

Python 将pytest作为预提交挂钩运行//没有此类文件或目录问题,python,pytest,pre-commit-hook,pre-commit,pre-commit.com,Python,Pytest,Pre Commit Hook,Pre Commit,Pre Commit.com,在我的Python项目中,我使用pytest作为pre-commit钩子。有些测试创建和删除临时文件,当我运行pytest时,一切正常。但是,当我运行git commit和pre commit钩子触发器pytest时,一些测试失败,因为FileNotFoundError:[Errno 2]没有这样的文件或目录:“”。我的印象是,当许多文件被更改并且位于暂存区域时(对于1-2个文件,我没有注意到这个问题),就会发生这种情况。这是.pre-commit config.yaml中我的pytest部分:

在我的Python项目中,我使用
pytest
作为
pre-commit
钩子。有些测试创建和删除临时文件,当我运行
pytest
时,一切正常。但是,当我运行
git commit
pre commit
钩子触发器
pytest
时,一些测试失败,因为
FileNotFoundError:[Errno 2]没有这样的文件或目录:“”
。我的印象是,当许多文件被更改并且位于暂存区域时(对于1-2个文件,我没有注意到这个问题),就会发生这种情况。这是
.pre-commit config.yaml
中我的
pytest
部分:

  - 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\u filenames:false
always\u 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命令

谢谢

最好的,
Alexey

显示您的输出,否则我们只能猜测问题

也就是说,您可能想使用,而且您的
条目是假的,不需要在bash中包装东西,只需直接调用可执行文件即可。综上所述:

-回购协议:本地
挂钩:
-id:pytest检查
名称:pytest检查
条目:pytest
语言:系统
传递文件名:false
总是跑:真的

免责声明:我是pre-commit的作者显示您的输出,否则我们只能猜测问题

也就是说,您可能想使用,而且您的
条目是假的,不需要在bash中包装东西,只需直接调用可执行文件即可。综上所述:

-回购协议:本地
挂钩:
-id:pytest检查
名称:pytest检查
条目:pytest
语言:系统
传递文件名:false
总是跑:真的

免责声明:我是pre commit的作者

感谢您的快速回复!我将在下面发布我的输出。顺便问一下,何时需要在bash中进行包装?例如,在没有bash包装器的情况下进行调用时,mypy和pylint不能作为预提交挂钩正常工作。请查看我在此页面上的输出。“不工作”不是一个有用的错误报告,请显示您所看到的内容--您应该永远不需要bash。不要发布答案,编辑您的原始问题谢谢您的快速回复!我将在下面发布我的输出。顺便问一下,何时需要在bash中进行包装?例如,在没有bash包装器的情况下进行调用时,mypy和pylint不能作为预提交挂钩正常工作。请查看我在此页面上的输出。“不工作”不是一个有用的错误报告,请显示您所看到的内容--您应该永远不需要bash。不要发布答案,编辑你的原始问题