1
1
"""Tox hook implementations."""
2
2
import os
3
+ from argparse import ArgumentParser
3
4
4
5
import git
5
6
@@ -26,29 +27,52 @@ def is_git_dirty(path: str) -> bool:
26
27
27
28
28
29
try : # tox3 support
29
- from tox import hookimpl
30
+ from tox import config , hookimpl
30
31
from tox .reporter import error
31
32
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
+
32
43
@hookimpl
33
44
def tox_runtest_post (venv ):
34
45
"""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 ):
36
48
venv .status = "failed"
37
49
error (MSG_GIT_DIRTY )
38
50
39
- except ImportError : # tox4 support
51
+ except ImportError as exc : # tox4 support
52
+ print (exc )
40
53
from typing import List
41
54
42
55
from tox .execute import Outcome
43
56
from tox .plugin import impl
44
57
from tox .tox_env .api import ToxEnv
45
58
from tox .tox_env .errors import Fail
46
59
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
+
47
70
@impl
48
71
# pylint: disable=unused-argument
49
72
def tox_after_run_commands (
50
73
tox_env : ToxEnv , exit_code : int , outcomes : List [Outcome ]
51
74
) -> None :
52
75
"""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 ("." ):
54
78
raise Fail (MSG_GIT_DIRTY )
0 commit comments