mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-10 12:47:04 +00:00
Introduce a single grammar for per-line, per-block, and per-file
suppression markers across the test suite, replacing seven ad-hoc
syntaxes that had drifted apart.
Helper
utils/annotations/suppress.py exposes is_suppressed_at,
is_suppressed_in_head, is_suppressed_anywhere, line_has_rule, and
suppressed_line_numbers. Markers may use either `noqa` or `nocheck`
(case-insensitive synonyms), live inside `#`, `//`, `{# ... #}`, or
`<\!-- ... -->` comments, and combine multiple kebab-case rules
comma-separated. 17 unit tests cover the grammar in isolation.
Refactored consumers
- tests/external/repository/test_urls_reachable.py (rule: url)
- tests/external/docker/test_image_versions.py via
utils/docker/version_updater.py (rule: docker-version)
- tests/lint/repository/test_no_direct_yaml_calls.py (rule:
direct-yaml)
- tests/lint/ansible/test_service_shared_consistency.py (rule:
shared)
- tests/lint/ansible/roles/test_web_app_email_integration.py (rule:
email)
- tests/lint/repository/test_python_file_size.py (rule: file-size)
- tests/lint/ansible/test_run_once_tags.py +
tests/integration/roles/run_once/test_schema.py (rules: run-once,
run-once-suffix)
Marker migration
12 role tasks/main.yml files: `# run_once_<key>: deactivated`
-> `# nocheck: run-once`. roles/web-app-suitecrm/meta/services.yml
drops a malformed inline `noqa: intentional legacy pin` from a TODO
comment so the new parser does not pick it up as a stray rule.
Caching wins
Test files that previously loaded YAML / read files via raw
yaml.safe_load + Path.read_text now route through utils.cache.yaml
(load_yaml_any / load_yaml_str) and utils.cache.files.read_text:
- test_service_shared_consistency.py
- test_web_app_email_integration.py
- tests/integration/roles/run_once/test_schema.py
- utils/docker/version_updater.py
This both speeds up repeated reads and stays consistent with the
direct-yaml lint policy.
Documentation
New SPOT page at docs/contributing/actions/testing/suppression.md
with grammar, position semantics, the catalog of rules, examples,
and an "adding a new rule" checklist. lint.md and external.md drop
their per-rule tables and link to the new page. All touched docs
conform to docs/contributing/documentation.md (no em-dash thought
breaks, no SPOT self-labels, RFC-2119 keywords used consistently).
Settings sort tooling
scripts/lint/sort_claude_settings.sh wraps utils/claude/settings_sort.py
and is wired into scripts/lint/autoformat.sh. The tooling fixes the
ASCII-ascending requirement enforced by
tests/lint/repository/test_claude_settings_sorted.py; the test's
failure message now points operators at the script. .claude/settings.json
is committed in sorted form.
142 lines
4.2 KiB
Python
142 lines
4.2 KiB
Python
"""Unified suppression markers for infinito-nexus tests.
|
|
|
|
A single grammar covers every per-line, per-block, and per-file
|
|
opt-out across the test suite. See
|
|
``docs/contributing/actions/testing/suppression.md`` for the catalog
|
|
of valid rule keys, their placement, and the test that consumes each.
|
|
|
|
Grammar
|
|
=======
|
|
|
|
<comment-prefix> (noqa|nocheck): <rule>(\\s*,\\s*<rule>)*
|
|
|
|
Accepted comment prefixes:
|
|
|
|
* ``#`` (Python, YAML, shell, conf, INI, …)
|
|
* ``//`` (JS, JSONC, …)
|
|
* ``{# … #}`` (Jinja2)
|
|
* ``<!-- … -->`` (HTML, Markdown)
|
|
|
|
``noqa`` and ``nocheck`` are synonyms (case-insensitive). The
|
|
convention is:
|
|
|
|
* ``noqa`` for code-level lints (analogous to flake8/ruff).
|
|
* ``nocheck`` for repo-content checks (URLs, image versions, file
|
|
size, run-once schema).
|
|
|
|
Both work for any rule; the catalog page documents the convention but
|
|
neither test enforces which keyword is used.
|
|
|
|
Multiple rules may be combined on one comment, comma-separated:
|
|
|
|
# noqa: shared, email
|
|
# nocheck: url, docker-version
|
|
|
|
Position semantics are per-rule and listed in the catalog page. This
|
|
module exposes three resolvers:
|
|
|
|
* :func:`is_suppressed_at`: same line, line above, or either.
|
|
* :func:`is_suppressed_in_head`: anywhere in the first N lines
|
|
(file-level opt-outs).
|
|
* :func:`is_suppressed_anywhere`: anywhere in the file (used by the
|
|
run-once schema check).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from collections.abc import Sequence
|
|
|
|
_KEYWORD_RE = re.compile(
|
|
r"(?:noqa|nocheck)\s*:\s*([a-z0-9][a-z0-9\-]*(?:\s*,\s*[a-z0-9][a-z0-9\-]*)*)",
|
|
re.IGNORECASE,
|
|
)
|
|
|
|
|
|
def _rules_on_line(line: str) -> set[str]:
|
|
"""Return the set of rule keys present in suppression markers on *line*."""
|
|
found: set[str] = set()
|
|
for match in _KEYWORD_RE.finditer(line):
|
|
for rule in match.group(1).split(","):
|
|
rule = rule.strip().lower()
|
|
if rule:
|
|
found.add(rule)
|
|
return found
|
|
|
|
|
|
def line_has_rule(line: str, rule: str) -> bool:
|
|
"""Return True iff *line* carries a marker for *rule*."""
|
|
return rule.lower() in _rules_on_line(line)
|
|
|
|
|
|
def is_suppressed_at(
|
|
lines: Sequence[str],
|
|
line_no: int,
|
|
rule: str,
|
|
*,
|
|
mode: str = "same-or-above",
|
|
) -> bool:
|
|
"""Check whether the construct at 1-based *line_no* is suppressed.
|
|
|
|
``mode`` selects placement semantics:
|
|
|
|
* ``"same-line"``: marker must be on the construct's line.
|
|
* ``"line-above"``: marker must be on the immediately preceding
|
|
non-empty line. Blank lines between marker and construct break
|
|
the association.
|
|
* ``"same-or-above"`` (default): either of the above.
|
|
"""
|
|
if line_no < 1 or line_no > len(lines):
|
|
return False
|
|
|
|
rule = rule.lower()
|
|
|
|
if mode in ("same-line", "same-or-above") and line_has_rule(
|
|
lines[line_no - 1], rule
|
|
):
|
|
return True
|
|
|
|
if mode in ("line-above", "same-or-above"):
|
|
prev = line_no - 2
|
|
while prev >= 0 and not lines[prev].strip():
|
|
prev -= 1
|
|
if prev >= 0 and line_has_rule(lines[prev], rule):
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
def is_suppressed_in_head(
|
|
lines: Sequence[str],
|
|
rule: str,
|
|
*,
|
|
scan_lines: int = 30,
|
|
) -> bool:
|
|
"""Return True iff *rule* appears in the first *scan_lines* lines.
|
|
|
|
File-level opt-outs (e.g. ``file-size``) MUST live near the top of
|
|
the file so the cost of carrying the exemption stays visible.
|
|
"""
|
|
rule = rule.lower()
|
|
for line in lines[:scan_lines]:
|
|
if line_has_rule(line, rule):
|
|
return True
|
|
return False
|
|
|
|
|
|
def is_suppressed_anywhere(lines: Sequence[str], rule: str) -> bool:
|
|
"""Return True iff *rule* appears on any line of *lines*.
|
|
|
|
Used for whole-file opt-outs whose semantics legitimately apply to
|
|
the entire file regardless of where the marker sits (e.g.
|
|
``run-once`` on roles that are intentionally executed every play).
|
|
"""
|
|
rule = rule.lower()
|
|
return any(line_has_rule(line, rule) for line in lines)
|
|
|
|
|
|
def suppressed_line_numbers(lines: Sequence[str], rule: str) -> set[int]:
|
|
"""Return the set of 1-based line numbers carrying a marker for *rule*."""
|
|
rule = rule.lower()
|
|
return {idx for idx, line in enumerate(lines, start=1) if line_has_rule(line, rule)}
|