-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
187 lines (162 loc) · 6.74 KB
/
Makefile
File metadata and controls
187 lines (162 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Variables
LIB_NAME = hatmax
MODULE_NAME = github.com/hatmaxkit/hatmax
LINT_CACHE_DIR = $(CURDIR)/.tmp/lint
LINT_GOCACHE = $(LINT_CACHE_DIR)/gocache
# Default target
all: test
# Help target
help:
@echo "Available targets:"
@echo ""
@echo "Testing:"
@echo " test - Run all tests"
@echo " test-v - Run tests with verbose output"
@echo " test-short - Run tests in short mode"
@echo " test-coverage - Run tests with coverage report"
@echo " test-coverage-profile - Generate coverage profile"
@echo " test-coverage-html - Generate HTML coverage report"
@echo " test-coverage-func - Show function-level coverage"
@echo " test-coverage-check - Check coverage meets 85% threshold"
@echo " test-coverage-100 - Check coverage is 100%"
@echo " test-coverage-summary - Display coverage table by package"
@echo ""
@echo "Quality Checks:"
@echo " lint - Alias of lint-check"
@echo " lint-check - Check formatting + lint rules"
@echo " lint-strict - Run strict lint checks"
@echo " lint-fix - Apply format + auto-fixable lint rules"
@echo " format - Format code"
@echo " vet - Run go vet"
@echo " check - Run all quality checks (fmt, vet, test, test-coverage-check, lint-strict)"
@echo " ci - Run CI pipeline (strict, 100% coverage)"
@echo ""
@echo "Utilities:"
@echo " clean - Clean coverage files and test cache"
@echo " tidy - Run go mod tidy"
@echo " download - Download dependencies"
@echo " install-hooks - Configure git to use .githooks/"
# Lint alias (soft/default)
lint: lint-check
lint-strict:
@mkdir -p $(LINT_GOCACHE)
@echo "Checking gofmt on tracked Go files..."
@UNFORMATTED=$$(gofmt -l $$(git ls-files '*.go')); \
if [ -n "$$UNFORMATTED" ]; then \
echo "❌ Unformatted files:"; \
echo "$$UNFORMATTED"; \
exit 1; \
fi
@echo "Running lint rules (nlreturn, noinlineerr, wsl_v5)..."
@GOCACHE=$(LINT_GOCACHE) golangci-lint run --default=none --enable=nlreturn --enable=noinlineerr --enable=wsl_v5
lint-check: lint-strict
lint-fix:
@mkdir -p $(LINT_GOCACHE)
@echo "Applying gofmt to tracked Go files..."
@gofmt -w $$(git ls-files '*.go')
@echo "Applying auto-fixable lint rules (nlreturn, wsl_v5)..."
@GOCACHE=$(LINT_GOCACHE) golangci-lint run --fix --default=none --enable=nlreturn --enable=wsl_v5
@echo "Reporting non-auto-fixable lint (noinlineerr)..."
@GOCACHE=$(LINT_GOCACHE) golangci-lint run --default=none --enable=noinlineerr --issues-exit-code=0
# Format code
format:
@echo "Formatting code..."
@gofmt -w .
# Run tests
test:
@go test ./...
# Run tests with verbose output
test-v:
@go test -v ./...
# Run tests in short mode
test-short:
@go test -short ./...
# Run tests with coverage
test-coverage:
@go test -cover ./...
# Generate coverage profile and show percentage
test-coverage-profile:
@go test -coverprofile=coverage.out ./...
@go tool cover -func=coverage.out | tail -1
# Generate HTML coverage report
test-coverage-html: test-coverage-profile
@go tool cover -html=coverage.out -o coverage.html
@echo "Coverage report generated: coverage.html"
# Show function-level coverage
test-coverage-func: test-coverage-profile
@go tool cover -func=coverage.out
# Check coverage percentage and fail if below threshold (85%)
test-coverage-check: test-coverage-profile
@COVERAGE=$$(go tool cover -func=coverage.out | tail -1 | awk '{print $$3}' | sed 's/%//'); \
echo "Current coverage: $$COVERAGE%"; \
if [ $$(awk -v cov="$$COVERAGE" 'BEGIN {print (cov < 85)}') -eq 1 ]; then \
echo "❌ Coverage $$COVERAGE% is below 85% threshold"; \
exit 1; \
else \
echo "✅ Coverage $$COVERAGE% meets the 85% threshold"; \
fi
# Check coverage percentage and fail if not 100%
test-coverage-100: test-coverage-profile
@COVERAGE=$$(go tool cover -func=coverage.out | tail -1 | awk '{print $$3}' | sed 's/%//'); \
echo "Current coverage: $$COVERAGE%"; \
if [ "$$COVERAGE" != "100.0" ]; then \
echo "❌ Coverage $$COVERAGE% is not 100%"; \
go tool cover -func=coverage.out | grep -v "100.0%"; \
exit 1; \
else \
echo "🎉 Perfect! 100% test coverage achieved!"; \
fi
# Display coverage summary table by package
test-coverage-summary:
@echo "🧪 Running coverage tests by package..."
@echo ""
@echo "Coverage by package:"
@echo "┌────────────────────────────────────────────────────────┬──────────┐"
@echo "│ Package │ Coverage │"
@echo "├────────────────────────────────────────────────────────┼──────────┤"
@for pkg in $$(go list ./... | grep -v -e "/tmp/" -e "/build/"); do \
pkgname=$$(echo $$pkg | sed 's|$(MODULE_NAME)||' | sed 's|^/||'); \
if [ -z "$$pkgname" ]; then pkgname="."; fi; \
result=$$(go test -cover $$pkg 2>&1); \
cov=$$(echo "$$result" | grep -oE '[0-9]+\.[0-9]+% of statements' | grep -v '^0\.0%' | tail -1 | grep -oE '[0-9]+\.[0-9]+%'); \
if [ -z "$$cov" ]; then \
if echo "$$result" | grep -qE '\[no test files\]|no test files'; then \
cov="no tests"; \
elif echo "$$result" | grep -q "FAIL"; then \
cov="FAIL"; \
else \
cov="0.0%"; \
fi; \
fi; \
printf "│ %-54s │ %8s │\n" "$$pkgname" "$$cov"; \
done
@echo "└────────────────────────────────────────────────────────┴──────────┘"
# Run go vet
vet:
@go vet ./...
# Run all quality checks
check: format vet test test-coverage-check lint-strict
@echo "✅ All quality checks passed!"
# CI pipeline - strict checks including 100% coverage
ci: format vet test test-coverage-100 lint-strict
@echo "🚀 CI pipeline passed!"
# Clean coverage files and test cache
clean:
@echo "Cleaning up..."
@go clean -testcache
@rm -f coverage.out coverage.html
@echo "Clean complete."
# Run go mod tidy
tidy:
@echo "Running go mod tidy..."
@go mod tidy
# Download dependencies
download:
@echo "Downloading dependencies..."
@go mod download
install-hooks:
@git config core.hooksPath .githooks
@chmod +x .githooks/pre-commit
@echo "✅ Git hooks installed (core.hooksPath=.githooks)"
# Phony targets
.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 lint-check lint-strict lint-fix format help clean tidy download install-hooks