git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] t4301: account for behavior differences between sed implementations

From
Eric Sunshine via GitGitGadget <gitgitgadget@gmail.com>
Date
Aug 28, 2022, 05:17 UTC
Message-ID
<a3576ff88226ffbdcc58bf837e4cd97dd299f77b.1661663880.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1339.git.1661663879.gitgitgadget@gmail.com>
From: Eric Sunshine <sunshine@sunshineco.com>

It is a common pattern in this script to write the result of `merge-tree -z` (NUL-termination mode) to an "actual" file and then manually append a newline to that file so that it can be diff'd easily with a hand-crafted "expect" file which itself ends with a newline since it has been created by standard Unix tools which terminate lines by default. For instance:

    git merge-tree --write-tree -z ... >out &&
    printf "\\n" >>out
    anonymize_hash out >actual &&
    q_to_nul <<-EOF >expect &&
    ...
    EOF
    test_cmp expect actual
However, one test gets this backward:
    git merge-tree --write-tree -z ... >out &&
    anonymize_hash out >actual &&
    printf "\\n" >>actual

which means that, unlike all other cases, when anonymize_hash() is called, the file being anonymized does not end with a newline. As a result, this test fails on some platforms.

anonymize_hash() is implemented like this:
    anonymize_hash() {
        sed -e "s/[0-9a-f]\{40,\}/HASH/g" "$@"
    }

The problem arises due to differences in behavior of various `sed` implementations when fed an incomplete line (lacking a newline). Although most modern `sed` implementations output such a line unmolested (i.e. without a newline), some older `sed` implementations forcibly add a newline to the incomplete line (giving the output an extra unexpected newline), while other very old implementations simply swallow an incomplete line and don't emit it at all (making the output shorter than expected).

Fix this test by manually adding the newline before passing it through `sed`, thus ensuring identical behavior with all `sed` implementation, and bringing the test in line with other tests in this script.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 t/t4301-merge-tree-write-tree.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
index c5fd56df28f..d44c7767f30 100755
--- a/t/t4301-merge-tree-write-tree.sh
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -760,8 +760,8 @@ test_expect_success 'NUL terminated conflicted file "lines"' '
 	git commit -m "Renamed numbers" &&
 
 	test_expect_code 1 git merge-tree --write-tree -z tweak1 side2 >out &&
+	printf "\\n" >>out &&
 	anonymize_hash out >actual &&
-	printf "\\n" >>actual &&
 
 	# Expected results:
 	#   "greeting" should merge with conflicts
-- 
gitgitgadget
Previous: Eric Sunshine via GitGitGadgetNext: Eric Sunshine via GitGitGadget
Message 2 of 12 in “fix failing t4301 test and &&-chain breakage”
  1. 0/3 fix failing t4301 test and &&-chain breakageEric Sunshine via GitGitGadget, Aug 28, 2022
  2. 1/3 t4301: account for behavior differences between sed implementationsEric Sunshine via GitGitGadget, Aug 28, 2022
  3. 2/3 t4031: fix broken &&-chains and add missing loop terminationEric Sunshine via GitGitGadget, Aug 28, 2022
  4. 3/3 t4301: emit blank line in more idiomatic fashionEric Sunshine via GitGitGadget, Aug 28, 2022
  5. Junio C HamanoAug 28, 2022
  6. Eric SunshineAug 28, 2022
  7. Junio C HamanoAug 29, 2022
  8. Elijah NewrenAug 30, 2022
  9. Eric SunshineAug 30, 2022
  10. Elijah NewrenAug 30, 2022
  11. Elijah NewrenAug 30, 2022
  12. Johannes SchindelinAug 30, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.