-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
215 lines (189 loc) · 8.04 KB
/
Makefile
File metadata and controls
215 lines (189 loc) · 8.04 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Variables
LIB_NAME = hatmax
MODULE_NAME = hatmax.adrianpk.com
LINT_CACHE_DIR = $(CURDIR)/.tmp/lint
LINT_GOCACHE = $(LINT_CACHE_DIR)/gocache
COVERAGE_PACKAGES = $(shell go list -f '{{if ne (len .TestGoFiles) 0}}{{.ImportPath}}{{end}}' ./... | grep -v -e "/examples/" -e "/tmp/" -e "/build/")
# 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 80% 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 and update badges"
@echo " update-badge - Update coverage badge"
@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 $(COVERAGE_PACKAGES)
@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 (80%)
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 < 80)}') -eq 1 ]; then \
echo "❌ Coverage $$COVERAGE% is below 80% threshold"; \
exit 1; \
else \
echo "✅ Coverage $$COVERAGE% meets the 80% 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 awk -v cov="$$COVERAGE" 'BEGIN {exit !(cov < 70)}'; then \
echo "❌ Coverage $$COVERAGE% is below 70%"; \
go tool cover -func=coverage.out | grep -v "100.0%"; \
exit 1; \
else \
echo "✅ Coverage $$COVERAGE% meets 70% threshold"; \
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 $(COVERAGE_PACKAGES); 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, updates badges
ci:
@if $(MAKE) format vet test test-coverage-100 lint-strict; then \
echo "{\"schemaVersion\":1,\"label\":\"CI\",\"message\":\"passing\",\"color\":\"brightgreen\"}" > .badges/ci.json; \
$(MAKE) update-badge; \
echo "🚀 CI pipeline passed, badges updated!"; \
else \
echo "{\"schemaVersion\":1,\"label\":\"CI\",\"message\":\"failing\",\"color\":\"red\"}" > .badges/ci.json; \
echo "❌ CI failed, badge updated"; \
exit 1; \
fi
# Update coverage badge
update-badge: test-coverage-profile
@mkdir -p .badges
@COVERAGE=$$(go tool cover -func=coverage.out | tail -1 | awk '{print $$3}' | sed 's/%//'); \
if awk -v cov="$$COVERAGE" 'BEGIN {exit !(cov >= 90)}'; then \
COLOR="brightgreen"; \
elif awk -v cov="$$COVERAGE" 'BEGIN {exit !(cov >= 80)}'; then \
COLOR="green"; \
elif awk -v cov="$$COVERAGE" 'BEGIN {exit !(cov >= 70)}'; then \
COLOR="yellowgreen"; \
elif awk -v cov="$$COVERAGE" 'BEGIN {exit !(cov >= 60)}'; then \
COLOR="yellow"; \
else \
COLOR="red"; \
fi; \
echo "{\"schemaVersion\":1,\"label\":\"coverage\",\"message\":\"$$COVERAGE%\",\"color\":\"$$COLOR\"}" > .badges/coverage.json; \
echo "Badge updated: $$COVERAGE% ($$COLOR)"
# 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 update-badge lint lint-check lint-strict lint-fix format help clean tidy download install-hooks