From e140ec17ff29d675d5d6f793fc05b4bd558ca3e0 Mon Sep 17 00:00:00 2001 From: taik0 Date: Wed, 7 Mar 2018 00:00:35 +0100 Subject: [PATCH] Add coveralls integration. --- .travis.yml | 2 +- Makefile | 7 ++++++- README.md | 2 +- coverage.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 coverage.sh diff --git a/.travis.yml b/.travis.yml index 01c0fc1..3431dc7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,4 +7,4 @@ before_install: - make prepare script: - - make \ No newline at end of file + - make coveralls diff --git a/Makefile b/Makefile index ace14f4..3004d7d 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ GOLANG_VERSION=1.9.3-alpine3.7 DEP_VERSION=0.4.1 OS=$(shell uname | tr '[:upper:]' '[:lower:]') +PACKAGES=$(shell go list ./...) all: deps test build @@ -20,7 +21,7 @@ deps: @echo "" test: - go test -cover -v ./... + go test -cover -v $(PACKAGES) build: @echo "Building the binary..." @@ -33,3 +34,7 @@ docker: server_build server_build: deps docker run --rm -it -e "GOPATH=/go" -v "${PWD}:/go/src/github.com/devopsfaith/api2html" -w /go/src/github.com/devopsfaith/api2html golang:${GOLANG_VERSION} go build -o api2html + +coveralls: all + go get github.com/mattn/goveralls + sh coverage.sh --coveralls diff --git a/README.md b/README.md index a8a5ec5..d6edfd4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ![api2html](https://raw.githubusercontent.com/devopsfaith/api2html.com/master/img/API2HTML-on-white.png) -[![Build Status](https://travis-ci.org/devopsfaith/api2html.svg?branch=master)](https://travis-ci.org/devopsfaith/api2html) [![Go Report Card](https://goreportcard.com/badge/github.com/devopsfaith/api2html)](https://goreportcard.com/report/github.com/devopsfaith/api2html) [![GoDoc](https://godoc.org/github.com/devopsfaith/api2html?status.svg)](https://godoc.org/github.com/devopsfaith/api2html) +[![Build Status](https://travis-ci.org/devopsfaith/api2html.svg?branch=master)](https://travis-ci.org/devopsfaith/api2html) [![Go Report Card](https://goreportcard.com/badge/github.com/devopsfaith/api2html)](https://goreportcard.com/report/github.com/devopsfaith/api2html) [![Coverage Status](https://coveralls.io/repos/github/devopsfaith/api2html/badge.svg?branch=master)](https://coveralls.io/github/devopsfaith/api2html?branch=master) [![GoDoc](https://godoc.org/github.com/devopsfaith/api2html?status.svg)](https://godoc.org/github.com/devopsfaith/api2html) ### On the fly HTML generator from API data diff --git a/coverage.sh b/coverage.sh new file mode 100644 index 0000000..8825ff8 --- /dev/null +++ b/coverage.sh @@ -0,0 +1,53 @@ +#!/bin/sh +# Generate test coverage statistics for Go packages. +# +# Works around the fact that `go test -coverprofile` currently does not work +# with multiple packages, see https://code.google.com/p/go/issues/detail?id=6909 +# +# Usage: script/coverage [--html|--coveralls] +# +# --html Additionally create HTML report and open it in browser +# --coveralls Push coverage statistics to coveralls.io +# +# File taken from https://github.com/mlafeldt/chef-runner/blob/v0.7.0/script/coverage + +set -e + +workdir=.cover +profile="$workdir/cover.out" +mode=count + +generate_cover_data() { + rm -rf "$workdir" + mkdir "$workdir" + + for pkg in "$@"; do + f="$workdir/$(echo $pkg | tr / -).cover" + go test -covermode="$mode" -coverprofile="$f" "$pkg" + done + + echo "mode: $mode" >"$profile" + grep -h -v "^mode:" "$workdir"/*.cover >>"$profile" +} + +show_cover_report() { + go tool cover -${1}="$profile" +} + +push_to_coveralls() { + echo "Pushing coverage statistics to coveralls.io" + goveralls -coverprofile="$profile" +} + +generate_cover_data $(go list ./...) +show_cover_report func +case "$1" in +"") + ;; +--html) + show_cover_report html ;; +--coveralls) + push_to_coveralls ;; +*) + echo >&2 "error: invalid option: $1"; exit 1 ;; +esac