|
| 1 | +SHELL := /bin/sh |
| 2 | + |
| 3 | +PKG := ./... |
| 4 | +COVER_DIR := coverage |
| 5 | +COVER_PROFILE := $(COVER_DIR)/coverage.out |
| 6 | +COVER_HTML := $(COVER_DIR)/coverage.html |
| 7 | + |
| 8 | +.PHONY: all test race vet fmt fmt-check lint lint-fix cover cover-html ci |
| 9 | + |
| 10 | +all: test |
| 11 | + |
| 12 | +# Run unit tests |
| 13 | +test: |
| 14 | + go test $(PKG) |
| 15 | + |
| 16 | +# Run tests with race detector |
| 17 | +race: |
| 18 | + go test -race $(PKG) |
| 19 | + |
| 20 | +# Static analysis |
| 21 | +vet: |
| 22 | + go vet $(PKG) |
| 23 | + |
| 24 | +# Auto-format code |
| 25 | +fmt: |
| 26 | + gofmt -s -w . |
| 27 | + |
| 28 | +# Check formatting without modifying files; fails if formatting needed |
| 29 | +fmt-check: |
| 30 | + @diff=$$(gofmt -s -l .); \ |
| 31 | + if [ -n "$$diff" ]; then \ |
| 32 | + echo "Files need formatting:"; echo "$$diff"; exit 1; \ |
| 33 | + else \ |
| 34 | + echo "Formatting OK"; \ |
| 35 | + fi |
| 36 | + |
| 37 | +# Lint: go vet + formatting check + optional golangci-lint if installed |
| 38 | +lint: |
| 39 | + @echo "Running go vet"; go vet $(PKG) |
| 40 | + @echo "Checking formatting"; \ |
| 41 | + diff=$$(gofmt -s -l .); if [ -n "$$diff" ]; then echo "Files need formatting:"; echo "$$diff"; exit 1; else echo "Formatting OK"; fi |
| 42 | + @if command -v golangci-lint >/dev/null 2>&1; then \ |
| 43 | + echo "Running golangci-lint"; golangci-lint run; \ |
| 44 | + else \ |
| 45 | + echo "golangci-lint not installed; skipping"; \ |
| 46 | + fi |
| 47 | + |
| 48 | +# Attempt to fix lint: gofmt + optional golangci-lint --fix if installed |
| 49 | +lint-fix: fmt |
| 50 | + @if command -v golangci-lint >/dev/null 2>&1; then \ |
| 51 | + echo "Running golangci-lint --fix"; golangci-lint run --fix || true; \ |
| 52 | + else \ |
| 53 | + echo "golangci-lint not installed; skipping"; \ |
| 54 | + fi |
| 55 | + |
| 56 | +# Generate coverage profile and print total coverage |
| 57 | +cover: |
| 58 | + mkdir -p $(COVER_DIR) |
| 59 | + go test -covermode=atomic -coverprofile=$(COVER_PROFILE) $(PKG) |
| 60 | + go tool cover -func=$(COVER_PROFILE) | tail -n 1 |
| 61 | + |
| 62 | +# Generate HTML coverage report |
| 63 | +cover-html: cover |
| 64 | + go tool cover -html=$(COVER_PROFILE) -o $(COVER_HTML) |
| 65 | + @echo "Wrote $(COVER_HTML)" |
| 66 | + |
| 67 | +# CI-style aggregate target |
| 68 | +ci: fmt-check vet test cover |
0 commit comments