Files
gochat/backend/scripts/coverage_report.sh
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

94 lines
2.4 KiB
Bash

#!/bin/bash
# Coverage rate report generator for gochat project
# Generates per-package and overall coverage metrics aligned with Chatwoot test patterns.
set -e
cd "$(dirname "$0")/.."
export GOPROXY=https://goproxy.cn,direct
export GONOSUMCHECK=*
export GONOSUMDB=*
export GOFLAGS=-mod=mod
REPORT_DIR="coverage_reports"
mkdir -p "$REPORT_DIR"
echo "=== GoChat Test Coverage Report ==="
echo "Generated: $(date)"
echo ""
# Packages to test (only those that compile cleanly)
TESTABLE_PKGS=(
"./internal/model"
"./internal/model/channel"
"./internal/auth"
"./internal/config"
"./pkg/testutil"
"./pkg/crypto"
"./internal/repository"
"./internal/pubsub"
)
TOTAL_COVERAGE=0
PKG_COUNT=0
PASS_COUNT=0
FAIL_COUNT=0
TOTAL_TESTS=0
TOTAL_ASSERTIONS=0
echo "| Package | Tests | Coverage | Status |"
echo "|---------|-------|----------|--------|"
for pkg in "${TESTABLE_PKGS[@]}"; do
COVERAGE_FILE="$REPORT_DIR/coverage_$(echo $pkg | tr '/' '_').out"
# Run tests with coverage
RESULT=$(go test -v -cover -coverprofile="$COVERAGE_FILE" "$pkg" 2>&1)
EXIT_CODE=$?
# Extract coverage percentage
COVERAGE=$(echo "$RESULT" | grep "coverage:" | awk '{print $2}' | sed 's/%//')
if [ -z "$COVERAGE" ]; then
COVERAGE="0.0"
fi
# Count tests
TEST_COUNT=$(echo "$RESULT" | grep -c "^--- PASS\|^--- FAIL" || echo 0)
# Determine status
if [ $EXIT_CODE -eq 0 ]; then
STATUS="PASS"
PASS_COUNT=$((PASS_COUNT+1))
else
STATUS="FAIL"
FAIL_COUNT=$((FAIL_COUNT+1))
fi
echo "| $pkg | $TEST_COUNT | ${COVERAGE}% | $STATUS |"
TOTAL_TESTS=$((TOTAL_TESTS+TEST_COUNT))
TOTAL_COVERAGE=$(echo "$TOTAL_COVERAGE + $COVERAGE" | bc)
PKG_COUNT=$((PKG_COUNT+1))
done
echo ""
echo "=== Summary ==="
echo "Total packages tested: $PKG_COUNT"
echo "Passed: $PASS_COUNT, Failed: $FAIL_COUNT"
echo "Total test functions: $TOTAL_TESTS"
if [ $PKG_COUNT -gt 0 ]; then
AVG_COVERAGE=$(echo "$TOTAL_COVERAGE / $PKG_COUNT" | bc -l)
echo "Average coverage: ${AVG_COVERAGE}%"
fi
echo ""
echo "=== Test File Inventory ==="
find . -name "*_test.go" -not -path "./vendor/*" | while read f; do
LINES=$(wc -l < "$f")
FUNCTIONS=$(grep -c "func Test" "$f" 2>/dev/null || echo 0)
echo " $f: ${LINES} lines, ${FUNCTIONS} test functions"
done
echo ""
echo "Coverage profiles saved to $REPORT_DIR/"
echo "To view HTML coverage: go tool cover -html=$REPORT_DIR/coverage_*.out"