我希望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
。我得到了相同的结果。
我该如何解决?请帮助。
您可以尝试以下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]
您应该将false
参数设置为pass_filenames
,因为在其他情况下,文件将作为参数传递,并且正如您在问题中提到的,这些都是“无法识别的”参数。