Skip to content

Commit bbfe530

Browse files
larsxschneiderpeff
authored andcommitted
implement test_might_fail using a refactored test_must_fail
Add an (optional) first parameter "ok=<special case>" to test_must_fail and return success for "<special case>". Add "success" as "<special case>" and use it to implement "test_might_fail". This removes redundancies in test-lib-function.sh. You can pass multiple <special case> arguments divided by comma (e.g. "test_must_fail ok=success,something") Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Lars Schneider <[email protected]> Signed-off-by: Ramsay Jones <[email protected]> Signed-off-by: Jeff King <[email protected]>
1 parent 0c83680 commit bbfe530

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

t/test-lib-functions.sh

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,21 @@ test_line_count () {
569569
fi
570570
}
571571

572+
# Returns success if a comma separated string of keywords ($1) contains a
573+
# given keyword ($2).
574+
# Examples:
575+
# `list_contains "foo,bar" bar` returns 0
576+
# `list_contains "foo" bar` returns 1
577+
578+
list_contains () {
579+
case ",$1," in
580+
*,$2,*)
581+
return 0
582+
;;
583+
esac
584+
return 1
585+
}
586+
572587
# This is not among top-level (test_expect_success | test_expect_failure)
573588
# but is a prefix that can be used in the test script, like:
574589
#
@@ -582,18 +597,31 @@ test_line_count () {
582597
# the failure could be due to a segv. We want a controlled failure.
583598

584599
test_must_fail () {
600+
case "$1" in
601+
ok=*)
602+
_test_ok=${1#ok=}
603+
shift
604+
;;
605+
*)
606+
_test_ok=
607+
;;
608+
esac
585609
"$@"
586610
exit_code=$?
587-
if test $exit_code = 0; then
611+
if test $exit_code -eq 0 && ! list_contains "$_test_ok" success
612+
then
588613
echo >&2 "test_must_fail: command succeeded: $*"
589614
return 1
590-
elif test $exit_code -gt 129 && test $exit_code -le 192; then
615+
elif test $exit_code -gt 129 && test $exit_code -le 192
616+
then
591617
echo >&2 "test_must_fail: died by signal: $*"
592618
return 1
593-
elif test $exit_code = 127; then
619+
elif test $exit_code -eq 127
620+
then
594621
echo >&2 "test_must_fail: command not found: $*"
595622
return 1
596-
elif test $exit_code = 126; then
623+
elif test $exit_code -eq 126
624+
then
597625
echo >&2 "test_must_fail: valgrind error: $*"
598626
return 1
599627
fi
@@ -612,16 +640,7 @@ test_must_fail () {
612640
# because we want to notice if it fails due to segv.
613641

614642
test_might_fail () {
615-
"$@"
616-
exit_code=$?
617-
if test $exit_code -gt 129 && test $exit_code -le 192; then
618-
echo >&2 "test_might_fail: died by signal: $*"
619-
return 1
620-
elif test $exit_code = 127; then
621-
echo >&2 "test_might_fail: command not found: $*"
622-
return 1
623-
fi
624-
return 0
643+
test_must_fail ok=success "$@"
625644
}
626645

627646
# Similar to test_must_fail and test_might_fail, but check that a

0 commit comments

Comments
 (0)