...
1
2GOLANGCI_LINT_VERSION=v1.48.0
3
4LINTER=./bin/golangci-lint
5LINTER_VERSION_FILE=./bin/.golangci-lint-version-$(GOLANGCI_LINT_VERSION)
6
7ALL_SOURCES := $(shell find * -type f -name "*.go")
8
9COVERAGE_PROFILE_RAW=./build/coverage_raw.out
10COVERAGE_PROFILE_RAW_HTML=./build/coverage_raw.html
11COVERAGE_PROFILE_FILTERED=./build/coverage.out
12COVERAGE_PROFILE_FILTERED_HTML=./build/coverage.html
13COVERAGE_ENFORCER_FLAGS=-skipcode "// COVERAGE" -packagestats -filestats -showcode
14
15TEST_BINARY=./build/go-sdk-common.test
16ALLOCATIONS_LOG=./build/allocations.out
17
18EASYJSON_TAG=launchdarkly_easyjson
19
20.PHONY: all build build-easyjson clean test test-easyjson test-coverage lint benchmarks benchmarks-easyjson benchmark-allocs
21
22all: build build-easyjson
23
24build:
25 go build ./...
26
27build-easyjson:
28 go build -tags $(EASYJSON_TAG) ./...
29
30clean:
31 go clean
32
33test: build
34 go test -run=not-a-real-test ./... # just ensures that the tests compile
35 go test -v -race ./...
36
37test-easyjson: build-easyjson
38 go test -run=not-a-real-test -tags $(EASYJSON_TAG) ./... # just ensures that the tests compile
39 go test -v -tags $(EASYJSON_TAG) -race ./...
40
41test-coverage: $(COVERAGE_PROFILE_RAW)
42 go run github.com/launchdarkly-labs/go-coverage-enforcer@latest $(COVERAGE_ENFORCER_FLAGS) -outprofile $(COVERAGE_PROFILE_FILTERED) $(COVERAGE_PROFILE_RAW)
43 go tool cover -html $(COVERAGE_PROFILE_FILTERED) -o $(COVERAGE_PROFILE_FILTERED_HTML)
44 go tool cover -html $(COVERAGE_PROFILE_RAW) -o $(COVERAGE_PROFILE_RAW_HTML)
45
46$(COVERAGE_PROFILE_RAW): $(ALL_SOURCES)
47 @mkdir -p ./build
48 go test -coverprofile $(COVERAGE_PROFILE_RAW) ./... >/dev/null
49
50benchmarks: build
51 @mkdir -p ./build
52 go test -benchmem '-run=^$$' '-bench=.*' ./... | tee build/benchmarks.out
53 @if grep <build/benchmarks.out 'NoAlloc.*[1-9][0-9]* allocs/op'; then echo "Unexpected heap allocations detected in benchmarks!"; exit 1; fi
54
55benchmarks-easyjson: build
56 @mkdir -p ./build
57 go test -tags $(EASYJSON_TAG) -benchmem '-run=^$$' '-bench=.*' ./... | tee build/benchmarks.out
58
59# See CONTRIBUTING.md regarding the use of the benchmark-allocs target. Notes about this implementation:
60# 1. We precompile the test code because otherwise the allocation traces will include the actions of the compiler itself.
61# 2. "benchtime=3x" means the run count (b.N) is set to 3. Setting it to 1 would produce less redundant output, but the
62# benchmark statistics seem to be less reliable if the run count is less than 3 - they will report a higher allocation
63# count per run, possibly because the first run
64benchmark-allocs:
65 @if [ -z "$$BENCHMARK" ]; then echo "must specify BENCHMARK=" && exit 1; fi
66 @mkdir -p ./build
67 @echo Precompiling test code to $(TEST_BINARY)
68 @go test -c -o $(TEST_BINARY) $(BENCHMARK_PACKAGE) >/dev/null 2>&1
69 @echo "Generating heap allocation traces in $(ALLOCATIONS_LOG) for benchmark(s): $$BENCHMARK"
70 @echo "You should see some benchmark result output; if you do not, you may have misspelled the benchmark name/regex"
71 @GODEBUG=allocfreetrace=1 $(TEST_BINARY) -test.run=none -test.bench=$$BENCHMARK -test.benchmem -test.benchtime=1x 2>$(ALLOCATIONS_LOG)
72
73$(LINTER_VERSION_FILE):
74 rm -f $(LINTER)
75 curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s $(GOLANGCI_LINT_VERSION)
76 touch $(LINTER_VERSION_FILE)
77
78lint: $(LINTER_VERSION_FILE)
79 $(LINTER) run ./...
View as plain text