From 1410477332dc86785c1666b6a4a825e9a6b91230 Mon Sep 17 00:00:00 2001 From: Will McCutchen <will@mccutch.org> Date: Wed, 3 Jul 2019 09:49:12 -0700 Subject: [PATCH] Wire up automated code coverage reports --- .codecov.yml | 32 ++++++++++++++++++++++++++++++++ .gitignore | 1 + .travis.yml | 21 +++++++++++++++++++-- Makefile | 41 ++++++++++++++++++++++++++++------------- 4 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 .codecov.yml diff --git a/.codecov.yml b/.codecov.yml new file mode 100644 index 0000000..fb04707 --- /dev/null +++ b/.codecov.yml @@ -0,0 +1,32 @@ +# To validate this config: +# +# cat codecov.yml | curl --data-binary @- https://codecov.io/validate +# +# See https://docs.codecov.io/docs for more info + +# https://docs.codecov.io/docs/coverage-configuration +coverage: + precision: 2 + round: down + range: "90..100" + + # https://docs.codecov.io/docs/commit-status + status: + target: auto + threshold: 2 + patch: no + changes: no + +parsers: + gcov: + branch_detection: + conditional: yes + loop: yes + method: no + macro: no + +# https://docs.codecov.io/docs/pull-request-comments +comment: + layout: "header, diff, files" + behavior: default + require_changes: no diff --git a/.gitignore b/.gitignore index 2e43c51..a3b9b4e 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ _testmain.go *.prof dist/* +coverage.txt diff --git a/.travis.yml b/.travis.yml index 2827b50..917169c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,9 +5,26 @@ go: - '1.10' - '1.11' - '1.12' -go_import_path: github.com/mccutchen/go-httpbin + script: - make lint - - make test + - make testci + +after_success: + # Upload test coverage results to codecov.io + # https://github.com/codecov/example-go/blob/b85638743b972bd0bd2af63421fe513c6f968930/README.md + - bash <(curl -s https://codecov.io/bash) + +# With this filter, we aim to restrict Travis's "Build Pushes" feature to build +# only pushes to master, while allowing the "Build Pull Requests" to build all +# incoming pull requests without redundant double-builds. +# +# This is confusing on Travis's end; this captures the exact problem we're +# trying to solve: +# https://stackoverflow.com/a/31882307 +branches: + only: + - master + notifications: email: false diff --git a/Makefile b/Makefile index 4885c36..0cdcfcf 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,20 @@ -.PHONY: clean deploy deps image imagepush lint run stagedeploy test testcover +.PHONY: clean deploy deps image imagepush lint run stagedeploy test testci testcover -GCLOUD_PROJECT ?= httpbingo -TEST_ARGS ?= -race +# The version that will be used in docker tags (e.g. to push a +# go-httpbin:latest image use `make imagepush VERSION=latest)` VERSION ?= $(shell git rev-parse --short HEAD) +# Override this to deploy to a different App Engine project +GCLOUD_PROJECT ?= httpbingo + +# Built binaries will be placed here +DIST_PATH ?= dist + +# Default flags used by the test, testci, testcover targets +COVERAGE_PATH ?= coverage.txt +COVERAGE_ARGS ?= -covermode=atomic -coverprofile=$(COVERAGE_PATH) +TEST_ARGS ?= -race + GENERATED_ASSETS_PATH := httpbin/assets/assets.go BIN_DIR := $(GOPATH)/bin @@ -15,16 +26,16 @@ GO_SOURCES = $(wildcard **/*.go) # ============================================================================= # build # ============================================================================= -build: dist/go-httpbin +build: $(DIST_PATH)/go-httpbin -dist/go-httpbin: assets $(GO_SOURCES) - mkdir -p dist - go build -o dist/go-httpbin ./cmd/go-httpbin +$(DIST_PATH)/go-httpbin: assets $(GO_SOURCES) + mkdir -p $(DIST_PATH) + go build -o $(DIST_PATH)/go-httpbin ./cmd/go-httpbin assets: $(GENERATED_ASSETS_PATH) clean: - rm -r dist + rm -rf $(DIST_PATH) $(COVERAGE_PATH) $(GENERATED_ASSETS_PATH): $(GOBINDATA) static/* $(GOBINDATA) -o $(GENERATED_ASSETS_PATH) -pkg=assets -prefix=static static @@ -42,10 +53,14 @@ $(GENERATED_ASSETS_PATH): $(GOBINDATA) static/* test: go test $(TEST_ARGS) ./... -testcover: - mkdir -p dist - go test $(TEST_ARGS) -coverprofile=dist/coverage.out github.com/mccutchen/go-httpbin/httpbin - go tool cover -html=dist/coverage.out +# Test command to run for continuous integration, which includes code coverage +# based on codecov.io's documentation: +# https://github.com/codecov/example-go/blob/b85638743b972bd0bd2af63421fe513c6f968930/README.md +testci: + go test $(TEST_ARGS) $(COVERAGE_ARGS) ./... + +testcover: testci + go tool cover -html=$(COVERAGE_PATH) lint: $(GOLINT) test -z "$$(gofmt -d -s -e .)" || (gofmt -d -s -e . ; exit 1) @@ -63,7 +78,7 @@ stagedeploy: build gcloud app deploy --quiet --project=$(GCLOUD_PROJECT) --version=$(VERSION) --no-promote run: build - ./dist/go-httpbin + $(DIST_PATH)/go-httpbin # ============================================================================= -- GitLab