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 <gitster@pobox.com> Signed-off-by: Lars Schneider <larsxschneider@gmail.com> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com> Signed-off-by: Jeff King <peff@peff.net>
Lars Schneider committed
Nov 27, 2015 at 10:15 UTC
bbfe5302d50891233cef645b74324c01d8b004c8
1 file changed
+33
-14
t/test-lib-functions.sh
+33
-14
@@ -569,6 +569,21 @@ test_line_count () {
569
fi
570
}
571
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
+
587
# This is not among top-level (test_expect_success | test_expect_failure)
588
# but is a prefix that can be used in the test script, like:
589
#
@@ -582,18 +597,31 @@ test_line_count () {
597
# the failure could be due to a segv. We want a controlled failure.
598
599
test_must_fail () {
600
+ case "$1" in
601
+ ok=*)
602
+ _test_ok=${1#ok=}
603
+ shift
604
+ ;;
605
+ *)
606
+ _test_ok=
607
+ ;;
608
+ esac
609
"$@"
610
exit_code=$?
587
- if test $exit_code = 0; then
611
+ if test $exit_code -eq 0 && ! list_contains "$_test_ok" success
612
+ then
613
echo >&2 "test_must_fail: command succeeded: $*"
614
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
617
echo >&2 "test_must_fail: died by signal: $*"
618
return 1
593
- elif test $exit_code = 127; then
619
+ elif test $exit_code -eq 127
620
+ then
621
echo >&2 "test_must_fail: command not found: $*"
622
return 1
596
- elif test $exit_code = 126; then
623
+ elif test $exit_code -eq 126
624
+ then
625
echo >&2 "test_must_fail: valgrind error: $*"
626
return 1
627
fi
@@ -612,16 +640,7 @@ test_must_fail () {
640
# because we want to notice if it fails due to segv.
641
642
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 "$@"
644
}
645
646
# Similar to test_must_fail and test_might_fail, but check that a