Skip to content

Commit 1f3296e

Browse files
authored
chore: add coverage reporting (#40)
1 parent 0f6a705 commit 1f3296e

11 files changed

+3152
-2
lines changed

.github/workflows/go.yml

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ jobs:
3333
- name: Tests
3434
run: make test-sqlite
3535

36+
- name: Coverage
37+
run: |
38+
make coverage-diff
39+
3640
postgres:
3741
strategy:
3842
matrix:

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ _test
1010
*.iws
1111

1212
### Logs ###
13-
*.log
1413
logs/
1514

1615
### direnv ###

Makefile

+20-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,23 @@ protobuild:
5050
protolint:
5151
@buf lint
5252
# if/when this becomes a public repo, we can add this check
53-
# @buf check breaking --against 'https://github.com/hashicorp/go-dbw.git#branch=main'
53+
# @buf check breaking --against
54+
# 'https://github.com/hashicorp/go-dbw.git#branch=main'
55+
56+
# coverage-diff will run a new coverage report and check coverage.log to see if
57+
# the coverage has changed.
58+
.PHONY: coverage-diff
59+
coverage-diff:
60+
cd coverage && \
61+
./coverage.sh && \
62+
./cov-diff.sh coverage.log && \
63+
if ./cov-diff.sh ./coverage.log; then git restore coverage.log; fi
64+
65+
# coverage will generate a report, badge and log. when you make changes, run
66+
# this and check in the changes to publish a new/latest coverage report and
67+
# badge.
68+
.PHONY: coverage
69+
coverage:
70+
cd coverage && \
71+
./coverage.sh && \
72+
if ./cov-diff.sh ./coverage.log; then git restore coverage.log; fi

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# dbw package
22
[![Go Reference](https://pkg.go.dev/badge/github.com/hashicorp/go-dbw.svg)](https://pkg.go.dev/github.com/hashicorp/go-dbw)
3+
[![Go Report Card](https://goreportcard.com/badge/github.com/hashicorp/go-dbw)](https://goreportcard.com/report/github.com/hashicorp/go-dbw)
4+
[![Go Coverage](https://raw.githack.com/hashicorp/go-dbw/main/coverage/coverage.svg)](https://raw.githack.com/hashicorp/go-dbw/main/coverage/coverage.html)
35

46
[dbw](https://pkg.go.dev/github.com/hashicorp/go-dbw) is a database wrapper that
57
supports connecting and using any database with a

clause.go

+2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ func (ev *ExprValue) toAssignment(column string) clause.Assignment {
5959
// column values for database operations. See: Expr(...)
6060
//
6161
// Set name column to null example:
62+
//
6263
// SetColumnValues(map[string]interface{}{"name": Expr("NULL")})
6364
//
6465
// Set exp_time column to N seconds from now:
66+
//
6567
// SetColumnValues(map[string]interface{}{"exp_time": Expr("wt_add_seconds_to_now(?)", 10)})
6668
func Expr(expr string, args ...interface{}) ExprValue {
6769
return ExprValue{Sql: expr, Vars: args}

coverage/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Coverage
2+
3+
This `coverage` directory contains the bits required to generate the code
4+
coverage report and badge which are published for this repo.
5+
6+
After making changes to source, please run `make coverage` in the root directory
7+
of this repo and check-in any changes.
8+
9+
- **cov-diff.sh** - generates a new coverage report and checks the previous
10+
entry in `coverage.log` for differences. It's used by the github action to
11+
ensure that the published coverage report and badge are up to date.
12+
- **coverage.sh** - generates `coverage.log`, `coverage.svg`, and
13+
`coverage.html`.
14+
- **coverage.log** - A log of coverage report runs.
15+
- **coverage.html** - The published coverage report.
16+
- **coverage.svg** - The published coverage badge.

coverage/cov-diff.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
# Check if the file exists
4+
if [ ! -f "$1" ]; then
5+
echo "File not found!"
6+
exit 1
7+
fi
8+
9+
# Read the last two lines of the file and extract the last columns
10+
last_col_last_line=$(tail -n 1 "$1" | awk -F',' '{print $NF}')
11+
last_col_second_last_line=$(tail -n 2 "$1" | head -n 1 | awk -F',' '{print $NF}')
12+
13+
# Compare the last columns
14+
if [ "$last_col_last_line" = "$last_col_second_last_line" ]; then
15+
exit 0
16+
else
17+
echo "coverage has changed."
18+
echo "generate a new report and badge using: make coverage"
19+
echo "and then check-in the new report and badge?"
20+
echo "coverage before: $last_col_second_last_line"
21+
echo "coverage now: $last_col_last_line"
22+
exit 1
23+
fi

0 commit comments

Comments
 (0)