1
1
# Copyright 2025 SAP SE
2
2
# SPDX-License-Identifier: Apache-2.0
3
3
4
- name : Test
4
+ name : Test and Report Coverage
5
5
6
6
on : [push]
7
7
@@ -15,10 +15,12 @@ jobs:
15
15
uses : actions/setup-go@v5
16
16
with :
17
17
go-version : 1.24.2
18
- - name : Test without Docker
18
+ - name : Test quickly without Docker
19
19
run : go test -v ./...
20
20
21
21
test-with-docker :
22
+ # We don't need to run this longer test if the previous one already failed.
23
+ needs : test-without-docker
22
24
runs-on : ubuntu-latest
23
25
services :
24
26
dind :
32
34
uses : actions/setup-go@v5
33
35
with :
34
36
go-version : 1.24.2
35
- - name : Test with Docker
36
- run : POSTGRES_CONTAINER=1 VERNEMQ_CONTAINER=1 go test -v ./...
37
+ - name : Run tests with Docker and calculate coverage
38
+ run : |
39
+ POSTGRES_CONTAINER=1 VERNEMQ_CONTAINER=1 go test -v -coverpkg=./internal/... -coverprofile=pr_profile.cov ./internal/...
40
+ CURRENT_COVERAGE=$(go tool cover -func pr_profile.cov | grep total | awk '{print $3}' | tr -d '%')
41
+ echo "CURRENT_COVERAGE=$CURRENT_COVERAGE" >> $GITHUB_ENV
42
+
43
+ # Steps below are only executed if the workflow is triggered by a pull request
44
+ - name : Checkout base commit (PR only)
45
+ if : ${{ github.event_name == 'pull_request' }}
46
+ uses : actions/checkout@v4
47
+ with :
48
+ ref : ${{ github.event.pull_request.base.sha }}
49
+ - name : Run tests on base commit and calculate previous coverage (PR only)
50
+ if : ${{ github.event_name == 'pull_request' }}
51
+ run : |
52
+ POSTGRES_CONTAINER=1 VERNEMQ_CONTAINER=1 go test -v -coverpkg=./internal/... -coverprofile=base_profile.cov ./internal/...
53
+ PREVIOUS_COVERAGE=$(go tool cover -func base_profile.cov | grep total | awk '{print $3}' | tr -d '%')
54
+ echo "PREVIOUS_COVERAGE=$PREVIOUS_COVERAGE" >> $GITHUB_ENV
55
+ - name : Calculate coverage change (PR only)
56
+ if : ${{ github.event_name == 'pull_request' }}
57
+ run : |
58
+ CHANGE=$(echo "$CURRENT_COVERAGE - $PREVIOUS_COVERAGE" | bc)
59
+ echo "CHANGE=$CHANGE" >> $GITHUB_ENV
60
+ - name : Delete old coverage comments (PR only)
61
+ if : ${{ github.event_name == 'pull_request' }}
62
+ uses : actions/github-script@v7
63
+ with :
64
+ script : |
65
+ const { data: comments } = await github.rest.issues.listComments({
66
+ owner: context.repo.owner,
67
+ repo: context.repo.repo,
68
+ issue_number: context.issue.number,
69
+ });
70
+ const coverageCommentTag = '<!-- coverage-comment -->';
71
+ for (const comment of comments) {
72
+ if (comment.body.includes(coverageCommentTag)) {
73
+ await github.rest.issues.deleteComment({
74
+ owner: context.repo.owner,
75
+ repo: context.repo.repo,
76
+ comment_id: comment.id,
77
+ });
78
+ }
79
+ }
80
+ - name : Post coverage comment (PR only)
81
+ if : ${{ github.event_name == 'pull_request' }}
82
+ uses : actions/github-script@v7
83
+ with :
84
+ script : |
85
+ const currentCoverage = process.env.CURRENT_COVERAGE || 'unknown';
86
+ const previousCoverage = process.env.PREVIOUS_COVERAGE || 'unknown';
87
+ const change = process.env.CHANGE || 'unknown';
88
+ const commentBody = `
89
+ <!-- coverage-comment -->
90
+ Coverage in go module **internal/**: **${currentCoverage}%** (${change >= 0 ? '+' : ''}${change}%)
91
+ `;
92
+ await github.rest.issues.createComment({
93
+ issue_number: context.issue.number,
94
+ owner: context.repo.owner,
95
+ repo: context.repo.repo,
96
+ body: commentBody,
97
+ });
0 commit comments