Subject: Re: [PATCH v9 2/8] t2400: print captured git output when finished

From: Junio C Hamano <[email-masked]>
Date: Mon, 17 Apr 2023 14:09:52 -0700

Jacob Abel <[email-masked]> writes:

>  test_expect_success 'add --quiet' '
> +	test_when_finished "git worktree remove -f -f another-worktree" &&
> +	test_when_finished cat actual >&2 &&

I doubt that this redirection does anything you expect it do.
Doesn't it redirect the standard output that is emitted by the
test_when_finished shell function when it registers another
test_cleanup scriptlet to the standard error, and when test_cleanup
is indeed run, wouldn't "cat actual" send its output to the standard
output?

No, I am not suggesting to write the line as:

	test_when_finished "cat >&2 actual" &&

>  	git worktree add --quiet another-worktree main 2>actual &&
>  	test_must_be_empty actual

The reason why I do not suggest "fixing" the above is because
test_must_be_empty, when fails, does this:

        test_must_be_empty () {
                test "$#" -ne 1 && BUG "1 param"
                test_path_is_file "$1" &&
                if test -s "$1"
                then
                        echo "'$1' is not empty, it contains:"
                        cat "$1"
                        return 1
                fi
        }

i.e. it sends the contents of "actual" to the standard output
already.  When it succeeds, of course "actual" is empty, and there
is no point in showing its contents.

So "sh t2400-*.sh -x -i" already shows "cat actual" output.  Try
the attached patch on top of this one and running it would show
the above message shown by test_must_be_empty and the contents of
the file 'actual'.  "git worktree remove" fails and your "cat" in
the test_cleanup does not even trigger, by the way.

There may be cases where having something like this might help, but
running the test with "-x" is not it---that case is already covered
by what test_must_be_empty gives us, I think.

 t/t2400-worktree-add.sh | 3 +++
 1 file changed, 3 insertions(+)

diff --git c/t/t2400-worktree-add.sh w/t/t2400-worktree-add.sh
index 9bc3db20e4..814642c8ae 100755
--- c/t/t2400-worktree-add.sh
+++ w/t/t2400-worktree-add.sh
@@ -329,9 +329,12 @@ test_expect_success 'add --quiet' '
 	test_when_finished "git worktree remove -f -f another-worktree" &&
 	test_when_finished cat actual >&2 &&
 	git worktree add --quiet another-worktree main 2>actual &&
+echo foo >>actual &&
 	test_must_be_empty actual
 '
 
+exit
+
 test_expect_success 'local clone from linked checkout' '
 	git clone --local here here-clone &&
 	( cd here-clone && git fsck )