Files
SystemSimulationApp/tests/test_regression_fixture_line_endings.py
T
2026-09-02 13:48:51 +08:00

86 lines
3.2 KiB
Python

from __future__ import annotations
import hashlib
import json
from pathlib import Path
import unittest
REPOSITORY_ROOT = Path(__file__).resolve().parents[1]
BASELINE_ROOT = REPOSITORY_ROOT / "tests" / "baselines" / "simulation"
GIT_ATTRIBUTES_PATH = REPOSITORY_ROOT / ".gitattributes"
EXPECTED_ATTRIBUTE_RULES = {
("tests/data/test-mql-8.xml", "text", "eol=lf"),
("tests/data/test-mql-8.json", "text", "eol=lf"),
("tests/data/test_mql-full-branches-01-04.xml", "text", "eol=lf"),
("tests/baselines/simulation/**/*.json", "text", "eol=lf"),
}
def _manifest_paths() -> tuple[Path, ...]:
return tuple(sorted(BASELINE_ROOT.glob("*/manifest.json")))
def _manifest_source_descriptors() -> tuple[tuple[Path, dict[str, object]], ...]:
descriptors: list[tuple[Path, dict[str, object]]] = []
for manifest_path in _manifest_paths():
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
source = manifest["source"]
descriptors.append((manifest_path, source))
companion = source.get("companionProject")
if companion is not None:
descriptors.append((manifest_path, companion))
return tuple(descriptors)
class RegressionFixtureLineEndingTests(unittest.TestCase):
def test_gitattributes_pin_byte_locked_artifacts_to_lf(self) -> None:
attribute_rules = {
tuple(line.split())
for raw_line in GIT_ATTRIBUTES_PATH.read_text(encoding="utf-8").splitlines()
if (line := raw_line.strip()) and not line.startswith("#")
}
self.assertTrue(
EXPECTED_ATTRIBUTE_RULES.issubset(attribute_rules),
"Raw-byte regression artifacts must be explicitly pinned to LF.",
)
def test_byte_locked_artifacts_contain_no_carriage_returns(self) -> None:
paths = {
path
for path in BASELINE_ROOT.rglob("*.json")
if path.is_file()
}
paths.update(
REPOSITORY_ROOT / str(descriptor["path"])
for _, descriptor in _manifest_source_descriptors()
)
self.assertTrue(paths, "No byte-locked regression artifacts were found.")
for path in sorted(paths):
with self.subTest(path=path.relative_to(REPOSITORY_ROOT).as_posix()):
self.assertNotIn(
b"\r",
path.read_bytes(),
"Byte-locked regression artifacts must use LF line endings.",
)
def test_manifest_source_identities_match_raw_worktree_bytes(self) -> None:
self.assertTrue(_manifest_paths(), "No regression manifests were found.")
for manifest_path, descriptor in _manifest_source_descriptors():
relative_path = str(descriptor["path"])
payload = (REPOSITORY_ROOT / relative_path).read_bytes()
label = f"{manifest_path.parent.name}:{relative_path}"
with self.subTest(source=label):
self.assertEqual(len(payload), descriptor["bytes"])
self.assertEqual(
hashlib.sha256(payload).hexdigest(),
descriptor["sha256"],
)
if __name__ == "__main__":
unittest.main()