@matheusbsbs / matheus-bagliano / commits / 892e6f7ea6
Fork 0

test-lib: make test_expect_code a test command

Change test_expect_code to be a normal test command instead of a top-level command. As a top-level command it would fail in cases like: test_expect_code 1 'phoney' ' foo && bar && (exit 1) ' Here the test might incorrectly succeed if "foo" or "bar" happened to fail with exit status 1. Instead we now do: test_expect_success 'phoney' ' foo && bar && test_expect_code 1 "(exit 1)" ' Which will only succeed if "foo" and "bar" return status 0, and "(exit 1)" returns status 1. Note that test_expect_code has been made slightly noisier, as it reports the exit code it receives even upon success. Some test code in t0000-basic.sh relied on the old semantics of test_expect_code to test the test_when_finished command. I've converted that code to use an external test similar to the TODO test I added in v1.7.3-rc0~2^2~3. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Acked-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Oct 3, 2010 at 13:59 UTC 892e6f7ea61e39d8504e3f227ee3ae874521f05c
5 files changed +82 -38
t/README
+9 -7
@@ -395,13 +395,6 @@ library for your script to use.
395 Like test_expect_success this function can optionally use a three
396 argument invocation with a prerequisite as the first argument.
397
398 - - test_expect_code [<prereq>] <code> <message> <script>
399 -
400 - Analogous to test_expect_success, but pass the test if it exits
401 - with a given exit <code>
402 -
403 - test_expect_code 1 'Merge with d/f conflicts' 'git merge "merge msg" B master'
404 -
398 - test_debug <script>
399
400 This takes a single argument, <script>, and evaluates it only
@@ -482,6 +475,15 @@ library for your script to use.
475 'Perl API' \
476 "$PERL_PATH" "$TEST_DIRECTORY"/t9700/test.pl
477
478 + - test_expect_code <exit-code> <command>
479 +
480 + Run a command and ensure that it exits with the given exit code.
481 + For example:
482 +
483 + test_expect_success 'Merge with d/f conflicts' '
484 + test_expect_code 1 git merge "merge msg" B master
485 + '
486 +
487 - test_must_fail <git-command>
488
489 Run a git command and ensure it fails in a controlled way. Use
t/t0000-basic.sh
+45 -10
@@ -130,22 +130,57 @@ test_expect_success 'tests clean up after themselves' '
130 test_when_finished clean=yes
131 '
132
133 -cleaner=no
134 -test_expect_code 1 'tests clean up even after a failure' '
135 - test_when_finished cleaner=yes &&
136 - (exit 1)
137 -'
138 -
139 -if test $clean$cleaner != yesyes
133 +if test $clean != yes
134 then
141 - say "bug in test framework: cleanup commands do not work reliably"
135 + say "bug in test framework: basic cleanup command does not work reliably"
136 exit 1
137 fi
138
145 -test_expect_code 2 'failure to clean up causes the test to fail' '
146 - test_when_finished "(exit 2)"
139 +test_expect_success 'tests clean up even on failures' "
140 + mkdir failing-cleanup &&
141 + (cd failing-cleanup &&
142 + cat >failing-cleanup.sh <<EOF &&
143 +#!$SHELL_PATH
144 +
145 +test_description='Failing tests with cleanup commands'
146 +
147 +# Point to the t/test-lib.sh, which isn't in ../ as usual
148 +TEST_DIRECTORY=\"$TEST_DIRECTORY\"
149 +. \"\$TEST_DIRECTORY\"/test-lib.sh
150 +
151 +test_expect_success 'tests clean up even after a failure' '
152 + touch clean-after-failure &&
153 + test_when_finished rm clean-after-failure &&
154 + (exit 1)
155 +'
156 +
157 +test_expect_success 'failure to clean up causes the test to fail' '
158 + test_when_finished \"(exit 2)\"
159 '
160
161 +test_done
162 +EOF
163 + chmod +x failing-cleanup.sh &&
164 + test_must_fail ./failing-cleanup.sh >out 2>err &&
165 + ! test -s err &&
166 + ! test -f \"trash directory.failing-cleanup/clean-after-failure\" &&
167 +sed -e 's/Z$//' >expect <<\EOF &&
168 +not ok - 1 tests clean up even after a failure
169 +# Z
170 +# touch clean-after-failure &&
171 +# test_when_finished rm clean-after-failure &&
172 +# (exit 1)
173 +# Z
174 +not ok - 2 failure to clean up causes the test to fail
175 +# Z
176 +# test_when_finished \"(exit 2)\"
177 +# Z
178 +# failed 2 among 2 test(s)
179 +1..2
180 +EOF
181 + test_cmp expect out)
182 +"
183 +
184 ################################################################
185 # Basics of the basics
186
t/t1504-ceiling-dirs.sh
+3 -2
@@ -9,8 +9,9 @@ test_prefix() {
9 }
10
11 test_fail() {
12 - test_expect_code 128 "$1: prefix" \
13 - "git rev-parse --show-prefix"
12 + test_expect_success "$1: prefix" '
13 + test_expect_code 128 git rev-parse --show-prefix
14 + '
15 }
16
17 TRASH_ROOT="$PWD"
t/t6020-merge-df.sh
+3 -1
@@ -20,7 +20,9 @@ echo "file dir" > dir &&
20 git add dir &&
21 git commit -m "File: dir"'
22
23 -test_expect_code 1 'Merge with d/f conflicts' 'git merge "merge msg" B master'
23 +test_expect_success 'Merge with d/f conflicts' '
24 + test_expect_code 1 git merge "merge msg" B master
25 +'
26
27 test_expect_success 'F/D conflict' '
28 git reset --hard &&
t/test-lib.sh
+22 -18
@@ -473,24 +473,6 @@ test_expect_success () {
473 echo >&3 ""
474 }
475
476 -test_expect_code () {
477 - test "$#" = 4 && { prereq=$1; shift; } || prereq=
478 - test "$#" = 3 ||
479 - error "bug in the test script: not 3 or 4 parameters to test-expect-code"
480 - if ! test_skip "$@"
481 - then
482 - say >&3 "expecting exit code $1: $3"
483 - test_run_ "$3"
484 - if [ "$?" = 0 -a "$eval_ret" = "$1" ]
485 - then
486 - test_ok_ "$2"
487 - else
488 - test_failure_ "$@"
489 - fi
490 - fi
491 - echo >&3 ""
492 -}
493 -
476 # test_external runs external test scripts that provide continuous
477 # test output about their progress, and succeeds/fails on
478 # zero/non-zero exit code. It outputs the test output on stdout even
@@ -658,6 +640,28 @@ test_might_fail () {
640 return 0
641 }
642
643 +# Similar to test_must_fail and test_might_fail, but check that a
644 +# given command exited with a given exit code. Meant to be used as:
645 +#
646 +# test_expect_success 'Merge with d/f conflicts' '
647 +# test_expect_code 1 git merge "merge msg" B master
648 +# '
649 +
650 +test_expect_code () {
651 + want_code=$1
652 + shift
653 + "$@"
654 + exit_code=$?
655 + if test $exit_code = $want_code
656 + then
657 + echo >&2 "test_expect_code: command exited with $exit_code: $*"
658 + return 0
659 + else
660 + echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
661 + return 1
662 + fi
663 +}
664 +
665 # test_cmp is a helper function to compare actual and expected output.
666 # You can use it like:
667 #