diff --git a/.github/workflows/main-golint.yml b/.github/workflows/main-golint.yml index e9e78a5d..4ddd7930 100644 --- a/.github/workflows/main-golint.yml +++ b/.github/workflows/main-golint.yml @@ -85,3 +85,14 @@ jobs: - name: Check build of nexusd run: make service + + - name: Run coverage and refresh README badge + if: matrix.os == 'ubuntu-latest' + run: make coverage + + - name: Report coverage drift in README badge + if: matrix.os == 'ubuntu-latest' + run: | + if ! git diff --exit-code -- README.md; then + echo "::warning::README.md coverage badge differs from committed version. Run 'make coverage' locally and commit the result if you want the badge refreshed." + fi diff --git a/Makefile b/Makefile index ebdb0dbd..826a9a69 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ SERVICE_DIR = nexusd -.PHONY: all vet test service clean install uninstall +.PHONY: all vet test service clean install uninstall test-coverage coverage-badge coverage all: vet test service @@ -27,6 +27,34 @@ benchmark: go test ./test -run=XXX -bench=. -scheme=tcps go test ./test -run=XXX -bench=. -scheme=ws -compress +# Run the full suite once with coverage instrumentation. The transport-matrix +# variants in `test` are not re-run here — they exercise transport edge cases, +# not different code paths in the router/wamp/client packages we measure. +test-coverage: + go test -race -coverprofile=coverage.out -covermode=atomic ./... + go tool cover -func=coverage.out | tail -1 | awk '{print $$3}' > coverage.txt + @printf 'total coverage: %s\n' "$$(cat coverage.txt)" + +# Rewrite the README coverage badge line to a shields.io URL with the +# current percentage and a color picked from the same thresholds shields.io +# uses for its built-in coverage badges. shields.io renders the badge on +# the fly, so no SVG file is committed. Idempotent: same percentage → no +# diff. Cross-platform sed -i (works under BSD and GNU sed). +coverage-badge: test-coverage + @PCT=$$(tr -d '%' < coverage.txt); \ + COLOR=$$(awk -v p="$$PCT" 'BEGIN { \ + if (p<50) print "red"; \ + else if (p<70) print "orange"; \ + else if (p<80) print "yellow"; \ + else if (p<90) print "yellowgreen"; \ + else print "brightgreen" }'); \ + sed -i.bak -E "s|coverage-[0-9.]+%25-[a-z]+|coverage-$${PCT}%25-$${COLOR}|" README.md; \ + rm -f README.md.bak; \ + printf 'coverage badge: %s%% (%s)\n' "$$PCT" "$$COLOR" + +# Top-level: tests with coverage + badge regen. Run before pushing. +coverage: coverage-badge + service: $(SERVICE_DIR)/nexusd $(SERVICE_DIR)/nexusd: diff --git a/README.md b/README.md index 2ea544dc..615543e2 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ # WAMP v2 router library, client library and router service -[![Build Status](https://travis-ci.org/gammazero/nexus.svg)](https://travis-ci.org/gammazero/nexus) +[![Main CI](https://github.com/gammazero/nexus/actions/workflows/main-golint.yml/badge.svg)](https://github.com/gammazero/nexus/actions/workflows/main-golint.yml) +[![Coverage](https://img.shields.io/badge/coverage-56.1%25-orange)](https://github.com/gammazero/nexus/actions/workflows/main-golint.yml) [![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) [![GoDoc](https://godoc.org/github.com/gammazero/nexus?status.svg)](https://godoc.org/github.com/gammazero/nexus)