-
-
Save WillAbides/e0bbffccb34d9922cecb2ab9d66defec to your computer and use it in GitHub Desktop.
preview code coverage changes for github.com/google/go-github
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#/ cov-preview [base] | |
#/ | |
#/ Compare coverage of the worktree against the base. Exits non-zero When the | |
#/ coverage of modified code is less than 100% or the net coverage decreases. | |
#/ This is intended to mimic the codecov rules on github.com/google/go-github. | |
#/ Changes that remove covered code may error here but not on codecov. | |
#/ | |
#/ Default base is HEAD (meaning you are comparing uncommitted changes against | |
#/ the last commit). | |
set -e | |
GO_PATH_COVER_TAG="v0.2.0" | |
if [ "$#" -gt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then | |
grep '^#/' <"$0" | cut -c4- | |
exit 1 | |
fi | |
BASE="${1:-HEAD}" | |
COV_DIR="$(mktemp -d)" | |
WORKTREE_DIR="$(mktemp -d)" | |
git worktree add -q --detach "$WORKTREE_DIR" "$BASE" | |
trap 'git worktree remove -f "$WORKTREE_DIR"; rm -rf "$WORKTREE_DIR" "$COV_DIR"' EXIT | |
git diff -U0 --no-color "$BASE" > "$COV_DIR"/patch.diff | |
echo "Running tests on the current worktree". | |
go test -coverprofile="$COV_DIR"/head.out ./... | |
( | |
cd "$WORKTREE_DIR" | |
echo "Running tests on $BASE" | |
go test -coverprofile="$COV_DIR"/base.out ./... | |
) | |
stats="$( | |
go run github.com/seriousben/go-patch-cover/cmd/go-patch-cover@"$GO_PATH_COVER_TAG" \ | |
-o json \ | |
"$COV_DIR"/head.out \ | |
"$COV_DIR"/patch.diff \ | |
"$COV_DIR"/base.out | |
)" | |
jq -r '. | " | |
Coverage: | |
head: | |
covered_lines: \(.cover_count) | |
total_lines: \(.num_stmt) | |
coverage: \(.coverage * 10000|round / 10000)% | |
base: | |
covered_lines: \(.prev_cover_count) | |
total_lines: \(.prev_num_stmt) | |
coverage: \(.prev_coverage * 10000|round / 10000)% | |
patch: | |
covered_lines: \(.patch_cover_count) | |
total_lines: \(.patch_num_stmt) | |
coverage: \(.patch_coverage * 10000|round / 10000)% | |
delta: | |
covered_lines: \(.cover_count - .prev_cover_count) | |
total_lines: \(.num_stmt - .prev_num_stmt) | |
coverage: \((.coverage - .prev_coverage) * 10000|round / 10000)% | |
"' <<<"$stats" | |
delta="$(jq -r ' | |
if .coverage < .prev_coverage then | |
(.prev_coverage - .coverage) * 10000|round / 10000 | |
else "" end | |
' <<<"$stats" | |
)" | |
if [ -n "$delta" ]; then | |
err=1 | |
echo "Coverage decreased by ${delta}%" 1>&2 | |
fi | |
patch_uncovered_lines="$(jq -r '.patch_num_stmt - .patch_cover_count' <<<"$stats")" | |
if [ "$patch_uncovered_lines" -gt 0 ]; then | |
err=1 | |
echo "Patch contains $patch_uncovered_lines uncovered lines" 1>&2 | |
fi | |
[ -z "$err" ] || exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment