|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Contract (#2003): the test runner must be immune to an inherited git |
| 3 | +# repository environment. |
| 4 | +# |
| 5 | +# Git exports GIT_DIR, GIT_INDEX_FILE, ... into hooks, and those variables take |
| 6 | +# precedence over `git -C <dir>`. The pre-commit hook runs the suite, so every |
| 7 | +# fixture git command in the runner used to act on the committer's REAL |
| 8 | +# repository: fixture assertions failed, and branches, commits and linked |
| 9 | +# worktrees were written into the contributor's clone. The runner now clears |
| 10 | +# git's local-env list at startup (th_clear_git_repo_env in tests/test_helpers.h). |
| 11 | +# |
| 12 | +# This drives the real runner with those variables aimed at a throwaway decoy |
| 13 | +# repository and asserts both halves: the git fixture suite passes, and the |
| 14 | +# decoy is left exactly as it was (no refs, no linked worktrees). |
| 15 | +# |
| 16 | +# Usage: tests/test_git_env_isolation_contract.sh [path/to/test-runner] |
| 17 | +set -euo pipefail |
| 18 | + |
| 19 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 20 | +RUNNER="${1:-$ROOT/build/c/test-runner}" |
| 21 | +SUITE="git_context" |
| 22 | + |
| 23 | +if [ ! -x "$RUNNER" ]; then |
| 24 | + echo "FAIL: test runner not found at '$RUNNER'; build it first (make -f Makefile.cbm build/c/test-runner)." >&2 |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | +if ! command -v git >/dev/null 2>&1; then |
| 28 | + echo "SKIP: git is not on PATH, so there is no git environment to leak." |
| 29 | + exit 0 |
| 30 | +fi |
| 31 | + |
| 32 | +# This script's own git calls must address the decoy, not whatever repository |
| 33 | +# the caller (e.g. the pre-commit hook) exported. |
| 34 | +while IFS= read -r variable; do |
| 35 | + unset "$variable" |
| 36 | +done < <(git rev-parse --local-env-vars) |
| 37 | + |
| 38 | +WORK="$(mktemp -d "${TMPDIR:-/tmp}/cbm-git-env-XXXXXX")" |
| 39 | +cleanup() { rm -rf "$WORK"; } |
| 40 | +trap cleanup EXIT |
| 41 | + |
| 42 | +decoy_state() { |
| 43 | + printf 'refs:\n' |
| 44 | + git -C "$1" for-each-ref |
| 45 | + printf 'worktrees:\n' |
| 46 | + git -C "$1" worktree list --porcelain |
| 47 | +} |
| 48 | + |
| 49 | +# Baseline: the same suite in a clean environment. Comparing summaries (not just |
| 50 | +# exit codes) matters: with GIT_WORK_TREE inherited, fixture commits fail and |
| 51 | +# the suite SKIPs as "git not available" instead of failing -- a silent green. |
| 52 | +BASE_LOG="$WORK/baseline.log" |
| 53 | +"$RUNNER" "$SUITE" >"$BASE_LOG" 2>&1 || { |
| 54 | + echo "FAIL: '$SUITE' is red even in a clean environment; fix that first." >&2 |
| 55 | + grep -E 'FAIL|passed' "$BASE_LOG" >&2 || true |
| 56 | + exit 1 |
| 57 | +} |
| 58 | + |
| 59 | +summary() { grep -E '^ *[0-9]+ passed' "$1" | tail -1; } |
| 60 | +BASE_SUMMARY="$(summary "$BASE_LOG")" |
| 61 | +FAILED=0 |
| 62 | + |
| 63 | +# Two inherited shapes: GIT_DIR alone (the reported `GIT_DIR=... scripts/test.sh` |
| 64 | +# case: fixture git commands write into the decoy) and the full set a hook |
| 65 | +# exports (fixture commits fail, so the suite degrades to SKIPs). |
| 66 | +check_variant() { |
| 67 | + local name="$1" |
| 68 | + local decoy="$WORK/decoy-$name" |
| 69 | + shift |
| 70 | + git init -q "$decoy" |
| 71 | + local before |
| 72 | + before="$(decoy_state "$decoy")" |
| 73 | + local -a assignments=() |
| 74 | + local var |
| 75 | + for var in "$@"; do |
| 76 | + assignments+=("$var=$decoy$(git_env_suffix "$var")") |
| 77 | + done |
| 78 | + local log="$WORK/runner-$name.log" |
| 79 | + local rc |
| 80 | + set +e |
| 81 | + env "${assignments[@]}" "$RUNNER" "$SUITE" >"$log" 2>&1 |
| 82 | + rc=$? |
| 83 | + set -e |
| 84 | + local env_summary after |
| 85 | + env_summary="$(summary "$log")" |
| 86 | + after="$(decoy_state "$decoy")" |
| 87 | + if [ "$rc" -ne 0 ]; then |
| 88 | + echo "FAIL[$name]: '$SUITE' exited $rc with an inherited git environment." >&2 |
| 89 | + grep -E 'FAIL|passed' "$log" >&2 || true |
| 90 | + FAILED=1 |
| 91 | + fi |
| 92 | + if [ -z "$BASE_SUMMARY" ] || [ "$BASE_SUMMARY" != "$env_summary" ]; then |
| 93 | + echo "FAIL[$name]: '$SUITE' behaves differently with an inherited git environment." >&2 |
| 94 | + echo " clean env: ${BASE_SUMMARY:-<no summary>}" >&2 |
| 95 | + echo " inherited env: ${env_summary:-<no summary>}" >&2 |
| 96 | + grep -E 'FAIL|SKIP' "$log" >&2 || true |
| 97 | + FAILED=1 |
| 98 | + fi |
| 99 | + if [ "$before" != "$after" ]; then |
| 100 | + echo "FAIL[$name]: the test runner modified the repository named by the inherited GIT_DIR." >&2 |
| 101 | + echo "--- before ---" >&2 |
| 102 | + echo "$before" >&2 |
| 103 | + echo "--- after ---" >&2 |
| 104 | + echo "$after" >&2 |
| 105 | + FAILED=1 |
| 106 | + fi |
| 107 | +} |
| 108 | + |
| 109 | +# Where each exported variable points inside a repository, as a hook sees it. |
| 110 | +git_env_suffix() { |
| 111 | + case "$1" in |
| 112 | + GIT_WORK_TREE) printf '' ;; |
| 113 | + GIT_INDEX_FILE) printf '/.git/index' ;; |
| 114 | + *) printf '/.git' ;; |
| 115 | + esac |
| 116 | +} |
| 117 | + |
| 118 | +check_variant git-dir GIT_DIR |
| 119 | +check_variant hook-env GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_COMMON_DIR |
| 120 | + |
| 121 | +if [ "$FAILED" -ne 0 ]; then |
| 122 | + exit 1 |
| 123 | +fi |
| 124 | +echo "PASS: test runner ignores an inherited git repository environment (#2003)." |
0 commit comments