Skip to content

Commit dbf27dd

Browse files
authored
Add --allow-dirty option (#41)
1 parent 18222ce commit dbf27dd

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ or untracked files before they commit the code. This plugin also does not take i
2828
consideration the global `.gitignore`, something that can make git miss reporting
2929
some untracked files, the goal being to assure that when a new developer clones and
3030
runs the tests they do not endup with an unexpected git status.
31+
32+
If you have any cases where you expect to have git report dirty, please
33+
add `--allow-dirty` to the command call to disable this check.

src/tox_extra/hooks.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tox hook implementations."""
22
import os
3+
from argparse import ArgumentParser
34

45
import git
56

@@ -26,29 +27,52 @@ def is_git_dirty(path: str) -> bool:
2627

2728

2829
try: # tox3 support
29-
from tox import hookimpl
30+
from tox import config, hookimpl
3031
from tox.reporter import error
3132

33+
@hookimpl
34+
def tox_addoption(parser: config.Parser) -> None:
35+
"""Add a command line option to the tox parser."""
36+
parser.add_argument(
37+
"--allow-dirty",
38+
action="store_true",
39+
default=False,
40+
help="If it should allow git to report dirty after executing commands.",
41+
)
42+
3243
@hookimpl
3344
def tox_runtest_post(venv):
3445
"""Hook that runs after test commands."""
35-
if is_git_dirty(venv.envconfig.config.toxinidir):
46+
allow_dirty = getattr(venv.envconfig.config.option, "allow_dirty", False)
47+
if not allow_dirty and is_git_dirty(venv.envconfig.config.toxinidir):
3648
venv.status = "failed"
3749
error(MSG_GIT_DIRTY)
3850

39-
except ImportError: # tox4 support
51+
except ImportError as exc: # tox4 support
52+
print(exc)
4053
from typing import List
4154

4255
from tox.execute import Outcome
4356
from tox.plugin import impl
4457
from tox.tox_env.api import ToxEnv
4558
from tox.tox_env.errors import Fail
4659

60+
@impl
61+
def tox_add_option(parser: ArgumentParser) -> None:
62+
"""Add a command line option to the tox parser."""
63+
parser.add_argument(
64+
"--allow-dirty",
65+
action="store_true",
66+
default=False,
67+
help="If it should allow git to report dirty after executing commands.",
68+
)
69+
4770
@impl
4871
# pylint: disable=unused-argument
4972
def tox_after_run_commands(
5073
tox_env: ToxEnv, exit_code: int, outcomes: List[Outcome]
5174
) -> None:
5275
"""Hook that runs after test commands."""
53-
if is_git_dirty("."):
76+
allow_dirty = getattr(tox_env.options, "allow_dirty", False)
77+
if not allow_dirty and is_git_dirty("."):
5478
raise Fail(MSG_GIT_DIRTY)

0 commit comments

Comments
 (0)