1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#!/bin/bash
#
# Intended for use in CI: check git commits, barf if no tests added.
#
# Docs-only changes are excused
if [[ "${CIRRUS_CHANGE_TITLE}" =~ CI:DOCS ]]; then
exit 0
fi
# So are PRs where 'NO NEW TESTS NEEDED' appears in the Github message
if [[ "${CIRRUS_CHANGE_MESSAGE}" =~ NO.NEW.TESTS.NEEDED ]]; then
exit 0
fi
if [[ "${CIRRUS_CHANGE_MESSAGE}" =~ NO.TESTS.NEEDED ]]; then
exit 0
fi
# HEAD should be good enough, but the CIRRUS envariable allows us to test
head=${CIRRUS_CHANGE_IN_REPO:-HEAD}
# Base of this PR. Here we absolutely rely on cirrus.
base=$(git merge-base ${GITVALIDATE_EPOCH:-main} $head)
# Sanity check:
if [[ -z "$base" ]]; then
echo "$(basename $0): internal error: could not determine merge-base"
echo " head = $head"
echo " CIRRUS_CHANGE_IN_REPO = $CIRRUS_CHANGE_IN_REPO"
echo " GITVALIDATE_EPOCH = $GITVALIDATE_EPOCH"
exit 1
fi
# This gives us a list of files touched in all commits, e.g.
# A foo.c
# M bar.c
# We look for Added or Modified (not Deleted!) files under 'tests'.
# --no-renames ensures that renamed tests show up as 'A'dded.
if git diff --name-status --no-renames $base $head | egrep -q '^[AM]\s+(tests/|.*_test\.go)'; then
exit 0
fi
# Nothing changed under test subdirectory.
#
# This is OK if the only files being touched are "safe" ones.
filtered_changes=$(git diff --name-status $base $head |
awk '{print $2}' |
fgrep -vx .cirrus.yml |
fgrep -vx .gitignore |
fgrep -vx changelog.txt |
fgrep -vx go.mod |
fgrep -vx go.sum |
egrep -v '^[^/]+\.md$' |
egrep -v '^\.github/' |
egrep -v '^contrib/' |
egrep -v '^docs/' |
egrep -v '^hack/' |
egrep -v '^nix/' |
egrep -v '^vendor/')
if [[ -z "$filtered_changes" ]]; then
exit 0
fi
# One last chance: perhaps the developer included the magic '[NO TESTS NEEDED]'
# string in an amended commit.
if git log --format=%B ${base}..${head} | fgrep '[NO NEW TESTS NEEDED]'; then
exit 0
fi
if git log --format=%B ${base}..${head} | fgrep '[NO TESTS NEEDED]'; then
exit 0
fi
cat <<EOF
$(basename $0): PR does not include changes in the 'tests' directory
Please write a regression test for what you're fixing. Even if it
seems trivial or obvious, try to add a test that will prevent
regressions.
If your change is minor, feel free to piggyback on already-written
tests, possibly just adding a small step to a similar existing test.
Every second counts in CI.
If your commit really, truly does not need tests, you can proceed
by adding '[NO NEW TESTS NEEDED]' to the body of your commit message.
Please think carefully before doing so.
EOF
exit 1
|