3

我希望pre-commit在提交代码之前运行测试。
该命令python -m unittest discover在命令行中运行。

D:\project_dir>python -m unittest discover
...
...
...
----------------------------------------------------------------------
Ran 5 tests in 6.743s

OK

但是当我试图提交时,我得到了

D:\project_dir>git commit -m "fix tests with hook"
run tests................................................................Failed
hookid: tests

usage: python.exe -m unittest discover [-h] [-v] [-q] [--locals] [-f] [-c]
                                       [-b] [-k TESTNAMEPATTERNS] [-s START]
                                       [-p PATTERN] [-t TOP]
python.exe -m unittest discover: error: unrecognized arguments: bigpipe_response/processors_manager.py
usage: python.exe -m unittest discover [-h] [-v] [-q] [--locals] [-f] [-c]
                                       [-b] [-k TESTNAMEPATTERNS] [-s START]
                                       [-p PATTERN] [-t TOP]
python.exe -m unittest discover: error: unrecognized arguments: tests/test_processors.py

这是我的.pre-commit-config.yaml文件。

-   repo: local
    hooks:
    -   id: tests
        name: run tests
        entry: python -m unittest discover
        language: python
        types: [python]
        stages: [commit]

另外对于我尝试使用的语言system。我得到了同样的结果。

我该如何解决这个问题?请帮忙。

4

1 回答 1

6

您可以尝试以下 YAML。当然,args如果您使用不同的模式,您应该更改选项中的模式。

-   id: unittest
    name: unittest
    entry: python -m unittest discover 
    language: python
    'types': [python]
    args: ["-p '*test.py'"] # Probably this option is absolutely not needed.
    pass_filenames: false
    stages: [commit]

您应该设置参数falsepass_filenames因为在其他情况下,文件将作为参数传递,并且正如您在问题中提到的那样,这些是“无法识别的”参数。

于 2019-12-16T15:16:59.540 回答