Skip to content

Latest commit

 

History

History
141 lines (110 loc) · 7.94 KB

fixture_lib.md

File metadata and controls

141 lines (110 loc) · 7.94 KB

fixture_lib.sh

Revision: 1 Last modified: 2026-12-04T18:15:21Z

Overview

This is a library. It is sourced by the tests/sync/* suites, never executed. Mode rw-rw-r--.

scripts/sync/fixture_lib.sh builds real git repositories for the sync tests: bare "upstream" repositories under $SB/remotes/<org>/<name>.git, producer clones, working clones with real submodules (via file:// URLs), plus a small assertion vocabulary and a runner that invokes the engine through a recording shim.

Nothing here touches the network, a forge, $HOME, or the repository the tests run from. That is the point: the tests exercise the real engine and the real git binary against real repositories, which is LIVE-class for git behaviour and fixture-class only for the network aspects (§11.4.289).

Two design details worth knowing:

  • The engine under test runs with PATH starting at a recording shim that logs every argument vector and then execs the same real git. So a test can assert which git calls the engine made, without replacing git.
  • The file lives outside tests/ on purpose (see the header's own note): the full-suite census counts every script under tests/, and a sourced helper there would need an excluded row in scripts/run_full_suite.sh.

Prerequisites

  • bash, git, cksum, cut, mktemp, python3 (only for sx_q).
  • scripts/lib/safe_rm.sh must exist — this file sources it and routes every recursive delete through safe_rm_rf, restricted by SAFE_RM_ALLOWED_ROOTS to the sandbox.
  • Sourced after any T_NAME you want in the summary line (default sync-tests).

Usage examples

In a test file:

. "$REPO/scripts/sync/fixture_lib.sh"

sx_sandbox                                   # create $SB, set git config, install the shim, trap cleanup
U_A="$(sx_bare HelixDevelopment liba)"       # a bare upstream with one commit on main
W="$(sx_seed "$U_A")"                        # a scratch producer clone
sx_commit "$U_A" "second commit"             # a new commit on the bare upstream's main
sx_clone "$U_A" "$SB/work/consumer"          # a working clone

sx_run --check --root "$SB/work/consumer"    # runs the engine through the shim
eq "check reports updates" 3 "$SX_RC"
has  "report names the repo" "consumer" "$(cat "$SX_JSON")"

sx_std_tree                                  # the standard own-org + nested + third-party tree
finish                                       # prints "NAME: PASS n FAIL m"; returns non-zero on any failure

Helper inventory:

Helper Effect
ok MSG / nok MSG count a pass / a failure and print a line
eq NAME EXPECTED ACTUAL equality assertion
has NAME NEEDLE HAYSTACK / hasnt NAME NEEDLE HAYSTACK substring present / absent
finish print NAME: PASS n FAIL m; returns 1 if any failure
sx_sandbox create $SB, write a private gitconfig, install the recording shim, trap cleanup
sx_cleanup chmod -R u+w then safe_rm_rf the sandbox
sx_bare ORG NAME new bare repository with one commit on main; prints its file:// URL
sx_seed URL scratch producer clone for that URL (cached per URL, cksum-keyed)
sx_commit URL MESSAGE a new commit on the bare repository's main
sx_add_sub PARENT_URL CHILD_URL PATH parent records the child as a submodule at PATH
sx_bump_pin PARENT_URL PATH the parent's gitlink at PATH moves to the child's current upstream main
sx_clone URL DEST working clone with all submodules initialised recursively
sx_run ARGS... run the engine through the shim; sets SX_RC, SX_OUT, SX_ERR, SX_JSON, SX_TSV
sx_q EXPR evaluate a Python expression over the last report, with r bound to it and P = path→repo
sx_head DIR / sx_remote_head DIR rev-parse HEAD of a work tree / rev-parse main of a bare repo
sx_std_tree the standard tree: own-org libs/a with nested own-org inner, plus third-party vendor/t; sets U_ROOT U_A U_IN U_T W

Exit codes and return values

The library does not exit on its own (except via sx_sandbox's exit 2 when mktemp fails).

Call Returns / sets
sx_sandbox exit 2 if mktemp fails; otherwise sets SB and cds into it
sx_run sets SX_RC to the engine's exit code; never fails the test by itself
finish 0 when T_FAIL == 0, 1 otherwise
ok / nok / eq / has / hasnt increment T_PASS / T_FAIL; return 0
sx_q prints the expression's value, or nothing when the report cannot be read
others 0 on success; a git failure propagates from the underlying git call

Edge cases

  • sx_sandbox must be called first. It creates $SB and cds into it; every other helper assumes $SB.
  • TMPDIR unset — mktemp -d "${TMPDIR:-/tmp}/helix-sync-test.XXXXXX" falls back to /tmp.
  • No global core.hooksPath is deliberately written into the sandbox gitconfig (a review finding: a global hooksPath hid hook regressions).
  • GIT_CEILING_DIRECTORIES is set to the sandbox's parent, so git discovery cannot escape upward into a real repository.
  • XDG_CACHE_HOME is redirected into the sandbox so a cache cannot leak between runs.
  • Repository-locating GIT_* variables are cleared at source time, so an inherited GIT_DIR cannot redirect the fixtures.
  • The recording shim writes one line per call with O_APPEND and under PIPE_BUF, so concurrent engine calls never interleave inside a line (the log stays parseable under --jobs).
  • sx_q uses eval on a literal written by the test author — the header says so explicitly. It is not an input path; do not pass external data through it.
  • sx_cleanup is registered with trap … EXIT, so an aborted test still removes the sandbox.

Internal behaviour

  1. Resolves SX_REPO from its own location, sets SX_ENGINE (overridable), sources scripts/lib/safe_rm.sh, records the real git path in SX_GIT, and pins deterministic git identity/dates (GIT_AUTHOR_DATE/GIT_COMMITTER_DATE = 2026-01-01T00:00:00Z) so fixtures are reproducible.
  2. sx_sandbox creates the sandbox with mktemp -d, sets SAFE_RM_ALLOWED_ROOTS="$SB", writes a private gitconfig, creates remotes/ work/ seed/ out/ shim/, sets GIT_CEILING_DIRECTORIES and XDG_CACHE_HOME, writes the recording shim to $SB/shim/git, traps cleanup, and cds into $SB.
  3. sx_bare creates a bare repository, seeds one commit in a scratch clone, and pushes main — a real upstream.
  4. sx_seed caches one producer clone per URL; a second call pull --ff-onlys it.
  5. sx_run increments SX_N, invokes the engine with --out and --run-id unique to that call, with PATH="$SB/shim:$PATH", capturing stdout/stderr separately and recording the exit code.
  6. sx_std_tree composes the canonical tree: a nested own-org child inside an own-org parent, plus a third-party submodule, then clones it — the shape the own-org/nesting rules need.

Related scripts

  • scripts/sync/sync_all_upstreams.sh / .py — the engine under test.
  • scripts/lib/safe_rm.sh — safe_rm_rf, the only sanctioned recursive delete used here.
  • scripts/sync/robustness_checks.py — the behavioural suite that drives the engine directly.
  • scripts/sync/race_git_shim.py — the shim that acts as a user mid-run.
  • scripts/sync/inproc_driver.py — in-process fault injection for the engine.
  • tests/sync/* — the consumers of this library.

Last verified

2026-12-04 — bash -n scripts/sync/fixture_lib.sh → exit 0 (a library: sourced, not executed).

A reader can confirm the library loads and the shim machinery builds without running a test suite:

bash -c 'cd "$(mktemp -d)" && . /path/to/constitution/scripts/sync/fixture_lib.sh \
  && sx_sandbox && echo "SB=$SB" && ls && finish'

Expected: SB=<a temp dir>, the directories remotes work seed out shim listed, then sync-tests: PASS 0 FAIL 0 (the suite name defaults to sync-tests).