Skip to content

Commit

Permalink
chore: refactor how coverage data is created.
Browse files Browse the repository at this point in the history
Remove github actions which commit a badge to the repo and
move all coverage bits internal to the repo
  • Loading branch information
jimlambrt committed Sep 29, 2023
1 parent f6e8076 commit d1c98fd
Show file tree
Hide file tree
Showing 8 changed files with 5,894 additions and 31 deletions.
48 changes: 19 additions & 29 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,35 @@ on:
jobs:
build:
strategy:
fail-fast: true
matrix:
go: ["1.19", "1.20", "1.18"]
go: ["1.21", "1.20", "1.19"]
platform: [ubuntu-latest] # can not run in windows OS
runs-on: ${{ matrix.platform }}

steps:
- uses: actions/checkout@v2
- name: Set up Go 1.x
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}

- name: Set up Go
uses: actions/setup-go@v2
- name: Check out code into the Go module directory
uses: actions/checkout@v4

- name: go mod package cache
uses: actions/cache@v3
with:
go-version: 1.18
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('tests/go.mod') }}

- name: Build
run: go build -v ./...

- name: Test
run: |
go test -v -cover ./... -coverprofile coverage.out -coverpkg ./...
go tool cover -func coverage.out -o coverage.out # Replaces coverage.out with the analysis of coverage.out
- name: Go Coverage Badge
if: matrix.platform == '1.20'
uses: tj-actions/coverage-badge-go@v1
with:
green: 80
filename: coverage.out
go test ./...
- uses: stefanzweifel/git-auto-commit-action@v4
if: matrix.platform == '1.20'
id: auto-commit-action
with:
commit_message: Apply Code Coverage Badge
skip_fetch: true
skip_checkout: true
file_pattern: ./README.md

- name: Push Changes
if: steps.auto-commit-action.outputs.changes_detected == 'true' && matrix.platform == '1.20'
uses: ad-m/github-push-action@master
with:
github_token: ${{ github.token }}
branch: ${{ github.ref }}
- name: Coverage
if: ${{ matrix.go != '1.19' }}
run: |
make coverage-diff
20 changes: 19 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,22 @@ tools:

.PHONY: fmt
fmt:
gofumpt -w $$(find . -name '*.go')
gofumpt -w $$(find . -name '*.go')

# coverage-diff will run a new coverage report and check coverage.log to see if
# the coverage has changed.
.PHONY: coverage-diff
coverage-diff:
cd coverage && \
./coverage.sh && \
./cov-diff.sh coverage.log && \
if ./cov-diff.sh ./coverage.log; then git restore coverage.log; fi

# coverage will generate a report, badge and log. when you make changes, run
# this and check in the changes to publish a new/latest coverage report and
# badge.
.PHONY: coverage
coverage:
cd coverage && \
./coverage.sh && \
if ./cov-diff.sh ./coverage.log; then git restore coverage.log; fi
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# gldap
[![Go Reference](https://pkg.go.dev/badge/github.com/jimlambrt/gldap/gldap.svg)](https://pkg.go.dev/github.com/jimlambrt/gldap)
![Coverage](https://img.shields.io/badge/Coverage-88.6%25-brightgreen)
[![Go Coverage](https://raw.githack.com/jimlambrt/gldap/main/coverage/coverage.svg)](https://raw.githack.com/jimlambrt/gldap/main/coverage/coverage.html)
[![Go Report Card](https://goreportcard.com/badge/github.com/jimlambrt/gldap)](https://goreportcard.com/report/github.com/jimlambrt/gldap)

<hr>
Expand Down
23 changes: 23 additions & 0 deletions coverage/cov-diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

# Check if the file exists
if [ ! -f "$1" ]; then
echo "File not found!"
exit 1
fi

# Read the last two lines of the file and extract the last columns
last_col_last_line=$(tail -n 1 "$1" | awk -F',' '{print $NF}')
last_col_second_last_line=$(tail -n 2 "$1" | head -n 1 | awk -F',' '{print $NF}')

# Compare the last columns
if [ "$last_col_last_line" = "$last_col_second_last_line" ]; then
exit 0
else
echo "coverage has changed."
echo "generate a new report and badge using: make coverage"
echo "and then check-in the new report and badge?"
echo "coverage before: $last_col_second_last_line"
echo "coverage now: $last_col_last_line"
exit 1
fi
Loading

0 comments on commit d1c98fd

Please sign in to comment.