|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 5 | +SCRIPT="$ROOT/scripts/check-bun-locks.sh" |
| 6 | + |
| 7 | +assert_file_contains() { |
| 8 | + local file="$1" |
| 9 | + local expected="$2" |
| 10 | + |
| 11 | + if ! grep -Fq "$expected" "$file"; then |
| 12 | + echo "expected $file to contain: $expected" >&2 |
| 13 | + echo "actual:" >&2 |
| 14 | + cat "$file" >&2 |
| 15 | + exit 1 |
| 16 | + fi |
| 17 | +} |
| 18 | + |
| 19 | +new_repo() { |
| 20 | + local dir |
| 21 | + dir="$(mktemp -d)" |
| 22 | + |
| 23 | + git -C "$dir" init -q |
| 24 | + git -C "$dir" config user.email test@example.com |
| 25 | + git -C "$dir" config user.name "Test User" |
| 26 | + |
| 27 | + mkdir -p "$dir/cli" "$dir/control-plane/dashboard-ui" |
| 28 | + printf '{"dependencies":{"left-pad":"1.0.0"}}\n' > "$dir/cli/package.json" |
| 29 | + printf 'lock\n' > "$dir/cli/bun.lock" |
| 30 | + printf '{"dependencies":{"react":"19.0.0"}}\n' > "$dir/control-plane/dashboard-ui/package.json" |
| 31 | + printf 'lock\n' > "$dir/control-plane/dashboard-ui/bun.lock" |
| 32 | + git -C "$dir" add . |
| 33 | + git -C "$dir" commit -qm "initial" |
| 34 | + |
| 35 | + printf '%s\n' "$dir" |
| 36 | +} |
| 37 | + |
| 38 | +with_fake_bun() { |
| 39 | + local bin_dir="$1" |
| 40 | + local log_file="$2" |
| 41 | + local mode="${3:-clean}" |
| 42 | + |
| 43 | + mkdir -p "$bin_dir" |
| 44 | + cat > "$bin_dir/bun" <<'BUN' |
| 45 | +#!/usr/bin/env bash |
| 46 | +set -euo pipefail |
| 47 | +
|
| 48 | +printf '%s|%s\n' "$PWD" "$*" >> "$FAKE_BUN_LOG" |
| 49 | +
|
| 50 | +if [[ "${FAKE_BUN_MODE:-clean}" == "dirty-lock" ]]; then |
| 51 | + printf 'changed by fake bun\n' >> bun.lock |
| 52 | +fi |
| 53 | +BUN |
| 54 | + chmod +x "$bin_dir/bun" |
| 55 | + |
| 56 | + export PATH="$bin_dir:$PATH" |
| 57 | + export FAKE_BUN_LOG="$log_file" |
| 58 | + export FAKE_BUN_MODE="$mode" |
| 59 | +} |
| 60 | + |
| 61 | +test_skips_when_no_package_manifest_is_staged() { |
| 62 | + local repo bin_dir log_file |
| 63 | + repo="$(new_repo)" |
| 64 | + bin_dir="$(mktemp -d)" |
| 65 | + log_file="$repo/bun.log" |
| 66 | + with_fake_bun "$bin_dir" "$log_file" |
| 67 | + |
| 68 | + printf 'docs\n' > "$repo/README.md" |
| 69 | + git -C "$repo" add README.md |
| 70 | + |
| 71 | + (cd "$repo" && "$SCRIPT") |
| 72 | + if [[ -e "$log_file" ]]; then |
| 73 | + echo "expected bun not to run for unrelated staged files" >&2 |
| 74 | + cat "$log_file" >&2 |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | +} |
| 78 | + |
| 79 | +test_skips_unrelated_staged_files_without_bun() { |
| 80 | + local repo |
| 81 | + repo="$(new_repo)" |
| 82 | + |
| 83 | + printf 'docs\n' > "$repo/README.md" |
| 84 | + git -C "$repo" add README.md |
| 85 | + |
| 86 | + (cd "$repo" && PATH="/usr/bin:/bin" "$SCRIPT") |
| 87 | +} |
| 88 | + |
| 89 | +test_checks_each_staged_manifest_workspace() { |
| 90 | + local repo bin_dir log_file |
| 91 | + repo="$(new_repo)" |
| 92 | + bin_dir="$(mktemp -d)" |
| 93 | + log_file="$repo/bun.log" |
| 94 | + with_fake_bun "$bin_dir" "$log_file" |
| 95 | + |
| 96 | + printf '{"dependencies":{"left-pad":"1.0.1"}}\n' > "$repo/cli/package.json" |
| 97 | + git -C "$repo" add cli/package.json |
| 98 | + |
| 99 | + (cd "$repo" && "$SCRIPT") |
| 100 | + assert_file_contains "$log_file" "$repo/cli|install --frozen-lockfile" |
| 101 | +} |
| 102 | + |
| 103 | +test_fails_when_bun_changes_lockfile() { |
| 104 | + local repo bin_dir log_file output |
| 105 | + repo="$(new_repo)" |
| 106 | + bin_dir="$(mktemp -d)" |
| 107 | + log_file="$repo/bun.log" |
| 108 | + output="$repo/output.log" |
| 109 | + with_fake_bun "$bin_dir" "$log_file" "dirty-lock" |
| 110 | + |
| 111 | + printf '{"dependencies":{"left-pad":"1.0.2"}}\n' > "$repo/cli/package.json" |
| 112 | + git -C "$repo" add cli/package.json |
| 113 | + |
| 114 | + if (cd "$repo" && "$SCRIPT") > "$output" 2>&1; then |
| 115 | + echo "expected hook check to fail when bun.lock changes" >&2 |
| 116 | + exit 1 |
| 117 | + fi |
| 118 | + |
| 119 | + assert_file_contains "$output" "cli/bun.lock changed after bun install --frozen-lockfile" |
| 120 | +} |
| 121 | + |
| 122 | +test_skips_when_no_package_manifest_is_staged |
| 123 | +test_skips_unrelated_staged_files_without_bun |
| 124 | +test_checks_each_staged_manifest_workspace |
| 125 | +test_fails_when_bun_changes_lockfile |
0 commit comments