Skip to content

Commit 74e5ec7

Browse files
committed
feat: only require cover.out instead of multiple files
1 parent 7d26b05 commit 74e5ec7

File tree

6 files changed

+28
-41
lines changed

6 files changed

+28
-41
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ jobs:
2424
- name: Generate Coverage Files
2525
run: |
2626
cd go-test-app
27-
make cover.txt
28-
make cover.html
29-
mv cover.* ../
27+
make test
28+
mv cover.out ../
3029
3130
- name: Go Coverage
3231
uses: './'

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.PHONY: release
2+
release:
3+
git tag -d v1
4+
git tag v1 HEAD
5+
git push -f origin v1

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
- name: Set up Go
3030
uses: actions/setup-go@v5
3131

32-
- name: Test # this should generate cover.<txt|html>
32+
- name: Test # this should generate cover.out
3333
run: make test
3434

3535
- name: Go Beautiful HTML Coverage
@@ -38,16 +38,18 @@ jobs:
3838
3939
## How it works
4040
41-
This GHA expects two files to be present in the root of your repo at runtime:
41+
This GHA expects `cover.out` to be present in the root of your repo at runtime. `cover.out` is usually generated by `go test` when passing the `-coverprofile=cover.out` flag:
4242

43-
- `cover.txt` is the output of `go tool cover -func=cover.out -o cover.txt`
44-
- `cover.html` is the output of `go tool cover -html=cover.out -o cover.html`
43+
```sh
44+
go test -coverprofile=cover.out ./...
45+
```
4546

46-
Both `go tool cover` commands can be configured to your liking. For examples on how you might do that you can peak at [`Makefile`](go-test-app/Makefile), or some of my other go projects like [`pretender`](https://github.com/kilianc/pretender/blob/main/Makefile#L44-L57) and [`base-go-cli`](https://github.com/kilianc/base-golang-cli/blob/main/Makefile#L76-L92).
47+
For examples on how you might do that you can peak at [`Makefile`](go-test-app/Makefile), or some of my other go projects like [`pretender`](https://github.com/kilianc/pretender/blob/main/Makefile#L44-L57) and [`base-go-cli`](https://github.com/kilianc/base-golang-cli/blob/main/Makefile#L76-L92).
4748

48-
Once the files are generated, the GHA does the following:
49+
Once your test has ran and `cover.out` has been generated, the GHA does the following:
4950

5051
1. Create and push [new orphan branch](https://github.com/gha-common/go-beautiful-html-coverage/tree/cover) if one doesn't exist.
52+
1. Generate `cover.html` and `cover.txt` from `cover.out`.
5153
1. Customize `cover.html` and rename it `<sha>.html`.
5254
1. `git-push` the `<sha>.html` file to the orphan branch. This will trigger a `GitHub Pages` deployment.
5355
1. Post a comment to your PR with your code coverage summary (`cover.txt`) and a link to your `<sha>.html`.
@@ -74,7 +76,7 @@ Once the files are generated, the GHA does the following:
7476
repository: ''
7577
7678
# The branch to checkout or create and push coverage to.
77-
# Defalut: 'cover'
79+
# Default: 'cover'
7880
branch: ''
7981
8082
# The token to use for pushing to the repository.

action.yaml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,20 @@ runs:
3636
git checkout --orphan ${{ inputs.branch }}
3737
rm .git/index
3838
git clean -fdx
39-
touch index.html
4039
fi
41-
cp ${GITHUB_ACTION_PATH}/assets/* .
42-
git add .
43-
git config user.email "go-coverage-action@github.com"
44-
git config user.name "go-coverage-action"
45-
if git diff --quiet; then
46-
echo "No changes to commit"
47-
exit 0
48-
fi
49-
git commit -m "chore: update assets"
50-
git push origin ${{ inputs.branch }}
51-
5240
5341
- name: Push Coverage
5442
shell: bash
5543
run: |
5644
export REVISION="${{ github.event.pull_request.head.sha || github.sha }}"
45+
go tool cover -func=cover.out -o cover.txt
46+
go tool cover -html=cover.out -o cover.html
5747
cd go-cover
5848
mv ../cover.html ${REVISION}.html
5949
ex -sc '%s/<style>/<style>@import url("nord.css");/' -c 'x' ${REVISION}.html
6050
ex -sc '%s/<\/script>/<\/script><script src="ln.js"><\/script>/' -c 'x' ${REVISION}.html
61-
git add -f ${REVISION}.html
51+
cp ${GITHUB_ACTION_PATH}/assets/* .
52+
git add .
6253
git config user.email "go-coverage-action@github.com"
6354
git config user.name "go-coverage-action"
6455
git commit -m "chore: add cover for ${REVISION}" || true

go-test-app/Makefile

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
cover.out:
1+
.PHONY: test
2+
test:
23
@go test -coverprofile=cover.out ./
34

4-
cover.txt: cover.out
5-
@go tool cover -func=cover.out -o cover.txt
6-
7-
cover.html: cover.out
8-
@go tool cover -html=cover.out -o cover.html
9-
105
.PHONY: clean
116
clean:
12-
@rm -f cover.out cover.txt
7+
@rm -f cover.*

src/update-comment.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@ const updateCodeCoverageComment = module.exports = async ({ context, github }, r
1212
return comment.body.startsWith('<!-- coverage -->')
1313
}) || {}
1414

15+
const coverageText = fs.readFileSync('cover.txt', 'utf8').split('\n').slice(0, -1)
16+
const coverageTextSummary = coverageText[coverageText.length-1].split('\t').pop()
17+
1518
const commentBody = [
1619
'<!-- coverage -->',
1720
`### [Code Coverage Report 🔗](https://${context.repo.owner}.github.io/${context.repo.repo}/?hash=${revision}) for ${revision}`,
18-
]
19-
20-
if (fs.existsSync('cover.txt') === true) {
21-
const coverageText = fs.readFileSync('cover.txt', 'utf8').split('\n').slice(0, -1)
22-
const coverageTextSummary = coverageText[coverageText.length-1].split('\t').pop()
23-
24-
commentBody.push(
25-
'```',
21+
'```',
2622
`Total: ${coverageTextSummary}`,
2723
'```',
2824
'<details>',
@@ -32,8 +28,7 @@ const updateCodeCoverageComment = module.exports = async ({ context, github }, r
3228
...coverageText,
3329
'```',
3430
'</details>',
35-
)
36-
}
31+
]
3732

3833
const upsertCommentOptions = {
3934
owner: context.repo.owner,

0 commit comments

Comments
 (0)