#!/usr/bin/env bash set -euo pipefail # Reusable, agnostic test runner script # Can be used in any Go project by adjusting configuration # Configuration variables TEST_LEVEL="${TEST_LEVEL:-basic}" # basic, unit, integration, comprehensive, audit COVERAGE="${COVERAGE:-false}" OUTPUT_DIR="${OUTPUT_DIR:-test-results}" PACKAGE_FILTER="${PACKAGE_FILTER:-./...}" TIMEOUT="${TIMEOUT:-10m}" JUNIT_OUTPUT="${JUNIT_OUTPUT:-false}" # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in -l|--level) TEST_LEVEL="$2" shift 2 ;; -c|--coverage) COVERAGE=true shift ;; -o|--output) OUTPUT_DIR="$2" shift 2 ;; -p|--package) PACKAGE_FILTER="$2" shift 2 ;; --junit) JUNIT_OUTPUT=true shift ;; -t|--timeout) TIMEOUT="$2" shift 2 ;; -h|--help) echo "Usage: $0 [OPTIONS]" echo "Run tests with different levels of coverage" echo "" echo "Options:" echo " -l, --level LEVEL Test level: basic, unit, integration, comprehensive, audit (default: basic)" echo " -c, --coverage Generate coverage report" echo " -o, --output DIR Output directory (default: test-results)" echo " -p, --package PKG Package filter (default: ./...)" echo " --junit Generate JUnit output" echo " -t, --timeout DURATION Timeout duration (default: 10m)" echo " -h, --help Show this help" exit 0 ;; *) echo "Unknown option: $1" exit 1 ;; esac done echo "Starting test execution (level: $TEST_LEVEL, coverage: $COVERAGE)" # Create output directory mkdir -p "$OUTPUT_DIR" # Run tests based on level case $TEST_LEVEL in "basic") echo "Running basic tests..." if [ "$COVERAGE" = "true" ]; then go test -v -short -coverprofile="$OUTPUT_DIR/coverage.out" -covermode=atomic "$PACKAGE_FILTER" go tool cover -html="$OUTPUT_DIR/coverage.out" -o "$OUTPUT_DIR/coverage.html" else go test -v -short "$PACKAGE_FILTER" fi ;; "unit") echo "Running unit tests..." if [ "$COVERAGE" = "true" ]; then go test -v ./test/unit/... -coverprofile="$OUTPUT_DIR/coverage.out" -covermode=atomic go tool cover -html="$OUTPUT_DIR/coverage.out" -o "$OUTPUT_DIR/coverage.html" else go test -v ./test/unit/... fi ;; "integration") echo "Running integration tests..." if [ "$COVERAGE" = "true" ]; then go test -v ./test/integration/... -coverprofile="$OUTPUT_DIR/coverage.out" -covermode=atomic go tool cover -html="$OUTPUT_DIR/coverage.out" -o "$OUTPUT_DIR/coverage.html" else go test -v ./test/integration/... fi ;; "comprehensive") echo "Running comprehensive tests..." if [ "$COVERAGE" = "true" ]; then go test -v ./test/unit/... ./test/integration/... ./test/e2e/... -coverprofile="$OUTPUT_DIR/coverage.out" -covermode=atomic go tool cover -html="$OUTPUT_DIR/coverage.out" -o "$OUTPUT_DIR/coverage.html" else go test -v ./test/unit/... ./test/integration/... ./test/e2e/... fi ;; "audit") echo "Running audit tests..." if [ "$COVERAGE" = "true" ]; then go test -v ./test/unit/... ./test/integration/... ./test/e2e/... ./test/property/... ./test/fuzzing/... ./test/security/... -coverprofile="$OUTPUT_DIR/coverage.out" -covermode=atomic go tool cover -html="$OUTPUT_DIR/coverage.out" -o "$OUTPUT_DIR/coverage.html" else go test -v ./test/unit/... ./test/integration/... ./test/e2e/... ./test/property/... ./test/fuzzing/... ./test/security/... fi ;; *) echo "Invalid test level: $TEST_LEVEL. Use: basic, unit, integration, comprehensive, audit" exit 1 ;; esac # Generate JUnit output if requested if [ "$JUNIT_OUTPUT" = "true" ]; then if command -v go-junit-report &> /dev/null; then go test -v "$PACKAGE_FILTER" 2>&1 | go-junit-report > "$OUTPUT_DIR/test-results.xml" else echo "go-junit-report not found. Install with: go install github.com/jstemmer/go-junit-report@latest" fi fi echo "Test execution completed. Results in $OUTPUT_DIR"