diff mbox series

[01/10] hook tests: turn exit code assertions into a loop

Message ID patch-01.10-706460d10b9-20220302T131859Z-avarab@gmail.com (mailing list archive)
State Superseded
Headers show
Series tests: add and use a "test_hook" wrapper + hook fixes | expand

Commit Message

Ævar Arnfjörð Bjarmason March 2, 2022, 1:22 p.m. UTC
Amend a test added in 96e7225b310 (hook: add 'run' subcommand,
2021-12-22) to use a for-loop instead of a copy/pasting the same test
for the four exit codes we test.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/t1800-hook.sh | 35 ++++++++++-------------------------
 1 file changed, 10 insertions(+), 25 deletions(-)

Comments

Junio C Hamano March 2, 2022, 8:55 p.m. UTC | #1
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> Amend a test added in 96e7225b310 (hook: add 'run' subcommand,
> 2021-12-22) to use a for-loop instead of a copy/pasting the same test
> for the four exit codes we test.
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  t/t1800-hook.sh | 35 ++++++++++-------------------------
>  1 file changed, 10 insertions(+), 25 deletions(-)
> ...
> +for code in 1 2 128 129
> +do
> +	test_expect_success "git hook run: exit code $code is passed along" '
> +		write_script .git/hooks/test-hook <<-EOF &&
> +		exit $code
> +		EOF
> +
> +		test_expect_code $code git hook run test-hook
> +	'
> +done

Looks good.  Each being separate test_expect_success means we do not
have to worry about breaking out of the loop upon a failure, which
is much cleaner than I would have thought ;-)

It also illustrates a good use of unquoted here document.
diff mbox series

Patch

diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh
index 29718aa9913..ff64597e959 100755
--- a/t/t1800-hook.sh
+++ b/t/t1800-hook.sh
@@ -53,31 +53,16 @@  test_expect_success 'git hook run: stdout and stderr both write to our stderr' '
 	test_must_be_empty stdout.actual
 '
 
-test_expect_success 'git hook run: exit codes are passed along' '
-	write_script .git/hooks/test-hook <<-EOF &&
-	exit 1
-	EOF
-
-	test_expect_code 1 git hook run test-hook &&
-
-	write_script .git/hooks/test-hook <<-EOF &&
-	exit 2
-	EOF
-
-	test_expect_code 2 git hook run test-hook &&
-
-	write_script .git/hooks/test-hook <<-EOF &&
-	exit 128
-	EOF
-
-	test_expect_code 128 git hook run test-hook &&
-
-	write_script .git/hooks/test-hook <<-EOF &&
-	exit 129
-	EOF
-
-	test_expect_code 129 git hook run test-hook
-'
+for code in 1 2 128 129
+do
+	test_expect_success "git hook run: exit code $code is passed along" '
+		write_script .git/hooks/test-hook <<-EOF &&
+		exit $code
+		EOF
+
+		test_expect_code $code git hook run test-hook
+	'
+done
 
 test_expect_success 'git hook run arg u ments without -- is not allowed' '
 	test_expect_code 129 git hook run test-hook arg u ments