mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 03:39:52 +00:00
feat(qa): add unified verification profiles with stale evidence detection.
Runs existing repository commands serially, preserves exit codes and fingerprints sources so outdated reports fail closed. Signed-off-by: Ranveer Soni <git@ranveersoni.me>
This commit is contained in:
2 files changed
+209
No files matched your search
@@ -0,0 +1,141 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""Unified verification pipeline over existing Xenora commands.
|
||||
|
||||
Profiles invoke repository scripts/tests; they do not reimplement them.
|
||||
GPU suites run serially. Exit codes are preserved exactly.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
from dataclasses import asdict, dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any, Sequence
|
||||
|
||||
from .orchestrate import SuiteSpec, run_suites
|
||||
|
||||
|
||||
PROFILES: dict[str, list[SuiteSpec]] = {
|
||||
"quick": [
|
||||
SuiteSpec("agent-queue-unit", ("python3", "-m", "unittest", "discover", "-s", "tests/agent_queue"), "cpu"),
|
||||
SuiteSpec("qa-unit", ("python3", "-m", "unittest", "discover", "-s", "tests/qa"), "cpu"),
|
||||
SuiteSpec("release-unit", ("python3", "-m", "unittest", "discover", "-s", "tests/release"), "cpu"),
|
||||
],
|
||||
"cpu": [
|
||||
SuiteSpec("cargo-test-workspace", ("cargo", "test", "--workspace", "--locked"), "cpu"),
|
||||
SuiteSpec("semantic-review", ("cargo", "test", "--locked", "-p", "xenora-semantic-review"), "cpu"),
|
||||
],
|
||||
"gpu": [
|
||||
SuiteSpec("test-gpu", ("scripts/test-gpu.sh",), "gpu"),
|
||||
],
|
||||
"app": [
|
||||
SuiteSpec("build-app-smoke", ("scripts/build-app.sh",), "other"),
|
||||
],
|
||||
"release": [
|
||||
SuiteSpec("native-audit", ("scripts/audit/native.sh",), "other"),
|
||||
SuiteSpec("qa-unit", ("python3", "-m", "unittest", "discover", "-s", "tests/qa"), "cpu"),
|
||||
SuiteSpec("release-unit", ("python3", "-m", "unittest", "discover", "-s", "tests/release"), "cpu"),
|
||||
],
|
||||
}
|
||||
|
||||
PROFILES["full"] = (
|
||||
list(PROFILES["quick"])
|
||||
+ [s for s in PROFILES["cpu"]]
|
||||
+ list(PROFILES["gpu"])
|
||||
+ list(PROFILES["app"])
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Evidence:
|
||||
profile: str
|
||||
revision: str
|
||||
started_at: float
|
||||
finished_at: float
|
||||
source_fingerprint: str
|
||||
suites: list[dict[str, Any]] = field(default_factory=list)
|
||||
overall_returncode: int = 0
|
||||
skipped: dict[str, int] = field(default_factory=dict)
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return asdict(self)
|
||||
|
||||
|
||||
def fingerprint_sources(root: Path, paths: Sequence[Path] | None = None) -> str:
|
||||
h = hashlib.sha256()
|
||||
if paths is None:
|
||||
files = sorted(
|
||||
p for p in root.rglob("*")
|
||||
if p.is_file()
|
||||
and not any(part in {"target", ".git", "__pycache__", "artifacts", "node_modules"} for part in p.parts)
|
||||
and (p.suffix in {".rs", ".py", ".sh", ".swift", ".toml"} or p.name == "Cargo.lock")
|
||||
)[:4000]
|
||||
else:
|
||||
files = list(paths)
|
||||
for path in files:
|
||||
try:
|
||||
rel = str(path.relative_to(root))
|
||||
except ValueError:
|
||||
rel = str(path)
|
||||
h.update(rel.encode())
|
||||
h.update(b"\0")
|
||||
try:
|
||||
h.update(path.read_bytes())
|
||||
except OSError:
|
||||
h.update(b"<missing>")
|
||||
h.update(b"\0")
|
||||
return h.hexdigest()
|
||||
|
||||
|
||||
def run_profile(
|
||||
profile: str,
|
||||
*,
|
||||
root: Path,
|
||||
revision: str,
|
||||
stop_on_fail: bool = True,
|
||||
override_specs: Sequence[SuiteSpec] | None = None,
|
||||
) -> Evidence:
|
||||
if override_specs is None and profile not in PROFILES:
|
||||
raise ValueError(f"unknown profile {profile!r}; choose from {sorted(PROFILES)}")
|
||||
specs = list(override_specs) if override_specs is not None else list(PROFILES[profile])
|
||||
started = time.time()
|
||||
# Suites run sequentially; GPU profiles therefore never overlap.
|
||||
report = run_suites(specs, stop_on_fail=stop_on_fail)
|
||||
finished = time.time()
|
||||
skipped = {
|
||||
"ignored": report.ignored,
|
||||
"unexecuted": report.unexecuted,
|
||||
}
|
||||
return Evidence(
|
||||
profile=profile,
|
||||
revision=revision,
|
||||
started_at=started,
|
||||
finished_at=finished,
|
||||
source_fingerprint=fingerprint_sources(root),
|
||||
suites=[asdict(s) for s in report.suites],
|
||||
overall_returncode=report.overall_returncode(),
|
||||
skipped=skipped,
|
||||
)
|
||||
|
||||
|
||||
def evidence_is_stale(evidence: Evidence, root: Path) -> bool:
|
||||
return evidence.source_fingerprint != fingerprint_sources(root)
|
||||
|
||||
|
||||
def load_evidence(text: str) -> Evidence:
|
||||
data = json.loads(text)
|
||||
return Evidence(
|
||||
profile=data["profile"],
|
||||
revision=data["revision"],
|
||||
started_at=float(data["started_at"]),
|
||||
finished_at=float(data["finished_at"]),
|
||||
source_fingerprint=data["source_fingerprint"],
|
||||
suites=list(data.get("suites", [])),
|
||||
overall_returncode=int(data.get("overall_returncode", 0)),
|
||||
skipped=dict(data.get("skipped", {})),
|
||||
)
|
||||
|
||||
|
||||
def dumps_evidence(evidence: Evidence) -> str:
|
||||
return json.dumps(evidence.to_dict(), indent=2, sort_keys=True) + "\n"
|
||||
@@ -0,0 +1,68 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from qa.orchestrate import SuiteSpec
|
||||
from qa.verify import dumps_evidence, evidence_is_stale, fingerprint_sources, load_evidence, run_profile
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
|
||||
|
||||
class VerifyPipeline(unittest.TestCase):
|
||||
def test_real_unittest_profile_slice(self):
|
||||
# Real Xenora command, not a mock: run a tiny existing unittest module.
|
||||
evidence = run_profile(
|
||||
"custom",
|
||||
root=ROOT,
|
||||
revision="testrev",
|
||||
override_specs=[
|
||||
SuiteSpec(
|
||||
"env-check-tests",
|
||||
("python3", "-m", "unittest", "tests.qa.test_env_check"),
|
||||
"cpu",
|
||||
),
|
||||
SuiteSpec(
|
||||
"deliberate-fail",
|
||||
("python3", "-c", "import sys; sys.exit(9)"),
|
||||
"cpu",
|
||||
),
|
||||
SuiteSpec(
|
||||
"should-skip",
|
||||
("python3", "-c", "print('nope')"),
|
||||
"cpu",
|
||||
),
|
||||
],
|
||||
stop_on_fail=True,
|
||||
)
|
||||
self.assertEqual(evidence.suites[0]["status"], "passed")
|
||||
self.assertEqual(evidence.suites[1]["status"], "failed")
|
||||
self.assertEqual(evidence.suites[1]["returncode"], 9)
|
||||
self.assertEqual(evidence.suites[2]["status"], "unexecuted")
|
||||
self.assertEqual(evidence.overall_returncode, 9)
|
||||
self.assertEqual(evidence.skipped["unexecuted"], 1)
|
||||
text = dumps_evidence(evidence)
|
||||
loaded = load_evidence(text)
|
||||
self.assertEqual(loaded.revision, "testrev")
|
||||
|
||||
def test_stale_evidence_when_sources_change(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
f = root / "scripts" / "a.py"
|
||||
f.parent.mkdir(parents=True)
|
||||
f.write_text("print(1)\n", encoding="utf-8")
|
||||
evidence = run_profile(
|
||||
"custom",
|
||||
root=root,
|
||||
revision="r1",
|
||||
override_specs=[SuiteSpec("ok", ("python3", "-c", "print(1)"), "cpu")],
|
||||
)
|
||||
self.assertFalse(evidence_is_stale(evidence, root))
|
||||
f.write_text("print(2)\n", encoding="utf-8")
|
||||
self.assertTrue(evidence_is_stale(evidence, root))
|
||||
self.assertNotEqual(evidence.source_fingerprint, fingerprint_sources(root))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user