frontend-patterns/tests/test_reference_preview.py

71 lines
2.3 KiB
Python
Raw Normal View History

"""Reject broken preview contracts before publishing generated references."""
import importlib.util
import json
from pathlib import Path
import shutil
import sys
import pytest
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'scripts'))
spec = importlib.util.spec_from_file_location('reference_preview', ROOT / 'scripts/reference_preview.py')
preview = importlib.util.module_from_spec(spec)
spec.loader.exec_module(preview)
@pytest.fixture
def copy(tmp_path):
for name in ('research', 'canon', 'workplans', 'sequences', 'technologies', 'reference'):
shutil.copytree(ROOT / name, tmp_path / name)
shutil.copy(ROOT / 'START.md', tmp_path / 'START.md')
(tmp_path / '.git').symlink_to(ROOT / '.git', target_is_directory=True)
return tmp_path
def mutate(root, edit):
path = root / 'reference/0.1/manifest.json'
data = json.loads(path.read_text())
edit(data)
path.write_text(json.dumps(data))
def test_current_export():
for name, content in preview.build().items():
assert (ROOT / 'reference/0.1' / name).read_text() == content
@pytest.mark.parametrize('field,value', [('target', 'CAND-9999'), ('type', 'magically-guarantees'), ('condition', ''), ('evidence_refs', ['missing.md'])])
def test_bad_relation(copy, field, value):
mutate(copy, lambda m: m['relations'][0].update({field: value}))
with pytest.raises(ValueError):
preview.build(copy)
def test_duplicate_identity(copy):
mutate(copy, lambda m: m['core'].append(m['core'][0]))
with pytest.raises(ValueError):
preview.build(copy)
def test_stale_candidate(copy):
with (copy / 'research/candidates/ConflictCopy.md').open('a') as f:
f.write('\nChanged contract.\n')
with pytest.raises(ValueError, match='Stale candidate'):
preview.build(copy)
def test_escaping_reference(copy):
mutate(copy, lambda m: m['entrypoints'].update(start='/etc/passwd'))
with pytest.raises(ValueError, match='Invalid reference'):
preview.build(copy)
def test_missing_metadata(copy):
path = copy / 'reference/0.1/manifest.json'
data = json.loads(path.read_text())
del data['relations'][0]['rationale']
path.write_text(json.dumps(data))
with pytest.raises((ValueError, KeyError)):
preview.build(copy)