Add coverage checks

This commit is contained in:
Ian Howell 2019-05-07 10:00:07 -05:00
parent b019ba757f
commit 2880bea640
2 changed files with 38 additions and 3 deletions

View File

@ -1,13 +1,22 @@
SHELL := /bin/bash
BINDIR := bin BINDIR := bin
EXECUTABLE_CLI := airshipadm EXECUTABLE_CLI := airshipadm
SCRIPTS_DIR := scripts
# linting # linting
LINTER_CMD := "github.com/golangci/golangci-lint/cmd/golangci-lint" run LINTER_CMD := "github.com/golangci/golangci-lint/cmd/golangci-lint" run
ADDTL_LINTERS := goconst,gofmt,lll,unparam ADDTL_LINTERS := goconst,gofmt,lll,unparam
# go options # go options
PKG := ./... PKG := ./...
TESTS := . TESTS := .
# coverage
COVER := $(SCRIPTS_DIR)/coverage_test.sh
COVER_FILE := cover.out
MIN_COVERAGE := 70
.PHONY: build .PHONY: build
@ -19,13 +28,19 @@ test: build
test: lint test: lint
test: TESTFLAGS += -race -v test: TESTFLAGS += -race -v
test: unit-tests test: unit-tests
test: cover
.PHONY: unit-tests .PHONY: unit-tests
unit-tests: build unit-tests: build
@echo "Performing unit test step..." @echo "Performing unit test step..."
@go test -run $(TESTS) $(PKG) $(TESTFLAGS) @go test -run $(TESTS) $(PKG) $(TESTFLAGS) -coverprofile=$(COVER_FILE)
@echo "All unit tests passed" @echo "All unit tests passed"
.PHONY: cover
cover: unit-tests
@./$(COVER) $(COVER_FILE) $(MIN_COVERAGE)
.PHONY: lint .PHONY: lint
lint: lint:
@echo "Performing linting step..." @echo "Performing linting step..."
@ -35,6 +50,7 @@ lint:
.PHONY: clean .PHONY: clean
clean: clean:
@rm -fr $(BINDIR) @rm -fr $(BINDIR)
@rm -fr $(COVER_FILE)
.PHONY: docs .PHONY: docs
docs: docs:

19
scripts/coverage_test.sh Executable file
View File

@ -0,0 +1,19 @@
#!/bin/bash
check_coverage() {
COVER_FILE=$1
MIN_COVERAGE=$2
coverage_float=$(go tool cover -func="${COVER_FILE}" | awk "/^total:/ { print \$3 }")
coverage_int=${coverage_float%.*}
if (( "${coverage_int}" < "${MIN_COVERAGE}" )) ; then
echo "Coverage is at ${coverage_float}, but the required coverage is ${MIN_COVERAGE}"
exit 1
fi
}
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <cover_file> <required_coverage>"
exit 1
fi
check_coverage $1 $2