#!/usr/bin/env python3
"""Adversarial tests for lint-skin.py's accessible-SVG contract."""
from __future__ import annotations
import importlib.util
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
LINTER = ROOT / "scripts/lint-skin.py"
BUILD_ICONS = ROOT / "scripts/build-icons.py"
VALID_SVG = """\
"""
def run_linter(path: Path) -> subprocess.CompletedProcess[str]:
return subprocess.run(
[sys.executable, str(LINTER), str(path)],
capture_output=True,
text=True,
check=False,
)
def require_failure(name: str, html: str, expected: str, directory: Path) -> None:
path = directory / f"{name}.html"
path.write_text(html, encoding="utf-8")
result = run_linter(path)
if result.returncode != 1:
raise AssertionError(
f"{name}: expected exit 1, got {result.returncode}\n{result.stdout}{result.stderr}"
)
if f"a11y: {expected}" not in result.stdout:
raise AssertionError(
f"{name}: missing expected a11y finding {expected!r}\n{result.stdout}"
)
print(f"OK: {name} rejected — {expected}")
def require_pass(name: str, html: str, directory: Path) -> None:
path = directory / f"{name}.html"
path.write_text(html, encoding="utf-8")
result = run_linter(path)
if result.returncode != 0:
raise AssertionError(
f"{name}: expected exit 0, got {result.returncode}\n{result.stdout}{result.stderr}"
)
if "a11y:" in result.stdout:
raise AssertionError(f"{name}: unexpected a11y finding\n{result.stdout}")
print(f"OK: {name} accepted")
def main() -> int:
with tempfile.TemporaryDirectory(prefix="lint-a11y-") as temp_dir:
directory = Path(temp_dir)
require_failure(
"missing-role",
VALID_SVG.replace(' role="img"', ""),
'diagram "),
directory,
)
project = directory / "baseline-project"
(project / "scripts").mkdir(parents=True)
(project / "skills/diagram-design/assets").mkdir(parents=True)
(project / "skills/diagram-design/references").mkdir(parents=True)
shutil.copy2(LINTER, project / "scripts/lint-skin.py")
shutil.copy2(
ROOT / "skills/diagram-design/references/style-guide.md",
project / "skills/diagram-design/references/style-guide.md",
)
(project / "scripts/lint-skin-baseline.txt").write_text(
"example-baseline.html\n", encoding="utf-8"
)
(project / "skills/diagram-design/assets/example-baseline.html").write_text(
'\n', encoding="utf-8"
)
baseline_result = subprocess.run(
[
sys.executable,
str(project / "scripts/lint-skin.py"),
"--all",
"--baseline",
],
capture_output=True,
text=True,
check=False,
)
if baseline_result.returncode != 1:
raise AssertionError(
"baseline-a11y: expected exit 1, got "
f"{baseline_result.returncode}\n"
f"{baseline_result.stdout}{baseline_result.stderr}"
)
if "a11y: diagram must carry role=\"img\"" not in baseline_result.stdout:
raise AssertionError(
"baseline-a11y: missing expected accessibility finding\n"
f"{baseline_result.stdout}"
)
if "color:" in baseline_result.stdout:
raise AssertionError(
"baseline-a11y: legacy visual finding was not exempted\n"
f"{baseline_result.stdout}"
)
print("OK: baseline file receives a11y checks but keeps visual exemptions")
print("All accessible-SVG lint cases passed.")
return 0
if __name__ == "__main__":
sys.exit(main())