Something went wrong. Try again.
Something went wrong. Try again.
2.4 kB 81 lines
at feature
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182#!/usr/bin/env bats# Tests for auto-format.sh wrapper.# The wrapper's job: invoke `auto-tools format` with stdin piped through and# propagate the exit code. Per-extension formatter dispatch is tested in# cmd/auto-tools/format_test.go (Go unit tests).
bats_require_minimum_version 1.5.0
SCRIPT="$(cd "$BATS_TEST_DIRNAME/.." && pwd)/scripts/auto-format.sh"
setup() { MOCK_BIN="$(mktemp -d)" CALLS_LOG="$(mktemp)" STDIN_LOG="$(mktemp)" export PATH="$MOCK_BIN:$PATH"
_MOCK_VERSION=$(sed -n 's/^PLUGIN_VERSION="v\(.*\)"/\1/p' "$SCRIPT")
# Default auto-tools mock: respond to --version, record invocation + capture stdin, exit 0. cat >"$MOCK_BIN/auto-tools" <<EOF#!/bin/shif [ "\$1" = "--version" ]; then echo "auto-tools version ${_MOCK_VERSION}" exit 0fiecho "auto-tools \$*" >> "$CALLS_LOG"cat >> "$STDIN_LOG"exit 0EOF chmod +x "$MOCK_BIN/auto-tools"}
teardown() { rm -rf "$MOCK_BIN" rm -f "$CALLS_LOG" "$STDIN_LOG"}
pipe_payload() { bash "$SCRIPT" <<<"$1"}
# --- Core wrapper contract ---
@test "calls auto-tools format when binary is in PATH" { run pipe_payload '{"tool_input":{"file_path":"/tmp/foo.go"}}' [ "$status" -eq 0 ] grep -q "auto-tools format" "$CALLS_LOG"}
@test "pipes stdin through to auto-tools" { payload='{"tool_input":{"file_path":"/tmp/foo.go"}}' run pipe_payload "$payload" grep -q "foo.go" "$STDIN_LOG"}
@test "exits 0 when auto-tools exits 0" { run pipe_payload '{"tool_input":{"file_path":"/tmp/foo.go"}}' [ "$status" -eq 0 ]}
@test "exits 0 even when auto-tools exits non-zero" { # auto-tools format always exits 0; if it doesn't (e.g. binary error), # exec propagates that exit code. This test documents the exec behavior. _ver=$(sed -n 's/^PLUGIN_VERSION="v\(.*\)"/\1/p' "$SCRIPT") cat >"$MOCK_BIN/auto-tools" <<EOF#!/bin/sh[ "\$1" = "--version" ] && echo "auto-tools version ${_ver}" && exit 0exit 42EOF chmod +x "$MOCK_BIN/auto-tools" run pipe_payload '{"tool_input":{"file_path":"/tmp/foo.go"}}' [ "$status" -eq 42 ]}
@test "exits 1 with version mismatch message when binary version differs" { printf '#!/bin/sh\n[ "$1" = "--version" ] && echo "auto-tools version 0.0.0" && exit 0\n' >"$MOCK_BIN/auto-tools" chmod +x "$MOCK_BIN/auto-tools" run --separate-stderr pipe_payload '{"tool_input":{"file_path":"/tmp/foo.go"}}' [ "$status" -eq 1 ] [[ "$stderr" == *"version mismatch"* ]]}