Skip to contentSkip to conversation

t2203: avoid masking exit codes in git status

PATCH Superseded Tian Yuchen 2 participants 4 messages Jan 17 – Jan 18

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 t2203: avoid masking exit codes in git status

The test script t2203-add-intent.sh uses the pattern git status | grep -v in multiple places. This pipeline masks the exit code of git status. If git status crashes, grep -v will successfully filter nothing and return 0, causing the test to pass incorrectly.

Update the tests to use an intermediate file, ensuring that the exit codecof git status is checked and any crash is caught immediately.

In one instance, we also need to update the grep pattern to ignore the temporary file it self, to avoid causing a regression in the test output comparison.

---
 t/t2203-add-intent.sh | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
t/t2203-add-intent.sh +16 -8
index 192ad14b5f..ac8bafa680 100755
@@ -16,7 +16,8 @@ test_expect_success 'intent to add' '
 '
 
 test_expect_success 'git status' '
-	git status --porcelain | grep -v actual >actual &&
+	git status --porcelain >tmp &&
+	grep -v -e actual -e tmp tmp >actual &&
 	cat >expect <<-\EOF &&
 	DA 1.t
 	A  elif
@@ -26,7 +27,8 @@ test_expect_success 'git status' '
 '
 
 test_expect_success 'git status with porcelain v2' '
-	git status --porcelain=v2 | grep -v "^?" >actual &&
+	git status --porcelain=v2 >tmp &&
+	grep -v "^?" tmp >actual &&
 	nam1=$(echo 1 | git hash-object --stdin) &&
 	nam2=$(git hash-object elif) &&
 	cat >expect <<-EOF &&
@@ -171,17 +173,20 @@ test_expect_success 'rename detection finds the right names' '
 		mv first third &&
 		git add -N third &&
 
-		git status | grep -v "^?" >actual.1 &&
+		git status >tmp &&
+		grep -v "^?" tmp >actual.1 &&
 		test_grep "renamed: *first -> third" actual.1 &&
 
-		git status --porcelain | grep -v "^?" >actual.2 &&
+		git status --porcelain >tmp &&
+		grep -v "^?" tmp >actual.2 &&
 		cat >expected.2 <<-\EOF &&
 		 R first -> third
 		EOF
 		test_cmp expected.2 actual.2 &&
 
 		hash=$(git hash-object third) &&
-		git status --porcelain=v2 | grep -v "^?" >actual.3 &&
+		git status --porcelain=v2 >tmp &&
+		grep -v "^?" tmp >actual.3 &&
 		cat >expected.3 <<-EOF &&
 		2 .R N... 100644 100644 100644 $hash $hash R100 third	first
 		EOF
@@ -211,11 +216,13 @@ test_expect_success 'double rename detection in status' '
 		mv second third &&
 		git add -N third &&
 
-		git status | grep -v "^?" >actual.1 &&
+		git status >tmp &&
+		grep -v "^?" tmp >actual.1 &&
 		test_grep "renamed: *first -> second" actual.1 &&
 		test_grep "renamed: *second -> third" actual.1 &&
 
-		git status --porcelain | grep -v "^?" >actual.2 &&
+		git status --porcelain >tmp &&
+		grep -v "^?" tmp >actual.2 &&
 		cat >expected.2 <<-\EOF &&
 		R  first -> second
 		 R second -> third
@@ -223,7 +230,8 @@ test_expect_success 'double rename detection in status' '
 		test_cmp expected.2 actual.2 &&
 
 		hash=$(git hash-object third) &&
-		git status --porcelain=v2 | grep -v "^?" >actual.3 &&
+		git status --porcelain=v2 >tmp &&
+		grep -v "^?" tmp >actual.3 &&
 		cat >expected.3 <<-EOF &&
 		2 R. N... 100644 100644 100644 $hash $hash R100 second	first
 		2 .R N... 100644 100644 100644 $hash $hash R100 third	second

base-commit: 7264e61d87e58b9d0f5e6424c47c11e9657dfb75

Signature · 1 line
2.43.0
#1 lore raw

t/t2203-add-intent.sh test_expect_success 'intent to add' ' · line 20

Quoted 4 lines · Tian Yuchen

test_expect_success 'git status' '

t/t2203-add-intent.sh +2 -1
-	git status --porcelain | grep -v actual >actual &&
+	git status --porcelain >tmp &&
+	grep -v -e actual -e tmp tmp >actual &&

Looking at other parts of this patch makes me wonder if we can get away by filtering lines that match "^?" out, instead of explicitly naming 'actual' and 'tmp'. It seems that the entire t2203 file does not care about untraacked files at all (in other words, there is no "expect" file that expects a line with '^?' in it), so rewriting the above (and everything that this patch touches) to use something like

	git status --porcelain -uno >actual

makes the intent clear (i.e., we do not care about untracked files) and simpler (i.e., we do not need "grep -v" to filter), wouldn't it?

Quoted 65 lines
t/t2203-add-intent.sh +14 -7
 	cat >expect <<-\EOF &&
 	DA 1.t
 	A  elif
@@ -26,7 +27,8 @@ test_expect_success 'git status' '
 '
 
 test_expect_success 'git status with porcelain v2' '
-	git status --porcelain=v2 | grep -v "^?" >actual &&
+	git status --porcelain=v2 >tmp &&
+	grep -v "^?" tmp >actual &&
 	nam1=$(echo 1 | git hash-object --stdin) &&
 	nam2=$(git hash-object elif) &&
 	cat >expect <<-EOF &&
@@ -171,17 +173,20 @@ test_expect_success 'rename detection finds the right names' '
 		mv first third &&
 		git add -N third &&
 
-		git status | grep -v "^?" >actual.1 &&
+		git status >tmp &&
+		grep -v "^?" tmp >actual.1 &&
 		test_grep "renamed: *first -> third" actual.1 &&
 
-		git status --porcelain | grep -v "^?" >actual.2 &&
+		git status --porcelain >tmp &&
+		grep -v "^?" tmp >actual.2 &&
 		cat >expected.2 <<-\EOF &&
 		 R first -> third
 		EOF
 		test_cmp expected.2 actual.2 &&
 
 		hash=$(git hash-object third) &&
-		git status --porcelain=v2 | grep -v "^?" >actual.3 &&
+		git status --porcelain=v2 >tmp &&
+		grep -v "^?" tmp >actual.3 &&
 		cat >expected.3 <<-EOF &&
 		2 .R N... 100644 100644 100644 $hash $hash R100 third	first
 		EOF
@@ -211,11 +216,13 @@ test_expect_success 'double rename detection in status' '
 		mv second third &&
 		git add -N third &&
 
-		git status | grep -v "^?" >actual.1 &&
+		git status >tmp &&
+		grep -v "^?" tmp >actual.1 &&
 		test_grep "renamed: *first -> second" actual.1 &&
 		test_grep "renamed: *second -> third" actual.1 &&
 
-		git status --porcelain | grep -v "^?" >actual.2 &&
+		git status --porcelain >tmp &&
+		grep -v "^?" tmp >actual.2 &&
 		cat >expected.2 <<-\EOF &&
 		R  first -> second
 		 R second -> third
@@ -223,7 +230,8 @@ test_expect_success 'double rename detection in status' '
 		test_cmp expected.2 actual.2 &&
 
 		hash=$(git hash-object third) &&
-		git status --porcelain=v2 | grep -v "^?" >actual.3 &&
+		git status --porcelain=v2 >tmp &&
+		grep -v "^?" tmp >actual.3 &&
 		cat >expected.3 <<-EOF &&
 		2 R. N... 100644 100644 100644 $hash $hash R100 second	first
 		2 .R N... 100644 100644 100644 $hash $hash R100 third	second

base-commit: 7264e61d87e58b9d0f5e6424c47c11e9657dfb75

#2 lore raw
Quoted 11 lines · Junio C Hamano

Looking at other parts of this patch makes me wonder if we can get away by filtering lines that match "^?" out, instead of explicitly naming 'actual' and 'tmp'. It seems that the entire t2203 file does not care about untraacked files at all (in other words, there is no "expect" file that expects a line with '^?' in it), so rewriting the above (and everything that this patch touches) to use something like

      git status --porcelain -uno >actual

makes the intent clear (i.e., we do not care about untracked files) and simpler (i.e., we do not need "grep -v" to filter), wouldn't it?

You are right. Using -uno is much simpler and semantically closer to what the tests are checking (changes in the index, not untracked files). It also naturally avoids the exit code masking issue since the pipe is removed.

I have checked t2203 and confirmed that no test cases expect untracked files in their output.

I will send a v2 patch shortly with this change, and I'll also add the [gsoc] prefix which I missed in the first iteration.

Thanks for you suggestion!

Quoted 85 lines

t/t2203-add-intent.sh test_expect_success 'intent to add' ' · line 20

Quoted 4 lines · Tian Yuchen

test_expect_success 'git status' '

t/t2203-add-intent.sh +2 -1
-     git status --porcelain | grep -v actual >actual &&
+     git status --porcelain >tmp &&
+     grep -v -e actual -e tmp tmp >actual &&

Looking at other parts of this patch makes me wonder if we can get away by filtering lines that match "^?" out, instead of explicitly naming 'actual' and 'tmp'. It seems that the entire t2203 file does not care about untraacked files at all (in other words, there is no "expect" file that expects a line with '^?' in it), so rewriting the above (and everything that this patch touches) to use something like

        git status --porcelain -uno >actual

makes the intent clear (i.e., we do not care about untracked files) and simpler (i.e., we do not need "grep -v" to filter), wouldn't it?

Quoted 65 lines
      cat >expect <<-\EOF &&
      DA 1.t
      A  elif
t/t2203-add-intent.sh +14 -7
@@ -26,7 +27,8 @@ test_expect_success 'git status' '
 '
 
 test_expect_success 'git status with porcelain v2' '
-     git status --porcelain=v2 | grep -v "^?" >actual &&
+     git status --porcelain=v2 >tmp &&
+     grep -v "^?" tmp >actual &&
      nam1=$(echo 1 | git hash-object --stdin) &&
      nam2=$(git hash-object elif) &&
      cat >expect <<-EOF &&
@@ -171,17 +173,20 @@ test_expect_success 'rename detection finds the right names' '
              mv first third &&
              git add -N third &&
 
-             git status | grep -v "^?" >actual.1 &&
+             git status >tmp &&
+             grep -v "^?" tmp >actual.1 &&
              test_grep "renamed: *first -> third" actual.1 &&
 
-             git status --porcelain | grep -v "^?" >actual.2 &&
+             git status --porcelain >tmp &&
+             grep -v "^?" tmp >actual.2 &&
              cat >expected.2 <<-\EOF &&
               R first -> third
              EOF
              test_cmp expected.2 actual.2 &&
 
              hash=$(git hash-object third) &&
-             git status --porcelain=v2 | grep -v "^?" >actual.3 &&
+             git status --porcelain=v2 >tmp &&
+             grep -v "^?" tmp >actual.3 &&
              cat >expected.3 <<-EOF &&
              2 .R N... 100644 100644 100644 $hash $hash R100 third   first
              EOF
@@ -211,11 +216,13 @@ test_expect_success 'double rename detection in status' '
              mv second third &&
              git add -N third &&
 
-             git status | grep -v "^?" >actual.1 &&
+             git status >tmp &&
+             grep -v "^?" tmp >actual.1 &&
              test_grep "renamed: *first -> second" actual.1 &&
              test_grep "renamed: *second -> third" actual.1 &&
 
-             git status --porcelain | grep -v "^?" >actual.2 &&
+             git status --porcelain >tmp &&
+             grep -v "^?" tmp >actual.2 &&
              cat >expected.2 <<-\EOF &&
              R  first -> second
               R second -> third
@@ -223,7 +230,8 @@ test_expect_success 'double rename detection in status' '
              test_cmp expected.2 actual.2 &&
 
              hash=$(git hash-object third) &&
-             git status --porcelain=v2 | grep -v "^?" >actual.3 &&
+             git status --porcelain=v2 >tmp &&
+             grep -v "^?" tmp >actual.3 &&
              cat >expected.3 <<-EOF &&
              2 R. N... 100644 100644 100644 $hash $hash R100 second  first
              2 .R N... 100644 100644 100644 $hash $hash R100 third   second

base-commit: 7264e61d87e58b9d0f5e6424c47c11e9657dfb75

#3 lore raw

PATCH v2 1/1 t2203: avoid masking exit codes in git status

In reply to Tian Yuchen

The test script t2203-add-intent.sh uses the pattern git status | grep -v in multiple places. This pipeline masks the exit code of git status.

Update the tests to use git status -uno >actual. This excludes untracked files from the output entirely, removing the need for filtering with grep and allowing us to drop the pipeline, ensuring any crash in git status is caught immediately.

---
 t/t2203-add-intent.sh | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
t/t2203-add-intent.sh +8 -8
index 192ad14b5f..5bdd12c91f 100755
@@ -16,7 +16,7 @@ test_expect_success 'intent to add' '
 '
 
 test_expect_success 'git status' '
-	git status --porcelain | grep -v actual >actual &&
+	git status --porcelain -uno >actual &&
 	cat >expect <<-\EOF &&
 	DA 1.t
 	A  elif
@@ -26,7 +26,7 @@ test_expect_success 'git status' '
 '
 
 test_expect_success 'git status with porcelain v2' '
-	git status --porcelain=v2 | grep -v "^?" >actual &&
+	git status --porcelain=v2 -uno >actual &&
 	nam1=$(echo 1 | git hash-object --stdin) &&
 	nam2=$(git hash-object elif) &&
 	cat >expect <<-EOF &&
@@ -171,17 +171,17 @@ test_expect_success 'rename detection finds the right names' '
 		mv first third &&
 		git add -N third &&
 
-		git status | grep -v "^?" >actual.1 &&
+		git status -uno >actual.1 &&
 		test_grep "renamed: *first -> third" actual.1 &&
 
-		git status --porcelain | grep -v "^?" >actual.2 &&
+		git status --porcelain -uno >actual.2 &&
 		cat >expected.2 <<-\EOF &&
 		 R first -> third
 		EOF
 		test_cmp expected.2 actual.2 &&
 
 		hash=$(git hash-object third) &&
-		git status --porcelain=v2 | grep -v "^?" >actual.3 &&
+		git status --porcelain=v2 -uno >actual.3 &&
 		cat >expected.3 <<-EOF &&
 		2 .R N... 100644 100644 100644 $hash $hash R100 third	first
 		EOF
@@ -211,11 +211,11 @@ test_expect_success 'double rename detection in status' '
 		mv second third &&
 		git add -N third &&
 
-		git status | grep -v "^?" >actual.1 &&
+		git status -uno >actual.1 &&
 		test_grep "renamed: *first -> second" actual.1 &&
 		test_grep "renamed: *second -> third" actual.1 &&
 
-		git status --porcelain | grep -v "^?" >actual.2 &&
+		git status --porcelain -uno >actual.2 &&
 		cat >expected.2 <<-\EOF &&
 		R  first -> second
 		 R second -> third
@@ -223,7 +223,7 @@ test_expect_success 'double rename detection in status' '
 		test_cmp expected.2 actual.2 &&
 
 		hash=$(git hash-object third) &&
-		git status --porcelain=v2 | grep -v "^?" >actual.3 &&
+		git status --porcelain=v2 -uno >actual.3 &&
 		cat >expected.3 <<-EOF &&
 		2 R. N... 100644 100644 100644 $hash $hash R100 second	first
 		2 .R N... 100644 100644 100644 $hash $hash R100 third	second

base-commit: 7264e61d87e58b9d0f5e6424c47c11e9657dfb75

Signature · 1 line
2.43.0