Skip to content

Commit

Permalink
feat: only require cover.out instead of multiple files (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
kilianc authored May 14, 2024
1 parent 7d26b05 commit 598a6ee
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 48 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ jobs:
go-version: '1.22'

- name: Generate Coverage Files
run: |
cd go-test-app
make cover.txt
make cover.html
mv cover.* ../
run: make test

- name: Go Coverage
- name: Go Beautiful HTML Coverage
uses: './'
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.PHONY: test
test:
@go test -coverprofile=cover.out ./...

.PHONY: release
release:
git tag -d v1
git tag v1 HEAD
git push -f origin v1

.PHONY: clean
clean:
@rm -f cover.*
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5

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

- name: Go Beautiful HTML Coverage
Expand All @@ -38,16 +38,18 @@ jobs:
## How it works
This GHA expects two files to be present in the root of your repo at runtime:
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:

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

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).
For examples on how you might do that you can peak at the [`Makefile`](./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).

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

1. Create and push [new orphan branch](https://github.com/gha-common/go-beautiful-html-coverage/tree/cover) if one doesn't exist.
1. Generate `cover.html` and `cover.txt` from `cover.out`.
1. Customize `cover.html` and rename it `<sha>.html`.
1. `git-push` the `<sha>.html` file to the orphan branch. This will trigger a `GitHub Pages` deployment.
1. Post a comment to your PR with your code coverage summary (`cover.txt`) and a link to your `<sha>.html`.
Expand All @@ -74,7 +76,7 @@ Once the files are generated, the GHA does the following:
repository: ''
# The branch to checkout or create and push coverage to.
# Defalut: 'cover'
# Default: 'cover'
branch: ''
# The token to use for pushing to the repository.
Expand Down
17 changes: 4 additions & 13 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,20 @@ runs:
git checkout --orphan ${{ inputs.branch }}
rm .git/index
git clean -fdx
touch index.html
fi
cp ${GITHUB_ACTION_PATH}/assets/* .
git add .
git config user.email "go-coverage-action@github.com"
git config user.name "go-coverage-action"
if git diff --quiet; then
echo "No changes to commit"
exit 0
fi
git commit -m "chore: update assets"
git push origin ${{ inputs.branch }}
- name: Push Coverage
shell: bash
run: |
export REVISION="${{ github.event.pull_request.head.sha || github.sha }}"
go tool cover -func=cover.out -o cover.txt
go tool cover -html=cover.out -o cover.html
cd go-cover
mv ../cover.html ${REVISION}.html
ex -sc '%s/<style>/<style>@import url("nord.css");/' -c 'x' ${REVISION}.html
ex -sc '%s/<\/script>/<\/script><script src="ln.js"><\/script>/' -c 'x' ${REVISION}.html
git add -f ${REVISION}.html
cp ${GITHUB_ACTION_PATH}/assets/* .
git add .
git config user.email "go-coverage-action@github.com"
git config user.name "go-coverage-action"
git commit -m "chore: add cover for ${REVISION}" || true
Expand Down
12 changes: 0 additions & 12 deletions go-test-app/Makefile

This file was deleted.

File renamed without changes.
15 changes: 5 additions & 10 deletions src/update-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ const updateCodeCoverageComment = module.exports = async ({ context, github }, r
return comment.body.startsWith('<!-- coverage -->')
}) || {}

const coverageText = fs.readFileSync('cover.txt', 'utf8').split('\n').slice(0, -1)
const coverageTextSummary = coverageText[coverageText.length-1].split('\t').pop()

const commentBody = [
'<!-- coverage -->',
`### [Code Coverage Report 🔗](https://${context.repo.owner}.github.io/${context.repo.repo}/?hash=${revision}) for ${revision}`,
]

if (fs.existsSync('cover.txt') === true) {
const coverageText = fs.readFileSync('cover.txt', 'utf8').split('\n').slice(0, -1)
const coverageTextSummary = coverageText[coverageText.length-1].split('\t').pop()

commentBody.push(
'```',
'```',
`Total: ${coverageTextSummary}`,
'```',
'<details>',
Expand All @@ -32,8 +28,7 @@ const updateCodeCoverageComment = module.exports = async ({ context, github }, r
...coverageText,
'```',
'</details>',
)
}
]

const upsertCommentOptions = {
owner: context.repo.owner,
Expand Down

0 comments on commit 598a6ee

Please sign in to comment.