1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 20:50:06 +00:00

test: speed up bats execution

This commit is contained in:
Tw93
2026-03-14 09:19:54 +08:00
parent 169be1e1f2
commit 7697acb711
3 changed files with 35 additions and 13 deletions

View File

@@ -7,20 +7,28 @@ setup_file() {
ORIGINAL_HOME="${HOME:-}"
export ORIGINAL_HOME
# Capture real GOCACHE before HOME is replaced with a temp dir.
# Without this, go build would use $HOME/Library/Caches/go-build inside the
# temp dir (empty), causing a full cold rebuild on every test run (~6s).
ORIGINAL_GOCACHE="$(go env GOCACHE 2>/dev/null || true)"
export ORIGINAL_GOCACHE
HOME="$(mktemp -d "${BATS_TEST_DIRNAME}/tmp-cli-home.XXXXXX")"
export HOME
mkdir -p "$HOME"
# Build Go binaries from current source for JSON tests.
# Point GOPATH/GOMODCACHE at the real home so go build doesn't write
# module caches into the fake HOME under tests/.
# Point GOPATH/GOMODCACHE/GOCACHE at the real home so go build can reuse
# the module and build caches rather than doing a cold rebuild every run.
if command -v go > /dev/null 2>&1; then
ANALYZE_BIN="$(mktemp "${TMPDIR:-/tmp}/analyze-go.XXXXXX")"
STATUS_BIN="$(mktemp "${TMPDIR:-/tmp}/status-go.XXXXXX")"
GOPATH="${ORIGINAL_HOME}/go" GOMODCACHE="${ORIGINAL_HOME}/go/pkg/mod" \
GOCACHE="${ORIGINAL_GOCACHE}" \
go build -o "$ANALYZE_BIN" "$PROJECT_ROOT/cmd/analyze" 2>/dev/null
GOPATH="${ORIGINAL_HOME}/go" GOMODCACHE="${ORIGINAL_HOME}/go/pkg/mod" \
GOCACHE="${ORIGINAL_GOCACHE}" \
go build -o "$STATUS_BIN" "$PROJECT_ROOT/cmd/status" 2>/dev/null
export ANALYZE_BIN STATUS_BIN
fi