Skip to content

Commit 8fa7bca

Browse files
authored
Restore func diff coverage report (#40)
1 parent 775315c commit 8fa7bca

File tree

2 files changed

+101
-8
lines changed

2 files changed

+101
-8
lines changed

templates/github/workflows/test-integration.yml

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ concurrency:
1414

1515
env:
1616
GO111MODULE: "on"
17+
RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing.
1718
DOCKER_COMPOSE_FILE: ./docker-compose.yml
1819
GO_VERSION: 1.18.x
20+
TARGET_DELTA_COV: 90 # Target coverage of changed lines, in percents
1921
jobs:
2022
test:
2123
runs-on: ubuntu-latest
@@ -34,8 +36,10 @@ jobs:
3436
tar -C ~/sdk/gotip -xzf gotip.tar.gz
3537
~/sdk/gotip/bin/go version
3638
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
39+
3740
- name: Checkout code
3841
uses: actions/checkout@v2
42+
3943
- name: Go cache
4044
uses: actions/cache@v2
4145
with:
@@ -48,43 +52,84 @@ jobs:
4852
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
4953
restore-keys: |
5054
${{ runner.os }}-go-cache
55+
56+
- name: Restore base test coverage
57+
id: base-coverage
58+
uses: actions/cache@v2
59+
with:
60+
path: |
61+
integration-base.txt
62+
# Use base sha for PR or new commit hash for master/main push in test result key.
63+
key: ${{ runner.os }}-integration-test-base-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
64+
65+
- name: Checkout base code
66+
if: env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
67+
uses: actions/checkout@v2
68+
with:
69+
ref: ${{ github.event.pull_request.base.sha }}
70+
path: __base
5171
- name: Docker Compose
5272
run: (test -f ${{ env.DOCKER_COMPOSE_FILE }} && docker-compose -f ${{ env.DOCKER_COMPOSE_FILE }} up -d && docker ps && docker-compose -f ${{ env.DOCKER_COMPOSE_FILE }} logs) || echo "::warning ::Missing ${{ env.DOCKER_COMPOSE_FILE }} file"
73+
- name: Run test for base code
74+
if: env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
75+
run: |
76+
cd __base
77+
make | grep test-integration && (make test-integration && go tool cover -func=./integration.coverprofile > ../integration-base.txt) || echo "No test-integration in base"
78+
5379
- name: Test
5480
id: test
5581
run: |
5682
make test-integration
57-
go tool cover -func=./integration.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > integration.txt
83+
go tool cover -func=./integration.coverprofile > integration.txt
5884
TOTAL=$(grep 'total:' integration.txt)
5985
echo "${TOTAL}"
6086
echo "::set-output name=total::$TOTAL"
87+
6188
- name: Annotate missing test coverage
6289
id: annotate
63-
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
90+
if: github.event.pull_request.base.sha != ''
6491
run: |
6592
git fetch origin master ${{ github.event.pull_request.base.sha }}
66-
curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.0.0/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && echo "6b8bec07488b84e46c1b8993353323effe863493013309fd3df4f2cba9a2bb29 gocovdiff" | shasum -c
67-
REP=$(./gocovdiff -cov unit.coverprofile -gha-annotations gha-integration.txt)
93+
curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.3.2/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && echo "78146b31ec37ac8037a25d33b0f3c70c85adb0f8b682441b04b3daa518eaa489 gocovdiff" | shasum -c
94+
REP=$(./gocovdiff -cov integration.coverprofile -gha-annotations gha-integration.txt -delta-cov-file delta-cov-integration.txt -target-delta-cov ${TARGET_DELTA_COV})
6895
echo "${REP}"
6996
REP="${REP//$'\n'/%0A}"
7097
cat gha-integration.txt
98+
test -e integration-base.txt && cat integration-base.txt
99+
DIFF=$(test -e integration-base.txt && ./gocovdiff -func-cov integration.txt -func-base-cov integration-base.txt || echo "Missing base coverage file")
100+
DIFF="${DIFF//$'\n'/%0A}"
101+
TOTAL=$(cat delta-cov-integration.txt)
71102
echo "::set-output name=rep::$REP"
103+
echo "::set-output name=diff::$DIFF"
104+
echo "::set-output name=total::$TOTAL"
105+
72106
- name: Comment Test Coverage
73-
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
74107
continue-on-error: true
108+
if: github.event.pull_request.base.sha != ''
75109
uses: marocchino/sticky-pull-request-comment@v2
76110
with:
77111
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78112
header: integration-test
79113
message: |
80114
### Integration Test Coverage
81115
${{ steps.test.outputs.total }}
116+
${{ steps.annotate.outputs.total }}
82117
<details><summary>Coverage of changed lines</summary>
83118
84119
${{ steps.annotate.outputs.rep }}
85120
86121
</details>
87122
123+
<details><summary>Coverage diff with base branch</summary>
124+
125+
${{ steps.annotate.outputs.diff }}
126+
127+
</details>
128+
129+
- name: Store base coverage
130+
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
131+
run: cp integration.txt integration-base.txt
132+
88133
- name: Upload code coverage
89134
uses: codecov/codecov-action@v1
90135
with:

templates/github/workflows/test-unit.yml

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ concurrency:
1414

1515
env:
1616
GO111MODULE: "on"
17+
RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing.
1718
COV_GO_VERSION: 1.18.x # Version of Go to collect coverage
19+
TARGET_DELTA_COV: 90 # Target coverage of changed lines, in percents
1820
jobs:
1921
test:
2022
strategy:
@@ -27,6 +29,7 @@ jobs:
2729
uses: actions/setup-go@v3
2830
with:
2931
go-version: ${{ matrix.go-version }}
32+
3033
- name: Install Go tip
3134
if: matrix.go-version == 'tip'
3235
run: |
@@ -36,8 +39,10 @@ jobs:
3639
tar -C ~/sdk/gotip -xzf gotip.tar.gz
3740
~/sdk/gotip/bin/go version
3841
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
42+
3943
- name: Checkout code
4044
uses: actions/checkout@v2
45+
4146
- name: Go cache
4247
uses: actions/cache@v2
4348
with:
@@ -50,25 +55,57 @@ jobs:
5055
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
5156
restore-keys: |
5257
${{ runner.os }}-go-cache
58+
59+
- name: Restore base test coverage
60+
id: base-coverage
61+
if: matrix.go-version == env.COV_GO_VERSION
62+
uses: actions/cache@v2
63+
with:
64+
path: |
65+
unit-base.txt
66+
# Use base sha for PR or new commit hash for master/main push in test result key.
67+
key: ${{ runner.os }}-unit-test-base-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
68+
69+
- name: Checkout base code
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+
uses: actions/checkout@v2
72+
with:
73+
ref: ${{ github.event.pull_request.base.sha }}
74+
path: __base
75+
76+
- name: Run test for base code
77+
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 != ''
78+
run: |
79+
cd __base
80+
make | grep test-unit && (make test-unit && go tool cover -func=./unit.coverprofile > ../unit-base.txt) || echo "No test-unit in base"
81+
5382
- name: Test
5483
id: test
5584
run: |
5685
make test-unit
57-
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
86+
go tool cover -func=./unit.coverprofile > unit.txt
5887
TOTAL=$(grep 'total:' unit.txt)
5988
echo "${TOTAL}"
6089
echo "::set-output name=total::$TOTAL"
90+
6191
- name: Annotate missing test coverage
6292
id: annotate
6393
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
6494
run: |
6595
git fetch origin master ${{ github.event.pull_request.base.sha }}
66-
curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.0.0/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && echo "6b8bec07488b84e46c1b8993353323effe863493013309fd3df4f2cba9a2bb29 gocovdiff" | shasum -c
67-
REP=$(./gocovdiff -cov unit.coverprofile -gha-annotations gha-unit.txt)
96+
curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.3.2/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && echo "78146b31ec37ac8037a25d33b0f3c70c85adb0f8b682441b04b3daa518eaa489 gocovdiff" | shasum -c
97+
REP=$(./gocovdiff -cov unit.coverprofile -gha-annotations gha-unit.txt -delta-cov-file delta-cov-unit.txt -target-delta-cov ${TARGET_DELTA_COV})
6898
echo "${REP}"
6999
REP="${REP//$'\n'/%0A}"
70100
cat gha-unit.txt
101+
test -e unit-base.txt && cat unit-base.txt
102+
DIFF=$(test -e unit-base.txt && ./gocovdiff -func-cov unit.txt -func-base-cov unit-base.txt || echo "Missing base coverage file")
103+
DIFF="${DIFF//$'\n'/%0A}"
104+
TOTAL=$(cat delta-cov-unit.txt)
71105
echo "::set-output name=rep::$REP"
106+
echo "::set-output name=diff::$DIFF"
107+
echo "::set-output name=total::$TOTAL"
108+
72109
- name: Comment Test Coverage
73110
continue-on-error: true
74111
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
@@ -79,12 +116,23 @@ jobs:
79116
message: |
80117
### Unit Test Coverage
81118
${{ steps.test.outputs.total }}
119+
${{ steps.annotate.outputs.total }}
82120
<details><summary>Coverage of changed lines</summary>
83121
84122
${{ steps.annotate.outputs.rep }}
85123
86124
</details>
87125
126+
<details><summary>Coverage diff with base branch</summary>
127+
128+
${{ steps.annotate.outputs.diff }}
129+
130+
</details>
131+
132+
- name: Store base coverage
133+
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
134+
run: cp unit.txt unit-base.txt
135+
88136
- name: Upload code coverage
89137
if: matrix.go-version == env.COV_GO_VERSION
90138
uses: codecov/codecov-action@v1

0 commit comments

Comments
 (0)