Skip to contentSkip to conversation

t6423: fix suppression of Git’s exit code in tests

PATCH In review ayu-ch 4 participants 9 messages Feb 2, 2025 – Feb 6, 2025

lore
Share & tools
lore

ayu-ch

author
#0 lore raw

PATCH 1/1 t6423: fix suppression of Git’s exit code in tests

From: Ayush Chandekar <[email protected]>

Some test in t6423 supress Git's exit code, which can cause test failures go unnoticed. Specifically using git <subcommand> | <other-command> masks potential failures of the Git command.

This commit ensures that Git's exit status is correctly propogated by:
- Avoiding pipes that suppress exit codes.

---
 t/t6423-merge-rename-directories.sh | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
t/t6423-merge-rename-directories.sh +6 -3
index 88d1cf2cde..94080c65d1 100755
@@ -5071,7 +5071,8 @@ test_expect_success '12i: Directory rename causes rename-to-self' '
 		test_path_is_file source/bar &&
 		test_path_is_file source/baz &&
 
-		git ls-files | uniq >tracked &&
+		git ls-files >actual &&
+		uniq <actual >tracked &&
 		test_line_count = 3 tracked &&
 
 		git status --porcelain -uno >actual &&
@@ -5129,7 +5130,8 @@ test_expect_success '12j: Directory rename to root causes rename-to-self' '
 		test_path_is_file bar &&
 		test_path_is_file baz &&
 
-		git ls-files | uniq >tracked &&
+		git ls-files >actual &&
+		uniq <actual >tracked &&
 		test_line_count = 3 tracked &&
 
 		git status --porcelain -uno >actual &&
@@ -5187,7 +5189,8 @@ test_expect_success '12k: Directory rename with sibling causes rename-to-self' '
 		test_path_is_file dirA/bar &&
 		test_path_is_file dirA/baz &&
 
-		git ls-files | uniq >tracked &&
+		git ls-files >actual &&
+		uniq <actual >tracked &&
 		test_line_count = 3 tracked &&
 
 		git status --porcelain -uno >actual &&
Signature · 1 line
2.48.GIT
#1 lore raw
Quoted 4 lines · ayu-ch

From: Ayush Chandekar <[email protected]>

Some test in t6423 supress Git's exit code, which can cause test

s/supress/suppress

Quoted 5 lines

failures go unnoticed. Specifically using git <subcommand> | <other-command> masks potential failures of the Git command.

This commit ensures that Git's exit status is correctly propogated by:
- Avoiding pipes that suppress exit codes.

s/propogated/propagated
The commit message should be in imperative mood (cf. Documentation/SubmittingPatches)

Quoted 41 lines
---
 t/t6423-merge-rename-directories.sh | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
t/t6423-merge-rename-directories.sh +6 -4
index 88d1cf2cde..94080c65d1 100755
@@ -5071,7 +5071,8 @@ test_expect_success '12i: Directory rename causes rename-to-self' '
                test_path_is_file source/bar &&
                test_path_is_file source/baz &&
 
-               git ls-files | uniq >tracked &&
+               git ls-files >actual &&
+               uniq <actual >tracked &&
                test_line_count = 3 tracked &&
 
                git status --porcelain -uno >actual &&
@@ -5129,7 +5130,8 @@ test_expect_success '12j: Directory rename to root causes rename-to-self' '
                test_path_is_file bar &&
                test_path_is_file baz &&
 
-               git ls-files | uniq >tracked &&
+               git ls-files >actual &&
+               uniq <actual >tracked &&
                test_line_count = 3 tracked &&
 
                git status --porcelain -uno >actual &&
@@ -5187,7 +5189,8 @@ test_expect_success '12k: Directory rename with sibling causes rename-to-self' '
                test_path_is_file dirA/bar &&
                test_path_is_file dirA/baz &&
 
-               git ls-files | uniq >tracked &&
+               git ls-files >actual &&
+               uniq <actual >tracked &&
                test_line_count = 3 tracked &&
 
                git status --porcelain -uno >actual &&
--
Quoted 3 lines

2.48.GIT

It should’ve been v2 of the patch you sent earlier [1] (cf. Documentation/MyFirstConribution),
but otherwise, it looks good.
[1]: https://lore.kernel.org/git/[email protected]/

Meet

#2 lore raw
In reply to ayu-ch
Quoted 11 lines · ayu-ch

Some test in t6423 supress Git's exit code, which can cause test failures go unnoticed. Specifically using git <subcommand> | <other-command> masks potential failures of the Git command.

This commit ensures that Git's exit status is correctly propogated by:
- Avoiding pipes that suppress exit codes.

---

t/t6423-merge-rename-directories.sh test_expect_success '12i: Directory rename causes rename-to-self' ' · line 5072

Quoted 3 lines
t/t6423-merge-rename-directories.sh +2 -1
@@ -5071,7 +5071,8 @@ test_expect_success '12i: Directory rename causes rename-to-self' '
-               git ls-files | uniq >tracked &&
+               git ls-files >actual &&
+               uniq <actual >tracked &&

I was curious if the project has a preference between uniq filename and uniq <filename, but apparently we haven't:

    % git grep 'uniq <' -- t | wc -l
    2
    git grep 'uniq [a-z0-9]' -- t | wc -l
    2

Though there does seem to be a global preference in the project to specify the filename directly to the command rather than redirecting from stdin. For instance:

    % git grep 'sort <' -- t | wc -l
    54
    % git grep 'sort [a-z0-9]' -- t | wc -l
    140

In any case, what you have here is probably fine, so no need to reroll just for this.

#3 lore raw
Quoted 8 lines · Eric Sunshine

I was curious if the project has a preference between uniq filename and uniq <filename, but apparently we haven't:

    % git grep 'uniq <' -- t | wc -l
    2
    git grep 'uniq [a-z0-9]' -- t | wc -l
    2
Quoted 8 lines

Though there does seem to be a global preference in the project to specify the filename directly to the command rather than redirecting from stdin. For instance:

    % git grep 'sort <' -- t | wc -l
    54
    % git grep 'sort [a-z0-9]' -- t | wc -l
    140

Have you inspected the hits from these grep runs?

    $ git grep -c 'sort [a-z0-9]' -- t/t7004-tag.sh
    t/t7004-tag.sh:17

Among 17 of them, 15 are on test titles.

    $ git grep -c '^test_expect_[sf].*sort [a-z0-9]' -- t/t7004-tag.sh
    t/t7004-tag.sh:15

So the above numbers are totally unreliable as a guide, I am afraid.

It is probably better to use sort/uniq without input redirection because your

    $ sort/uniq input >output

can be easily extended to

    $ sort/uniq input-a input-b input-c >output

but

    $ sort/uniq <input >output

cannot be extended the same way, and you'd end up doing nonsense pipe like this:

    $ cat input-a input-b input-c | sort >output

which is a no-no.

In reality, however, we are not all that logical.

    $ git grep -e '^[ 	]*sort [a-z0-9][-a-z0-9]* ' -- t | wc -l
    46
    $ git grep -e '^[ 	]*sort <' -- t | wc -l
    51

with "s/sort/uniq/", the numbers are 0 vs 1.

There are a handful of sort invocations that take their input from redirected <<HEREDOC included in the latter number, but the overall picture does not change with them excluded.

#4 lore raw

Do you see any other changes needed in this patch? Let me know if there's anything you want me to adjust, especially in my commit message. Since my previous attempt wasn't very suitable.

Regards,
Ayush

#5 lore raw
Quoted 3 lines · Ayush Chandekar

Do you see any other changes needed in this patch? Let me know if there's anything you want me to adjust, especially in my commit message. Since my previous attempt wasn't very suitable.

If I were to change something, there are two minor things, but they are so minor that I'd be OK without these changes.

If this is supposed to be a part of microproject exchange (sorry, I lost track), then I am also OK to do the second (and hopefully final) iteration to give us a chance to practice.

If I were you and I chose to iterate one more time, I'd rephrase this

This commit ensures that Git's exit status is correctly propogated by:
- Avoiding pipes that suppress exit codes.

to more like

Instead of placing a git command on the upstream side of a pipe, redirect its output to a file and process the file contents in two separate steps to avoid losing the exit status.

Also I'd not redirect into "uniq", i.e. instead of

	uniq <actual >tracked &&

I'd write

	uniq actual >tracked &&

but as discussed with Eric, this "better style" is not followed by existing code.

#6 lore raw

PATCH v2 1/1 t6422: avoid suppressing Git’s exit code in tests

Some test in t6423 supress Git's exit code, which can cause test failures go unnoticed. Specifically using git <subcommand> | <other-command> masks potential failures of the Git command.

Instead of executing a Git command as the upstream component of a pipe, which can result in the exit status being lost, redirect its output to a file and then process that file in two steps to ensure the exit status is properly preserved.

---
 t/t6423-merge-rename-directories.sh | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
t/t6423-merge-rename-directories.sh +6 -3
index 88d1cf2cde..a6c5b5a494 100755
@@ -5071,7 +5071,8 @@ test_expect_success '12i: Directory rename causes rename-to-self' '
 		test_path_is_file source/bar &&
 		test_path_is_file source/baz &&
 
-		git ls-files | uniq >tracked &&
+		git ls-files >actual &&
+		uniq actual >tracked &&
 		test_line_count = 3 tracked &&
 
 		git status --porcelain -uno >actual &&
@@ -5129,7 +5130,8 @@ test_expect_success '12j: Directory rename to root causes rename-to-self' '
 		test_path_is_file bar &&
 		test_path_is_file baz &&
 
-		git ls-files | uniq >tracked &&
+		git ls-files >actual &&
+		uniq actual >tracked &&
 		test_line_count = 3 tracked &&
 
 		git status --porcelain -uno >actual &&
@@ -5187,7 +5189,8 @@ test_expect_success '12k: Directory rename with sibling causes rename-to-self' '
 		test_path_is_file dirA/bar &&
 		test_path_is_file dirA/baz &&
 
-		git ls-files | uniq >tracked &&
+		git ls-files >actual &&
+		uniq actual >tracked &&
 		test_line_count = 3 tracked &&
 
 		git status --porcelain -uno >actual &&
Signature · 1 line
2.48.GIT
#7 lore raw

Re: [GSOC][PATCH v2] t6422: avoid suppressing Git’s exit code in tests

Ayush Chandekar <[email protected]> writes:

Thanks for practicing yet another iteration. I am not going to actually replace the previous one with this one, as the previous one is just OK, but let's pretend I would to complete the "simulated" iteration.

Below, pretend that we will discard the previous one and replace it with this one, and plan to merge the result to 'next', but that is only for practice.

---

Quoted 1 line

Subject: Re: [GSOC][PATCH v2] t6422: avoid suppressing Git’s exit code in tests

This is about 6423 ;-) I'll amend while applying the patch.

Quoted 9 lines

Some test in t6423 supress Git's exit code, which can cause test failures go unnoticed. Specifically using git <subcommand> | <other-command> masks potential failures of the Git command.

Instead of executing a Git command as the upstream component of a pipe, which can result in the exit status being lost, redirect its output to a file and then process that file in two steps to ensure the exit status is properly preserved.

Quoted 4 lines
---
 t/t6423-merge-rename-directories.sh | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

OK. And the change to the test body to lose input redirection into "uniq" look OK, too.

Thanks. Let's replace it and mark the topic for 'next'.

#8 lore raw

Re: [GSOC][PATCH v2] t6423: avoid suppressing Git’s exit code in tests

Thanks, Junio!

Appreciate the clarification about the test script number. I’ll be more careful next time.

Thanks for the review!

Regards,
Ayush