Skip to content

test coverage

test coverage #6

Workflow file for this run

name: Go CI
on:
push:
branches:
- main
pull_request:
branches:
- main
permissions:
contents: write
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Cache Go modules
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install dependencies
run: go mod download
# Run tests with coverage and generate the coverage.out
- name: Run tests with coverage
run: go test ./... -coverprofile=coverage.out
# Calculate coverage percentage and store it in a JSON file
- name: Generate coverage badge
run: |
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//')
cat <<EOF > badge.json
{
"schemaVersion": 1,
"label": "coverage",
"message": "$COVERAGE%",
"color": "brightgreen"
}
EOF
# Configure Git for committing the badge.json
- name: Configure Git
run: |
git config --global user.name 'GitHub Action'
git config --global user.email '[email protected]'
# Commit badge.json to the repository
- name: Commit badge
run: |
git add badge.json
git commit -m "Update coverage badge"
# Push the changes to the current branch
- name: Push changes
run: |
git push origin HEAD:${{ github.ref }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}