Skip to content

Commit 26fc7b5

Browse files
pks-tgitster
authored andcommitted
t/helper: improve "genrandom" test helper
The `test-tool genrandom` test helper can be used to generate random data, either as an infinite stream or with a specified number of bytes. The way we handle parsing the number of bytes is lacking though: - We don't have good error handling, so if the caller for example uses `test-tool genrandom 200xyz` then we'll end up generating 200 bytes of random data successfully. - Many callers want to generate e.g. 1 kilobyte or megabyte of data, but they have to either use unwieldy numbers like 1048576, or they have to precompute them. Fix both of these issues by using `git_parse_ulong()` to parse the argument. This function has better error handling, and it knows to handle unit suffixes. Adapt a couple of our tests to use suffixes instead of manual computations. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 67ad421 commit 26fc7b5

8 files changed

Lines changed: 16 additions & 13 deletions

t/helper/test-genrandom.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "test-tool.h"
88
#include "git-compat-util.h"
9+
#include "parse.h"
910

1011
int cmd__genrandom(int argc, const char **argv)
1112
{
@@ -22,7 +23,9 @@ int cmd__genrandom(int argc, const char **argv)
2223
next = next * 11 + *c;
2324
} while (*c++);
2425

25-
count = (argc == 3) ? strtoul(argv[2], NULL, 0) : ULONG_MAX;
26+
count = ULONG_MAX;
27+
if (argc == 3 && !git_parse_ulong(argv[2], &count))
28+
return error_errno("cannot parse argument '%s'", argv[2]);
2629

2730
while (count--) {
2831
next = next * 1103515245 + 12345;

t/t1006-cat-file.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ test_expect_success 'object reference via commit text search' '
643643
'
644644

645645
test_expect_success 'setup blobs which are likely to delta' '
646-
test-tool genrandom foo 10240 >foo &&
646+
test-tool genrandom foo 10k >foo &&
647647
{ cat foo && echo plus; } >foo-plus &&
648648
git add foo foo-plus &&
649649
git commit -m foo &&

t/t1050-large.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ test_expect_success 'packsize limit' '
104104
# mid1 and mid2 will fit within 256k limit but
105105
# appending mid3 will bust the limit and will
106106
# result in a separate packfile.
107-
test-tool genrandom "a" $(( 66 * 1024 )) >mid1 &&
108-
test-tool genrandom "b" $(( 80 * 1024 )) >mid2 &&
109-
test-tool genrandom "c" $(( 128 * 1024 )) >mid3 &&
107+
test-tool genrandom "a" 66k >mid1 &&
108+
test-tool genrandom "b" 80k >mid2 &&
109+
test-tool genrandom "c" 128k >mid3 &&
110110
git add mid1 mid2 mid3 &&
111111
112112
count=0 &&

t/t1450-fsck.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ test_expect_success 'fsck detects trailing loose garbage (large blob)' '
918918
test_expect_success 'fsck detects truncated loose object' '
919919
# make it big enough that we know we will truncate in the data
920920
# portion, not the header
921-
test-tool genrandom truncate 4096 >file &&
921+
test-tool genrandom truncate 4k >file &&
922922
blob=$(git hash-object -w file) &&
923923
file=$(sha1_file $blob) &&
924924
test_when_finished "remove_object $blob" &&

t/t5301-sliding-window.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test_expect_success 'setup' '
1212
for i in a b c
1313
do
1414
echo $i >$i &&
15-
test-tool genrandom "$i" 32768 >>$i &&
15+
test-tool genrandom "$i" 32k >>$i &&
1616
git update-index --add $i || return 1
1717
done &&
1818
echo d >d && cat c >>d && git update-index --add d &&

t/t5310-pack-bitmaps.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ test_bitmap_cases () {
242242
'
243243

244244
test_expect_success 'splitting packs does not generate bogus bitmaps' '
245-
test-tool genrandom foo $((1024 * 1024)) >rand &&
245+
test-tool genrandom foo 1m >rand &&
246246
git add rand &&
247247
git commit -m "commit with big file" &&
248248
git -c pack.packSizeLimit=500k repack -adb &&

t/t5710-promisor-remote-capability.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test_expect_success 'setup: create "template" repository' '
2020
test_commit -C template 1 &&
2121
test_commit -C template 2 &&
2222
test_commit -C template 3 &&
23-
test-tool genrandom foo 10240 >template/foo &&
23+
test-tool genrandom foo 10k >template/foo &&
2424
git -C template add foo &&
2525
git -C template commit -m foo
2626
'
@@ -376,7 +376,7 @@ test_expect_success "clone with promisor.advertise set to 'true' but don't delet
376376

377377
test_expect_success "setup for subsequent fetches" '
378378
# Generate new commit with large blob
379-
test-tool genrandom bar 10240 >template/bar &&
379+
test-tool genrandom bar 10k >template/bar &&
380380
git -C template add bar &&
381381
git -C template commit -m bar &&
382382

t/t7700-repack.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ test_expect_success 'no bitmaps created if .keep files present' '
319319

320320
test_expect_success 'auto-bitmaps do not complain if unavailable' '
321321
test_config -C bare.git pack.packSizeLimit 1M &&
322-
blob=$(test-tool genrandom big $((1024*1024)) |
322+
blob=$(test-tool genrandom big 1m |
323323
git -C bare.git hash-object -w --stdin) &&
324324
git -C bare.git update-ref refs/tags/big $blob &&
325325
@@ -495,9 +495,9 @@ test_expect_success '--filter works with --max-pack-size' '
495495
cd max-pack-size &&
496496
test_commit base &&
497497
# two blobs which exceed the maximum pack size
498-
test-tool genrandom foo 1048576 >foo &&
498+
test-tool genrandom foo 1m >foo &&
499499
git hash-object -w foo &&
500-
test-tool genrandom bar 1048576 >bar &&
500+
test-tool genrandom bar 1m >bar &&
501501
git hash-object -w bar &&
502502
git add foo bar &&
503503
git commit -m "adding foo and bar"

0 commit comments

Comments
 (0)