From 598a6ee5dd4da40ba164c88dad7453fdb3b8a333 Mon Sep 17 00:00:00 2001 From: Kilian Ciuffolo <385716+kilianc@users.noreply.github.com> Date: Tue, 14 May 2024 11:21:21 -0700 Subject: [PATCH] feat: only require `cover.out` instead of multiple files (#29) --- .github/workflows/ci.yaml | 8 ++------ Makefile | 13 +++++++++++++ README.md | 16 +++++++++------- action.yaml | 17 ++++------------- go-test-app/Makefile | 12 ------------ go-test-app/go.mod => go.mod | 0 src/update-comment.js | 15 +++++---------- 7 files changed, 33 insertions(+), 48 deletions(-) create mode 100644 Makefile delete mode 100644 go-test-app/Makefile rename go-test-app/go.mod => go.mod (100%) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index aef8acc..0218dfa 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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: './' diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..69da99e --- /dev/null +++ b/Makefile @@ -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.* diff --git a/README.md b/README.md index 8bd55a4..e960361 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 - - name: Test # this should generate cover. + - name: Test # this should generate cover.out run: make test - name: Go Beautiful HTML Coverage @@ -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 `.html`. 1. `git-push` the `.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 `.html`. @@ -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. diff --git a/action.yaml b/action.yaml index 87ab43e..fedb75c 100644 --- a/action.yaml +++ b/action.yaml @@ -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/