test-lib functions: add --printf option to test_commit

Add a --printf option to test_commit to allow writing to the file with "printf" instead of "echo". This is useful for writing "\n", "\0" etc., in particular in combination with the --append option added in 3373518cc8 (test-lib functions: add an --append option to test_commit, 2021-01-12). I'm converting a few tests to use the new option rather than a manual printf/add/commit combination to demonstrate its usefulness. While I'm at it use "test_create_repo" where appropriate, and give the first/second commit a meaningful/more conventional log message in cases where no test cared about that message. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed May 10, 2021 at 16:19 UTC 47c88d16ba6c5c0237238ac600ee8b74a522e41c
6 files changed +20 -25
t/t1307-config-blob.sh
+1 -3
@@ -65,9 +65,7 @@ test_expect_success 'parse errors in blobs are properly attributed' '
65 '
66
67 test_expect_success 'can parse blob ending with CR' '
68 - printf "[some]key = value\\r" >config &&
69 - git add config &&
70 - git commit -m CR &&
68 + test_commit --printf CR config "[some]key = value\\r" &&
69 echo value >expect &&
70 git config --blob=HEAD:config some.key >actual &&
71 test_cmp expect actual
t/t2030-unresolve-info.sh
+1 -2
@@ -179,8 +179,7 @@ test_expect_success 'rerere and rerere forget (subdirectory)' '
179
180 test_expect_success 'rerere forget (binary)' '
181 git checkout -f side &&
182 - printf "a\0c" >binary &&
183 - git commit -a -m binary &&
182 + test_commit --printf binary binary "a\0c" &&
183 test_must_fail git merge second &&
184 git rerere forget binary
185 '
t/t4006-diff-mode.sh
+2 -4
@@ -26,10 +26,8 @@ test_expect_success 'chmod' '
26 '
27
28 test_expect_success 'prepare binary file' '
29 - git commit -m rezrov &&
30 - printf "\00\01\02\03\04\05\06" >binbin &&
31 - git add binbin &&
32 - git commit -m binbin
29 + git commit -m one &&
30 + test_commit --printf two binbin "\00\01\02\03\04\05\06"
31 '
32
33 test_expect_success '--stat output after text chmod' '
t/t4030-diff-textconv.sh
+2 -6
@@ -26,12 +26,8 @@ EOF
26 chmod +x hexdump
27
28 test_expect_success 'setup binary file with history' '
29 - printf "\\0\\n" >file &&
30 - git add file &&
31 - git commit -m one &&
32 - printf "\\01\\n" >>file &&
33 - git add file &&
34 - git commit -m two
29 + test_commit --printf one file "\\0\\n" &&
30 + test_commit --printf --append two file "\\01\\n"
31 '
32
33 test_expect_success 'file is considered binary by porcelain' '
t/t5520-pull.sh
+2 -8
@@ -746,14 +746,8 @@ test_expect_success 'pull --rebase fails on corrupt HEAD' '
746 '
747
748 test_expect_success 'setup for detecting upstreamed changes' '
749 - mkdir src &&
750 - (
751 - cd src &&
752 - git init &&
753 - printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" > stuff &&
754 - git add stuff &&
755 - git commit -m "Initial revision"
756 - ) &&
749 + test_create_repo src &&
750 + test_commit -C src --printf one stuff "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" &&
751 git clone src dst &&
752 (
753 cd src &&
t/test-lib-functions.sh
+12 -2
@@ -173,6 +173,12 @@ debug () {
173 # Do not call test_tick before making a commit
174 # --append
175 # Use ">>" instead of ">" when writing "<contents>" to "<file>"
176 +# --printf
177 +# Use "printf" instead of "echo" when writing "<contents>" to
178 +# "<file>", use this to write escape sequences such as "\0", a
179 +# trailing "\n" won't be added automatically. This option
180 +# supports nothing but the FORMAT of printf(1), i.e. no custom
181 +# ARGUMENT(s).
182 # --signoff
183 # Invoke "git commit" with --signoff
184 # --author <author>
@@ -191,6 +197,7 @@ debug () {
197
198 test_commit () {
199 notick= &&
200 + echo=echo &&
201 append= &&
202 author= &&
203 signoff= &&
@@ -202,6 +209,9 @@ test_commit () {
209 --notick)
210 notick=yes
211 ;;
212 + --printf)
213 + echo=printf
214 + ;;
215 --append)
216 append=yes
217 ;;
@@ -238,9 +248,9 @@ test_commit () {
248 file=${2:-"$1.t"} &&
249 if test -n "$append"
250 then
241 - echo "${3-$1}" >>"$indir$file"
251 + $echo "${3-$1}" >>"$indir$file"
252 else
243 - echo "${3-$1}" >"$indir$file"
253 + $echo "${3-$1}" >"$indir$file"
254 fi &&
255 git ${indir:+ -C "$indir"} add "$file" &&
256 if test -z "$notick"