Makefile 3.1 KB

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