Files
computer-playbook/tests/integration/workflows/test_pr_scope.py
T
kevinveenbirkenbachandClaude Opus 4.7 26ef3ff504 chore(ruff): enable strict ruleset (E/F/I/B/UP, py311) — 2819 → 0
Adds ``[tool.ruff.lint]`` with ``select = ["E", "F", "I", "B", "UP"]``
and ``target-version = "py311"``. ``E501`` is ignored because
``ruff format`` already reflows what it can; the remaining long lines
are unsplittable string literals / URLs.

Auto-fixed (2753 issues, behavior-preserving):
- UP006/UP035: ``List[X]`` → ``list[X]``, ``Dict[X]`` → ``dict[X]``,
  ``from typing import …`` redirected where superseded by builtins.
- UP045: ``Optional[X]`` → ``X | None``.
- UP010/UP009/UP015: dropped Python-2-era pragmas (``from __future__
  import absolute_import, division, print_function``,
  ``# -*- coding: utf-8 -*-``), redundant ``open(..., "r")`` mode.
- UP004: dropped implicit ``class X(object):``.
- UP022/UP008/UP032: stdlib idiom modernization (``capture_output``,
  super-call simplification, etc.).
- B905: ``zip(...)`` → ``zip(..., strict=False)`` (preserves prior
  behavior; switching to ``strict=True`` is a separate semantic
  choice not made here).
- I001: import-block ordering.

Hand-fixed (66 issues):
- B904 × 54: appended ``from <bound>`` (when the enclosing ``except``
  binds a name) or ``from None`` (when it doesn't) so chained
  exceptions stay distinguishable from errors-during-exception-
  handling. Applied via a small AST-aware script that walked back to
  the enclosing ``except`` clause and matched the binding.
- B017 × 4: narrowed broad ``assertRaises(Exception)`` to the actual
  ``AnsibleError`` / ``AnsibleFilterError`` raised by the code under
  test.
- B023 × 2: bound captured loop variables via default args
  (``def _record(name, *, _path_real=path_real, ...)``) so each
  closure gets its own snapshot.
- B007 × 2: unused loop control variables renamed to ``_`` /
  rewritten to ``.values()``.
- UP007 × 2: ``Union[X, Y]`` → ``X | Y`` (plus dropping the now-unused
  ``from typing import Union``).
- B018 × 1: Odoo addon manifests are bare dict expressions at module
  level by design — suppressed with an inline ``# noqa: B018`` and
  rationale comment.
- UP031 × 1: printf-style format → f-string.

The 65 ``# noqa: …`` markers introduced by the auto-fixer all carry
real ruff codes (``B018``, ``E402``, ``F401``, etc.), so
``test_noqa_only_ruff_codes`` stays green.

Verification
============
- ``ruff check .`` → All checks passed
- ``ruff format --check .`` → 894 files already formatted
- ``tests.lint.repository.{test_project_root_import,
  test_no_direct_yaml_calls, test_noqa_only_ruff_codes}`` and
  ``tests.unit.utils.annotations.test_suppress`` → 20 tests OK
- 1621 unit tests → all pass
- Integration suite: 44 failures (pre-existing baseline, none from
  this change), 1 skipped.

Going forward, the new rules block regressions on E (style), F
(pyflakes), I (import order), B (likely-bug patterns), and UP
(modernization to py311) for any new code.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 16:57:20 +02:00

117 lines
3.4 KiB
Python

import os
import shutil
import subprocess
import tempfile
import textwrap
import unittest
from pathlib import Path
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 output_file.read_text(encoding="utf-8").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()