Skip to content

Commit

Permalink
chore: add coverage reporting (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimlambrt authored Aug 28, 2023
1 parent 0f6a705 commit 1f3296e
Show file tree
Hide file tree
Showing 11 changed files with 3,152 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jobs:
- name: Tests
run: make test-sqlite

- name: Coverage
run: |
make coverage-diff
postgres:
strategy:
matrix:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ _test
*.iws

### Logs ###
*.log
logs/

### direnv ###
Expand Down
21 changes: 20 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,23 @@ protobuild:
protolint:
@buf lint
# if/when this becomes a public repo, we can add this check
# @buf check breaking --against 'https://github.com/hashicorp/go-dbw.git#branch=main'
# @buf check breaking --against
# 'https://github.com/hashicorp/go-dbw.git#branch=main'

# 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: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# dbw package
[![Go Reference](https://pkg.go.dev/badge/github.com/hashicorp/go-dbw.svg)](https://pkg.go.dev/github.com/hashicorp/go-dbw)
[![Go Report Card](https://goreportcard.com/badge/github.com/hashicorp/go-dbw)](https://goreportcard.com/report/github.com/hashicorp/go-dbw)
[![Go Coverage](https://raw.githack.com/hashicorp/go-dbw/main/coverage/coverage.svg)](https://raw.githack.com/hashicorp/go-dbw/main/coverage/coverage.html)

[dbw](https://pkg.go.dev/github.com/hashicorp/go-dbw) is a database wrapper that
supports connecting and using any database with a
Expand Down
2 changes: 2 additions & 0 deletions clause.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ func (ev *ExprValue) toAssignment(column string) clause.Assignment {
// column values for database operations. See: Expr(...)
//
// Set name column to null example:
//
// SetColumnValues(map[string]interface{}{"name": Expr("NULL")})
//
// Set exp_time column to N seconds from now:
//
// SetColumnValues(map[string]interface{}{"exp_time": Expr("wt_add_seconds_to_now(?)", 10)})
func Expr(expr string, args ...interface{}) ExprValue {
return ExprValue{Sql: expr, Vars: args}
Expand Down
16 changes: 16 additions & 0 deletions coverage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Coverage

This `coverage` directory contains the bits required to generate the code
coverage report and badge which are published for this repo.

After making changes to source, please run `make coverage` in the root directory
of this repo and check-in any changes.

- **cov-diff.sh** - generates a new coverage report and checks the previous
entry in `coverage.log` for differences. It's used by the github action to
ensure that the published coverage report and badge are up to date.
- **coverage.sh** - generates `coverage.log`, `coverage.svg`, and
`coverage.html`.
- **coverage.log** - A log of coverage report runs.
- **coverage.html** - The published coverage report.
- **coverage.svg** - The published coverage badge.
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 1f3296e

Please sign in to comment.