Skip to content
Merged
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
27 changes: 18 additions & 9 deletions .claude/skills/review-issue/scripts/check_issue_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from __future__ import annotations

import argparse
import contextlib
import json
import re
import subprocess
Expand Down Expand Up @@ -1018,7 +1019,7 @@ def result_to_dict(result: CheckerResult) -> dict:
return {
"verdict": result.verdict,
"issue_kind": result.issue_kind,
"generated_at": datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
"generated_at": datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"deterministic_checks": {
"C1_required_sections": result.deterministic_checks.C1_required_sections,
"C2_stop_conditions_6": result.deterministic_checks.C2_stop_conditions_6,
Expand Down Expand Up @@ -1056,17 +1057,25 @@ def main() -> None:
print("ERROR: --issue には --repo が必要です", file=sys.stderr)
sys.exit(2)

if args.file:
body, labels, title = load_fixture_file(args.file)
else:
body, labels, title = fetch_issue_body(args.issue, args.repo)

result = run_checks(body, labels, title, args.vc_preflight_json)
output = result_to_dict(result)

if args.json:
# In --json mode: redirect stdout to stderr during fetch/check so that any
# incidental stdout (e.g. from sub-libraries) does not pollute the JSON output.
with contextlib.redirect_stdout(sys.stderr):
if args.file:
body, labels, title = load_fixture_file(args.file)
else:
body, labels, title = fetch_issue_body(args.issue, args.repo)
result = run_checks(body, labels, title, args.vc_preflight_json)
output = result_to_dict(result)
# Emit JSON exclusively to stdout (after redirect is restored)
print(json.dumps(output, ensure_ascii=False, indent=2))
else:
if args.file:
body, labels, title = load_fixture_file(args.file)
else:
body, labels, title = fetch_issue_body(args.issue, args.repo)
result = run_checks(body, labels, title, args.vc_preflight_json)
output = result_to_dict(result)
print(f"verdict: {result.verdict}")
print(f"issue_kind: {result.issue_kind}")
print()
Expand Down
93 changes: 93 additions & 0 deletions .claude/skills/review-issue/tests/test_check_issue_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,96 @@ def test_shell_false_enforced(self):
"contract_readiness_check.py must not use shell=True. "
"Existing shell=False convention must be preserved."
)


class TestJsonStdoutPurity:
"""GIVEN --json flag WHEN checker runs THEN stdout contains only valid JSON (AC1/AC3: #574)."""

def _run_raw(self, fixture_name: str, extra_env: dict | None = None) -> subprocess.CompletedProcess:
"""Run checker with --json and return raw CompletedProcess (stdout/stderr unmodified)."""
import os
fixture_path = FIXTURES_DIR / fixture_name
assert fixture_path.exists(), f"Fixture file not found: {fixture_path}"

env = os.environ.copy()
if extra_env:
env.update(extra_env)

return subprocess.run(
[sys.executable, str(SCRIPT_PATH), "--file", str(fixture_path), "--json"],
capture_output=True,
text=True,
env=env,
)

def test_stdout_is_valid_json_only(self):
"""GIVEN pass_issue.md with --json flag WHEN checker runs
THEN stdout is exactly one valid JSON object with no extra bytes."""
result = self._run_raw("pass_issue.md")
assert result.returncode in (0, 1), (
f"Unexpected exit code {result.returncode}. stderr: {result.stderr}"
)
# stdout must parse as JSON without any leading/trailing non-JSON text
stripped = result.stdout.strip()
assert stripped, "stdout must not be empty in --json mode"
try:
parsed = json.loads(stripped)
except json.JSONDecodeError as e:
raise AssertionError(
f"stdout is not valid JSON: {e}\nstdout repr: {repr(result.stdout[:500])}"
) from e
assert isinstance(parsed, dict), f"JSON output must be a dict, got {type(parsed)}"
assert "verdict" in parsed, "JSON output must contain 'verdict' field"

def test_stdout_contains_no_warning_text(self):
"""GIVEN pass_issue.md with --json flag WHEN checker runs
THEN stdout contains no DeprecationWarning or warning text."""
result = self._run_raw("pass_issue.md")
assert "DeprecationWarning" not in result.stdout, (
f"stdout must not contain DeprecationWarning. stdout: {repr(result.stdout[:500])}"
)
assert "Warning" not in result.stdout, (
f"stdout must not contain Warning text. stdout: {repr(result.stdout[:500])}"
)

def test_no_deprecation_warning_with_pythonwarnings_error(self):
"""GIVEN PYTHONWARNINGS=error::DeprecationWarning WHEN --json checker runs
THEN exit code is 0 or 1 (not 2/crash from DeprecationWarning treated as error)."""
result = self._run_raw(
"pass_issue.md",
extra_env={"PYTHONWARNINGS": "error::DeprecationWarning"},
)
assert result.returncode in (0, 1), (
f"DeprecationWarning was raised as an error (exit code {result.returncode}). "
f"stderr: {result.stderr}\nstdout: {result.stdout}"
)
# stdout must still be valid JSON
stripped = result.stdout.strip()
assert stripped, "stdout must not be empty even with PYTHONWARNINGS=error"
parsed = json.loads(stripped)
assert "verdict" in parsed

def test_utcnow_replaced_in_source(self):
"""GIVEN check_issue_contract.py source WHEN inspected
THEN datetime.datetime.utcnow() is not present (AC2: #574)."""
script_content = SCRIPT_PATH.read_text(encoding="utf-8")
assert "datetime.utcnow()" not in script_content, (
"datetime.datetime.utcnow() must be replaced with "
"datetime.datetime.now(datetime.timezone.utc) — see Issue #574 AC2."
)

def test_generated_at_field_is_present_in_json_output(self):
"""GIVEN pass_issue.md with --json flag WHEN checker runs
THEN output JSON contains 'generated_at' field with UTC timestamp."""
result = self._run_raw(
"pass_issue.md",
extra_env={"PYTHONWARNINGS": "error::DeprecationWarning"},
)
assert result.returncode in (0, 1)
parsed = json.loads(result.stdout.strip())
assert "generated_at" in parsed, "JSON output must contain 'generated_at' field"
# Verify it looks like a UTC timestamp (ends with Z)
generated_at = parsed["generated_at"]
assert generated_at.endswith("Z"), (
f"generated_at must end with 'Z' (UTC), got: {generated_at}"
)
Loading