Skip to content

Commit

Permalink
Make git dirty an error only under CI/CD pipelines (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Nov 18, 2024
1 parent b7acaeb commit e1e8f6f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
21 changes: 15 additions & 6 deletions src/tox_extra/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import logging
import os
import pathlib
import shutil
Expand All @@ -22,10 +23,14 @@
from tox.tox_env.api import ToxEnv


MSG_GIT_DIRTY = (
"exit code 1 due to 'git status -s' reporting dirty. "
"That should not happen regardless if status is passed, failed or aborted. "
"Modify .gitignore file to avoid this."
logger = logging.getLogger(__name__)
WARNING_MSG_GIT_DIRTY = (
"'git status -s' reported dirty. "
"Modify .gitignore file as this will cause an error under CI/CD pipelines."
)

ERROR_MSG_GIT_DIRTY = (
"::error title=tox-extra detected git dirty status:: " + WARNING_MSG_GIT_DIRTY
)


Expand All @@ -37,7 +42,9 @@ def is_git_dirty(path: str) -> bool:
try:
repo = git.Repo(pathlib.Path.cwd())
if repo.is_dirty(untracked_files=True):
os.system(f"{git_path} status -s") # noqa: S605
# stderr is hidden to avoid noise like occasional:
# warning: untracked cache is disabled on this system or location
os.system(f"{git_path} status -s 2>/dev/null") # noqa: S605
# We want to display long diff only on non-interactive shells,
# like CI/CD pipelines because on local shell, the user can
# decide to run it himself if the status line was not enogh.
Expand Down Expand Up @@ -95,4 +102,6 @@ def tox_after_run_commands(
"""Hook that runs after test commands."""
allow_dirty = getattr(tox_env.options, "allow_dirty", False)
if not allow_dirty and is_git_dirty("."):
raise Fail(MSG_GIT_DIRTY)
if os.environ.get("CI") == "true":
raise Fail(ERROR_MSG_GIT_DIRTY)
logger.error(WARNING_MSG_GIT_DIRTY)
5 changes: 3 additions & 2 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
from runpy import run_module
from subprocess import PIPE, check_output, run
from unittest.mock import patch

import pytest

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

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

0 comments on commit e1e8f6f

Please sign in to comment.