Skip to content

Commit e1e8f6f

Browse files
authored
Make git dirty an error only under CI/CD pipelines (#120)
1 parent b7acaeb commit e1e8f6f

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/tox_extra/hooks.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import logging
56
import os
67
import pathlib
78
import shutil
@@ -22,10 +23,14 @@
2223
from tox.tox_env.api import ToxEnv
2324

2425

25-
MSG_GIT_DIRTY = (
26-
"exit code 1 due to 'git status -s' reporting dirty. "
27-
"That should not happen regardless if status is passed, failed or aborted. "
28-
"Modify .gitignore file to avoid this."
26+
logger = logging.getLogger(__name__)
27+
WARNING_MSG_GIT_DIRTY = (
28+
"'git status -s' reported dirty. "
29+
"Modify .gitignore file as this will cause an error under CI/CD pipelines."
30+
)
31+
32+
ERROR_MSG_GIT_DIRTY = (
33+
"::error title=tox-extra detected git dirty status:: " + WARNING_MSG_GIT_DIRTY
2934
)
3035

3136

@@ -37,7 +42,9 @@ def is_git_dirty(path: str) -> bool:
3742
try:
3843
repo = git.Repo(pathlib.Path.cwd())
3944
if repo.is_dirty(untracked_files=True):
40-
os.system(f"{git_path} status -s") # noqa: S605
45+
# stderr is hidden to avoid noise like occasional:
46+
# warning: untracked cache is disabled on this system or location
47+
os.system(f"{git_path} status -s 2>/dev/null") # noqa: S605
4148
# We want to display long diff only on non-interactive shells,
4249
# like CI/CD pipelines because on local shell, the user can
4350
# decide to run it himself if the status line was not enogh.
@@ -95,4 +102,6 @@ def tox_after_run_commands(
95102
"""Hook that runs after test commands."""
96103
allow_dirty = getattr(tox_env.options, "allow_dirty", False)
97104
if not allow_dirty and is_git_dirty("."):
98-
raise Fail(MSG_GIT_DIRTY)
105+
if os.environ.get("CI") == "true":
106+
raise Fail(ERROR_MSG_GIT_DIRTY)
107+
logger.error(WARNING_MSG_GIT_DIRTY)

tests/test_plugin.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55
from runpy import run_module
66
from subprocess import PIPE, check_output, run
7+
from unittest.mock import patch
78

89
import pytest
910

@@ -75,7 +76,7 @@ def test_fail_if_dirty(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
7576
# check tox is failing while dirty
7677
# We use runpy to call tox in order to assure that coverage happens, as
7778
# running a subprocess would prevent it from working.
78-
with pytest.raises(SystemExit) as exc:
79+
with patch.dict("os.environ", {"CI": "true"}), pytest.raises(SystemExit) as exc:
7980
run_module("tox", run_name="__main__", alter_sys=True)
8081
assert exc.type is SystemExit
8182
assert exc.value.code == 1
@@ -85,7 +86,7 @@ def test_fail_if_dirty(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
8586
run("git commit -m 'Add untracked files'", shell=True, check=True)
8687

8788
# check that tox is now passing
88-
with pytest.raises(SystemExit) as exc:
89+
with patch.dict("os.environ", {"CI": "true"}), pytest.raises(SystemExit) as exc:
8990
run_module("tox", run_name="__main__", alter_sys=True)
9091
assert exc.type is SystemExit
9192
assert exc.value.code == 0

0 commit comments

Comments
 (0)