Files

88 lines
3.6 KiB
Python
Raw Permalink Normal View History

2026-07-23 14:13:45 -06:00
from __future__ import annotations
import importlib.util
2026-07-24 15:14:23 -06:00
import json
2026-07-23 14:13:45 -06:00
from pathlib import Path
2026-07-24 15:14:23 -06:00
import shutil
2026-07-23 14:13:45 -06:00
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
VALIDATOR_PATH = ROOT / "scripts" / "validate_repository.py"
SPEC = importlib.util.spec_from_file_location("validate_repository", VALIDATOR_PATH)
VALIDATOR = importlib.util.module_from_spec(SPEC)
assert SPEC.loader is not None
SPEC.loader.exec_module(VALIDATOR)
class RepositoryValidationTests(unittest.TestCase):
def test_current_repository_passes(self) -> None:
result = VALIDATOR.validate(ROOT)
self.assertTrue(result["passed"], result["findings"])
def test_missing_repository_fails(self) -> None:
with tempfile.TemporaryDirectory() as directory:
result = VALIDATOR.validate(Path(directory))
self.assertFalse(result["passed"])
self.assertTrue(
any("Missing required path" in item["message"] for item in result["findings"])
)
2026-07-24 15:14:23 -06:00
def test_variable_identity_mismatch_fails(self) -> None:
with tempfile.TemporaryDirectory() as directory:
copy = Path(directory) / "repository"
shutil.copytree(ROOT, copy, ignore=shutil.ignore_patterns(".git", "__pycache__"))
variables_path = copy / "repository-variables.json"
variables = json.loads(variables_path.read_text(encoding="utf-8"))
variables["identity"]["publisher"]["name"] = "Different Publisher"
variables_path.write_text(json.dumps(variables, indent=2) + "\n", encoding="utf-8")
result = VALIDATOR.validate(copy)
self.assertFalse(result["passed"])
self.assertTrue(
any(
"Publisher differs between repository-variables.json and repository.json"
in item["message"]
for item in result["findings"]
)
)
2026-07-24 16:17:28 -06:00
def test_duplicate_content_id_fails(self) -> None:
with tempfile.TemporaryDirectory() as directory:
copy = Path(directory) / "repository"
shutil.copytree(ROOT, copy, ignore=shutil.ignore_patterns(".git", "__pycache__"))
index_path = copy / "content-index.json"
index = json.loads(index_path.read_text(encoding="utf-8"))
duplicate = dict(index["items"][0])
duplicate["path"] = "docs/README.md"
index["items"].append(duplicate)
index_path.write_text(json.dumps(index, indent=2) + "\n", encoding="utf-8")
result = VALIDATOR.validate(copy)
self.assertFalse(result["passed"])
self.assertTrue(
any(
"Duplicate content_id in content-index.json" in item["message"]
for item in result["findings"]
)
)
def test_missing_indexed_content_fails(self) -> None:
with tempfile.TemporaryDirectory() as directory:
copy = Path(directory) / "repository"
shutil.copytree(ROOT, copy, ignore=shutil.ignore_patterns(".git", "__pycache__"))
index_path = copy / "content-index.json"
index = json.loads(index_path.read_text(encoding="utf-8"))
index["items"][0]["path"] = "docs/missing-document.md"
index_path.write_text(json.dumps(index, indent=2) + "\n", encoding="utf-8")
result = VALIDATOR.validate(copy)
self.assertFalse(result["passed"])
self.assertTrue(
any(
"Indexed content path does not exist" in item["message"]
for item in result["findings"]
)
)
2026-07-23 14:13:45 -06:00
if __name__ == "__main__":
unittest.main()