Skip to content

Commit b0b4be3

Browse files
committed
test: separate tests from coverage
Signed-off-by: Daniel Vérité <[email protected]>
1 parent 45126ca commit b0b4be3

File tree

2 files changed

+85
-24
lines changed

2 files changed

+85
-24
lines changed

.github/workflows/test.yaml

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,7 @@ jobs:
2727
run: make build
2828
- name: Run Go tests
2929
run: make test
30-
- name: Code Coverage Report
31-
uses: irongut/[email protected]
32-
with:
33-
filename: coverage.xml
34-
badge: true
35-
fail_below_min: false
36-
format: markdown
37-
hide_branch_rate: false
38-
hide_complexity: true
39-
indicators: true
40-
output: both
41-
thresholds: '60 80'
42-
- uses: jwalton/gh-find-current-pr@v1
43-
id: finder
44-
- name: Add Coverage PR Comment
45-
uses: marocchino/sticky-pull-request-comment@v2
46-
with:
47-
number: ${{ steps.finder.outputs.pr }}
48-
path: code-coverage-results.md
49-
recreate: true
30+
5031
- name: Upload artifact
5132
uses: actions/upload-artifact@v4
5233
with:
@@ -156,6 +137,75 @@ jobs:
156137
- name: Run Debian package tests
157138
run: make debian-test-ci
158139

140+
coverage:
141+
name: Test Coverage
142+
needs: go
143+
runs-on: ubuntu-latest
144+
permissions:
145+
contents: read
146+
pull-requests: write
147+
env:
148+
BATS_LIB_PATH: "${{ github.workspace }}/test/bats/lib/"
149+
PGHOST: localhost
150+
PGUSER: postgres
151+
PGPASSWORD: hackme
152+
PGDATABASE: unittest
153+
services:
154+
postgres:
155+
image: postgres:14
156+
options: >-
157+
--health-cmd pg_isready
158+
--health-interval 10s
159+
--health-timeout 5s
160+
--health-retries 5
161+
--hostname postgres
162+
env:
163+
POSTGRES_PASSWORD: hackme
164+
ports:
165+
- 5432:5432
166+
steps:
167+
- uses: actions/checkout@v4
168+
- uses: actions/setup-go@v5
169+
with:
170+
go-version: '1.21'
171+
- name: Install dependencies
172+
run: |
173+
go get .
174+
- name: Build
175+
run: make build
176+
177+
- name: Setup Bats and bats libs
178+
uses: bats-core/[email protected]
179+
with:
180+
support-path: ${{ github.workspace }}/test/bats/lib/bats-support
181+
assert-path: ${{ github.workspace }}/test/bats/lib/bats-assert
182+
file-install: false # Unused
183+
detik-install: false # Unused
184+
185+
- name: Run Go and e2e tests
186+
run: make coverage
187+
188+
- name: Code Coverage Report
189+
uses: irongut/[email protected]
190+
with:
191+
filename: coverage.xml
192+
badge: true
193+
fail_below_min: false
194+
format: markdown
195+
hide_branch_rate: false
196+
hide_complexity: true
197+
indicators: true
198+
output: both
199+
thresholds: '60 80'
200+
- uses: jwalton/gh-find-current-pr@v1
201+
id: finder
202+
- name: Add Coverage PR Comment
203+
uses: marocchino/sticky-pull-request-comment@v2
204+
with:
205+
number: ${{ steps.finder.outputs.pr }}
206+
path: code-coverage-results.md
207+
recreate: true
208+
159209
# checkcov:
160210
# permissions:
161211
# security-events: write # for github/codeql-action/upload-sarif to upload SARIF results

Makefile

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ format:
1919
build:
2020
CGO_ENABLED=0 go build -v -ldflags="-X '$(BUILD_INFO_PACKAGE_PATH).Version=development' -X '$(BUILD_INFO_PACKAGE_PATH).CommitSHA=$(GIT_COMMIT_SHA)' -X '$(BUILD_INFO_PACKAGE_PATH).Date=$(BUILD_DATE)'" -o $(BINARY)
2121

22+
coverage: $(COVER_BINARY)
23+
test -d ./coverage-data || mkdir ./coverage-data
24+
rm -f ./coverage-data/cov*
25+
# run tests with the testing package
26+
go test -v ./... -cover -test.gocoverdir=$(PWD)/coverage-data -covermode set
27+
# run e2e test collecting coverage data
28+
cd scripts/bats && GOCOVERDIR=$(PWD)/coverage-data bats *.bats
29+
# merge the collected traces
30+
go tool covdata textfmt -i=./coverage-data -o coverage.txt
31+
go run github.com/boumenot/gocover-cobertura@latest < coverage.txt > coverage.xml
32+
go tool cover -html coverage.txt -o cover.html
33+
34+
2235
# build for coverage
2336
$(COVER_BINARY): build
2437
CGO_ENABLED=0 go build -cover -v -ldflags="-X '$(BUILD_INFO_PACKAGE_PATH).Version=development' -X '$(BUILD_INFO_PACKAGE_PATH).CommitSHA=$(GIT_COMMIT_SHA)' -X '$(BUILD_INFO_PACKAGE_PATH).Date=$(BUILD_DATE)'" -o $(COVER_BINARY)
@@ -31,7 +44,8 @@ run:
3144
install: build
3245
GOBIN=/usr/local/bin/ go install -v -ldflags="-X '$(BUILD_INFO_PACKAGE_PATH).Version=development' -X '$(BUILD_INFO_PACKAGE_PATH).CommitSHA=$(GIT_COMMIT_SHA)' -X '$(BUILD_INFO_PACKAGE_PATH).Date=$(BUILD_DATE)'"
3346

34-
bats-test: $(COVER_BINARY)
47+
.PHONY: bats-test
48+
bats-test: build $(COVER_BINARY)
3549
test -d ./coverage-data || mkdir ./coverage-data
3650
cd scripts/bats && GOCOVERDIR=$(PWD)/coverage-data bats *.bats
3751

@@ -67,9 +81,6 @@ checkcov:
6781
test:
6882
test -d ./coverage-data || mkdir ./coverage-data
6983
go test -v ./... -cover -test.gocoverdir=$(PWD)/coverage-data -covermode set
70-
go tool covdata textfmt -i=./coverage-data -o coverage.txt
71-
go run github.com/boumenot/gocover-cobertura@latest < coverage.txt > coverage.xml
72-
go tool cover -html coverage.txt -o cover.html
7384

7485
.PHONY: lint
7586
lint:

0 commit comments

Comments
 (0)