Skip to content

Commit c9dea89

Browse files
add code coverage
1 parent e1a8b4d commit c9dea89

File tree

1 file changed

+57
-5
lines changed

1 file changed

+57
-5
lines changed

.github/workflows/test.yaml

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,77 @@ on:
99
jobs:
1010
test-and-build:
1111
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
pull-requests: write
1215

1316
steps:
14-
- uses: actions/checkout@v3
17+
- uses: actions/checkout@v4
1518

1619
- name: Set up Go
17-
uses: actions/setup-go@v4
20+
uses: actions/setup-go@v5
1821
with:
1922
go-version: "1.23"
2023

2124
- name: Install dependencies
2225
run: go mod download
2326

2427
- name: Run golangci-lint
25-
uses: golangci/golangci-lint-action@v3
28+
uses: golangci/golangci-lint-action@v6
2629
with:
2730
version: latest
2831

29-
- name: Run tests
30-
run: go test -v ./...
32+
- name: Run tests with coverage
33+
run: |
34+
go test -v -race -coverprofile=coverage.out -covermode=atomic ./...
35+
go tool cover -html=coverage.out -o coverage.html
36+
37+
- name: Calculate coverage
38+
id: coverage
39+
run: |
40+
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}')
41+
echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT
42+
echo "Coverage: $COVERAGE"
43+
44+
- name: Generate coverage summary
45+
id: coverage-summary
46+
run: |
47+
echo "## 📊 Test Coverage Report" > coverage-summary.md
48+
echo "" >> coverage-summary.md
49+
echo "**Overall Coverage:** \`${{ steps.coverage.outputs.coverage }}\`" >> coverage-summary.md
50+
echo "" >> coverage-summary.md
51+
echo "### Coverage by Package" >> coverage-summary.md
52+
echo "\`\`\`" >> coverage-summary.md
53+
go tool cover -func=coverage.out >> coverage-summary.md
54+
echo "\`\`\`" >> coverage-summary.md
55+
echo "" >> coverage-summary.md
56+
echo "- 🧪 All tests passed" >> coverage-summary.md
57+
echo "- 📈 Full coverage report available in workflow artifacts" >> coverage-summary.md
58+
echo "" >> coverage-summary.md
59+
echo "_Generated by GitHub Actions_" >> coverage-summary.md
60+
61+
- name: Comment PR with coverage
62+
if: github.event_name == 'pull_request'
63+
uses: actions/github-script@v7
64+
with:
65+
script: |
66+
const fs = require('fs');
67+
const coverageSummary = fs.readFileSync('coverage-summary.md', 'utf8');
68+
69+
github.rest.issues.createComment({
70+
issue_number: context.issue.number,
71+
owner: context.repo.owner,
72+
repo: context.repo.repo,
73+
body: coverageSummary
74+
});
75+
76+
- name: Upload coverage artifact
77+
uses: actions/upload-artifact@v4
78+
with:
79+
name: coverage-report
80+
path: |
81+
coverage.out
82+
coverage.html
3183
3284
- name: Build
3385
run: go build -v ./...

0 commit comments

Comments
 (0)