diff mbox series

[v3,1/3] t4020: test exit code with external diffs

Message ID 79a951f8-cf35-4159-a90e-f95d69773413@web.de (mailing list archive)
State New, archived
Headers show
Series diff: fix --exit-code with external diff | expand

Commit Message

René Scharfe June 9, 2024, 7:38 a.m. UTC
Add tests to check the exit code of git diff with its options --quiet
and --exit-code when using an external diff program.  Currently we
cannot tell whether it found significant changes or not.

While at it, document briefly that --quiet turns off execution of
external diff programs because that behavior surprised me for a moment
while writing the tests.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 Documentation/diff-options.txt |  1 +
 t/t4020-diff-external.sh       | 53 ++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

--
2.45.2

Comments

Junio C Hamano June 10, 2024, 4:33 p.m. UTC | #1
René Scharfe <l.s.r@web.de> writes:

> +check_external_diff () {
> +	expect_code=$1
> +	expect_out=$2
> +	expect_err=$3
> +	command_code=$4
> +	shift 4
> +	options="$@"

Tiny nit, but I'd prefer to see "$@" reserved for its magic and all
other times where it is equivalent to "$*", see the latter used.

Other than that, all three patches looked good to me.  Thanks.
diff mbox series

Patch

diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt
index c7df20e571..6b73daf540 100644
--- a/Documentation/diff-options.txt
+++ b/Documentation/diff-options.txt
@@ -820,6 +820,7 @@  ifndef::git-log[]

 --quiet::
 	Disable all output of the program. Implies `--exit-code`.
+	Disables execution of external diff helpers.
 endif::git-log[]
 endif::git-format-patch[]

diff --git a/t/t4020-diff-external.sh b/t/t4020-diff-external.sh
index fdd865f7c3..4070523cdb 100755
--- a/t/t4020-diff-external.sh
+++ b/t/t4020-diff-external.sh
@@ -172,6 +172,59 @@  test_expect_success 'no diff with -diff' '
 	grep Binary out
 '

+check_external_diff () {
+	expect_code=$1
+	expect_out=$2
+	expect_err=$3
+	command_code=$4
+	shift 4
+	options="$@"
+
+	command="echo output; exit $command_code;"
+	desc="external diff '$command'"
+	with_options="${options:+ with }$options"
+
+	test_expect_success "$desc via attribute$with_options" "
+		test_config diff.foo.command \"$command\" &&
+		echo \"file diff=foo\" >.gitattributes &&
+		test_expect_code $expect_code git diff $options >out 2>err &&
+		test_cmp $expect_out out &&
+		test_cmp $expect_err err
+	"
+
+	test_expect_success "$desc via diff.external$with_options" "
+		test_config diff.external \"$command\" &&
+		>.gitattributes &&
+		test_expect_code $expect_code git diff $options >out 2>err &&
+		test_cmp $expect_out out &&
+		test_cmp $expect_err err
+	"
+
+	test_expect_success "$desc via GIT_EXTERNAL_DIFF$with_options" "
+		>.gitattributes &&
+		test_expect_code $expect_code env \
+			GIT_EXTERNAL_DIFF=\"$command\" \
+			git diff $options >out 2>err &&
+		test_cmp $expect_out out &&
+		test_cmp $expect_err err
+	"
+}
+
+test_expect_success 'setup output files' '
+	: >empty &&
+	echo output >output &&
+	echo "fatal: external diff died, stopping at file" >error
+'
+
+check_external_diff   0 output empty 0
+check_external_diff 128 output error 1
+
+check_external_diff   1 output empty 0 --exit-code
+check_external_diff 128 output error 1 --exit-code
+
+check_external_diff   1 empty  empty 0 --quiet
+check_external_diff   1 empty  empty 1 --quiet # we don't even call the program
+
 echo NULZbetweenZwords | perl -pe 'y/Z/\000/' > file

 test_expect_success 'force diff with "diff"' '