Skip to content

Commit

Permalink
Add --allow-dirty option (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Apr 11, 2022
1 parent 18222ce commit dbf27dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
32 changes: 28 additions & 4 deletions src/tox_extra/hooks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tox hook implementations."""
import os
from argparse import ArgumentParser

import git

Expand All @@ -26,29 +27,52 @@ 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
from tox.plugin import impl
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)

0 comments on commit dbf27dd

Please sign in to comment.