Files
podman/contrib/cirrus/pr-should-include-tests
Ed Santiago f877d7dcd0 Replace egrep/fgrep with grep -E/-F
There are days when I really, really, really hate GNU. Remember
when someone decided that 'head -1' would no longer work, and
that it was OK to break an infinite number of legacy production
scripts? Someone now decided that egrep/fgrep are deprecated,
and our CI logs (especially pr-should-include-tests) are now
filled with hundreds of warning lines, making it difficult
to find actual errors.

I expect that those warnings will be removed quickly after
furious community backlash, just like the 'head -1' fiasco
was quietly reverted, but ITM the warnings are annoying
so I capitulate.

Signed-off-by: Ed Santiago <santiago@redhat.com>
2023-05-03 07:32:42 -06:00

77 lines
2.8 KiB
Bash
Executable File

#!/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
# 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 ${DEST_BRANCH:-main} $head)
# 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 'test'.
# --no-renames ensures that renamed tests (#9420) show up as 'A'dded.
if git diff --name-status --no-renames $base $head | grep -E -q '^[AM]\s+(test/|.*_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-only $base $head |
grep -F -vx .cirrus.yml |
grep -F -vx .pre-commit-config.yaml |
grep -F -vx .gitignore |
grep -F -vx go.mod |
grep -F -vx go.sum |
grep -F -vx podman.spec.rpkg |
grep -F -vx .golangci.yml |
grep -E -v '/*Makefile$' |
grep -E -v '^[^/]+\.md$' |
grep -E -v '^.github' |
grep -E -v '^contrib/' |
grep -E -v '^docs/' |
grep -E -v '^hack/' |
grep -E -v '^nix/' |
grep -E -v '^vendor/' |
grep -E -v '^version/')
if [[ -z "$filtered_changes" ]]; then
exit 0
fi
# One last chance: perhaps the developer included the magic '[NO NEW TESTS NEEDED]'
# string in an amended commit.
if git log --format=%B ${base}..${head} | grep -F '[NO NEW 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