Skip to content

Commit 47c88d1

Browse files
avargitster
authored andcommitted
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 3373518 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8cfe386 commit 47c88d1

File tree

6 files changed

+20
-25
lines changed

6 files changed

+20
-25
lines changed

t/t1307-config-blob.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ test_expect_success 'parse errors in blobs are properly attributed' '
6565
'
6666

6767
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" &&
7169
echo value >expect &&
7270
git config --blob=HEAD:config some.key >actual &&
7371
test_cmp expect actual

t/t2030-unresolve-info.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ test_expect_success 'rerere and rerere forget (subdirectory)' '
179179

180180
test_expect_success 'rerere forget (binary)' '
181181
git checkout -f side &&
182-
printf "a\0c" >binary &&
183-
git commit -a -m binary &&
182+
test_commit --printf binary binary "a\0c" &&
184183
test_must_fail git merge second &&
185184
git rerere forget binary
186185
'

t/t4006-diff-mode.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ test_expect_success 'chmod' '
2626
'
2727

2828
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"
3331
'
3432

3533
test_expect_success '--stat output after text chmod' '

t/t4030-diff-textconv.sh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ EOF
2626
chmod +x hexdump
2727

2828
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"
3531
'
3632

3733
test_expect_success 'file is considered binary by porcelain' '

t/t5520-pull.sh

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -746,14 +746,8 @@ test_expect_success 'pull --rebase fails on corrupt HEAD' '
746746
'
747747

748748
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" &&
757751
git clone src dst &&
758752
(
759753
cd src &&

t/test-lib-functions.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ debug () {
173173
# Do not call test_tick before making a commit
174174
# --append
175175
# 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).
176182
# --signoff
177183
# Invoke "git commit" with --signoff
178184
# --author <author>
@@ -191,6 +197,7 @@ debug () {
191197

192198
test_commit () {
193199
notick= &&
200+
echo=echo &&
194201
append= &&
195202
author= &&
196203
signoff= &&
@@ -202,6 +209,9 @@ test_commit () {
202209
--notick)
203210
notick=yes
204211
;;
212+
--printf)
213+
echo=printf
214+
;;
205215
--append)
206216
append=yes
207217
;;
@@ -238,9 +248,9 @@ test_commit () {
238248
file=${2:-"$1.t"} &&
239249
if test -n "$append"
240250
then
241-
echo "${3-$1}" >>"$indir$file"
251+
$echo "${3-$1}" >>"$indir$file"
242252
else
243-
echo "${3-$1}" >"$indir$file"
253+
$echo "${3-$1}" >"$indir$file"
244254
fi &&
245255
git ${indir:+ -C "$indir"} add "$file" &&
246256
if test -z "$notick"

0 commit comments

Comments
 (0)