diff --git a/Makefile b/Makefile index 9e3d55d..c6424e0 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,36 @@ PKG := "gitlab.lehouerou.net/laurent/$(PROJECT_NAME)" PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/) GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go) +.PHONY: all dep build clean test coverage coverhtml lint + all: build +lint: ## Lint the files + @golint -set_exit_status ${PKG_LIST} + +test: ## Run unittests + @go test -short ${PKG_LIST} + +race: dep ## Run data race detector + @go test -race -short ${PKG_LIST} + +msan: dep ## Run memory sanitizer + @go test -msan -short ${PKG_LIST} + +coverage: ## Generate global code coverage report + ./tools/coverage.sh; + +coverhtml: ## Generate global code coverage report in HTML + ./tools/coverage.sh html; + dep: ## Get the dependencies - @go get -v -d ./... + @go get -v -d ./... build: dep ## Build the binary file - @go build -i -v $(PKG) \ No newline at end of file + @go build -i -v $(PKG) + +clean: ## Remove previous build + @rm -f $(PROJECT_NAME) + +help: ## Display this help screen + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' \ No newline at end of file diff --git a/tools/coverage.sh b/tools/coverage.sh new file mode 100644 index 0000000..529bf73 --- /dev/null +++ b/tools/coverage.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# +# Code coverage generation + +COVERAGE_DIR="${COVERAGE_DIR:-coverage}" +PKG_LIST=$(go list ./... | grep -v /vendor/) + +# Create the coverage files directory +mkdir -p "$COVERAGE_DIR"; + +# Create a coverage file for each package +for package in ${PKG_LIST}; do + go test -covermode=count -coverprofile "${COVERAGE_DIR}/${package##*/}.cov" "$package" ; +done ; + +# Merge the coverage profile files +echo 'mode: count' > "${COVERAGE_DIR}"/coverage.cov ; +tail -q -n +2 "${COVERAGE_DIR}"/*.cov >> "${COVERAGE_DIR}"/coverage.cov ; + +# Display the global code coverage +go tool cover -func="${COVERAGE_DIR}"/coverage.cov ; + +# If needed, generate HTML report +if [ "$1" == "html" ]; then + go tool cover -html="${COVERAGE_DIR}"/coverage.cov -o coverage.html ; +fi + +# Remove the coverage files directory +rm -rf "$COVERAGE_DIR"; \ No newline at end of file