From 88057f0ef191873f52c6433f9c20c1ef8fa907db Mon Sep 17 00:00:00 2001 From: Marko Petzold Date: Mon, 4 May 2026 19:23:42 +0200 Subject: [PATCH] ci: add coverage make targets and shields.io coverage badge Replaces the dead Travis badge in README.md with a GitHub Actions CI badge and a shields.io coverage badge whose percentage is regenerated in place by `make coverage`. - `make test-coverage` runs `go test -race -coverprofile` once over the whole module and writes the total percentage to coverage.txt. The transport-matrix variants in `make test` are intentionally not re-run here since they exercise transport edge cases, not different code paths in the router/wamp/client packages we measure. - `make coverage-badge` (depends on test-coverage) reads coverage.txt, picks a shields.io-compatible color from the same thresholds shields uses for its built-in coverage badges (red/orange/yellow/yellowgreen/ brightgreen at 50/70/80/90), and rewrites the Coverage badge URL in README.md in place via cross-platform sed. shields.io renders the badge on the fly so no SVG artifact is committed. - `make coverage` is a convenience alias. CI runs `make coverage` on ubuntu-latest and warns (does not fail) if the badge in README.md drifted, so PRs with stale badges surface in the workflow logs without blocking merge. Re-running `make coverage` against the same percentage produces no diff (deterministic output). --- .github/workflows/main-golint.yml | 11 +++++++++++ Makefile | 30 +++++++++++++++++++++++++++++- README.md | 3 ++- 3 files changed, 42 insertions(+), 2 deletions(-) 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)