-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
114 lines (97 loc) · 3.69 KB
/
Copy pathMakefile
File metadata and controls
114 lines (97 loc) · 3.69 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
# --- Variables ---
# The name of the resulting binary. Defaults to the repo directory name.
# Update this if your main package is not in the root directory.
TARGET_NAME := $(shell basename $(shell pwd))
# The default build path. This repo keeps a root main package for `go build .`.
MAIN_PACKAGE := .
# The directory where the final binary will be placed
BIN_DIR := ./bin
# Coverage thresholds
COVERAGE_THRESHOLD := 30
PACKAGE_COVERAGE_THRESHOLD := 20
CORE_PACKAGES := $(shell go list ./... | grep -Ev '/(cmd|mocks|tests)$$')
# OpenAPI generation now lives in syfon.
SYFON_DIR ?= ../syfon
# --- Targets ---
.PHONY: all build test test-coverage coverage-html coverage-check generate tidy clean help
# The default target run when you type 'make'
all: build
## build: Compiles the application binary
build:
@echo "--> Building $(TARGET_NAME)..."
@go build -o $(BIN_DIR)/$(TARGET_NAME) $(MAIN_PACKAGE)
@echo "Build successful! Binary placed in $(BIN_DIR)/$(TARGET_NAME)"
## test: Runs all unit tests (including tests in subdirectories)
test:
@echo "--> Running all tests..."
@go test -v ./...
## test-coverage: Runs tests with coverage profiling
test-coverage:
@echo "--> Running tests with coverage..."
@go test -coverprofile=coverage.out -covermode=atomic $(CORE_PACKAGES)
@echo "--> Coverage report generated: coverage.out"
@go tool cover -func=coverage.out | tail -1
## coverage-html: Generates HTML coverage report
coverage-html: test-coverage
@echo "--> Generating HTML coverage report..."
@go tool cover -html=coverage.out -o coverage.html
@echo "--> HTML coverage report generated: coverage.html"
## coverage-check: Verifies coverage meets minimum thresholds
coverage-check: test-coverage
@echo "--> Checking coverage thresholds..."
@set -euo pipefail; \
OVERALL=$$(go tool cover -func=coverage.out | awk '/^total:/ {gsub(/%/, "", $$3); print $$3}'); \
if ! awk -v val="$$OVERALL" -v min=$(COVERAGE_THRESHOLD) 'BEGIN { if (val + 0 < min) exit 1; exit 0 }'; then \
echo "Overall coverage $$OVERALL% is below the required minimum of $(COVERAGE_THRESHOLD)%"; \
exit 1; \
fi; \
go test -coverprofile=/dev/null -covermode=atomic $(CORE_PACKAGES) 2>&1 | \
awk '/^ok[[:space:]]/ { \
pkg=$$2; \
cov=$$5; \
gsub(/github.com\\/calypr\\/calypr-cli\\//, "", pkg); \
print pkg, cov; \
}' | \
while read -r pkg cov; do \
case "$$pkg" in \
cmd|mocks|tests) continue ;; \
esac; \
cov=$${cov%%%}; \
if ! awk -v val="$$cov" -v min=$(PACKAGE_COVERAGE_THRESHOLD) 'BEGIN { if (val + 0 < min) exit 1; exit 0 }'; then \
echo "Package $$pkg coverage $$cov% is below the required minimum of $(PACKAGE_COVERAGE_THRESHOLD)%"; \
exit 1; \
fi; \
done
## generate: Runs go generate commands to create mocks, embedded assets, etc.
generate:
@echo "--> Running code generation (go generate)..."
@go generate ./...
## gen: Generates Go models from OpenAPI specs
gen:
@set -euo pipefail; \
if [[ ! -d "$(SYFON_DIR)" ]]; then \
echo "ERROR: syfon repo not found at $(SYFON_DIR)"; \
exit 1; \
fi; \
echo "--> OpenAPI generation is centralized in syfon"; \
$(MAKE) -C "$(SYFON_DIR)" gen
.PHONY: gen-internal
gen-internal:
@set -euo pipefail; \
if [[ ! -d "$(SYFON_DIR)" ]]; then \
echo "ERROR: syfon repo not found at $(SYFON_DIR)"; \
exit 1; \
fi; \
echo "--> OpenAPI generation is centralized in syfon make gen"; \
$(MAKE) -C "$(SYFON_DIR)" gen
## tidy: Cleans up module dependencies and formats go files
tidy:
@echo "--> Tidying go.mod and formatting files..."
@go mod tidy
@go fmt ./...
## clean: Removes the compiled binary and coverage files
clean:
@echo "--> Cleaning up..."
@rm -f $(BIN_DIR)/$(TARGET_NAME)
@rm -f coverage.out coverage.html
@rm -rf .tmp