Skip to content

Commit 775315c

Browse files
authored
Use gocovdiff in tests instead of base diff (#39)
1 parent d3da2a9 commit 775315c

2 files changed

Lines changed: 32 additions & 66 deletions

File tree

templates/github/workflows/test-integration.yml

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ concurrency:
1414

1515
env:
1616
GO111MODULE: "on"
17-
RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing.
1817
DOCKER_COMPOSE_FILE: ./docker-compose.yml
1918
GO_VERSION: 1.18.x
2019
jobs:
@@ -49,43 +48,29 @@ jobs:
4948
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
5049
restore-keys: |
5150
${{ runner.os }}-go-cache
52-
- name: Restore base test coverage
53-
id: base-coverage
54-
uses: actions/cache@v2
55-
with:
56-
path: |
57-
integration-base.txt
58-
# Use base sha for PR or new commit hash for master/main push in test result key.
59-
key: ${{ runner.os }}-integration-test-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
60-
- name: Checkout base code
61-
if: env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
62-
uses: actions/checkout@v2
63-
with:
64-
ref: ${{ github.event.pull_request.base.sha }}
65-
path: __base
6651
- name: Docker Compose
6752
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"
68-
- name: Run test for base code
69-
if: env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
70-
run: |
71-
cd __base
72-
make | grep test-integration && (make test-integration && go tool cover -func=./integration.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../integration-base.txt) || echo "No test-integration in base"
7353
- name: Test
7454
id: test
7555
run: |
7656
make test-integration
7757
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
78-
OUTPUT=$(test -e integration-base.txt && (diff integration-base.txt integration.txt || exit 0) || cat integration.txt)
79-
echo "${OUTPUT}"
80-
OUTPUT="${OUTPUT//$'\n'/%0A}"
8158
TOTAL=$(grep 'total:' integration.txt)
8259
echo "${TOTAL}"
83-
echo "::set-output name=diff::$OUTPUT"
8460
echo "::set-output name=total::$TOTAL"
85-
- name: Store base coverage
86-
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
87-
run: cp integration.txt integration-base.txt
61+
- name: Annotate missing test coverage
62+
id: annotate
63+
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
64+
run: |
65+
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)
68+
echo "${REP}"
69+
REP="${REP//$'\n'/%0A}"
70+
cat gha-integration.txt
71+
echo "::set-output name=rep::$REP"
8872
- name: Comment Test Coverage
73+
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
8974
continue-on-error: true
9075
uses: marocchino/sticky-pull-request-comment@v2
9176
with:
@@ -94,11 +79,10 @@ jobs:
9479
message: |
9580
### Integration Test Coverage
9681
${{ steps.test.outputs.total }}
97-
<details><summary>Coverage diff with base branch</summary>
82+
<details><summary>Coverage of changed lines</summary>
83+
84+
${{ steps.annotate.outputs.rep }}
9885
99-
```diff
100-
${{ steps.test.outputs.diff }}
101-
```
10286
</details>
10387
10488
- name: Upload code coverage

templates/github/workflows/test-unit.yml

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ concurrency:
1414

1515
env:
1616
GO111MODULE: "on"
17-
RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing.
1817
COV_GO_VERSION: 1.18.x # Version of Go to collect coverage
1918
jobs:
2019
test:
2120
strategy:
2221
matrix:
23-
go-version: [ 1.16.x, 1.17.x, 1.18.x, tip ]
22+
go-version: [ 1.16.x, 1.17.x, 1.18.x ]
2423
runs-on: ubuntu-latest
2524
steps:
2625
- name: Install Go stable
@@ -51,56 +50,39 @@ jobs:
5150
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
5251
restore-keys: |
5352
${{ runner.os }}-go-cache
54-
- name: Restore base test coverage
55-
id: base-coverage
56-
if: matrix.go-version == env.COV_GO_VERSION
57-
uses: actions/cache@v2
58-
with:
59-
path: |
60-
unit-base.txt
61-
# Use base sha for PR or new commit hash for master/main push in test result key.
62-
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
69-
- name: Run test for 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-
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"
7453
- name: Test
7554
id: test
7655
run: |
7756
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}"
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
8258
TOTAL=$(grep 'total:' unit.txt)
8359
echo "${TOTAL}"
84-
echo "::set-output name=diff::$OUTPUT"
8560
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
61+
- name: Annotate missing test coverage
62+
id: annotate
63+
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
64+
run: |
65+
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)
68+
echo "${REP}"
69+
REP="${REP//$'\n'/%0A}"
70+
cat gha-unit.txt
71+
echo "::set-output name=rep::$REP"
8972
- name: Comment Test Coverage
9073
continue-on-error: true
91-
if: matrix.go-version == env.COV_GO_VERSION
74+
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
9275
uses: marocchino/sticky-pull-request-comment@v2
9376
with:
9477
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9578
header: unit-test
9679
message: |
9780
### Unit Test Coverage
9881
${{ steps.test.outputs.total }}
99-
<details><summary>Coverage diff with base branch</summary>
82+
<details><summary>Coverage of changed lines</summary>
83+
84+
${{ steps.annotate.outputs.rep }}
10085
101-
```diff
102-
${{ steps.test.outputs.diff }}
103-
```
10486
</details>
10587
10688
- name: Upload code coverage

0 commit comments

Comments
 (0)