#!/usr/bin/env bash
set -euo pipefail

export CGO_ENABLED=0
if [ -z "${GOCACHE:-}" ]; then
  suite_gate_cache="$(mktemp -d)"
  export GOCACHE="$suite_gate_cache"
  trap 'rm -rf -- "$suite_gate_cache"' EXIT
fi

unformatted="$(git ls-files -z -- '*.go' | xargs -0 -r gofmt -l)"
if [ -n "$unformatted" ]; then
  printf '%s\n' "$unformatted" >&2
  exit 1
fi

go vet ./...
go build ./...
go test -count=1 ./...
