Skip to content

Commit e390e6a

Browse files
pks-tgitster
authored andcommitted
t: detect errors outside of test cases
We have recently merged a patch series that had a simple misspelling of `test_expect_success`. Instead of making our tests fail though, this typo went completely undetected and all of our tests passed, which is of course unfortunate. This is a more general issue with our test suite: all commands that run outside of a specific test case can fail, and if we don't explicitly check for such failure then this failure will be silently ignored. Improve the status quo by enabling the errexit option so that any such unchecked failures will cause us to abort immediately. Note that for now, we only enable this option for Bash 5 and newer. This is because other shells have wildly different behaviour, and older versions of Bash (especially on macOS) are buggy. The list of enabled shells may be extended going forward. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 8425063 commit e390e6a

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

ci/run-build-and-tests.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
export TEST_CONTRIB_TOO=yes
99

10+
case "$jobname" in
11+
almalinux-*|debian-*|fedora-*|linux-*)
12+
export GIT_TEST_USE_SET_E=yes
13+
;;
14+
esac
15+
1016
case "$jobname" in
1117
fedora-breaking-changes-musl|linux-breaking-changes)
1218
export WITH_BREAKING_CHANGES=YesPlease

t/test-lib.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@
1515
# You should have received a copy of the GNU General Public License
1616
# along with this program. If not, see https://www.gnu.org/licenses/ .
1717

18+
# Enable the use of errexit so that any unexpected failures will cause us to
19+
# abort tests, even when outside of a specific test case.
20+
#
21+
# Note that we only enable this on Bash 5 and newer, or when explicitly
22+
# requested by the user via `GIT_TEST_USE_SET_E=true`. This ib secause `set -e`
23+
# has wildly different behaviour across shells. The list of default-enabled
24+
# shells may be extended going forward.
25+
if test -z "$GIT_TEST_USE_SET_E" && test "${BASH_VERSINFO:=0}" -ge 5
26+
then
27+
GIT_TEST_USE_SET_E=true
28+
fi
29+
30+
# We cannot use `test-tool env-helper` here, as it's not yet available.
31+
case "${GIT_TEST_USE_SET_E:-false}" in
32+
1|on|true|yes)
33+
set -e
34+
;;
35+
0|off|false|no)
36+
;;
37+
*)
38+
echo "GIT_TEST_USE_SET_E requires a boolean" >&2
39+
exit 1
40+
;;
41+
esac
42+
1843
# Test the binaries we have just built. The tests are kept in
1944
# t/ subdirectory and are run in 'trash directory' subdirectory.
2045
if test -z "$TEST_DIRECTORY"

0 commit comments

Comments
 (0)