diff --git a/README.md b/README.md index 2c461e6..140b285 100644 --- a/README.md +++ b/README.md @@ -28,3 +28,6 @@ or untracked files before they commit the code. This plugin also does not take i consideration the global `.gitignore`, something that can make git miss reporting some untracked files, the goal being to assure that when a new developer clones and runs the tests they do not endup with an unexpected git status. + +If you have any cases where you expect to have git report dirty, please +add `--allow-dirty` to the command call to disable this check. diff --git a/src/tox_extra/hooks.py b/src/tox_extra/hooks.py index 1ea49d6..840da1b 100644 --- a/src/tox_extra/hooks.py +++ b/src/tox_extra/hooks.py @@ -1,5 +1,6 @@ """Tox hook implementations.""" import os +from argparse import ArgumentParser import git @@ -26,17 +27,29 @@ def is_git_dirty(path: str) -> bool: try: # tox3 support - from tox import hookimpl + from tox import config, hookimpl from tox.reporter import error + @hookimpl + def tox_addoption(parser: config.Parser) -> None: + """Add a command line option to the tox parser.""" + parser.add_argument( + "--allow-dirty", + action="store_true", + default=False, + help="If it should allow git to report dirty after executing commands.", + ) + @hookimpl def tox_runtest_post(venv): """Hook that runs after test commands.""" - if is_git_dirty(venv.envconfig.config.toxinidir): + allow_dirty = getattr(venv.envconfig.config.option, "allow_dirty", False) + if not allow_dirty and is_git_dirty(venv.envconfig.config.toxinidir): venv.status = "failed" error(MSG_GIT_DIRTY) -except ImportError: # tox4 support +except ImportError as exc: # tox4 support + print(exc) from typing import List from tox.execute import Outcome @@ -44,11 +57,22 @@ def tox_runtest_post(venv): from tox.tox_env.api import ToxEnv from tox.tox_env.errors import Fail + @impl + def tox_add_option(parser: ArgumentParser) -> None: + """Add a command line option to the tox parser.""" + parser.add_argument( + "--allow-dirty", + action="store_true", + default=False, + help="If it should allow git to report dirty after executing commands.", + ) + @impl # pylint: disable=unused-argument def tox_after_run_commands( tox_env: ToxEnv, exit_code: int, outcomes: List[Outcome] ) -> None: """Hook that runs after test commands.""" - if is_git_dirty("."): + allow_dirty = getattr(tox_env.options, "allow_dirty", False) + if not allow_dirty and is_git_dirty("."): raise Fail(MSG_GIT_DIRTY)