srs/.openclaw/skills/srs-develop/scripts/proxy-utest.sh
Winlin cd11a6720f
Proxy: Harden internal/env tests and add counterfeiter fakes. v7.0.144 (#4665)
- **Refactor `internal/env` for testability.** Route every
`os`/filesystem
  call in `env.go` through swappable package-level function variables
(`getEnv`, `setEnv`, `lookupEnv`, `openFile`). Split `parseEnvFile` into
a
thin file-opening wrapper plus a pure `parseEnvReader(io.Reader)` so the
  parser can be tested directly without touching disk.
- **Hermetic tests, 96.9% coverage.** Rewrite `internal/env/env_test.go`
to
install in-memory fakes via `withFakeEnv` / `withFakeOpen` helpers that
  swap the package vars and restore them on `t.Cleanup`. Tests no longer
mutate real process env or write temp `.env` files, removing a source of
flakiness under parallel test execution. New cases cover
`NewEnvironment`,
`setEnvDefault`, `loadEnvFile` error paths, and edge cases in the
parser.
- **Counterfeiter-based fake generation.** Add `counterfeiter` as a Go
tool
dependency, a `//go:generate` directive for the `Environment` interface
  (`internal/env/gen.go`), and commit the generated
  `internal/env/envfakes/fake_environment.go` so downstream packages can
test against a spec-faithful fake instead of hand-rolling stubs. Expose
  the step as `make generate`.
- **Tooling.** `scripts/proxy-utest.sh` gains a `--coverage` / `-c` flag
  that runs `go test -coverprofile=...` across `./cmd/...` and
  `./internal/...` and prints per-function coverage via `go tool cover
  -func`. The `srs-develop` skill doc is updated to include the
  regenerate-fakes step and the new coverage flag.
- **Go version.** Bump `go.mod` to Go 1.25 (required for the `go tool`
  directive used to pin the counterfeiter CLI as a tool dep).

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-18 20:33:07 -04:00

54 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# Run unit tests for the proxy server (cmd/ and internal/ packages).
set -e
usage() {
cat <<'EOF'
Run unit tests for the proxy server (cmd/ and internal/ packages).
Usage:
proxy-utest.sh # run tests
proxy-utest.sh --coverage # run tests and print per-function coverage
proxy-utest.sh -c # short form of --coverage
EOF
}
COVERAGE=0
for arg in "$@"; do
case "$arg" in
-c|--coverage) COVERAGE=1 ;;
-h|--help) usage; exit 0 ;;
*)
echo "Error: unknown argument: $arg" >&2
usage >&2
exit 2
;;
esac
done
SCRIPT_DIR="$(cd -P "$(dirname "$0")" && pwd)"
# Navigate: scripts/ -> srs-develop/ -> skills/ -> .openclaw/ -> srs
WORKSPACE="$(cd -P "$SCRIPT_DIR/../../../.." && pwd)"
if [[ ! -f "$WORKSPACE/go.mod" ]]; then
echo "Error: go.mod not found in WORKSPACE: $WORKSPACE" >&2
exit 1
fi
cd "$WORKSPACE"
PACKAGES=(./cmd/... ./internal/...)
if [[ $COVERAGE -eq 1 ]]; then
COVER_FILE="$(mktemp -t proxy-utest-coverage.XXXXXX.out)"
trap 'rm -f "$COVER_FILE"' EXIT
echo "Running proxy unit tests with coverage in: $WORKSPACE"
go test "${PACKAGES[@]}" -v -coverprofile="$COVER_FILE" -coverpkg=./cmd/...,./internal/...
echo
echo "=== Coverage (per function) ==="
go tool cover -func="$COVER_FILE"
else
echo "Running proxy unit tests in: $WORKSPACE"
go test "${PACKAGES[@]}" -v
fi