From 00889b2b0947cad38d770c898aafe42aa676da68 Mon Sep 17 00:00:00 2001 From: Jaakko Pallari Date: Sat, 19 Apr 2025 12:33:40 +0300 Subject: [PATCH] Code coverage reporting Post a coverage report on every PR. Also, modified the Makefile such that tests are not re-run needlessly. --- .github/actions/build/action.yml | 23 +++++++++++++++++++++++ .github/workflows/build.yml | 2 +- .gitignore | 3 ++- Makefile | 10 +++++----- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/.github/actions/build/action.yml b/.github/actions/build/action.yml index 2b785f2..190cb53 100644 --- a/.github/actions/build/action.yml +++ b/.github/actions/build/action.yml @@ -11,13 +11,36 @@ runs: with: go-version-file: go.mod cache-dependency-path: go.sum + - name: Run tests shell: bash run: make test + - name: Run lint uses: golangci/golangci-lint-action@v7 with: version: v2.1 + - name: Build shell: bash run: make ${{ inputs.build-target }} + + - name: Generate coverage report + if: github.event_name == 'pull_request' + shell: bash + run: make coverage + + - name: Convert coverage report to Markdown + if: github.event_name == 'pull_request' + uses: irongut/CodeCoverageSummary@v1.3.0 + with: + filename: coverage.xml + format: markdown + output: both + + - name: Post coverage report to PR + if: github.event_name == 'pull_request' + uses: marocchino/sticky-pull-request-comment@v2 + with: + recreate: true + path: code-coverage-results.md diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b40d31e..ffe300b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,7 +16,7 @@ jobs: contents: read id-token: write packages: write - pull-requests: read + pull-requests: write steps: - name: Checkout uses: actions/checkout@v4 diff --git a/.gitignore b/.gitignore index 7ea86b0..db4d047 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ -coverage.txt keruu example/*.html .makerc.mk .envrc +coverage.* +code-coverage-results.md diff --git a/Makefile b/Makefile index c93f264..9dd40aa 100644 --- a/Makefile +++ b/Makefile @@ -29,23 +29,23 @@ $(TARGET): go.* $(SOURCES) lint: golangci-lint run -v --skip-dirs '(^|/)\.go($|/)' -.PHONY: test -test: +coverage.txt: go.* $(SOURCES) $(GO) version CGO_ENABLED=1 $(GO) test \ -race \ - -coverprofile=coverage.txt \ + -coverprofile=$@ \ -covermode=atomic \ $(GO_LOCAL_MODS) -coverage.txt: test - coverage.xml: coverage.txt $(GO) tool gocover-cobertura < $< > $@ .PHONY: coverage coverage: coverage.xml +.PHONY: test +test: coverage.txt + .PHONY: clean clean: rm -f keruu