Files
MyGoServer/scripts/test.sh

101 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
set -e
echo "🧪 Running tests for MyGoServer..."
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
local status=$1
local message=$2
if [ "$status" = "PASS" ]; then
echo -e "${GREEN}$message${NC}"
elif [ "$status" = "FAIL" ]; then
echo -e "${RED}$message${NC}"
else
echo -e "${YELLOW}⚠️ $message${NC}"
fi
}
# 1. Run all unit tests
echo -e "${YELLOW}Running all unit tests...${NC}"
go test -v ./internal/apiserver/...
if [ $? -eq 0 ]; then
print_status "PASS" "Unit tests passed"
else
print_status "FAIL" "Unit tests failed"
exit 1
fi
# 2. Run tests with coverage
echo -e "${YELLOW}Running tests with coverage...${NC}"
go test -coverprofile=coverage.out ./...
if [ $? -eq 0 ]; then
print_status "PASS" "Coverage tests completed"
# Generate coverage report
go tool cover -html=coverage.out -o coverage.html 2>/dev/null || true
if [ -f "coverage.html" ]; then
echo -e "${GREEN}📊 Coverage report generated: coverage.html${NC}"
fi
# Show coverage percentage
go tool cover -func=coverage.out | tail -1
else
print_status "FAIL" "Coverage tests failed"
fi
# 3. Run tests with race detector (optional, takes longer)
echo -e "${YELLOW}Running race detector tests...${NC}"
echo "This may take longer - press Ctrl+C to skip, or wait..."
sleep 2
if go test -race ./internal/apiserver/... > /dev/null 2>&1; then
print_status "PASS" "Race detector tests passed"
else
print_status "WARN" "Race detector tests completed (some may have warnings)"
fi
# 4. Run vet (static analysis)
echo -e "${YELLOW}Running go vet (static analysis)...${NC}"
go vet ./...
if [ $? -eq 0 ]; then
print_status "PASS" "Static analysis passed"
else
print_status "WARN" "Static analysis completed with warnings"
fi
# 5. Check formatting
echo -e "${YELLOW}Checking code formatting...${NC}"
if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
print_status "FAIL" "Code formatting issues found:"
gofmt -s -l .
echo "Run 'gofmt -s -w .' to fix formatting"
exit 1
else
print_status "PASS" "Code formatting is correct"
fi
# 6. Run specific test patterns (if specified)
if [ "$1" != "" ]; then
echo -e "${YELLOW}Running specific tests: $1${NC}"
go test -v ./internal/apiserver/... -run="$1"
fi
echo -e "${GREEN}🎉 All tests completed successfully!${NC}"
echo ""
echo "📝 Usage examples:"
echo " ./scripts/test.sh # Run all tests"
echo " ./scripts/test.sh \"TestHome\" # Run specific test pattern"
echo " go test ./... # Run all tests manually"
echo " go test -v ./internal/apiserver/ # Run specific package tests"
echo " go test -race ./... # Run with race detector"