|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +load "$BATS_PLUGIN_PATH/load.bash" |
| 4 | + |
| 5 | +# Uncomment the following line to debug stubbed commands |
| 6 | +# export COMMAND_STUB_DEBUG=/dev/tty |
| 7 | + |
| 8 | +setup() { |
| 9 | + export BUILDKITE_PLUGIN_MONOREPO_DIFF_BUILDKITE_PLUGIN_TEST_MODE=true |
| 10 | + # Set a minimal plugin config (only used by Go binary, not the bash hook's download logic) |
| 11 | + export BUILDKITE_PLUGINS='[{"monorepo-diff": {}}]' |
| 12 | + |
| 13 | + # Create a mock binary in the current directory for download mode tests |
| 14 | + cat > "$PWD/monorepo-diff-buildkite-plugin" << 'MOCKBIN' |
| 15 | +#!/bin/bash |
| 16 | +echo "Mock binary executed with args: $@" |
| 17 | +MOCKBIN |
| 18 | + chmod +x "$PWD/monorepo-diff-buildkite-plugin" |
| 19 | +} |
| 20 | + |
| 21 | +teardown() { |
| 22 | + # Clean up the mock binary |
| 23 | + rm -f "$PWD/monorepo-diff-buildkite-plugin" |
| 24 | +} |
| 25 | + |
| 26 | +@test "download=false with binary in PATH succeeds" { |
| 27 | + # Buildkite sets this env var based on plugin config |
| 28 | + export BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD=false |
| 29 | + export PATH="$PWD:$PATH" |
| 30 | + |
| 31 | + run "$PWD/hooks/command" |
| 32 | + |
| 33 | + assert_success |
| 34 | + assert_output --partial "Mock binary executed" |
| 35 | +} |
| 36 | + |
| 37 | +@test "download=false without binary in PATH fails with error" { |
| 38 | + # Buildkite sets this env var based on plugin config |
| 39 | + export BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD=false |
| 40 | + |
| 41 | + # Remove mock binary from current directory and don't add to PATH |
| 42 | + rm -f "$PWD/monorepo-diff-buildkite-plugin" |
| 43 | + export PATH="/usr/bin:/bin" |
| 44 | + |
| 45 | + run "$PWD/hooks/command" |
| 46 | + |
| 47 | + assert_failure |
| 48 | + assert_output --partial "Binary 'monorepo-diff-buildkite-plugin' not found in PATH" |
| 49 | + assert_output --partial "Please install it or set download: true" |
| 50 | +} |
| 51 | + |
| 52 | +@test "download=true executes binary in test mode" { |
| 53 | + export BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD=true |
| 54 | + |
| 55 | + # In test mode, it skips download but still executes the mock binary we created |
| 56 | + run "$PWD/hooks/command" |
| 57 | + |
| 58 | + assert_success |
| 59 | + assert_output --partial "Mock binary executed" |
| 60 | +} |
| 61 | + |
| 62 | +@test "download defaults to true when not set" { |
| 63 | + # Don't set BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD - should default to true |
| 64 | + |
| 65 | + # Should default to true, skip download in test mode, and execute mock binary |
| 66 | + run "$PWD/hooks/command" |
| 67 | + |
| 68 | + assert_success |
| 69 | + assert_output --partial "Mock binary executed" |
| 70 | +} |
| 71 | + |
| 72 | +@test "download=false passes arguments to preinstalled binary" { |
| 73 | + export BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD=false |
| 74 | + export PATH="$PWD:$PATH" |
| 75 | + |
| 76 | + run "$PWD/hooks/command" arg1 arg2 |
| 77 | + |
| 78 | + assert_success |
| 79 | + assert_output --partial "Mock binary executed with args: arg1 arg2" |
| 80 | +} |
| 81 | + |
| 82 | +@test "download=true in test mode skips actual download" { |
| 83 | + export BUILDKITE_PLUGIN_MONOREPO_DIFF_DOWNLOAD=true |
| 84 | + |
| 85 | + # Stub curl to verify it's NOT called in test mode |
| 86 | + stub curl \ |
| 87 | + ": echo 'ERROR: curl should not be called in test mode'; exit 1" |
| 88 | + |
| 89 | + run "$PWD/hooks/command" |
| 90 | + |
| 91 | + # Should succeed without calling curl |
| 92 | + assert_success |
| 93 | + |
| 94 | + # Clean up stub (it should not have been called) |
| 95 | + unstub curl || true |
| 96 | +} |
0 commit comments