diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c63a14d..5ae9bb8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,7 +1,7 @@ name: CI -# Editable install against a freshly built cdylib at the pinned engine rev, -# then the full corpus-parity suite. Written, not yet run (no GitHub repo yet). +# Editable install against the Ruby tier cdylib at FONTISAN_REV, +# then the full corpus-parity suite. on: push: @@ -21,24 +21,24 @@ jobs: python: ['3.9', '3.12'] steps: - uses: actions/checkout@v4 - - name: Read the engine pin + - name: Read the Ruby source pin id: pin shell: bash - run: echo "rev=$(cat ENGINE_REV)" >> "$GITHUB_OUTPUT" + run: echo "rev=$(cat FONTISAN_REV)" >> "$GITHUB_OUTPUT" - uses: actions/checkout@v4 with: - repository: fontist/fontisan-rs + repository: fontist/fontisan ref: ${{ steps.pin.outputs.rev }} - path: fontisan-rs + path: fontisan-source - uses: dtolnay/rust-toolchain@stable - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python }} - name: Build the C-ABI cdylib - run: cargo build -p fontisan-ffi --release - working-directory: fontisan-rs - - name: Stage the cdylib and stamp the engine rev - run: python3 scripts/stage_cdylib.py fontisan-rs/target/release/libfontisan${{ matrix.os == 'macos-15' && '.dylib' || '.so' }} + run: cargo build --release --manifest-path ext/fontisan_cdylib/Cargo.toml + working-directory: fontisan-source + - name: Stage and verify the cdylib + run: python3 scripts/stage_cdylib.py --fontisan-source fontisan-source fontisan-source/ext/fontisan_cdylib/target/release/libfontisan_cdylib${{ matrix.os == 'macos-15' && '.dylib' || '.so' }} - name: Fetch the shared corpus run: python3 scripts/corpus_fetch.py - name: Install diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index bf7e365..8b020db 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -20,6 +20,7 @@ on: - 'pyproject.toml' - 'src/**' - 'ENGINE_REV' + - 'FONTISAN_REV' - 'CORPUS_REV' - 'scripts/**' - '.github/workflows/wheels.yml' @@ -34,26 +35,35 @@ jobs: fail-fast: false matrix: include: - - { name: linux-glibc-x86_64, os: ubuntu-22.04, lib: libfontisan.so, platform_tag: manylinux_2_35_x86_64 } - - { name: linux-musl-x86_64, os: ubuntu-22.04, lib: libfontisan.so, platform_tag: musllinux_1_2_x86_64, container: 'alpine:3.20' } - - { name: macos-arm64, os: macos-15, lib: libfontisan.dylib, platform_tag: macosx_11_0_arm64 } - - { name: macos-x64, os: macos-15-intel, lib: libfontisan.dylib, platform_tag: macosx_11_0_x86_64 } - - { name: windows-x64, os: windows-2022, lib: fontisan.dll, platform_tag: win_amd64 } + - { name: linux-glibc-x86_64, os: ubuntu-22.04, lib: libfontisan_cdylib.so, platform_tag: manylinux_2_35_x86_64 } + - { name: linux-musl-x86_64, os: ubuntu-22.04, lib: libfontisan_cdylib.so, platform_tag: musllinux_1_2_x86_64, container: 'alpine:3.20' } + - { name: macos-arm64, os: macos-15, lib: libfontisan_cdylib.dylib, platform_tag: macosx_11_0_arm64 } + - { name: macos-x64, os: macos-15-intel, lib: libfontisan_cdylib.dylib, platform_tag: macosx_11_0_x86_64 } + - { name: windows-x64, os: windows-2022, lib: fontisan_cdylib.dll, platform_tag: win_amd64 } container: ${{ matrix.container }} + env: + # Rust's musl target defaults to static CRT linkage, incompatible with cdylib. + RUSTFLAGS: ${{ matrix.container && '-C target-feature=-crt-static' || '' }} steps: - name: Alpine prerequisites (musl) if: matrix.container - run: apk add --no-cache bash git curl build-base python3 py3-pip py3-virtualenv rust cargo nodejs + run: apk add --no-cache bash git curl build-base python3 py3-pip py3-virtualenv nodejs + - name: Install stable Rust for musl + if: matrix.container + run: | + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o /tmp/rustup-init.sh + sh /tmp/rustup-init.sh -y --profile minimal --default-toolchain stable + echo "$HOME/.cargo/bin" >> "$GITHUB_PATH" - uses: actions/checkout@v4 - - name: Read the engine pin + - name: Read the Ruby source pin id: pin shell: bash - run: echo "rev=$(cat ENGINE_REV)" >> "$GITHUB_OUTPUT" + run: echo "rev=$(cat FONTISAN_REV)" >> "$GITHUB_OUTPUT" - uses: actions/checkout@v4 with: - repository: fontist/fontisan-rs + repository: fontist/fontisan ref: ${{ steps.pin.outputs.rev }} - path: fontisan-rs + path: fontisan-source - uses: dtolnay/rust-toolchain@stable if: ${{ !matrix.container }} - uses: actions/setup-python@v5 @@ -61,11 +71,11 @@ jobs: with: python-version: '3.12' - name: Build the cdylib - run: cargo build -p fontisan-ffi --release - working-directory: fontisan-rs - - name: Stage the vendored engine (stamps the engine rev) + run: cargo build --release --manifest-path ext/fontisan_cdylib/Cargo.toml + working-directory: fontisan-source + - name: Stage and verify the vendored engine shell: bash - run: python3 scripts/stage_cdylib.py "fontisan-rs/target/release/${{ matrix.lib }}" + run: python3 scripts/stage_cdylib.py --fontisan-source fontisan-source "fontisan-source/ext/fontisan_cdylib/target/release/${{ matrix.lib }}" - name: Build and retag the wheel shell: bash run: | diff --git a/ENGINE_REV b/ENGINE_REV index 654697e..b0039d0 100644 --- a/ENGINE_REV +++ b/ENGINE_REV @@ -1 +1 @@ -614dca354822aa5a20df50bbfb15e5e2f64f5c8a +f45057a12e15ba9f2c02526cd8ca0603c80edaf9 diff --git a/FONTISAN_REV b/FONTISAN_REV new file mode 100644 index 0000000..6f5afe3 --- /dev/null +++ b/FONTISAN_REV @@ -0,0 +1 @@ +2f4966d791354432d3780600188d026bfb61e864 diff --git a/README.md b/README.md index 187be5f..f20ffff 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,10 @@ The Python face of the [fontisan](https://github.com/fontist/fontisan) font engine (fontist/fontisan#166). A setuptools package that rides the existing -fontisan C-ABI cdylib (`fontisan-rs`, the same one the Ruby `:ffi` tier uses) -through `ctypes`, shipped as per-platform package data -(`fontisan/lib/libfontisan.{so,dylib}` / `fontisan.dll`). No second engine, no -new FFI surface. Modeled on `parsanol/parsanol-python`. +Ruby `:ffi` tier’s C-ABI cdylib (`fontist/fontisan` +`ext/fontisan_cdylib`) through `ctypes`, shipped as per-platform package data +(`fontisan/lib/libfontisan_cdylib.{so,dylib}` / `fontisan_cdylib.dll`). +Modeled on `parsanol/parsanol-python`. > **Provisional.** The package name `fontisan-python` and the Python floor > (`requires-python >= 3.9`) are placeholders. The PyPI name (`fontisan` vs @@ -38,21 +38,45 @@ with `status: "unsupported"`. Errors carry `kind` (`ErrorKind`, from ## Engine pin -`ENGINE_REV` holds the fontisan-rs commit the cdylib is built from. The pinned -engine exports no rev symbol, so `scripts/stage_cdylib.py` stamps -`fontisan/lib/ENGINE_REV` next to the vendored library; the loader refuses a -cdylib whose stamp (or `fontisan_cdylib_engine_rev`, where a newer engine -exports it) differs from the pin, or whose ABI version is not 1. +`FONTISAN_REV` pins the `fontist/fontisan` source commit containing +`ext/fontisan_cdylib`. Its `Cargo.toml` pins the `fontist/fontisan-rs` engine +dependency. `ENGINE_REV` and `src/fontisan/_engine.py` are generated from that +dependency using `scripts/stage_cdylib.py --update-engine-rev`; do not edit +them by hand. + +Staging requires a checkout at `FONTISAN_REV` with its committed Cargo.toml, +checks that both generated engine pins match the dependency, and loads the +built binary to check its `fontisan_cdylib_engine_rev` export before copying +it. The runtime loader also requires that export to match `ENGINE_REV` and +requires ABI version 1. There is no revision stamp file. ## Develop +Building requires Rust and Cargo. The binding tests also require `rustc` on +`PATH` to compile small cdylib fixtures for revision-validation checks. + ```sh -cargo build -p fontisan-ffi --release # in a fontisan-rs checkout at ENGINE_REV -python scripts/stage_cdylib.py /target/release/libfontisan.dylib +git clone https://github.com/fontist/fontisan /tmp/fontisan-source +git -C /tmp/fontisan-source checkout "$(cat FONTISAN_REV)" +cargo build --release --manifest-path /tmp/fontisan-source/ext/fontisan_cdylib/Cargo.toml +# macOS; use libfontisan_cdylib.so on Linux or fontisan_cdylib.dll on Windows +python scripts/stage_cdylib.py --fontisan-source /tmp/fontisan-source \ + /tmp/fontisan-source/ext/fontisan_cdylib/target/release/libfontisan_cdylib.dylib python scripts/corpus_fetch.py # shared fontisan corpus -> .corpus/fontisan pip install -e . pytest && pytest -q -s ``` +To change the source pin, update `FONTISAN_REV`, check out that commit, then +regenerate the engine pins before building and staging: + +```sh +python scripts/stage_cdylib.py --fontisan-source /tmp/fontisan-source --update-engine-rev +``` + +The generator and staging script support Python 3.9 without a TOML package. +They expect the pinned crate’s inline Git dependency declaration and fail if +upstream changes that layout. + `FONTISAN_CDYLIB` points at a cdylib elsewhere; `FONTISAN_CORPUS_DIR` at an existing corpus root (the directory that contains `corpus/`). @@ -70,5 +94,7 @@ not vendored; only the ci tier is used. `.github/workflows/wheels.yml` builds one wheel per platform (linux glibc and musl x86_64, macOS arm64 and x64, windows x64), retags each with its real platform tag, and smokes the installed wheel with the parity suite. -`ci.yml` runs the suite against a freshly built cdylib. Neither has been run -yet; only the local macOS arm64 build is verified. +`ci.yml` runs the suite against a freshly built cdylib. Both workflows check +out `fontist/fontisan` at `FONTISAN_REV`, build `ext/fontisan_cdylib`, and +verify the binary and Cargo dependency pin during staging. The wheels +workflow also runs when `FONTISAN_REV` changes. diff --git a/pyproject.toml b/pyproject.toml index 2b638c6..cbcc653 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ Repository = "https://github.com/fontist/fontisan-python" where = ["src"] [tool.setuptools.package-data] -fontisan = ["lib/*.so", "lib/*.dylib", "lib/*.dll", "lib/ENGINE_REV"] +fontisan = ["lib/libfontisan_cdylib.so", "lib/libfontisan_cdylib.dylib", "lib/fontisan_cdylib.dll"] [tool.pytest.ini_options] testpaths = ["tests"] diff --git a/scripts/corpus_fetch.py b/scripts/corpus_fetch.py index 51cf051..c8d2aaf 100644 --- a/scripts/corpus_fetch.py +++ b/scripts/corpus_fetch.py @@ -14,11 +14,21 @@ import json import os import shutil +import stat import subprocess import sys import urllib.request from pathlib import Path + +def _remove_readonly(function, path, exc_info): + """Windows cannot unlink Git's read-only pack files until made writable.""" + if not isinstance(exc_info[1], PermissionError): + raise exc_info[1] + os.chmod(path, os.stat(path).st_mode | stat.S_IWRITE) + function(path) + + root = Path(__file__).resolve().parents[1] dest = Path(sys.argv[1]) if len(sys.argv) > 1 else root / ".corpus" / "fontisan" rev = (root / "CORPUS_REV").read_text().strip() @@ -31,10 +41,14 @@ shutil.copytree(Path(src) / "corpus", corpus) else: tmp = dest / "_clone" - subprocess.check_call(["git", "clone", "--filter=blob:none", "--no-checkout", "https://github.com/fontist/fontisan", str(tmp)]) - subprocess.check_call(["git", "-C", str(tmp), "checkout", rev, "--", "corpus"]) + # Fetch all objects at the pin before checkout; no lazy blob hydration. + tmp.mkdir() + subprocess.check_call(["git", "-C", str(tmp), "init"]) + subprocess.check_call(["git", "-C", str(tmp), "remote", "add", "origin", "https://github.com/fontist/fontisan"]) + subprocess.check_call(["git", "-C", str(tmp), "fetch", "--depth=1", "origin", rev]) + subprocess.check_call(["git", "-C", str(tmp), "checkout", "FETCH_HEAD", "--", "corpus"]) shutil.move(str(tmp / "corpus"), str(corpus)) - shutil.rmtree(tmp) + shutil.rmtree(tmp, onerror=_remove_readonly) manifest = json.loads((corpus / "manifest.json").read_text()) n = 0 diff --git a/scripts/stage_cdylib.py b/scripts/stage_cdylib.py index 0e18f56..8378b42 100644 --- a/scripts/stage_cdylib.py +++ b/scripts/stage_cdylib.py @@ -1,24 +1,114 @@ #!/usr/bin/env python3 -"""Vendor a built fontisan cdylib into the package and stamp the engine rev. - -usage: stage_cdylib.py [] - -The rev defaults to the repo-root ENGINE_REV pin; the stamp (lib/ENGINE_REV) -is what the loader checks, because the pinned engine exports no rev symbol. -""" +"""Derive the engine pin from the pinned Ruby crate and verify a built cdylib.""" +import argparse +import ast +import ctypes +import re import shutil +import subprocess import sys from pathlib import Path -root = Path(__file__).resolve().parents[1] -src = Path(sys.argv[1]) -rev = sys.argv[2] if len(sys.argv) > 2 else (root / "ENGINE_REV").read_text().strip() -if rev != (root / "ENGINE_REV").read_text().strip(): - sys.exit(f"refusing to stage rev {rev}: repo pins {(root / 'ENGINE_REV').read_text().strip()}") -dest = root / "src" / "fontisan" / "lib" -dest.mkdir(parents=True, exist_ok=True) -for old in dest.iterdir(): - old.unlink() -shutil.copy2(src, dest / src.name) -(dest / "ENGINE_REV").write_text(rev + "\n") -print(f"staged {src.name} @ {rev} -> {dest}") +ROOT = Path(__file__).resolve().parents[1] +LIBRARIES = ("libfontisan_cdylib.so", "libfontisan_cdylib.dylib", "fontisan_cdylib.dll") +CARGO_PATH = "ext/fontisan_cdylib/Cargo.toml" + + +def source_engine_rev(source): + """Read the inline dependency used by the pinned standalone Rust crate. + + Keep this deliberately narrow so Python 3.9 needs no TOML dependency. If + upstream changes the dependency layout, fail instead of guessing a pin. + """ + source = source.resolve() + expected = (ROOT / "FONTISAN_REV").read_text().strip() + actual = subprocess.check_output( + ["git", "-C", str(source), "rev-parse", "HEAD"], text=True + ).strip() + if actual != expected: + raise ValueError(f"source checkout is {actual}; FONTISAN_REV pins {expected}") + manifest = (source / CARGO_PATH).read_text() + committed = subprocess.check_output( + ["git", "-C", str(source), "show", f"HEAD:{CARGO_PATH}"], text=True + ) + if manifest != committed: + raise ValueError("source Cargo.toml differs from the pinned commit") + section = re.search(r"(?ms)^\[dependencies\]\s*\n(.*?)(?=^\[|\Z)", manifest) + dependency = re.search(r"(?ms)^fontisan\s*=\s*\{([^}]+)\}", section[1]) if section else None + if dependency is None: + raise ValueError("expected an inline fontisan dependency in Cargo.toml") + git = re.search(r'\bgit\s*=\s*"([^"]+)"', dependency[1]) + rev = re.search(r'\brev\s*=\s*"([0-9a-f]{40})"', dependency[1]) + if git is None or git[1] != "https://github.com/fontist/fontisan-rs" or rev is None: + raise ValueError("expected fontist/fontisan-rs with an exact 40-character rev") + return rev[1] + + +def check_engine_pin(rev): + pinned = (ROOT / "ENGINE_REV").read_text().strip() + module = ast.parse((ROOT / "src/fontisan/_engine.py").read_text()) + generated = [ + ast.literal_eval(node.value) + for node in module.body + if isinstance(node, ast.Assign) + and any(isinstance(target, ast.Name) and target.id == "ENGINE_REV" for target in node.targets) + ] + if pinned != rev or generated != [rev]: + raise ValueError("engine pins differ from Cargo.toml; run with --update-engine-rev") + + +def update_engine_pin(rev): + (ROOT / "ENGINE_REV").write_text(rev + "\n") + (ROOT / "src/fontisan/_engine.py").write_text( + '\"\"\"Generated from the pinned fontisan cdylib Cargo.toml.\n\n' + 'Regenerate with scripts/stage_cdylib.py --fontisan-source --update-engine-rev.\n\"\"\"\n\n' + f'ENGINE_REV = "{rev}"\n' + ) + + +def stage(binary, rev): + binary = binary.resolve() + if binary.name not in LIBRARIES: + raise ValueError(f"unexpected library name: {binary.name}") + library = ctypes.CDLL(str(binary)) + exported_rev = library.fontisan_cdylib_engine_rev + exported_rev.argtypes = [] + exported_rev.restype = ctypes.c_char_p + actual = exported_rev() + if actual != rev.encode("ascii"): + raise ValueError(f"binary exports engine rev {actual!r}; Cargo.toml pins {rev}") + dest = ROOT / "src/fontisan/lib" + dest.mkdir(parents=True, exist_ok=True) + # Validate everything before replacing any previously staged library. + target = dest / binary.name + if binary != target.resolve(): + shutil.copy2(binary, target) + for name in (*LIBRARIES, "libfontisan.so", "libfontisan.dylib", "fontisan.dll", "ENGINE_REV"): + old = dest / name + if old != target: + old.unlink(missing_ok=True) + print(f"staged {binary.name} @ {rev} -> {dest}") + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("binary", nargs="?", type=Path, help="built cdylib to verify and stage") + parser.add_argument("--fontisan-source", required=True, type=Path, help="checkout at FONTISAN_REV") + parser.add_argument("--update-engine-rev", action="store_true", help="regenerate ENGINE_REV and _engine.py from Cargo.toml") + args = parser.parse_args() + if args.binary is None and not args.update_engine_rev: + parser.error("binary is required unless --update-engine-rev is used") + try: + rev = source_engine_rev(args.fontisan_source) + if args.update_engine_rev: + update_engine_pin(rev) + print(f"generated engine pins @ {rev}") + check_engine_pin(rev) + if args.binary is not None: + stage(args.binary, rev) + except (OSError, ValueError, AttributeError, subprocess.CalledProcessError) as exc: + sys.exit(f"refusing to stage: {exc}") + + +if __name__ == "__main__": + main() diff --git a/src/fontisan/_engine.py b/src/fontisan/_engine.py index e1eff21..958c4c1 100644 --- a/src/fontisan/_engine.py +++ b/src/fontisan/_engine.py @@ -1,4 +1,6 @@ -"""The fontisan-rs rev this package is built against (kept in step with the -repo-root ENGINE_REV file by tests/test_engine_rev.py).""" +"""Generated from the pinned fontisan cdylib Cargo.toml. -ENGINE_REV = "614dca354822aa5a20df50bbfb15e5e2f64f5c8a" +Regenerate with scripts/stage_cdylib.py --fontisan-source --update-engine-rev. +""" + +ENGINE_REV = "f45057a12e15ba9f2c02526cd8ca0603c80edaf9" diff --git a/src/fontisan/_ffi.py b/src/fontisan/_ffi.py index f03bb85..9e858e7 100644 --- a/src/fontisan/_ffi.py +++ b/src/fontisan/_ffi.py @@ -41,7 +41,7 @@ READ_LAZY = 2 AUDIT_VALIDATE = 1 -_LIB_NAMES = ("libfontisan.dylib", "libfontisan.so", "fontisan.dll", "libfontisan.dll") +_LIB_NAMES = ("libfontisan_cdylib.dylib", "libfontisan_cdylib.so", "fontisan_cdylib.dll", "libfontisan_cdylib.dll") class EngineError(RuntimeError): @@ -118,14 +118,6 @@ def _candidate_paths() -> list[str]: return out -def _stamped_rev(lib_path: str) -> Optional[str]: - stamp = Path(lib_path).resolve().parent / "ENGINE_REV" - try: - return stamp.read_text().strip() or None - except OSError: - return None - - def load() -> ctypes.CDLL: """dlopen the cdylib once, bind it, and check ABI version and engine rev.""" global _lib, _lib_path @@ -147,8 +139,9 @@ def load() -> ctypes.CDLL: errors.append(f"{candidate}: {error}") if lib is None or path is None: raise EngineError( - "libfontisan not found (build it with `cargo build -p fontisan-ffi " - "--release` and point FONTISAN_CDYLIB at it, or install a wheel that " + "libfontisan_cdylib not found (build it with `cargo build --release " + "--manifest-path ext/fontisan_cdylib/Cargo.toml` in fontisan at FONTISAN_REV " + "and point FONTISAN_CDYLIB at it, or install a wheel that " "vendors it). Tried:\n " + "\n ".join(errors) ) @@ -163,18 +156,21 @@ def load() -> ctypes.CDLL: if found != ABI_VERSION: raise EngineError(f"{path} speaks ABI {found}, this package expects {ABI_VERSION}") - # Newer engines export the rev they were built from; engines at the pinned - # rev do not, so the build stamps lib/ENGINE_REV instead. Either must match. - reported = None - if hasattr(lib, "fontisan_cdylib_engine_rev"): - lib.fontisan_cdylib_engine_rev.argtypes = [] - lib.fontisan_cdylib_engine_rev.restype = ctypes.c_char_p - raw = lib.fontisan_cdylib_engine_rev() - reported = raw.decode() if raw else None - stamped = _stamped_rev(path) - for source, rev in (("symbol", reported), ("ENGINE_REV stamp", stamped)): - if rev is not None and rev != ENGINE_REV: - raise EngineError(f"{path} was built from engine {rev} ({source}), this package pins {ENGINE_REV}") + try: + revision = lib.fontisan_cdylib_engine_rev + except AttributeError as error: + raise EngineError(f"{path} exports no engine rev (fontisan_cdylib_engine_rev)") from error + revision.argtypes = [] + revision.restype = ctypes.c_char_p + raw = revision() + if not raw: + raise EngineError(f"{path} exports an empty engine rev") + try: + reported = raw.decode("ascii") + except UnicodeDecodeError as error: + raise EngineError(f"{path} exports an invalid engine rev") from error + if reported != ENGINE_REV: + raise EngineError(f"{path} was built from engine {reported}, this package pins {ENGINE_REV}") _lib, _lib_path = lib, path return lib @@ -185,19 +181,9 @@ def cdylib_path() -> Optional[str]: return _lib_path -def engine_rev() -> Optional[str]: - """The fontisan-rs rev the loaded cdylib was built from. - - None when the cdylib carries neither the symbol nor the stamp (a cdylib - pointed at through FONTISAN_CDYLIB without a sibling ENGINE_REV): the pin - is then unverified rather than assumed. - """ - lib = load() - if hasattr(lib, "fontisan_cdylib_engine_rev"): - raw = lib.fontisan_cdylib_engine_rev() - if raw: - return raw.decode() - return _stamped_rev(_lib_path or "") +def engine_rev() -> str: + """The verified fontisan-rs rev exported by the loaded cdylib.""" + return load().fontisan_cdylib_engine_rev().decode("ascii") def abi_version() -> int: @@ -238,7 +224,7 @@ def call(op: int, input_bytes: bytes, flags: int = 0) -> Reply: def lib_filename() -> str: if sys.platform == "darwin": - return "libfontisan.dylib" + return "libfontisan_cdylib.dylib" if sys.platform.startswith("win"): - return "fontisan.dll" - return "libfontisan.so" + return "fontisan_cdylib.dll" + return "libfontisan_cdylib.so" diff --git a/tests/conftest.py b/tests/conftest.py index eb87be1..bb7d7e1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -38,4 +38,4 @@ def font_path(corpus: Path, name: str) -> Path: def load_json(path: Path): - return json.loads(path.read_text()) + return json.loads(path.read_text(encoding="utf-8")) diff --git a/tests/test_binding.py b/tests/test_binding.py index 6fd2ff4..e889d0c 100644 --- a/tests/test_binding.py +++ b/tests/test_binding.py @@ -3,6 +3,10 @@ from __future__ import annotations import ctypes +import importlib.util +import os +import shlex +import subprocess from pathlib import Path import pytest @@ -31,14 +35,126 @@ def test_engine_rev_pin_matches_repo_file_and_loaded_cdylib(): assert fontisan.engine_rev() == ENGINE_REV -def test_mismatched_stamp_is_refused(tmp_path, monkeypatch): - lib = Path(fontisan.cdylib_path()) - (tmp_path / lib.name).write_bytes(lib.read_bytes()) - (tmp_path / "ENGINE_REV").write_text("0" * 40 + "\n") +@pytest.fixture +def revision_cdylib(tmp_path): + """Build real ABI stubs so revision failures exercise dlopen and symbols.""" + def build(rev, abi=1): + source = tmp_path / "revision.rs" + exports = f""" +#[no_mangle] +pub extern "C" fn fontisan_abi_version() -> u32 {{ {abi} }} +#[no_mangle] +pub extern "C" fn fontisan_call() -> i32 {{ 0 }} +#[no_mangle] +pub extern "C" fn fontisan_result_free() {{}} +""" + if rev is not None: + value = 'std::ptr::null()' if rev == "null" else rf'b"{rev}\0".as_ptr()' + exports += f""" +#[no_mangle] +pub extern "C" fn fontisan_cdylib_engine_rev() -> *const u8 {{ {value} }} +""" + source.write_text(exports) + library = tmp_path / _ffi.lib_filename() + subprocess.run( + ["rustc", *shlex.split(os.environ.get("RUSTFLAGS", "")), + "--crate-type", "cdylib", str(source), "-o", str(library)], + check=True, capture_output=True, text=True, + ) + return library + return build + + +@pytest.mark.parametrize("rev", ["0" * 40, None, "", "null"]) +def test_unverified_or_mismatched_export_is_refused(revision_cdylib, monkeypatch, rev): + library = revision_cdylib(rev) + # A matching legacy stamp must not rescue a missing or incorrect export. + (library.parent / "ENGINE_REV").write_text(ENGINE_REV + "\n") monkeypatch.setattr(_ffi, "_lib", None) - monkeypatch.setenv("FONTISAN_CDYLIB", str(tmp_path / lib.name)) - with pytest.raises(fontisan.EngineError, match="built from engine"): + monkeypatch.setattr(_ffi, "_lib_path", None) + monkeypatch.setenv("FONTISAN_CDYLIB", str(library)) + with pytest.raises(fontisan.EngineError, match="engine rev|built from engine"): _ffi.load() + assert _ffi._lib is None + assert _ffi._lib_path is None + + +def test_exported_pin_is_used_without_a_stamp(revision_cdylib, monkeypatch): + library = revision_cdylib(ENGINE_REV) + monkeypatch.setattr(_ffi, "_lib", None) + monkeypatch.setattr(_ffi, "_lib_path", None) + monkeypatch.setenv("FONTISAN_CDYLIB", str(library)) + assert fontisan.engine_rev() == ENGINE_REV + assert fontisan.cdylib_path() == str(library) + # Old staging metadata is no longer an authority for the binary revision. + (library.parent / "ENGINE_REV").write_text("0" * 40 + "\n") + monkeypatch.setattr(_ffi, "_lib", None) + assert fontisan.engine_rev() == ENGINE_REV + + +def test_mismatched_abi_is_refused(revision_cdylib, monkeypatch): + library = revision_cdylib(ENGINE_REV, abi=2) + monkeypatch.setattr(_ffi, "_lib", None) + monkeypatch.setenv("FONTISAN_CDYLIB", str(library)) + with pytest.raises(fontisan.EngineError, match="speaks ABI 2"): + _ffi.load() + + +@pytest.fixture +def stage_script(tmp_path, monkeypatch): + spec = importlib.util.spec_from_file_location("stage_cdylib", ROOT / "scripts/stage_cdylib.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + monkeypatch.setattr(module, "ROOT", tmp_path) + return module + + +def test_staging_rejects_wrong_export_before_replacing_library(stage_script, revision_cdylib): + library = revision_cdylib("0" * 40) + dest = stage_script.ROOT / "src/fontisan/lib" + dest.mkdir(parents=True) + staged = dest / library.name + staged.write_bytes(b"previous library") + with pytest.raises(ValueError, match="binary exports engine rev"): + stage_script.stage(library, ENGINE_REV) + assert staged.read_bytes() == b"previous library" + + +def test_staging_uses_new_library_name_without_stamp(stage_script, revision_cdylib): + library = revision_cdylib(ENGINE_REV) + dest = stage_script.ROOT / "src/fontisan/lib" + dest.mkdir(parents=True) + (dest / "ENGINE_REV").write_text(ENGINE_REV) + (dest / "libfontisan.so").write_bytes(b"legacy library") + stage_script.stage(library, ENGINE_REV) + assert sorted(path.name for path in dest.iterdir()) == [library.name] + assert (dest / library.name).read_bytes() == library.read_bytes() + + +def test_engine_pin_is_generated_from_pinned_crate(stage_script, monkeypatch): + source = stage_script.ROOT / "fontisan" + manifest = source / stage_script.CARGO_PATH + manifest.parent.mkdir(parents=True) + manifest.write_text( + '[dependencies]\n' + 'fontisan = { git = "https://github.com/fontist/fontisan-rs", ' + f'rev = "{ENGINE_REV}", features = ["ffi"] }}\n' + ) + revision = "1" * 40 + (stage_script.ROOT / "FONTISAN_REV").write_text(revision) + monkeypatch.setattr( + stage_script.subprocess, "check_output", + lambda args, **kwargs: revision if "rev-parse" in args else manifest.read_text(), + ) + (stage_script.ROOT / "src/fontisan").mkdir(parents=True) + derived = stage_script.source_engine_rev(source) + assert derived == ENGINE_REV + stage_script.update_engine_pin(derived) + stage_script.check_engine_pin(derived) + assert (stage_script.ROOT / "ENGINE_REV").read_text().strip() == ENGINE_REV + (stage_script.ROOT / "FONTISAN_REV").write_text("2" * 40) + with pytest.raises(ValueError, match="source checkout"): + stage_script.source_engine_rev(source) def test_garbage_font_is_a_typed_error_not_a_crash(): diff --git a/tests/test_corpus_fetch.py b/tests/test_corpus_fetch.py new file mode 100644 index 0000000..665f4ad --- /dev/null +++ b/tests/test_corpus_fetch.py @@ -0,0 +1,91 @@ +import hashlib +import json +import os +import runpy +import stat +import subprocess +import sys +from pathlib import Path + +import pytest + + +SCRIPT = Path(__file__).parents[1] / "scripts" / "corpus_fetch.py" + + +def git(path, *args, env=None): + return subprocess.check_output(["git", "-C", str(path), *args], env=env, text=True).strip() + + +def test_fetches_pinned_corpus_without_partial_clone(tmp_path): + source = tmp_path / "source" + source.mkdir() + git(source, "init") + git(source, "config", "user.email", "fixture@example.test") + git(source, "config", "user.name", "fixture") + corpus = source / "corpus" + (corpus / "fonts" / "fixture").mkdir(parents=True) + derived = b"pinned derived font bytes" + (corpus / "fonts" / "fixture" / "derived.ttf").write_bytes(derived) + manifest = {"fonts": [{"id": "fixture", "type": "derived", "tier": "ci", "file": "derived.ttf"}]} + (corpus / "manifest.json").write_text(json.dumps(manifest)) + source_bytes = b"pinned source font bytes" + source_file = tmp_path / "source-font.ttf" + source_file.write_bytes(source_bytes) + source_entry = { + "id": "source-fixture", "type": "source", "tier": "ci", "file": "source.ttf", + "sha256": hashlib.sha256(source_bytes).hexdigest(), + "source": {"url": source_file.as_uri()}, + } + manifest["fonts"].append(source_entry) + (corpus / "manifest.json").write_text(json.dumps(manifest)) + git(source, "add", "corpus") + git(source, "-c", "commit.gpgsign=false", "commit", "-m", "pinned corpus") + pin = git(source, "rev-parse", "HEAD") + (corpus / "fonts" / "fixture" / "derived.ttf").write_bytes(b"later HEAD bytes") + git(source, "-c", "commit.gpgsign=false", "commit", "-am", "later corpus") + + harness = tmp_path / "harness" + (harness / "scripts").mkdir(parents=True) + (harness / "scripts" / "corpus_fetch.py").write_bytes(SCRIPT.read_bytes()) + (harness / "CORPUS_REV").write_text(pin) + # Git's URL rewrite substitutes the local fixture for the script's fixed remote. + env = os.environ.copy() + env.pop("FONTISAN_CORPUS_SRC", None) + env.update({"GIT_CONFIG_COUNT": "1", "GIT_CONFIG_KEY_0": "url." + source.as_uri() + ".insteadOf", + "GIT_CONFIG_VALUE_0": "https://github.com/fontist/fontisan"}) + dest = tmp_path / "result" + subprocess.check_call([sys.executable, str(harness / "scripts" / "corpus_fetch.py"), str(dest)], env=env) + + assert (dest / "corpus" / "fonts" / "fixture" / "derived.ttf").read_bytes() == derived + source_target = dest / "corpus" / "fonts" / "source-fixture" / "source.ttf" + assert source_target.read_bytes() == source_bytes + assert json.loads((dest / "corpus" / "manifest.json").read_text()) == manifest + assert not (dest / "_clone").exists() + + source_target.write_bytes(b"corrupted bytes") + subprocess.check_call([sys.executable, str(harness / "scripts" / "corpus_fetch.py"), str(dest)], env=env) + assert source_target.read_bytes() == source_bytes + assert hashlib.sha256(source_target.read_bytes()).hexdigest() == source_entry["sha256"] + + +def test_cleanup_retries_readonly_git_pack_and_propagates_other_errors(tmp_path, monkeypatch): + corpus = tmp_path / "corpus" + corpus.mkdir() + (corpus / "manifest.json").write_text('{"fonts": []}') + monkeypatch.setattr(sys, "argv", [str(SCRIPT), str(tmp_path)]) + cleanup = runpy.run_path(str(SCRIPT))["_remove_readonly"] + pack = tmp_path / "pack.pack" + pack.write_bytes(b"pack") + pack.chmod(stat.S_IREAD) + + def windows_unlink(path): + if not path.stat().st_mode & stat.S_IWRITE: + raise PermissionError("read-only pack") + path.unlink() + + cleanup(windows_unlink, pack, (PermissionError, PermissionError("read-only pack"), None)) + assert not pack.exists() + error = OSError("other cleanup error") + with pytest.raises(OSError, match="other cleanup error"): + cleanup(windows_unlink, pack, (OSError, error, None)) diff --git a/tests/test_golden_loading.py b/tests/test_golden_loading.py new file mode 100644 index 0000000..b344752 --- /dev/null +++ b/tests/test_golden_loading.py @@ -0,0 +1,17 @@ +"""Goldens use UTF-8 regardless of the runner's locale.""" + +from pathlib import Path + +from conftest import load_json + + +def test_load_json_preserves_unicode_with_windows_default_encoding(tmp_path, monkeypatch): + golden = tmp_path / "golden.json" + golden.write_bytes('{"name": "Mix Café", "message": "widths — alignment"}'.encode("utf-8")) + original_read_text = Path.read_text + + def windows_read_text(path, encoding=None, errors=None, **kwargs): + return original_read_text(path, encoding=encoding or "cp1252", errors=errors, **kwargs) + + monkeypatch.setattr(Path, "read_text", windows_read_text) + assert load_json(golden) == {"name": "Mix Café", "message": "widths — alignment"}