ChemLab-Assets / tests /conftest.py
Ziruibest's picture
Release 1.4.0: working front-panel menus for 106 instruments; menu step preview
a6c006f verified
Raw History Blame Contribute Delete
2.61 kB
"""Test configuration for the public ChemLab-Assets release.
The release does not redistribute cached third-party source documents (outputs/**/sources, source_cache, raw/
folders, references/). Some tests re-hash those caches to prove provenance; when a cache is absent, the test is
reported as skipped with the missing path instead of failing. Contract checks with check_files=True skip only the
artifacts that release_manifest.json lists as not published.
"""
import json
import re
import sys
from pathlib import Path
import pytest
UNPUBLISHED = ("/outputs/", "/references/", "/raw/", "source_cache", "/sources/", "/vendor/")
def _unpublished(exc):
name = getattr(exc, "filename", None)
return isinstance(exc, FileNotFoundError) and name and any(k in str(name) for k in UNPUBLISHED)
def _release_validator():
"""Contracts list the files of the original build; release_manifest.json names the artifacts this release does not ship.
validate_contract(..., check_files=True) reports those as missing files. Drop exactly those reports, so every shipped
file is still checked, including its sha256 pin."""
root = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(root / "skills" / "chemistry-lab-assets" / "scripts"))
try:
import asset_tool
manifest = json.load(open(root / "release_manifest.json", encoding="utf-8"))["assets"]
except Exception: # noqa: BLE001
return
real = asset_tool.validate_contract
missing = re.compile(r"^\$\.artifacts\.(.+)\.path: available 文件不存在或不是普通文件: ")
def validate_contract(contract, asset_root=None, check_files=False, schema=None):
errors = real(contract, asset_root, check_files=check_files, schema=schema)
if not check_files or not isinstance(contract, dict):
return errors
rows = (manifest.get(contract.get("asset_type_id")) or {}).get("not_published") or {}
unpublished = {a for ids in rows.values() for a in ids}
return [e for e in errors if not ((m := missing.match(e)) and m.group(1) in unpublished)]
asset_tool.validate_contract = validate_contract
_release_validator()
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
rep = outcome.get_result()
if call.excinfo is not None and call.when in ("setup", "call") and _unpublished(call.excinfo.value):
rep.outcome = "skipped"
rep.longrepr = (str(item.path), item.location[1] or 0,
f"Skipped: needs an unpublished source cache ({call.excinfo.value.filename})")