From 7002d6cd16047c0ed0b6befc22b5a7d54d4d6fde Mon Sep 17 00:00:00 2001 From: Siddharth Shrimali Date: Tue, 21 Apr 2026 11:03:32 +0530 Subject: [PATCH 1/3] t7004: drop hardcoded tag count for state verification The test 'trying to create a tag with a non-valid name should fail', checked that exactly one tag existed in the repository before and after attempting to create invalid tags. As pointed out by Junio, this makes the test brittle by relying on a specific global tag count. If future tests are added or removed before this test, the expected state changes and this test would break for completely unrelated reasons. Modernize the test by taking a snapshot of the existing tags before the failure attempts and comparing it to a snapshot taken after. This provides a "belt-and-suspenders" approach: we verify that 'git tag' both exits with the expected error code and leaves the repository state untouched, without being brittle to the specific number of tags present. This replaces the hardcoded 'test_line_count = 1' checks with 'test_cmp' to ensure the tag list remains identical. Suggested-by: Junio C Hamano Signed-off-by: Siddharth Shrimali Signed-off-by: Junio C Hamano --- t/t7004-tag.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh index faf7d97fc4..77a7a9777d 100755 --- a/t/t7004-tag.sh +++ b/t/t7004-tag.sh @@ -191,15 +191,14 @@ test_expect_success 'trying to create a tag with the name of one existing should ' test_expect_success 'trying to create a tag with a non-valid name should fail' ' - git tag -l >actual && - test_line_count = 1 actual && + git tag -l >tags-before && test_must_fail git tag "" && test_must_fail git tag .othertag && test_must_fail git tag "other tag" && test_must_fail git tag "othertag^" && test_must_fail git tag "other~tag" && - git tag -l >actual && - test_line_count = 1 actual + git tag -l >tags-after && + test_cmp tags-before tags-after ' test_expect_success 'creating a tag using HEAD directly should succeed' ' From e3253255d3e1c007e80742c304ddde9421dca9ca Mon Sep 17 00:00:00 2001 From: Siddharth Shrimali Date: Tue, 21 Apr 2026 11:03:33 +0530 Subject: [PATCH 2/3] t7004: dynamically grab expected state in tests The tests for 'Multiple -l or --list options' and 'trying to delete tags without params', hardcodes that exactly one or two specific tags ('myhead', 'mytag') exist in the repository. If other tests are added, modified, or removed earlier in the script, this expected global state will change, resulting in these tests to fail for completely unrelated reasons. Instead of hardcoding the expected tags, dynamically grab the state of the repository before running the commands under test ('git tag -l' and 'git tag -d'), and verify that the output matches or remains unchanged afterward. This keeps the tests independent from the script's overall state. Signed-off-by: Siddharth Shrimali Signed-off-by: Junio C Hamano --- t/t7004-tag.sh | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh index 77a7a9777d..bef7618da2 100755 --- a/t/t7004-tag.sh +++ b/t/t7004-tag.sh @@ -145,9 +145,7 @@ test_expect_success 'listing all tags if one exists should succeed' ' ' test_expect_success 'Multiple -l or --list options are equivalent to one -l option' ' - cat >expect <<-\EOF && - mytag - EOF + git tag -l >expect && git tag -l -l >actual && test_cmp expect actual && git tag --list --list >actual && @@ -226,12 +224,7 @@ test_expect_success 'trying to delete an unknown tag should fail' ' ' test_expect_success 'trying to delete tags without params should succeed and do nothing' ' - cat >expect <<-\EOF && - myhead - mytag - EOF - git tag -l >actual && - test_cmp expect actual && + git tag -l >expect && git tag -d && git tag -l >actual && test_cmp expect actual From ef85286e511b4cebfdce0c4bffc7c8985274f142 Mon Sep 17 00:00:00 2001 From: Siddharth Shrimali Date: Tue, 21 Apr 2026 11:03:34 +0530 Subject: [PATCH 3/3] t7004: avoid subshells to capture git exit codes Several tests in t7004 use the 'test$(git ...) = ...' or the '! (git ...)' subshell pattern. This swallows git's exit code. If git crashes (e.g. segmentation fault) the crash would go undetected, and the test would fail due to a mismatch or an inverted exit code. Modernize these tests by directly writing output to files(actual) and verifying them with 'test_cmp' or 'test_grep'. Replace subshell negations with 'test_must_fail'. This way, if git crashes, the test fails immediately and clearly instead of hiding the error behind a string mismatch. Signed-off-by: Siddharth Shrimali Signed-off-by: Junio C Hamano --- t/t7004-tag.sh | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh index bef7618da2..d918005dd9 100755 --- a/t/t7004-tag.sh +++ b/t/t7004-tag.sh @@ -155,8 +155,10 @@ test_expect_success 'Multiple -l or --list options are equivalent to one -l opti ' test_expect_success 'listing all tags if one exists should output that tag' ' - test $(git tag -l) = mytag && - test $(git tag) = mytag + git tag -l >actual && + test_grep "^mytag$" actual && + git tag >actual && + test_grep "^mytag$" actual ' # pattern matching: @@ -166,11 +168,15 @@ test_expect_success 'listing a tag using a matching pattern should succeed' ' ' test_expect_success 'listing a tag with --ignore-case' ' - test $(git tag -l --ignore-case MYTAG) = mytag + echo mytag >expect && + git tag -l --ignore-case MYTAG >actual && + test_cmp expect actual ' test_expect_success 'listing a tag using a matching pattern should output that tag' ' - test $(git tag -l mytag) = mytag + echo mytag >expect && + git tag -l mytag >actual && + test_cmp expect actual ' test_expect_success 'listing tags using a non-matching pattern should succeed' ' @@ -430,8 +436,12 @@ test_expect_success 'listing tags -n in column with column.ui ignored' ' test_expect_success 'a non-annotated tag created without parameters should point to HEAD' ' git tag non-annotated-tag && - test $(git cat-file -t non-annotated-tag) = commit && - test $(git rev-parse non-annotated-tag) = $(git rev-parse HEAD) + echo commit >expect && + git cat-file -t non-annotated-tag >actual && + test_cmp expect actual && + git rev-parse HEAD >expect && + git rev-parse non-annotated-tag >actual && + test_cmp expect actual ' test_expect_success 'trying to verify an unknown tag should fail' ' @@ -1520,11 +1530,11 @@ test_expect_success GPG 'verify signed tag fails when public key is not present' ' test_expect_success 'git tag -a fails if tag annotation is empty' ' - ! (GIT_EDITOR=cat git tag -a initial-comment) + test_must_fail env GIT_EDITOR=cat git tag -a initial-comment ' test_expect_success 'message in editor has initial comment' ' - ! (GIT_EDITOR=cat git tag -a initial-comment >actual) + test_must_fail env GIT_EDITOR=cat git tag -a initial-comment >actual ' test_expect_success 'message in editor has initial comment: first line' '