|
10 | 10 |
|
11 | 11 | `go-test-coverage` is tool which reports issues when test coverage of a file or package is below set threshold.
|
12 | 12 |
|
13 |
| -### Usage |
| 13 | +## Usage |
14 | 14 |
|
15 | 15 | ```yml
|
16 | 16 | name: Go test coverage check
|
|
19 | 19 | - uses: actions/checkout@v3
|
20 | 20 | - uses: actions/setup-go@v3
|
21 | 21 |
|
22 |
| - - name: test (generate coverage) |
| 22 | + - name: generate test coverage |
23 | 23 | run: go test ./... -coverprofile=./cover.out
|
24 | 24 |
|
25 | 25 | - name: check test coverage
|
|
37 | 37 | threshold-total: 95
|
38 | 38 | ```
|
39 | 39 |
|
40 |
| -### Config |
41 |
| -Example of [.testcoverage.yml](./.testcoverage.example.yml) config file. |
| 40 | +## Config |
| 41 | +Example of [.testcoverage.yml](./.testcoverage.example.yml) config file: |
42 | 42 |
|
43 | 43 | ```yml
|
44 | 44 | # (mandatory)
|
@@ -68,6 +68,59 @@ threshold:
|
68 | 68 | total: 95
|
69 | 69 | ```
|
70 | 70 |
|
| 71 | +## Badge |
| 72 | +
|
| 73 | +Generate self hosted coverage badge with `go-test-coverage` and `action-badges/core`. |
| 74 | + |
| 75 | +Example: |
| 76 | + |
| 77 | +```yml |
| 78 | +name: Go test coverage check |
| 79 | +runs-on: ubuntu-latest |
| 80 | +steps: |
| 81 | + - uses: actions/checkout@v3 |
| 82 | + - uses: actions/setup-go@v3 |
| 83 | + |
| 84 | + - name: generate test generate coverage |
| 85 | + run: go test ./... -coverprofile=./cover.out |
| 86 | +
|
| 87 | + - name: check test coverage |
| 88 | + id: coverage |
| 89 | + uses: vladopajic/go-test-coverage@v2 |
| 90 | + with: |
| 91 | + profile: cover.out |
| 92 | + local-prefix: github.com/org/project |
| 93 | + threshold-file: 80 |
| 94 | + threshold-package: 80 |
| 95 | + threshold-total: 95 |
| 96 | + |
| 97 | + - name: make coverage badge |
| 98 | + uses: action-badges/core@0.2.2 |
| 99 | + if: contains(github.ref, 'main') |
| 100 | + with: |
| 101 | + label: coverage |
| 102 | + message: ${{ steps.coverage.outputs.badge-text }} |
| 103 | + message-color: ${{ steps.coverage.outputs.badge-color }} |
| 104 | + file-name: coverage.svg |
| 105 | + badge-branch: badges ## orphan branch where badge will be committed |
| 106 | + github-token: "${{ secrets.GITHUB_TOKEN }}" |
| 107 | +``` |
| 108 | + |
| 109 | +Orphan branch needs to be created prior to running workflow, to create an orphan branch manually: |
| 110 | + |
| 111 | +``` |
| 112 | +git checkout --orphan badges |
| 113 | +git rm -rf . |
| 114 | +rm -f .gitignore |
| 115 | +echo '# Badges' > README.md |
| 116 | +git add README.md |
| 117 | +git commit -m 'init' |
| 118 | +git push origin badges |
| 119 | +``` |
| 120 | + |
| 121 | +Lastly, check output of 'make coverage badge' step to see markdown snippet which can be added to README.md. |
| 122 | + |
| 123 | + |
71 | 124 | ## Contribution
|
72 | 125 |
|
73 | 126 | All contributions are useful, whether it is a simple typo, a more complex change, or just pointing out an issue. We welcome any contribution so feel free to open PR or issue.
|
0 commit comments