Files
computer-playbook/tests/lint/filesystem/env/test_infinito_prefix.py
kevinveenbirkenbachandClaude Opus 4.8 d9a53a0545 chore(lint): pin ruff to 0.16.0 and resolve the rules that version enforces
The ruff toolchain was unpinned, so host venvs, the lint container and CI
installed different releases over time. That drift is why lint-python only
failed on CI: locally ruff 0.15.x preview-gated ISC004/FURB192 and (falsely)
fired S310 on urllib.request.Request, while CI's 0.16.0 has ISC004/FURB192
stable and drops the S310-on-Request false positive.

Pin ruff==0.16.0 in pyproject.toml's dev extras (Dependabot keeps it current)
and have utils.install.lint.ruff read that pin and reinstall whenever the
found version drifts, so every environment resolves the same ruff. Then fix
what 0.16.0 flags: parenthesise the implicit string concatenations (ISC004),
use min() over sorted()[0] (FURB192), drop the now-unused S310 noqa on the
Request() builders (RUF100), and delete the narration comments that the
comment lint surfaces in the touched files.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-23 23:08:26 +02:00

81 lines
2.4 KiB
Python

"""Every key declared in `default.env` must carry the `INFINITO_` prefix.
The shared namespace exists so project env-vars never collide with
generic shell or Ansible-role variables of the same short name. Any
key without the prefix is either leaking into the wrong scope or
needs the prefix added.
"""
from __future__ import annotations
import re
import unittest
from dataclasses import dataclass
from typing import TYPE_CHECKING
from utils.cache.files import read_text
from . import PROJECT_ROOT
if TYPE_CHECKING:
from pathlib import Path
_KEY_RE = re.compile(r"^\s*(?P<key>[A-Za-z_][A-Za-z0-9_]*)\s*=")
_INFINITO_KEY_RE = re.compile(r"^INFINITO_[A-Z0-9_]+$")
@dataclass(frozen=True)
class _Violation:
file: str
line_no: int
key: str
def _scan_file(path: Path) -> list[_Violation]:
rel = path.relative_to(PROJECT_ROOT).as_posix()
out: list[_Violation] = []
for index, line in enumerate(read_text(str(path)).splitlines(), start=1):
match = _KEY_RE.match(line)
if match is None:
continue
key = match.group("key")
if _INFINITO_KEY_RE.match(key):
continue
out.append(_Violation(rel, index, key))
return out
def _scan_targets() -> list[Path]:
default_env = PROJECT_ROOT / "default.env"
return [default_env] if default_env.is_file() else []
class TestEnvFilesInfinitoPrefix(unittest.TestCase):
def test_every_env_file_key_carries_infinito_prefix(self) -> None:
targets = _scan_targets()
self.assertTrue(targets, "default.env not found to scan")
violations: list[_Violation] = []
for path in targets:
violations.extend(_scan_file(path))
if not violations:
return
lines = [
(
f"default.env keys without the INFINITO_ prefix "
f"({len(violations)} violations):"
),
"",
"Every key in default.env must start with INFINITO_ so it cannot collide with generic shell or Ansible-role variables of the same short name. Rename the key to INFINITO_<NAME> (and update callers) or move it out of default.env if it is not part of the project's INFINITO_* namespace.",
"",
"Offenders:",
]
lines.extend(f" {v.file}:{v.line_no}: {v.key}" for v in violations)
self.fail("\n".join(lines))
if __name__ == "__main__":
unittest.main()