Skip to content

Commit b32c7ec

Browse files
peffgitster
authored andcommitted
test-lib: teach test_seq the -f option
The "seq" tool has a "-f" option to produce printf-style formatted lines. Let's teach our test_seq helper the same trick. This lets us get rid of some shell loops in test snippets (which are particularly verbose in our test suite because we have to "|| return 1" to keep the &&-chain going). This converts a few call-sites I found by grepping around the test suite. A few notes on these: - In "seq", the format specifier is a "%g" float. Since test_seq only supports integers, I've kept the more natural "%d" (which is what these call sites were using already). - Like "seq", test_seq automatically adds a newline to the specified format. This is what all callers are doing already except for t0021, but there we do not care about the exact format. We are just trying to printf a large number of bytes to a file. It's not worth complicating other callers or adding an option to avoid the newline in that caller. - Most conversions are just replacing a shell loop (which does get rid of an extra fork, since $() requires a subshell). In t0612 we can replace an awk invocation, which I think makes the end result more readable, as there's less quoting. - In t7422 we can replace one loop, but sadly we have to leave the loop directly above it. This is because that earlier loop wants to include the seq value twice in the output, which test_seq does not support (nor does regular seq). If you run: test_seq -f "foo-%d %d" 10 the second "%d" will always be the empty string. You might naively think that test_seq could add some extra arguments, like: # 3 ought to be enough for anyone... printf "$fmt\n" "$i "$i" $i" but that just triggers printf to format multiple lines, one per extra set of arguments. So we'd have to actually parse the format string, figure out how many "%" placeholders are there, and then feed it that many instances of the sequence number. The complexity isn't worth it. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1129596 commit b32c7ec

9 files changed

+32
-62
lines changed

t/t0021-conversion.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ test_expect_success 'required filter with absent smudge field' '
281281
test_expect_success 'filtering large input to small output should use little memory' '
282282
test_config filter.devnull.clean "cat >/dev/null" &&
283283
test_config filter.devnull.required true &&
284-
for i in $(test_seq 1 30); do printf "%1048576d" 1 || return 1; done >30MB &&
284+
test_seq -f "%1048576d" 1 30 >30MB &&
285285
echo "30MB filter=devnull" >.gitattributes &&
286286
GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB
287287
'
@@ -299,7 +299,7 @@ test_expect_success 'filter that does not read is fine' '
299299
test_expect_success EXPENSIVE 'filter large file' '
300300
test_config filter.largefile.smudge cat &&
301301
test_config filter.largefile.clean cat &&
302-
for i in $(test_seq 1 2048); do printf "%1048576d" 1 || return 1; done >2GB &&
302+
test_seq -f "%1048576d" 1 2048 >2GB &&
303303
echo "2GB filter=largefile" >.gitattributes &&
304304
git add 2GB 2>err &&
305305
test_must_be_empty err &&

t/t0610-reftable-basics.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,7 @@ test_expect_success !CYGWIN 'ref transaction: many concurrent writers' '
477477
test_commit --no-tag initial &&
478478
479479
head=$(git rev-parse HEAD) &&
480-
for i in $(test_seq 100)
481-
do
482-
printf "%s commit\trefs/heads/branch-%s\n" "$head" "$i" ||
483-
return 1
484-
done >expect &&
480+
test_seq -f "$head commit\trefs/heads/branch-%d" 100 >expect &&
485481
printf "%s commit\trefs/heads/main\n" "$head" >>expect &&
486482
487483
for i in $(test_seq 100)

t/t0612-reftable-jgit-compatibility.sh

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,11 @@ test_expect_success 'JGit can read multi-level index' '
112112
cd repo &&
113113
114114
test_commit A &&
115-
awk "
116-
BEGIN {
117-
print \"start\";
118-
for (i = 0; i < 10000; i++)
119-
printf \"create refs/heads/branch-%d HEAD\n\", i;
120-
print \"commit\";
121-
}
122-
" >input &&
115+
{
116+
echo start &&
117+
test_seq -f "create refs/heads/branch-%d HEAD" 10000 &&
118+
echo commit
119+
} >input &&
123120
git update-ref --stdin <input &&
124121
125122
test_same_refs &&

t/t0613-reftable-write-options.sh

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ test_expect_success 'many refs results in multiple blocks' '
6666
(
6767
cd repo &&
6868
test_commit initial &&
69-
for i in $(test_seq 200)
70-
do
71-
printf "update refs/heads/branch-%d HEAD\n" "$i" ||
72-
return 1
73-
done >input &&
69+
test_seq -f "update refs/heads/branch-%d HEAD" 200 >input &&
7470
git update-ref --stdin <input &&
7571
git pack-refs &&
7672
@@ -180,11 +176,7 @@ test_expect_success 'restart interval at every single record' '
180176
(
181177
cd repo &&
182178
test_commit initial &&
183-
for i in $(test_seq 10)
184-
do
185-
printf "update refs/heads/branch-%d HEAD\n" "$i" ||
186-
return 1
187-
done >input &&
179+
test_seq -f "update refs/heads/branch-%d HEAD" 10 >input &&
188180
git update-ref --stdin <input &&
189181
git -c reftable.restartInterval=1 pack-refs &&
190182
@@ -224,11 +216,7 @@ test_expect_success 'object index gets written by default with ref index' '
224216
(
225217
cd repo &&
226218
test_commit initial &&
227-
for i in $(test_seq 5)
228-
do
229-
printf "update refs/heads/branch-%d HEAD\n" "$i" ||
230-
return 1
231-
done >input &&
219+
test_seq -f "update refs/heads/branch-%d HEAD" 5 >input &&
232220
git update-ref --stdin <input &&
233221
git -c reftable.blockSize=100 pack-refs &&
234222
@@ -263,11 +251,7 @@ test_expect_success 'object index can be disabled' '
263251
(
264252
cd repo &&
265253
test_commit initial &&
266-
for i in $(test_seq 5)
267-
do
268-
printf "update refs/heads/branch-%d HEAD\n" "$i" ||
269-
return 1
270-
done >input &&
254+
test_seq -f "update refs/heads/branch-%d HEAD" 5 >input &&
271255
git update-ref --stdin <input &&
272256
git -c reftable.blockSize=100 -c reftable.indexObjects=false pack-refs &&
273257

t/t1400-update-ref.sh

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,21 +1380,15 @@ test_expect_success 'fails with duplicate ref update via symref' '
13801380

13811381
test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction creating branches does not burst open file limit' '
13821382
(
1383-
for i in $(test_seq 33)
1384-
do
1385-
echo "create refs/heads/$i HEAD" || exit 1
1386-
done >large_input &&
1383+
test_seq -f "create refs/heads/%d HEAD" 33 >large_input &&
13871384
run_with_limited_open_files git update-ref --stdin <large_input &&
13881385
git rev-parse --verify -q refs/heads/33
13891386
)
13901387
'
13911388

13921389
test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction deleting branches does not burst open file limit' '
13931390
(
1394-
for i in $(test_seq 33)
1395-
do
1396-
echo "delete refs/heads/$i HEAD" || exit 1
1397-
done >large_input &&
1391+
test_seq -f "delete refs/heads/%d HEAD" 33 >large_input &&
13981392
run_with_limited_open_files git update-ref --stdin <large_input &&
13991393
test_must_fail git rev-parse --verify -q refs/heads/33
14001394
)

t/t5004-archive-corner-cases.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,7 @@ test_expect_success EXPENSIVE,UNZIP,UNZIP_ZIP64_SUPPORT \
176176
blob=$(echo $s | git hash-object -w --stdin) &&
177177
178178
# create tree containing 65500 entries of that blob
179-
for i in $(test_seq 1 65500)
180-
do
181-
echo "100644 blob $blob $i" || return 1
182-
done >tree &&
179+
test_seq -f "100644 blob $blob\t%d" 1 65500 >tree &&
183180
tree=$(git mktree <tree) &&
184181
185182
# zip it, creating an archive a bit bigger than 4GB

t/t6422-merge-rename-corner-cases.sh

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,10 +1146,7 @@ test_conflicts_with_adds_and_renames() {
11461146
cd simple_${sideL}_${sideR} &&
11471147

11481148
# Create some related files now
1149-
for i in $(test_seq 1 10)
1150-
do
1151-
echo Random base content line $i
1152-
done >file_v1 &&
1149+
test_seq -f "Random base content line %d" 1 10 >file_v1 &&
11531150
cp file_v1 file_v2 &&
11541151
echo modification >>file_v2 &&
11551152

@@ -1293,10 +1290,7 @@ test_setup_nested_conflicts_from_rename_rename () {
12931290
cd nested_conflicts_from_rename_rename &&
12941291

12951292
# Create some related files now
1296-
for i in $(test_seq 1 10)
1297-
do
1298-
echo Random base content line $i
1299-
done >file_v1 &&
1293+
test_seq -f "Random base content line %d" 1 10 >file_v1 &&
13001294

13011295
cp file_v1 file_v2 &&
13021296
cp file_v1 file_v3 &&

t/t7422-submodule-output.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,7 @@ test_expect_success !MINGW 'git submodule status --recursive propagates SIGPIPE'
187187
BLOB=$(git hash-object -w --stdin <gitmodules) &&
188188
189189
printf "100644 blob $BLOB\t.gitmodules\n" >tree &&
190-
for i in $(test_seq 2000)
191-
do
192-
printf "160000 commit $COMMIT\trecursive-submodule-path-%d\n" "$i" ||
193-
return 1
194-
done >>tree &&
190+
test_seq -f "160000 commit $COMMIT\trecursive-submodule-path-%d" 2000 >>tree &&
195191
TREE=$(git mktree <tree) &&
196192
197193
COMMIT=$(git commit-tree "$TREE") &&

t/test-lib-functions.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,9 +1451,21 @@ test_cmp_fspath () {
14511451
# test_seq 1 5 -- outputs 1 2 3 4 5 one line at a time
14521452
#
14531453
# or with one argument (end), in which case it starts counting
1454-
# from 1.
1454+
# from 1. In addition to the start/end arguments, you can pass an optional
1455+
# printf format. For example:
1456+
#
1457+
# test_seq -f "line %d" 1 5
1458+
#
1459+
# would print 5 lines, "line 1" through "line 5".
14551460

14561461
test_seq () {
1462+
local fmt="%d"
1463+
case "$1" in
1464+
-f)
1465+
fmt="$2"
1466+
shift 2
1467+
;;
1468+
esac
14571469
case $# in
14581470
1) set 1 "$@" ;;
14591471
2) ;;
@@ -1462,7 +1474,7 @@ test_seq () {
14621474
test_seq_counter__=$1
14631475
while test "$test_seq_counter__" -le "$2"
14641476
do
1465-
echo "$test_seq_counter__"
1477+
printf "$fmt\n" "$test_seq_counter__"
14661478
test_seq_counter__=$(( $test_seq_counter__ + 1 ))
14671479
done
14681480
}

0 commit comments

Comments
 (0)