Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions scripts/drift_check/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ def _read_meta_version(repo_root: Path) -> Version:
raw = (repo_root / "STANDARDS_VERSION").read_text(encoding="utf-8").strip()
v = parse_version(raw)
if v is None:
raise SystemExit(f"meta-repo STANDARDS_VERSION is not a valid semver: {raw!r}")
print(
f"error: meta-repo STANDARDS_VERSION is not a valid semver: {raw!r}",
file=sys.stderr,
)
raise SystemExit(2)
return v


Expand Down Expand Up @@ -368,8 +372,8 @@ def main(argv: Optional[Sequence[str]] = None) -> int:

try:
meta_version = _read_meta_version(meta_repo_path)
except SystemExit:
raise
except SystemExit as exc:
return int(exc.code) if exc.code is not None else 2
except Exception as exc: # pragma: no cover - defensive
print(f"error: cannot read STANDARDS_VERSION: {exc}", file=sys.stderr)
return 2
Expand Down
42 changes: 30 additions & 12 deletions scripts/release_doc_sync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,18 @@ def sync_repo(


_SEMVER_RE = re.compile(r"^\d+\.\d+\.\d+$")
_DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")


def _arg_error(message: str) -> None:
"""Report a bad argument to stderr and exit with the tool-error code.

The calling action treats rc=0 and rc=1 as success. A string passed to
SystemExit would be printed but exit with 1, silently looking like a
successful sync that changed files.
"""
print(f"::error::{message}", file=sys.stderr)
raise SystemExit(2)


def _parse_args(argv: Optional[List[str]] = None) -> argparse.Namespace:
Expand Down Expand Up @@ -416,21 +428,24 @@ def _parse_args(argv: Optional[List[str]] = None) -> argparse.Namespace:

def _validate_args(ns: argparse.Namespace) -> None:
if not _SEMVER_RE.match(ns.plugin_version):
raise SystemExit(
f"::error::--plugin-version must be MAJOR.MINOR.PATCH; got {ns.plugin_version!r}"
_arg_error(
f"--plugin-version must be MAJOR.MINOR.PATCH; got {ns.plugin_version!r}"
)
if not _SEMVER_RE.match(ns.previous_version):
raise SystemExit(
f"::error::--previous-version must be MAJOR.MINOR.PATCH; got {ns.previous_version!r}"
_arg_error(
f"--previous-version must be MAJOR.MINOR.PATCH; got {ns.previous_version!r}"
)
if "/" not in ns.repository:
raise SystemExit(
f"::error::--repository must be owner/name; got {ns.repository!r}"
)
_arg_error(f"--repository must be owner/name; got {ns.repository!r}")
if not ns.repo_path.is_dir():
raise SystemExit(
f"::error::--repo-path does not exist or is not a directory: {ns.repo_path}"
)
_arg_error(f"--repo-path does not exist or is not a directory: {ns.repo_path}")
if ns.date is not None:
try:
if not _DATE_RE.match(ns.date):
raise ValueError
_dt.date.fromisoformat(ns.date)
except ValueError:
_arg_error(f"--date must be YYYY-MM-DD; got {ns.date!r}")


def _print_result(result: SyncResult) -> None:
Expand Down Expand Up @@ -476,8 +491,11 @@ def main(argv: Optional[List[str]] = None) -> int:
repository=ns.repository,
release_date=release_date,
)
except OSError as exc:
print(f"::error::I/O failure during sync: {exc}", file=sys.stderr)
except (OSError, ValueError) as exc:
# UnicodeDecodeError is a ValueError; an undecodable doc must be a
# tool error (2), not an uncaught traceback that exits 1 and is read
# by the action as "files changed".
print(f"::error::cannot process docs during sync: {exc}", file=sys.stderr)
return 2

_print_result(result)
Expand Down
103 changes: 103 additions & 0 deletions tests/test_issue_152_exit_codes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"""Regression tests for issue #152: tool errors must exit 2.

The release-doc-sync composite action treats rc=0 and rc=1 as success and
only fails on rc>=2. A bad argument that exits 1 is therefore reported as a
green step with no files changed. These tests pin the documented exit-code
contract in both release_doc_sync and drift_check.
"""
from __future__ import annotations

import subprocess
import sys
from pathlib import Path

import pytest

from scripts.drift_check import cli
from scripts.release_doc_sync.sync import main
from tests.conftest import FIXTURES, REPO_ROOT

SYNC_SCRIPT = REPO_ROOT / "scripts" / "release_doc_sync" / "sync.py"


def _args(repo: Path, **overrides: str) -> list[str]:
values = {
"repo-path": str(repo),
"plugin-version": "1.2.1",
"previous-version": "1.0.0",
"repository": "TMHSDigital/Docker-Developer-Tools",
}
values.update(overrides)
args: list[str] = []
for key, value in values.items():
args.extend((f"--{key}", value))
return args


@pytest.mark.parametrize(
("overrides", "message"),
[
({"plugin-version": "v1.2.1"}, "--plugin-version must be MAJOR.MINOR.PATCH"),
({"previous-version": ""}, "--previous-version must be MAJOR.MINOR.PATCH"),
({"repository": "no-slash"}, "--repository must be owner/name"),
({"date": "25-04-2026"}, "--date must be YYYY-MM-DD"),
({"date": "2026-02-30"}, "--date must be YYYY-MM-DD"),
],
)
def test_bad_argument_exits_2(
tmp_path: Path, capsys, overrides: dict[str, str], message: str
) -> None:
with pytest.raises(SystemExit) as exc:
main(_args(tmp_path, **overrides))
assert exc.value.code == 2
err = capsys.readouterr().err
assert "::error::" in err
assert message in err


def test_missing_repo_path_exits_2(tmp_path: Path, capsys) -> None:
missing = tmp_path / "no-such-dir"
with pytest.raises(SystemExit) as exc:
main(_args(missing))
assert exc.value.code == 2
assert "--repo-path does not exist" in capsys.readouterr().err


def test_bad_argument_subprocess_exit_code_is_2(tmp_path: Path) -> None:
"""The action reads the process exit code, not the in-process return."""
proc = subprocess.run(
[
sys.executable,
str(SYNC_SCRIPT),
*_args(tmp_path, **{"plugin-version": "v1.2.1"}),
],
capture_output=True,
text=True,
)
assert proc.returncode == 2
assert "::error::" in proc.stderr


def test_non_utf8_doc_exits_2(tmp_path: Path, capsys) -> None:
(tmp_path / "CHANGELOG.md").write_bytes(b"\xff\xfe not valid utf-8\n")
rc = main(_args(tmp_path))
assert rc == 2
assert "::error::" in capsys.readouterr().err


def test_malformed_standards_version_exits_2(tmp_path: Path, capsys) -> None:
meta = tmp_path / "meta"
meta.mkdir()
(meta / "STANDARDS_VERSION").write_text("not-a-semver", encoding="utf-8")
rc = cli.main(
[
"--local",
str(FIXTURES / "clean_repo"),
"--meta-repo",
str(meta),
"--config",
"nonexistent.json",
]
)
assert rc == 2
assert "not a valid semver" in capsys.readouterr().err
10 changes: 6 additions & 4 deletions tests/test_release_doc_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,25 +630,27 @@ def test_rc_0_on_second_run(self, tmp_path: Path):
assert first == 1
assert second == 0

def test_rejects_garbage_plugin_version(self, tmp_path: Path):
def test_rejects_garbage_plugin_version(self, tmp_path: Path, capsys):
with pytest.raises(SystemExit) as exc:
main([
"--repo-path", str(tmp_path),
"--plugin-version", "v1.2.1", # 'v' prefix forbidden
"--previous-version", "1.0.0",
"--repository", "TMHSDigital/X",
])
assert "must be MAJOR.MINOR.PATCH" in str(exc.value)
assert exc.value.code == 2
assert "must be MAJOR.MINOR.PATCH" in capsys.readouterr().err

def test_rejects_bad_repository(self, tmp_path: Path):
def test_rejects_bad_repository(self, tmp_path: Path, capsys):
with pytest.raises(SystemExit) as exc:
main([
"--repo-path", str(tmp_path),
"--plugin-version", "1.2.1",
"--previous-version", "1.0.0",
"--repository", "no-slash",
])
assert "owner/name" in str(exc.value)
assert exc.value.code == 2
assert "owner/name" in capsys.readouterr().err

def test_github_output_emitted(self, tmp_path: Path, monkeypatch):
(tmp_path / "CLAUDE.md").write_text(DOCKER_CLAUDE, encoding="utf-8")
Expand Down
Loading