The suite takes its data from the vectors and fixtures: 3635 tests, 97.9% branch coverage, and paperspec check and paperspec stress give the same results as before. Fixed: receipts.verify_signature raised KeyError for a record without a format; traces.DOC_TAG matched a tag followed by a line feed; paperspec load and paperspec check left SQLite connections open; the site builder copied the output folder's subfolders into itself. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B8cL4zN4XpaQF5cBtASzfq
131 lines
4.1 KiB
Python
131 lines
4.1 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# Copyright 2026 Scienting
|
|
#
|
|
# Source Code: https://git.scient.ing/papers/paperspec
|
|
# See the LICENSE.md file for full license terms.
|
|
|
|
"""Helpers shared by the package, corrections and command line tests.
|
|
|
|
They read the repository's fixtures, write edited copies of a package under a temporary
|
|
folder, and run `paperspec` commands in process.
|
|
"""
|
|
|
|
import json
|
|
import shutil
|
|
from collections.abc import Callable
|
|
from pathlib import Path
|
|
|
|
from conftest import FIXTURES, ROOT
|
|
from paperspec import crypto, jcs, package, resources
|
|
from paperspec.schema import Store
|
|
|
|
PACKAGES = FIXTURES / "packages"
|
|
INVALID = FIXTURES / "invalid"
|
|
HALE = PACKAGES / "hale2026harmonic-damb"
|
|
CHECKS_ONLY = PACKAGES / "hale2026harmonic-damb.checks-only"
|
|
TINY = PACKAGES / "tiny2026tiny-j4qm"
|
|
TINY_ZIP = PACKAGES / "tiny2026tiny-j4qm.sct.zip"
|
|
|
|
# Folders and files that are not part of the repository and are not copied.
|
|
COPY_IGNORE = shutil.ignore_patterns(
|
|
".git",
|
|
".pixi",
|
|
".claude",
|
|
".hypothesis",
|
|
".pytest_cache",
|
|
".ruff_cache",
|
|
"__pycache__",
|
|
"_site",
|
|
"work",
|
|
"tests",
|
|
"htmlcov",
|
|
"*.egg-info",
|
|
".DS_Store",
|
|
)
|
|
|
|
|
|
def store() -> Store:
|
|
"""The schema store of the installed package."""
|
|
return Store(resources.path("schemas"))
|
|
|
|
|
|
def team_key() -> bytes:
|
|
"""The HMAC key of the checks-only fixture, derived from the published team secret."""
|
|
secret = (FIXTURES / "keys" / "smithlab-team-secret.hex").read_text().strip()
|
|
return crypto.team_key(bytes.fromhex(secret))
|
|
|
|
|
|
def package_dirs() -> list[Path]:
|
|
"""The valid package folders of `fixtures/packages/`, by name."""
|
|
return sorted(p for p in PACKAGES.iterdir() if p.is_dir())
|
|
|
|
|
|
def key_for(path: Path) -> bytes | None:
|
|
"""The hashing key a fixture package needs to validate: the team key for a checks-only one."""
|
|
return team_key() if path.name.endswith("checks-only") else None
|
|
|
|
|
|
def read_package(path: Path) -> tuple[dict[str, bytes], list[dict]]:
|
|
"""The files of a package folder by path, and its manifest entries for absent files."""
|
|
manifest = json.loads((path / "MANIFEST.json").read_text(encoding="utf-8"))
|
|
files = {}
|
|
absent = []
|
|
for entry in manifest["files"]:
|
|
if entry.get("present", True) is False:
|
|
absent.append(entry)
|
|
else:
|
|
files[entry["path"]] = (path / entry["path"]).read_bytes()
|
|
return files, absent
|
|
|
|
|
|
def encode(path: str, value) -> bytes:
|
|
"""A parsed package file as the canonical bytes a producer writes."""
|
|
if path.endswith(".jsonl"):
|
|
return package.jsonl(value)
|
|
return package.line(value).encode("utf-8")
|
|
|
|
|
|
def decode(path: str, data: bytes):
|
|
"""A package file's records: a list for a JSON Lines file, the value for a JSON file."""
|
|
if path.endswith(".jsonl"):
|
|
return [json.loads(line) for line in data.split(b"\n") if line]
|
|
return json.loads(data)
|
|
|
|
|
|
def edit_package(
|
|
dest: Path, source: Path, edits: dict[str, Callable | bytes | None]
|
|
) -> Path:
|
|
"""Write a copy of package `source` at `dest/<name>` with `edits` applied, and a fresh manifest.
|
|
|
|
Each edit is keyed by a package path. A function takes the file's parsed records and returns the
|
|
new ones; bytes replace the file as they are; None removes the file.
|
|
"""
|
|
files, absent = read_package(source)
|
|
for path, change in edits.items():
|
|
if change is None:
|
|
files.pop(path, None)
|
|
elif isinstance(change, bytes):
|
|
files[path] = change
|
|
else:
|
|
files[path] = encode(path, change(decode(path, files[path])))
|
|
root = dest / source.name
|
|
package.write(root, files, absent)
|
|
return root
|
|
|
|
|
|
def rules_of(report) -> set[str]:
|
|
"""The rule IDs of a validation report."""
|
|
return {e["rule"] for e in report.errors}
|
|
|
|
|
|
def copy_repository(dest: Path) -> Path:
|
|
"""A copy of the repository's tracked content under `dest/paperspec`, for the commands that work on a checkout."""
|
|
root = dest / "paperspec"
|
|
shutil.copytree(ROOT, root, ignore=COPY_IGNORE)
|
|
return root
|
|
|
|
|
|
def canonical_line(value) -> str:
|
|
"""One canonical JSON line, as the package files hold records."""
|
|
return jcs.dumps(value) + "\n"
|