# 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. """`paperspec build`: the fixtures and vectors it writes are the committed ones, byte for byte.""" from pathlib import Path import pytest from conftest import FIXTURES, VECTORS from helpers_cli import copy_repository from paperspec.cli import build, main as cli_main # Files that are hand-written or local, not output of the build. NOT_BUILT = {"README.md", ".DS_Store"} def committed(folder: Path, *, skip_src: bool = False) -> list[str]: """The built files under `folder`, by path relative to it.""" files = [] for path in sorted(folder.rglob("*")): rel = path.relative_to(folder) if not path.is_file() or path.name in NOT_BUILT: continue if skip_src and rel.parts[0] == "src": continue files.append(rel.as_posix()) return files FIXTURE_FILES = committed(FIXTURES, skip_src=True) VECTOR_FILES = committed(VECTORS) @pytest.fixture(scope="module") def rebuilt(tmp_path_factory): out = tmp_path_factory.mktemp("rebuilt") status = cli_main.main(["build", "--out", str(out)]) assert status == 0 return out class TestBuildElsewhere: @pytest.mark.parametrize("name", VECTOR_FILES) def test_every_vector_file_is_rebuilt_byte_for_byte(self, rebuilt, name): assert (rebuilt / "vectors" / name).read_bytes() == ( VECTORS / name ).read_bytes() @pytest.mark.parametrize("name", FIXTURE_FILES) def test_every_fixture_file_is_rebuilt_byte_for_byte(self, rebuilt, name): assert (rebuilt / "fixtures" / name).read_bytes() == ( FIXTURES / name ).read_bytes() def test_it_builds_exactly_the_committed_vector_files(self, rebuilt): assert committed(rebuilt / "vectors") == VECTOR_FILES def test_it_builds_exactly_the_committed_fixture_files(self, rebuilt): assert committed(rebuilt / "fixtures", skip_src=True) == FIXTURE_FILES def test_the_sources_the_cases_read_are_copied_beside_the_output(self, rebuilt): for name in committed(FIXTURES / "src"): assert (rebuilt / "fixtures" / "src" / name).read_bytes() == ( FIXTURES / "src" / name ).read_bytes() assert (rebuilt / "registry" / "rules.json").is_file() def test_a_second_build_replaces_what_the_first_left(self, rebuilt, tmp_path): out = tmp_path / "again" (out / "vectors").mkdir(parents=True) (out / "vectors" / "stale.jsonl").write_text("{}\n") (out / "vectors" / "kept.txt").write_text("kept\n") (out / "fixtures" / "packages" / "stale").mkdir(parents=True) (out / "fixtures" / "packages" / "stale" / "file").write_text("x") assert cli_main.main(["build", "--out", str(out)]) == 0 assert not (out / "vectors" / "stale.jsonl").exists() assert not (out / "fixtures" / "packages" / "stale").exists() assert (out / "vectors" / "kept.txt").read_text() == "kept\n" assert committed(out / "vectors") == sorted([*VECTOR_FILES, "kept.txt"]) def test_it_prints_nothing_when_it_builds_elsewhere(self, tmp_path, capsys): assert cli_main.main(["build", "--out", str(tmp_path / "quiet")]) == 0 assert capsys.readouterr() == ("", "") class TestBuildInPlace: def test_rebuilding_a_checkout_leaves_it_as_it_was( self, tmp_path, monkeypatch, capsys ): root = copy_repository(tmp_path) before = {n: (root / "vectors" / n).read_bytes() for n in VECTOR_FILES} before |= { "fixtures/" + n: (root / "fixtures" / n).read_bytes() for n in FIXTURE_FILES } (root / "vectors" / "normalize.jsonl").write_text("{}\n") monkeypatch.setattr(build, "ROOT", root) status = cli_main.main(["build"]) out = capsys.readouterr().out assert status == 0 assert out.startswith(f"Rebuilt fixtures/ and vectors/ in {root}.") assert "git diff" in out after = {n: (root / "vectors" / n).read_bytes() for n in VECTOR_FILES} after |= { "fixtures/" + n: (root / "fixtures" / n).read_bytes() for n in FIXTURE_FILES } assert after == before