add coverage + makefile improvements

This commit is contained in:
Alex Goodman 2020-07-06 06:59:55 -04:00
parent 3ed0513800
commit b2151d080a
No known key found for this signature in database
GPG key ID: 86E2870463D5E890
2 changed files with 61 additions and 31 deletions

View file

@ -19,7 +19,7 @@ jobs:
keys:
- go-<< parameters.version >>-{{ checksum "go.sum" }}-{{ checksum "Makefile" }}
- run: make bootstrap
- run: make ci-bootstrap
- save_cache:
key: go-<< parameters.version >>-{{ checksum "go.sum" }}-{{ checksum "Makefile" }}
@ -49,7 +49,7 @@ jobs:
keys:
- go-<< parameters.version >>-{{ checksum "go.sum" }}-{{ checksum "Makefile" }}
- run: make bootstrap
- run: make ci-bootstrap
- save_cache:
key: go-<< parameters.version >>-{{ checksum "go.sum" }}-{{ checksum "Makefile" }}

View file

@ -1,60 +1,90 @@
TEMPDIR = ./.tmp
RESULTSDIR = $(TEMPDIR)/results
COVER_REPORT = $(RESULTSDIR)/cover.report
COVER_TOTAL = $(RESULTSDIR)/cover.total
LINTCMD = $(TEMPDIR)/golangci-lint run --tests=false --config .golangci.yaml
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)
# the quality gate lower threshold for unit test total % coverage (by function statements)
COVERAGE_THRESHOLD := 40
ifndef TEMPDIR
$(error TEMPDIR is not set)
$(error TEMPDIR is not set)
endif
.PHONY: all boostrap lint lint-fix unit coverage integration build-release
define title
@printf '$(TITLE)$(1)$(RESET)\n'
endef
all: lint unit # integration
.PHONY: all bootstrap lint lint-fix unit coverage integration check-pipeline clear-cache help test
all: lint test ## Run all checks (linting, unit tests, and integration tests)
@printf '$(SUCCESS)All checks pass!$(RESET)\n'
bootstrap:
@printf '$(TITLE)Downloading dependencies$(RESET)\n'
# TODO: add me back in when integration tests are implemented
test: unit #integration ## Run all tests (currently only unit)
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "$(BOLD)$(CYAN)%-25s$(RESET)%s\n", $$1, $$2}'
ci-bootstrap: bootstrap
sudo apt install -y bc
bootstrap: ## Download and install all project dependencies (+ prep tooling in the ./tmp dir)
$(call title,Downloading dependencies)
# prep temp dirs
mkdir -p $(TEMPDIR)
mkdir -p $(RESULTSDIR)
# install project dependencies
go get ./...
mkdir -p $(TEMPDIR)
# install golangci-lint
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b .tmp/ v1.26.0
# install go-acc
GOPATH=$(shell realpath ${TEMPDIR}) GO111MODULE=off go get github.com/ory/go-acc
# cleanup
rm -rf $(TEMPDIR)/src
lint:
@printf '$(TITLE)Running linters$(RESET)\n'
test -z "$(shell gofmt -l -s .)"
lint: ## Run gofmt + golangci lint checks
$(call title,Running linters)
@printf "files with gofmt issues: [$(shell gofmt -l -s .)]\n"
@test -z "$(shell gofmt -l -s .)"
$(LINTCMD)
lint-fix:
@printf '$(TITLE)Running lint fixers$(RESET)\n'
lint-fix: ## Auto-format all source code + run golangci lint fixers
$(call title,Running lint fixers)
gofmt -w -s .
$(LINTCMD) --fix
unit:
@printf '$(TITLE)Running unit tests$(RESET)\n'
go test ./...
unit: ## Run unit tests (with coverage)
$(call title,Running unit tests)
go test -coverprofile $(COVER_REPORT) ./...
@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
coverage:
@printf '$(TITLE)Running unit tests + coverage$(RESET)\n'
$(TEMPDIR)/bin/go-acc -o $(TEMPDIR)/coverage.txt ./...
# TODO: add benchmarks
# TODO: add me back in when there are integration tests
#integration:
# @printf '$(TITLE)Running integration tests...$(RESET)\n'
# TODO: add me back in when integration tests are implemented
#integration: ## Run integration tests
# $(call title,Running integration tests)
# go test -tags=integration ./integration
build-release:
clear-test-cache: ## Delete all test cache (built docker image tars)
find . -type f -wholename "**/test-fixtures/tar-cache/*.tar" -delete
check-pipeline: ## Run local CircleCI pipeline locally (sanity check)
$(call title,Check pipeline)
# note: this is meant for local development & testing of the pipeline, NOT to be run in CI
mkdir -p $(TEMPDIR)
circleci config process .circleci/config.yml > .tmp/circleci.yml
circleci local execute -c .tmp/circleci.yml --job "Static Analysis"
circleci local execute -c .tmp/circleci.yml --job "Unit & Integration Tests (go-latest)"
@printf '$(SUCCESS)Pipeline checks pass!$(RESET)\n'
# todo: replace this with goreleaser
build-release: ## Build final release binary
@mkdir -p dist
go build -s -w -X main.version="$(git describe --tags --dirty --always)" \
-X main.commit="$(git describe --dirty --always)" \
-X main.buildTime="$(date --rfc-3339=seconds --utc)"
-o dist/vulnscan