"""Tests for rendered-site metadata and link validation.""" from __future__ import annotations import json import sys import tempfile import unittest from pathlib import Path ROOT = Path(__file__).resolve().parent.parent TMP_ROOT = ROOT / "tmp" / "tests" TMP_ROOT.mkdir(parents=True, exist_ok=True) sys.path.insert(0, str(ROOT / "scripts")) from validate_site import validate_site # noqa: E402 def _page(title: str, description: str, body: str = "") -> str: structured = json.dumps( {"@context": "https://schema.org", "@type": "TechArticle"} ) return f"""
',
),
encoding="utf-8",
)
(self.site / "assets" / "logo.png").write_bytes(b"png")
self.assertEqual(validate_site(self.site, self.config), [])
def test_broken_fragment_and_missing_alt_fail(self):
(self.site / "index.html").write_text(
_page(
"Home",
"Unique home description.",
'Missing
',
),
encoding="utf-8",
)
issues = validate_site(self.site, self.config)
self.assertTrue(any("missing fragment" in issue for issue in issues))
self.assertTrue(any("images missing alt" in issue for issue in issues))
self.assertTrue(any("broken local link" in issue for issue in issues))
def test_duplicate_search_metadata_fails(self):
(self.site / "index.html").write_text(
_page("Repeated", "Repeated description."), encoding="utf-8"
)
child = self.site / "child"
child.mkdir()
(child / "index.html").write_text(
_page("Repeated", "Repeated description."), encoding="utf-8"
)
issues = validate_site(self.site, self.config)
self.assertTrue(any("duplicate title" in issue for issue in issues))
self.assertTrue(any("duplicate description" in issue for issue in issues))
class EveryTestDependencyIsInstalledInCi(unittest.TestCase):
"""The markdown rendering test skipped on every CI run: the test job
installed pyyaml and jsonschema only. A test that skips where it is
meant to run guards nothing."""
PACKAGES = {"yaml": "pyyaml", "jsonschema": "jsonschema", "markdown": "markdown"}
def test_the_test_job_installs_what_the_tests_import(self):
import ast
root = Path(__file__).resolve().parents[1]
local = (
{p.stem for p in (root / "scripts").rglob("*.py")}
| {p.name for p in (root / "scripts").iterdir() if p.is_dir()}
| {p.stem for p in (root / "tests").glob("*.py")}
| {"scripts", "tests", "install"}
)
imported: set[str] = set()
for path in (root / "tests").glob("*.py"):
for node in ast.walk(ast.parse(path.read_text(encoding="utf-8"))):
if isinstance(node, ast.Import):
imported.update(a.name.split(".")[0] for a in node.names)
elif isinstance(node, ast.ImportFrom) and node.level == 0 and node.module:
imported.add(node.module.split(".")[0])
third_party = {
m for m in imported if m not in sys.stdlib_module_names and m not in local
}
workflow = (root / ".github" / "workflows" / "validate.yml").read_text()
job = workflow[workflow.index("run-tests:"):]
install_line = next(
line for line in job.splitlines() if "pip install" in line
)
missing = sorted(
m for m in third_party if self.PACKAGES.get(m, m) not in install_line
)
self.assertEqual(missing, [])
if __name__ == "__main__":
unittest.main()