|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +setup() { |
| 4 | + load "${BATS_PLUGIN_PATH}/load.bash" |
| 5 | + HOOK_DIR="$PWD" |
| 6 | +} |
| 7 | + |
| 8 | +teardown() { |
| 9 | + cd "$HOOK_DIR" 2>/dev/null || true |
| 10 | + if [[ -n "$WORK_DIR" && -d "$WORK_DIR" ]]; then |
| 11 | + rm -rf "$WORK_DIR" |
| 12 | + fi |
| 13 | + unstub git 2>/dev/null || true |
| 14 | +} |
| 15 | + |
| 16 | +@test "Cleanup sparse state enabled - removes config.worktree and unsets all sparse config keys" { |
| 17 | + export BUILDKITE_PLUGIN_SPARSE_CHECKOUT_CLEANUP_SPARSE_STATE="true" |
| 18 | + |
| 19 | + WORK_DIR="$(mktemp -d)" |
| 20 | + mkdir -p "$WORK_DIR/.git" |
| 21 | + echo '[core]' > "$WORK_DIR/.git/config.worktree" |
| 22 | + |
| 23 | + stub git \ |
| 24 | + "ls-files -z : true" \ |
| 25 | + "update-index -z --no-skip-worktree --stdin : true" \ |
| 26 | + "config --unset extensions.worktreeConfig : true" \ |
| 27 | + "config --unset core.sparseCheckout : true" \ |
| 28 | + "config --unset core.sparseCheckoutCone : true" |
| 29 | + |
| 30 | + cd "$WORK_DIR" |
| 31 | + run "$HOOK_DIR/hooks/pre-exit" |
| 32 | + |
| 33 | + assert_success |
| 34 | + assert_output --partial 'Cleaning up sparse-checkout config' |
| 35 | + assert_output --partial 'Sparse-checkout config cleaned up' |
| 36 | + [[ ! -f "$WORK_DIR/.git/config.worktree" ]] |
| 37 | +} |
| 38 | + |
| 39 | +@test "Cleanup sparse state enabled - succeeds even when no prior sparse config exists" { |
| 40 | + export BUILDKITE_PLUGIN_SPARSE_CHECKOUT_CLEANUP_SPARSE_STATE="true" |
| 41 | + |
| 42 | + WORK_DIR="$(mktemp -d)" |
| 43 | + mkdir -p "$WORK_DIR/.git" |
| 44 | + |
| 45 | + stub git \ |
| 46 | + "ls-files -z : true" \ |
| 47 | + "update-index -z --no-skip-worktree --stdin : true" \ |
| 48 | + "config --unset extensions.worktreeConfig : true" \ |
| 49 | + "config --unset core.sparseCheckout : true" \ |
| 50 | + "config --unset core.sparseCheckoutCone : true" |
| 51 | + |
| 52 | + cd "$WORK_DIR" |
| 53 | + run "$HOOK_DIR/hooks/pre-exit" |
| 54 | + |
| 55 | + assert_success |
| 56 | + assert_output --partial 'Cleaning up sparse-checkout config' |
| 57 | + assert_output --partial 'Sparse-checkout config cleaned up' |
| 58 | +} |
| 59 | + |
| 60 | +@test "Cleanup sparse state enabled - skips when no .git directory" { |
| 61 | + export BUILDKITE_PLUGIN_SPARSE_CHECKOUT_CLEANUP_SPARSE_STATE="true" |
| 62 | + |
| 63 | + WORK_DIR="$(mktemp -d)" |
| 64 | + |
| 65 | + cd "$WORK_DIR" |
| 66 | + run "$HOOK_DIR/hooks/pre-exit" |
| 67 | + |
| 68 | + assert_success |
| 69 | + assert_output --partial 'No .git directory found, skipping sparse-checkout config cleanup' |
| 70 | + refute_output --partial 'Cleaning up sparse-checkout config' |
| 71 | +} |
| 72 | + |
| 73 | +@test "Cleanup sparse state enabled - pipes tracked files to update-index" { |
| 74 | + export BUILDKITE_PLUGIN_SPARSE_CHECKOUT_CLEANUP_SPARSE_STATE="true" |
| 75 | + |
| 76 | + WORK_DIR="$(mktemp -d)" |
| 77 | + mkdir -p "$WORK_DIR/.git" |
| 78 | + |
| 79 | + stub git \ |
| 80 | + "ls-files -z : echo 'lib/excluded.rb'" \ |
| 81 | + "update-index -z --no-skip-worktree --stdin : true" \ |
| 82 | + "config --unset extensions.worktreeConfig : true" \ |
| 83 | + "config --unset core.sparseCheckout : true" \ |
| 84 | + "config --unset core.sparseCheckoutCone : true" |
| 85 | + |
| 86 | + cd "$WORK_DIR" |
| 87 | + run "$HOOK_DIR/hooks/pre-exit" |
| 88 | + |
| 89 | + assert_success |
| 90 | + assert_output --partial 'Sparse-checkout config cleaned up' |
| 91 | +} |
| 92 | + |
| 93 | +@test "Cleanup sparse state not configured - no cleanup runs" { |
| 94 | + run "$HOOK_DIR/hooks/pre-exit" |
| 95 | + |
| 96 | + assert_success |
| 97 | + refute_output --partial 'sparse-checkout config' |
| 98 | +} |
0 commit comments