|
| 1 | +# Variables |
| 2 | +LIB_NAME = hatmax |
| 3 | +MODULE_NAME = github.com/hatmaxkit/hatmax |
| 4 | + |
| 5 | +# Default target |
| 6 | +all: test |
| 7 | + |
| 8 | +# Help target |
| 9 | +help: |
| 10 | + @echo "Available targets:" |
| 11 | + @echo "" |
| 12 | + @echo "Testing:" |
| 13 | + @echo " test - Run all tests" |
| 14 | + @echo " test-v - Run tests with verbose output" |
| 15 | + @echo " test-short - Run tests in short mode" |
| 16 | + @echo " test-coverage - Run tests with coverage report" |
| 17 | + @echo " test-coverage-profile - Generate coverage profile" |
| 18 | + @echo " test-coverage-html - Generate HTML coverage report" |
| 19 | + @echo " test-coverage-func - Show function-level coverage" |
| 20 | + @echo " test-coverage-check - Check coverage meets 85% threshold" |
| 21 | + @echo " test-coverage-100 - Check coverage is 100%" |
| 22 | + @echo " test-coverage-summary - Display coverage table by package" |
| 23 | + @echo "" |
| 24 | + @echo "Quality Checks:" |
| 25 | + @echo " lint - Run golangci-lint" |
| 26 | + @echo " format - Format code" |
| 27 | + @echo " vet - Run go vet" |
| 28 | + @echo " check - Run all quality checks (fmt, vet, test, test-coverage-check, lint)" |
| 29 | + @echo " ci - Run CI pipeline (strict, 100% coverage)" |
| 30 | + @echo "" |
| 31 | + @echo "Utilities:" |
| 32 | + @echo " clean - Clean coverage files and test cache" |
| 33 | + @echo " tidy - Run go mod tidy" |
| 34 | + @echo " download - Download dependencies" |
| 35 | + |
| 36 | +# Run linter |
| 37 | +lint: |
| 38 | + @echo "Running linter and fixing issues..." |
| 39 | + @golangci-lint run --fix |
| 40 | + |
| 41 | +# Format code |
| 42 | +format: |
| 43 | + @echo "Formatting code..." |
| 44 | + @gofmt -w . |
| 45 | + |
| 46 | +# Run tests |
| 47 | +test: |
| 48 | + @go test ./... |
| 49 | + |
| 50 | +# Run tests with verbose output |
| 51 | +test-v: |
| 52 | + @go test -v ./... |
| 53 | + |
| 54 | +# Run tests in short mode |
| 55 | +test-short: |
| 56 | + @go test -short ./... |
| 57 | + |
| 58 | +# Run tests with coverage |
| 59 | +test-coverage: |
| 60 | + @go test -cover ./... |
| 61 | + |
| 62 | +# Generate coverage profile and show percentage |
| 63 | +test-coverage-profile: |
| 64 | + @go test -coverprofile=coverage.out ./... |
| 65 | + @go tool cover -func=coverage.out | tail -1 |
| 66 | + |
| 67 | +# Generate HTML coverage report |
| 68 | +test-coverage-html: test-coverage-profile |
| 69 | + @go tool cover -html=coverage.out -o coverage.html |
| 70 | + @echo "Coverage report generated: coverage.html" |
| 71 | + |
| 72 | +# Show function-level coverage |
| 73 | +test-coverage-func: test-coverage-profile |
| 74 | + @go tool cover -func=coverage.out |
| 75 | + |
| 76 | +# Check coverage percentage and fail if below threshold (85%) |
| 77 | +test-coverage-check: test-coverage-profile |
| 78 | + @COVERAGE=$$(go tool cover -func=coverage.out | tail -1 | awk '{print $$3}' | sed 's/%//'); \ |
| 79 | + echo "Current coverage: $$COVERAGE%"; \ |
| 80 | + if [ $$(awk -v cov="$$COVERAGE" 'BEGIN {print (cov < 85)}') -eq 1 ]; then \ |
| 81 | + echo "❌ Coverage $$COVERAGE% is below 85% threshold"; \ |
| 82 | + exit 1; \ |
| 83 | + else \ |
| 84 | + echo "✅ Coverage $$COVERAGE% meets the 85% threshold"; \ |
| 85 | + fi |
| 86 | + |
| 87 | +# Check coverage percentage and fail if not 100% |
| 88 | +test-coverage-100: test-coverage-profile |
| 89 | + @COVERAGE=$$(go tool cover -func=coverage.out | tail -1 | awk '{print $$3}' | sed 's/%//'); \ |
| 90 | + echo "Current coverage: $$COVERAGE%"; \ |
| 91 | + if [ "$$COVERAGE" != "100.0" ]; then \ |
| 92 | + echo "❌ Coverage $$COVERAGE% is not 100%"; \ |
| 93 | + go tool cover -func=coverage.out | grep -v "100.0%"; \ |
| 94 | + exit 1; \ |
| 95 | + else \ |
| 96 | + echo "🎉 Perfect! 100% test coverage achieved!"; \ |
| 97 | + fi |
| 98 | + |
| 99 | +# Display coverage summary table by package |
| 100 | +test-coverage-summary: |
| 101 | + @echo "🧪 Running coverage tests by package..." |
| 102 | + @echo "" |
| 103 | + @echo "Coverage by package:" |
| 104 | + @echo "┌────────────────────────────────────────────────────────┬──────────┐" |
| 105 | + @echo "│ Package │ Coverage │" |
| 106 | + @echo "├────────────────────────────────────────────────────────┼──────────┤" |
| 107 | + @for pkg in $$(go list ./... | grep -v -e "/tmp/" -e "/build/"); do \ |
| 108 | + pkgname=$$(echo $$pkg | sed 's|$(MODULE_NAME)||' | sed 's|^/||'); \ |
| 109 | + if [ -z "$$pkgname" ]; then pkgname="."; fi; \ |
| 110 | + result=$$(go test -cover $$pkg 2>&1); \ |
| 111 | + cov=$$(echo "$$result" | grep -oE '[0-9]+\.[0-9]+% of statements' | grep -v '^0\.0%' | tail -1 | grep -oE '[0-9]+\.[0-9]+%'); \ |
| 112 | + if [ -z "$$cov" ]; then \ |
| 113 | + if echo "$$result" | grep -qE '\[no test files\]|no test files'; then \ |
| 114 | + cov="no tests"; \ |
| 115 | + elif echo "$$result" | grep -q "FAIL"; then \ |
| 116 | + cov="FAIL"; \ |
| 117 | + else \ |
| 118 | + cov="0.0%"; \ |
| 119 | + fi; \ |
| 120 | + fi; \ |
| 121 | + printf "│ %-54s │ %8s │\n" "$$pkgname" "$$cov"; \ |
| 122 | + done |
| 123 | + @echo "└────────────────────────────────────────────────────────┴──────────┘" |
| 124 | + |
| 125 | +# Run go vet |
| 126 | +vet: |
| 127 | + @go vet ./... |
| 128 | + |
| 129 | +# Run all quality checks |
| 130 | +check: format vet test test-coverage-check lint |
| 131 | + @echo "✅ All quality checks passed!" |
| 132 | + |
| 133 | +# CI pipeline - strict checks including 100% coverage |
| 134 | +ci: format vet test test-coverage-100 lint |
| 135 | + @echo "🚀 CI pipeline passed!" |
| 136 | + |
| 137 | +# Clean coverage files and test cache |
| 138 | +clean: |
| 139 | + @echo "Cleaning up..." |
| 140 | + @go clean -testcache |
| 141 | + @rm -f coverage.out coverage.html |
| 142 | + @echo "Clean complete." |
| 143 | + |
| 144 | +# Run go mod tidy |
| 145 | +tidy: |
| 146 | + @echo "Running go mod tidy..." |
| 147 | + @go mod tidy |
| 148 | + |
| 149 | +# Download dependencies |
| 150 | +download: |
| 151 | + @echo "Downloading dependencies..." |
| 152 | + @go mod download |
| 153 | + |
| 154 | +# Phony targets |
| 155 | +.PHONY: all test test-v test-short test-coverage test-coverage-profile test-coverage-html test-coverage-func test-coverage-check test-coverage-100 test-coverage-summary vet check ci lint format help clean tidy download |
0 commit comments