0

Everything is inside a docker container. Tox works fine separately inside docker. What I'm trying to do is to add Tox to the pre-commit hook to run it for files with changes to be committed.

.pre-commit-config.yaml

-   repo: local
    hooks:
    -   id: tox-project
        name: tox
        entry: tox
        language: system
        types: [python]
        args: [-c, path_inside_docker/tox.ini,--workdir=path_inside_docker, -e, "fix,pylint,[other_tests]"]

When tox runs as a part of a pre-commit hook (defined as above), it generates an error that refers to the file with changes that I want to commit:

tox......................................................................Failed
- hook id: tox-project
- exit code: 2

usage: tox [-h] [--colored {yes,no}] [-v | -q] [--exit-and-dump-after seconds] [-c file] 
[--workdir dir] [--root dir] [--runner {virtualenv}] [--version] [--no-provision [REQ_JSON]] 
[--no-recreate-provision] [-r] [-x OVERRIDE]
{run,r,run-parallel,p,depends,de,list,l,devenv,d,config,c,quickstart,q,exec,e,legacy,
le} ... 

tox: error: unrecognized arguments: path/to/file/to/be/commited/main.py hint: if you tried to pass 
    arguments to a command use -- to separate them from tox ones

What I tried:

1st solution. To add "--" to args for tox, to pass files from commit. Which I found here: Add -- as argument for tox

args: [-c, path_inside_docker/tox.ini,--workdir=path_inside_docker, -e,"fix,pylint,[other_tests]",--]

Now tox runs as a hook, but it doesn't work properly. Hook pass tox with success, and commit is done. But the tox is not generating any results as supposed to (when the same file is tested by a separate tox outside hook, it generates a list of errors).

2nd solution. Turn off passing filenames.

I've found this solution for a similar issue, solved by: pass_filenames: false. I tried this as well. But then tox is (as I understand) not getting any files from the commit to process and finish everything with success - again not generating any results that it suppose to generate.

Any ideas?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
QbS
  • 425
  • 1
  • 4
  • 17
  • 1
    Your setup is highly uncommon. Both we, the tox developers, and the pre-commit developer, and many, many other projects, use tox to drive pre-commit, not the other way round. See e.g. https://github.com/pre-commit/pre-commit/blob/b474a83463c4cd3ab228a145e9d6ab65e6073337/tox.ini#L12-L15 – Jürgen Gmach Dec 14 '22 at 12:49
  • 1
    running tests via `pre-commit` is a bad idea -- it's too slow and will lead to your contributors being frustrated and turning off the hooks entirely. that said -- you're looking for the [`pass_filenames`'](https://pre-commit.com/#hooks-pass_filenames) option if you want to continue to force through the bad idea – anthony sottile Dec 14 '22 at 17:06
  • @anthonysottile thanks! Indeed 'pass_filenames: False' solved everything. – QbS Jan 11 '23 at 13:35

0 Answers0