Files
computer-playbook/tests/integration/meta/workflows/test_pr_scope.py
T
kevinveenbirkenbachandClaude Opus 4.7 a5ba14d5b3 test(lint/cache-read): extend scope to all tests/ + convert callers
Widens the cache-read lint guard from ``tests/lint/`` to every ``.py``
under ``tests/``. Integration tests show the same compounding cost
pattern (one role's compose template / nginx conf / scrape fragment
read across many shape tests) that motivated the original lint for
lint/.

Converts 91 violations across 28 test files:

- **Cache (utils.cache.files.read_text)** for project-tracked reads
  and single-read tempdir fixtures, e.g. test_prometheus.py (32x),
  test_keycloak_import_flow.py (11x), test_dashboard_oidc.py (3x),
  test_changelog_md.py (8x), test_package_mirror.py (8x), plus
  test_logging.py / test_summarize.py / test_version_updater.py /
  test_get_url_retry.py / test_pull_specific_host.py.

- **Cache + UnicodeDecodeError handler** for project-walk scanners
  that previously used ``errors="replace"`` / ``errors="ignore"`` to
  defend against non-UTF-8 files (compose volumes, password quoting,
  template image, mailu dependency, URL reachability). Wraps each
  read in ``try/except (OSError, UnicodeDecodeError): continue``,
  which has the same observable effect (skip undecodable files).

- **Refactor away the binary heuristic** in test_no_raw_docker.py:
  ``read_bytes() + b"\x00" not in raw`` becomes
  ``read_text() / except UnicodeDecodeError``. Same files skipped,
  with cache benefit.

- **Variable extraction** in test_changelog_md.py / test_package_mirror.py
  for cases where ``self.assertEqual(path.read_text(...), expected)``
  exceeded ruff's 88-char limit. Ruff was wrapping the marker onto a
  line outside the AST span of the read_text call.

- **Compute-from-setup** for two test methods that previously read
  ``self.changelog`` twice (before/after) to verify a no-op:
  ``before`` is now derived from the known input, dropping the first
  read.

Five ``# nocheck: cache-read`` markers remain, each annotated inline
with the reason: idempotency tests where the second read must hit
disk to catch non-idempotent regressions, and tests with intermediate
``write_text`` whose subsequent read would return stale cache data.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-15 14:23:32 +02:00

119 lines
3.4 KiB
Python

import os
import shutil
import subprocess
import tempfile
import textwrap
import unittest
from pathlib import Path
from utils.cache.files import read_text
from . import PROJECT_ROOT
SCRIPT_PATH = PROJECT_ROOT / "scripts" / "meta" / "resolve" / "pr" / "scope.sh"
@unittest.skipUnless(
shutil.which("jq"), "jq is required for the shell script under test"
)
class TestPullRequestScope(unittest.TestCase):
def _write_fake_gh(self, temp_dir: Path) -> None:
fake_gh = temp_dir / "gh"
fake_gh.write_text(
textwrap.dedent(
"""\
#!/usr/bin/env bash
set -euo pipefail
if [[ "${1:-}" != "api" ]]; then
echo "Unsupported gh invocation: $*" >&2
exit 1
fi
printf '%s\n' "${GH_FAKE_PULL_FILES_JSON:-}"
"""
),
encoding="utf-8",
)
fake_gh.chmod(0o755)
def _run_script(self, *, files_json: str):
with tempfile.TemporaryDirectory() as temp_dir_str:
temp_dir = Path(temp_dir_str)
output_file = temp_dir / "output.txt"
self._write_fake_gh(temp_dir)
env = os.environ.copy()
env.update(
{
"PR_NUMBER": "42",
"REPOSITORY": "kevinveenbirkenbach/infinito-nexus",
"GH_TOKEN": "test-token",
"GITHUB_OUTPUT": str(output_file),
"GH_FAKE_PULL_FILES_JSON": files_json,
"PATH": f"{temp_dir}:{env['PATH']}",
}
)
result = subprocess.run(
["bash", str(SCRIPT_PATH)],
cwd=PROJECT_ROOT,
env=env,
capture_output=True,
text=True,
check=True,
)
outputs = {}
for line in read_text(str(output_file)).splitlines():
if "=" not in line:
continue
key, value = line.split("=", 1)
outputs[key] = value
return result, outputs
def test_agents_only_scope_skips_ci_orchestrator(self):
files_json = """\
[
{"filename":"AGENTS.md"},
{"filename":"docs/agents/action/debug/local.md"},
{"filename":"docs/agents/action/develop.md"}
]
"""
_, outputs = self._run_script(files_json=files_json)
self.assertEqual(outputs["scope"], "agents")
self.assertEqual(outputs["run_ci_orchestrator"], "false")
def test_documentation_only_scope_skips_ci_orchestrator(self):
files_json = """\
[
{"filename":"README.md"},
{"filename":"docs/contributing/flow/pull-request.md"},
{"filename":".github/PULL_REQUEST_TEMPLATE/documentation.md"},
{"filename":"docs/contributing/setup.rst"}
]
"""
_, outputs = self._run_script(files_json=files_json)
self.assertEqual(outputs["scope"], "documentation")
self.assertEqual(outputs["run_ci_orchestrator"], "false")
def test_renamed_agent_file_falls_back_to_full_ci(self):
files_json = """\
[
{
"status": "renamed",
"filename": "docs/contributing/flow/workflow.md",
"previous_filename": "AGENTS.md"
}
]
"""
_, outputs = self._run_script(files_json=files_json)
self.assertEqual(outputs["scope"], "full")
self.assertEqual(outputs["run_ci_orchestrator"], "true")
if __name__ == "__main__":
unittest.main()