diff mbox series

[v2] t/README: suggest how to leave test early with failure

Message ID xmqqpncwca6k.fsf_-_@gitster.c.googlers.com (mailing list archive)
State New, archived
Headers show
Series [v2] t/README: suggest how to leave test early with failure | expand

Commit Message

Junio C Hamano March 28, 2020, 11:49 p.m. UTC
Over time, we added the support to our test framework to make it
easy to leave a test early with failure, but it was not clearly
documented in t/README to help developers writing new tests.

Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---

 * Trimmed the description quite a bit and dropped alternatives.

 t/README | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

Comments

Eric Sunshine March 29, 2020, 7:23 a.m. UTC | #1
On Sat, Mar 28, 2020 at 7:49 PM Junio C Hamano <gitster@pobox.com> wrote:
> Over time, we added the support to our test framework to make it
> easy to leave a test early with failure, but it was not clearly
> documented in t/README to help developers writing new tests.
>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
> diff --git a/t/README b/t/README
> @@ -550,6 +550,40 @@ Here are the "do's:"
> +       test_expect_success 'test three things' '
> +           for i in one two three
> +           do
> +               test_something "$i"
> +           done &&
> +           test_something_else
> +       '
> +
> +   Because the status of the loop itself is the exit status of the
> +   test_something in the last round, the loop does not fail when
> +   "test_something" for "one" or "two".  This is not what you want.

s/"two"/& fails/
Jeff King March 29, 2020, 2:33 p.m. UTC | #2
On Sat, Mar 28, 2020 at 04:49:07PM -0700, Junio C Hamano wrote:

> Over time, we added the support to our test framework to make it
> easy to leave a test early with failure, but it was not clearly
> documented in t/README to help developers writing new tests.
> 
> Helped-by: Jeff King <peff@peff.net>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>

With the exception of the typo-fix from Eric, this looks good to
me. Thanks for tying this up.

-Peff
diff mbox series

Patch

diff --git a/t/README b/t/README
index 9afd61e3ca..0dca346950 100644
--- a/t/README
+++ b/t/README
@@ -550,6 +550,40 @@  Here are the "do's:"
    reports "ok" or "not ok" to the end user running the tests. Under
    --verbose, they are shown to help debug the tests.
 
+ - Be careful when you loop
+
+   You may need to verify multiple things in a loop, but the
+   following does not work correctly:
+
+	test_expect_success 'test three things' '
+	    for i in one two three
+	    do
+		test_something "$i"
+	    done &&
+	    test_something_else
+	'
+
+   Because the status of the loop itself is the exit status of the
+   test_something in the last round, the loop does not fail when
+   "test_something" for "one" or "two".  This is not what you want.
+
+   Instead, you can break out of the loop immediately when you see a
+   failure.  Because all test_expect_* snippets are executed inside
+   a function, "return 1" can be used to fail the test immediately
+   upon a failure:
+
+	test_expect_success 'test three things' '
+	    for i in one two three
+	    do
+		test_something "$i" || return 1
+	    done &&
+	    test_something_else
+	'
+
+   Note that we still &&-chain the loop to propagate failures from
+   earlier commands.
+
+
 And here are the "don'ts:"
 
  - Don't exit() within a <script> part.