| TEMPDIR = ./.tmp | |
| # commands and versions | |
| LINTCMD = $(TEMPDIR)/golangci-lint run --tests=false --timeout=5m --config .golangci.yaml | |
| GOIMPORTS_CMD = $(TEMPDIR)/gosimports -local github.com/anchore | |
| # tool versions | |
| GOLANGCILINT_VERSION = v1.50.1 | |
| GOSIMPORTS_VERSION = v0.3.4 | |
| BOUNCER_VERSION = v0.4.0 | |
| # formatting variables | |
| BOLD := $(shell tput -T linux bold) | |
| PURPLE := $(shell tput -T linux setaf 5) | |
| GREEN := $(shell tput -T linux setaf 2) | |
| CYAN := $(shell tput -T linux setaf 6) | |
| RED := $(shell tput -T linux setaf 1) | |
| RESET := $(shell tput -T linux sgr0) | |
| TITLE := $(BOLD)$(PURPLE) | |
| SUCCESS := $(BOLD)$(GREEN) | |
| # test variables | |
| RESULTSDIR = test/results | |
| COVER_REPORT = $(RESULTSDIR)/unit-coverage-details.txt | |
| COVER_TOTAL = $(RESULTSDIR)/unit-coverage-summary.txt | |
| # the quality gate lower threshold for unit test total % coverage (by function statements) | |
| COVERAGE_THRESHOLD := 80 | |
| $(RESULTSDIR): | |
| mkdir -p $(RESULTSDIR) | |
| $(TEMPDIR): | |
| mkdir -p $(TEMPDIR) | |
| .PHONY: bootstrap-tools | |
| bootstrap-tools: $(TEMPDIR) | |
| curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(TEMPDIR)/ $(GOLANGCILINT_VERSION) | |
| curl -sSfL https://raw.githubusercontent.com/wagoodman/go-bouncer/master/bouncer.sh | sh -s -- -b $(TEMPDIR)/ $(BOUNCER_VERSION) | |
| # the only difference between goimports and gosimports is that gosimports removes extra whitespace between import blocks (see https://github.com/golang/go/issues/20818) | |
| GOBIN="$(realpath $(TEMPDIR))" go install github.com/rinchsan/gosimports/cmd/gosimports@$(GOSIMPORTS_VERSION) | |
| .PHONY: static-analysis | |
| static-analysis: check-licenses lint | |
| .PHONY: lint | |
| lint: ## Run gofmt + golangci lint checks | |
| $(call title,Running linters) | |
| # ensure there are no go fmt differences | |
| @printf "files with gofmt issues: [$(shell gofmt -l -s .)]\n" | |
| @test -z "$(shell gofmt -l -s .)" | |
| # run all golangci-lint rules | |
| $(LINTCMD) | |
| @[ -z "$(shell $(GOIMPORTS_CMD) -d .)" ] || (echo "goimports needs to be fixed" && false) | |
| # go tooling does not play well with certain filename characters, ensure the common cases don't result in future "go get" failures | |
| $(eval MALFORMED_FILENAMES := $(shell find . | grep -e ':')) | |
| @bash -c "[[ '$(MALFORMED_FILENAMES)' == '' ]] || (printf '\nfound unsupported filename characters:\n$(MALFORMED_FILENAMES)\n\n' && false)" | |
| .PHONY: lint-fix | |
| lint-fix: ## Auto-format all source code + run golangci lint fixers | |
| $(call title,Running lint fixers) | |
| gofmt -w -s . | |
| $(GOIMPORTS_CMD) -w . | |
| $(LINTCMD) --fix | |
| go mod tidy | |
| .PHONY: check-licenses | |
| check-licenses: ## Ensure transitive dependencies are compliant with the current license policy | |
| $(TEMPDIR)/bouncer check ./... | |
| .PHONY: unit | |
| unit: $(RESULTSDIR) ## Run unit tests (with coverage) | |
| $(call title,Running unit tests) | |
| go test -coverprofile $(COVER_REPORT) $(shell go list ./... | grep -v anchore/syft/test) | |
| @go tool cover -func $(COVER_REPORT) | grep total | awk '{print substr($$3, 1, length($$3)-1)}' > $(COVER_TOTAL) | |
| @echo "Coverage: $$(cat $(COVER_TOTAL))" | |
| @if [ $$(echo "$$(cat $(COVER_TOTAL)) >= $(COVERAGE_THRESHOLD)" | bc -l) -ne 1 ]; then echo "$(RED)$(BOLD)Failed coverage quality gate (> $(COVERAGE_THRESHOLD)%)$(RESET)" && false; fi | |
| .PHONY: test | |
| test: unit |