@@ -15,19 +15,21 @@ concurrency:
15
15
env :
16
16
GO111MODULE : " on"
17
17
RUN_BASE_COVERAGE : " on" # Runs test for PR base in case base test coverage is missing.
18
- COV_GO_VERSION : 1.18.x # Version of Go to collect coverage
18
+ COV_GO_VERSION : 1.22.x # Version of Go to collect coverage
19
+ TARGET_DELTA_COV : 90 # Target coverage of changed lines, in percents
19
20
jobs :
20
21
test :
21
22
strategy :
22
23
matrix :
23
- go-version : [ 1.15 .x, 1.16 .x, 1.17.x, 1.18 .x ]
24
+ go-version : [ 1.19 .x, 1.21 .x, 1.22 .x ]
24
25
runs-on : ubuntu-latest
25
26
steps :
26
27
- name : Install Go stable
27
28
if : matrix.go-version != 'tip'
28
- uses : actions/setup-go@v3
29
+ uses : actions/setup-go@v4
29
30
with :
30
31
go-version : ${{ matrix.go-version }}
32
+
31
33
- name : Install Go tip
32
34
if : matrix.go-version == 'tip'
33
35
run : |
@@ -37,10 +39,12 @@ jobs:
37
39
tar -C ~/sdk/gotip -xzf gotip.tar.gz
38
40
~/sdk/gotip/bin/go version
39
41
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
42
+
40
43
- name : Checkout code
41
- uses : actions/checkout@v2
44
+ uses : actions/checkout@v3
45
+
42
46
- name : Go cache
43
- uses : actions/cache@v2
47
+ uses : actions/cache@v3
44
48
with :
45
49
# In order:
46
50
# * Module download cache
@@ -51,58 +55,79 @@ jobs:
51
55
key : ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
52
56
restore-keys : |
53
57
${{ runner.os }}-go-cache
58
+
54
59
- name : Restore base test coverage
55
60
id : base-coverage
56
- if : matrix.go-version == env.COV_GO_VERSION
61
+ if : matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
57
62
uses : actions/cache@v2
58
63
with :
59
64
path : |
60
65
unit-base.txt
61
66
# Use base sha for PR or new commit hash for master/main push in test result key.
62
67
key : ${{ runner.os }}-unit-test-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
63
- - name : Checkout base code
64
- if : matrix.go-version == env.COV_GO_VERSION && env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
65
- uses : actions/checkout@v2
66
- with :
67
- ref : ${{ github.event.pull_request.base.sha }}
68
- path : __base
68
+
69
69
- name : Run test for base code
70
70
if : matrix.go-version == env.COV_GO_VERSION && env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
71
71
run : |
72
- cd __base
73
- make | grep test-unit && (make test-unit && go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt) || echo "No test-unit in base"
72
+ git fetch origin master ${{ github.event.pull_request.base.sha }}
73
+ HEAD=$(git rev-parse HEAD)
74
+ git reset --hard ${{ github.event.pull_request.base.sha }}
75
+ (make test-unit && go tool cover -func=./unit.coverprofile > unit-base.txt) || echo "No test-unit in base"
76
+ git reset --hard $HEAD
77
+
74
78
- name : Test
75
79
id : test
76
80
run : |
77
81
make test-unit
78
- go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > unit.txt
79
- OUTPUT=$(test -e unit-base.txt && (diff unit-base.txt unit.txt || exit 0) || cat unit.txt)
80
- echo "${OUTPUT}"
81
- OUTPUT="${OUTPUT//$'\n'/%0A}"
82
+ go tool cover -func=./unit.coverprofile > unit.txt
82
83
TOTAL=$(grep 'total:' unit.txt)
83
84
echo "${TOTAL}"
84
- echo "::set-output name=diff::$OUTPUT"
85
- echo "::set-output name=total::$TOTAL"
86
- - name : Store base coverage
87
- if : ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
88
- run : cp unit.txt unit-base.txt
89
- - name : Comment Test Coverage
85
+ echo "total=$TOTAL" >> $GITHUB_OUTPUT
86
+
87
+ - name : Annotate missing test coverage
88
+ id : annotate
89
+ if : matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
90
+ run : |
91
+ curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.4.2/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && rm linux_amd64.tar.gz
92
+ gocovdiff_hash=$(git hash-object ./gocovdiff)
93
+ [ "$gocovdiff_hash" == "c37862c73a677e5a9c069470287823ab5bbf0244" ] || (echo "::error::unexpected hash for gocovdiff, possible tampering: $gocovdiff_hash" && exit 1)
94
+ git fetch origin master ${{ github.event.pull_request.base.sha }}
95
+ REP=$(./gocovdiff -mod github.com/$GITHUB_REPOSITORY -cov unit.coverprofile -gha-annotations gha-unit.txt -delta-cov-file delta-cov-unit.txt -target-delta-cov ${TARGET_DELTA_COV})
96
+ echo "${REP}"
97
+ cat gha-unit.txt
98
+ DIFF=$(test -e unit-base.txt && ./gocovdiff -mod github.com/$GITHUB_REPOSITORY -func-cov unit.txt -func-base-cov unit-base.txt || echo "Missing base coverage file")
99
+ TOTAL=$(cat delta-cov-unit.txt)
100
+ echo "rep<<EOF" >> $GITHUB_OUTPUT && echo "$REP" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
101
+ echo "diff<<EOF" >> $GITHUB_OUTPUT && echo "$DIFF" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
102
+ echo "total<<EOF" >> $GITHUB_OUTPUT && echo "$TOTAL" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
103
+
104
+ - name : Comment test coverage
90
105
continue-on-error : true
91
- if : matrix.go-version == env.COV_GO_VERSION
106
+ if : matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
92
107
uses : marocchino/sticky-pull-request-comment@v2
93
108
with :
94
109
GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
95
110
header : unit-test
96
111
message : |
97
112
### Unit Test Coverage
98
113
${{ steps.test.outputs.total }}
114
+ ${{ steps.annotate.outputs.total }}
115
+ <details><summary>Coverage of changed lines</summary>
116
+
117
+ ${{ steps.annotate.outputs.rep }}
118
+
119
+ </details>
120
+
99
121
<details><summary>Coverage diff with base branch</summary>
100
122
101
- ```diff
102
- ${{ steps.test.outputs.diff }}
103
- ```
123
+ ${{ steps.annotate.outputs.diff }}
124
+
104
125
</details>
105
126
127
+ - name : Store base coverage
128
+ if : ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
129
+ run : cp unit.txt unit-base.txt
130
+
106
131
- name : Upload code coverage
107
132
if : matrix.go-version == env.COV_GO_VERSION
108
133
uses : codecov/codecov-action@v1
0 commit comments