4

I wish pre-commit to run the tests before committing my code.
The command python -m unittest discover is working in the command line.

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

OK

But when trying to commit I am getting

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

Here is my .pre-commit-config.yaml file.

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

Also for language I try to use system. I got the same result.

How can I solve this? Please help.

shay te
  • 1,028
  • 3
  • 12
  • 23
  • What program are you using to run this pre-commit hook? – magikid Dec 16 '19 at 14:35
  • @magikid what do you mean by what program ? – shay te Dec 17 '19 at 09:54
  • git doesn't natively understand your `.pre-commit-config.yaml` file. All git understands are scripts in the `.git/hooks/` folder. It looks like the program that uses this file is the [pre-commit](https://pre-commit.com/) program. – magikid Dec 19 '19 at 17:03

2 Answers2

9

You can try the following YAML. Of course, you should change the pattern in args option if you are using different one.

-   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]

You should set to false the pass_filenames parameter because in other case the files will be passed as arguments and as you mentioned in your question these are "unrecognized" parameters.

milanbalazs
  • 4,811
  • 4
  • 23
  • 45
2

The accepted answer won't work if your application depends on some packages. In this case, the language:python setting lets pre-commit use a custom virtual environment (within the ~/.cache folder), that you can add dependencies using the additional-dependencies flag. However this usually means copying the contents of 'requirements.txt`

So in order to use your application's virtual environment, you have to set the language:system setting and put your tests into the repo: local. Then the virtual environment that is activated when running the pre-commit hooks is used.

A working configuration would look like this:

- repo: local
    hooks:
      - id: unittests
        name: run unit tests
        entry: python -m unittest
        language: system
        pass_filenames: false
        args: ["discover"]
Flo Win
  • 154
  • 10