|
1 |
| -TEMPDIR = ./.tmp |
2 |
| - |
3 |
| -# commands and versions |
4 |
| -LINTCMD = $(TEMPDIR)/golangci-lint run --tests=false --timeout=5m --config .golangci.yaml |
5 |
| -GOIMPORTS_CMD = $(TEMPDIR)/gosimports -local github.com/anchore |
6 |
| - |
7 |
| -# tool versions |
8 |
| -GOLANGCILINT_VERSION = v1.50.1 |
9 |
| -GOSIMPORTS_VERSION = v0.3.4 |
10 |
| -BOUNCER_VERSION = v0.4.0 |
11 |
| - |
12 |
| -# formatting variables |
13 |
| -BOLD := $(shell tput -T linux bold) |
14 |
| -PURPLE := $(shell tput -T linux setaf 5) |
15 |
| -GREEN := $(shell tput -T linux setaf 2) |
16 |
| -CYAN := $(shell tput -T linux setaf 6) |
17 |
| -RED := $(shell tput -T linux setaf 1) |
18 |
| -RESET := $(shell tput -T linux sgr0) |
19 |
| -TITLE := $(BOLD)$(PURPLE) |
20 |
| -SUCCESS := $(BOLD)$(GREEN) |
21 |
| - |
22 |
| -# test variables |
23 |
| -RESULTSDIR = test/results |
24 |
| -COVER_REPORT = $(RESULTSDIR)/unit-coverage-details.txt |
25 |
| -COVER_TOTAL = $(RESULTSDIR)/unit-coverage-summary.txt |
26 |
| -# the quality gate lower threshold for unit test total % coverage (by function statements) |
27 |
| -COVERAGE_THRESHOLD := 80 |
28 |
| - |
29 |
| -$(RESULTSDIR): |
30 |
| - mkdir -p $(RESULTSDIR) |
31 |
| - |
32 |
| -$(TEMPDIR): |
33 |
| - mkdir -p $(TEMPDIR) |
34 |
| - |
35 |
| -.PHONY: bootstrap-tools |
36 |
| -bootstrap-tools: $(TEMPDIR) |
37 |
| - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(TEMPDIR)/ $(GOLANGCILINT_VERSION) |
38 |
| - curl -sSfL https://raw.githubusercontent.com/wagoodman/go-bouncer/master/bouncer.sh | sh -s -- -b $(TEMPDIR)/ $(BOUNCER_VERSION) |
39 |
| - # the only difference between goimports and gosimports is that gosimports removes extra whitespace between import blocks (see https://github.com/golang/go/issues/20818) |
40 |
| - GOBIN="$(realpath $(TEMPDIR))" go install github.com/rinchsan/gosimports/cmd/gosimports@$(GOSIMPORTS_VERSION) |
41 |
| - |
42 |
| -.PHONY: static-analysis |
43 |
| -static-analysis: check-licenses lint |
44 |
| - |
45 |
| -.PHONY: lint |
46 |
| -lint: ## Run gofmt + golangci lint checks |
47 |
| - $(call title,Running linters) |
48 |
| - # ensure there are no go fmt differences |
49 |
| - @printf "files with gofmt issues: [$(shell gofmt -l -s .)]\n" |
50 |
| - @test -z "$(shell gofmt -l -s .)" |
51 |
| - |
52 |
| - # run all golangci-lint rules |
53 |
| - $(LINTCMD) |
54 |
| - @[ -z "$(shell $(GOIMPORTS_CMD) -d .)" ] || (echo "goimports needs to be fixed" && false) |
| 1 | +.PHONY: test |
| 2 | +test: unit |
55 | 3 |
|
56 |
| - # go tooling does not play well with certain filename characters, ensure the common cases don't result in future "go get" failures |
57 |
| - $(eval MALFORMED_FILENAMES := $(shell find . | grep -e ':')) |
58 |
| - @bash -c "[[ '$(MALFORMED_FILENAMES)' == '' ]] || (printf '\nfound unsupported filename characters:\n$(MALFORMED_FILENAMES)\n\n' && false)" |
| 4 | +.PHONY: bootstrap |
| 5 | +bootstrap: |
| 6 | + go install github.com/rinchsan/gosimports/cmd/[email protected] |
59 | 7 |
|
60 |
| -.PHONY: lint-fix |
61 |
| -lint-fix: ## Auto-format all source code + run golangci lint fixers |
62 |
| - $(call title,Running lint fixers) |
63 |
| - gofmt -w -s . |
64 |
| - $(GOIMPORTS_CMD) -w . |
65 |
| - $(LINTCMD) --fix |
| 8 | +.PHONY: format |
| 9 | +format: |
| 10 | + gofmt -w . |
66 | 11 | go mod tidy
|
67 |
| - |
68 |
| -.PHONY: check-licenses |
69 |
| -check-licenses: ## Ensure transitive dependencies are compliant with the current license policy |
70 |
| - $(TEMPDIR)/bouncer check ./... |
| 12 | + gosimports -w -local github.com/anchore . |
71 | 13 |
|
72 | 14 | .PHONY: unit
|
73 |
| -unit: $(RESULTSDIR) ## Run unit tests (with coverage) |
74 |
| - $(call title,Running unit tests) |
75 |
| - go test -coverprofile $(COVER_REPORT) $(shell go list ./... | grep -v anchore/syft/test) |
76 |
| - @go tool cover -func $(COVER_REPORT) | grep total | awk '{print substr($$3, 1, length($$3)-1)}' > $(COVER_TOTAL) |
77 |
| - @echo "Coverage: $$(cat $(COVER_TOTAL))" |
78 |
| - @if [ $$(echo "$$(cat $(COVER_TOTAL)) >= $(COVERAGE_THRESHOLD)" | bc -l) -ne 1 ]; then echo "$(RED)$(BOLD)Failed coverage quality gate (> $(COVERAGE_THRESHOLD)%)$(RESET)" && false; fi |
79 |
| - |
80 |
| -.PHONY: test |
81 |
| -test: unit |
| 15 | +unit: |
| 16 | + go test -v -covermode=count -coverprofile=profile.cov.tmp ./... |
| 17 | + cat profile.cov.tmp | grep -v /model.go > profile.cov # ignore generated model file |
0 commit comments