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>

Patrick Steinhardt committed Feb 23, 2026 at 17:00 UTC 26fc7b59cd00ee4042494b0a01afbda62c9d5b1a
8 files changed +16 -13
t/helper/test-genrandom.c
+4 -1
@@ -6,6 +6,7 @@
6
7 #include "test-tool.h"
8 #include "git-compat-util.h"
9 +#include "parse.h"
10
11 int cmd__genrandom(int argc, const char **argv)
12 {
@@ -22,7 +23,9 @@ int cmd__genrandom(int argc, const char **argv)
23 next = next * 11 + *c;
24 } while (*c++);
25
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]);
29
30 while (count--) {
31 next = next * 1103515245 + 12345;
t/t1006-cat-file.sh
+1 -1
@@ -643,7 +643,7 @@ test_expect_success 'object reference via commit text search' '
643 '
644
645 test_expect_success 'setup blobs which are likely to delta' '
646 - test-tool genrandom foo 10240 >foo &&
646 + test-tool genrandom foo 10k >foo &&
647 { cat foo && echo plus; } >foo-plus &&
648 git add foo foo-plus &&
649 git commit -m foo &&
t/t1050-large.sh
+3 -3
@@ -104,9 +104,9 @@ test_expect_success 'packsize limit' '
104 # mid1 and mid2 will fit within 256k limit but
105 # appending mid3 will bust the limit and will
106 # 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 &&
110 git add mid1 mid2 mid3 &&
111
112 count=0 &&
t/t1450-fsck.sh
+1 -1
@@ -918,7 +918,7 @@ test_expect_success 'fsck detects trailing loose garbage (large blob)' '
918 test_expect_success 'fsck detects truncated loose object' '
919 # make it big enough that we know we will truncate in the data
920 # portion, not the header
921 - test-tool genrandom truncate 4096 >file &&
921 + test-tool genrandom truncate 4k >file &&
922 blob=$(git hash-object -w file) &&
923 file=$(sha1_file $blob) &&
924 test_when_finished "remove_object $blob" &&
t/t5301-sliding-window.sh
+1 -1
@@ -12,7 +12,7 @@ test_expect_success 'setup' '
12 for i in a b c
13 do
14 echo $i >$i &&
15 - test-tool genrandom "$i" 32768 >>$i &&
15 + test-tool genrandom "$i" 32k >>$i &&
16 git update-index --add $i || return 1
17 done &&
18 echo d >d && cat c >>d && git update-index --add d &&
t/t5310-pack-bitmaps.sh
+1 -1
@@ -242,7 +242,7 @@ test_bitmap_cases () {
242 '
243
244 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 &&
246 git add rand &&
247 git commit -m "commit with big file" &&
248 git -c pack.packSizeLimit=500k repack -adb &&
t/t5710-promisor-remote-capability.sh
+2 -2
@@ -20,7 +20,7 @@ test_expect_success 'setup: create "template" repository' '
20 test_commit -C template 1 &&
21 test_commit -C template 2 &&
22 test_commit -C template 3 &&
23 - test-tool genrandom foo 10240 >template/foo &&
23 + test-tool genrandom foo 10k >template/foo &&
24 git -C template add foo &&
25 git -C template commit -m foo
26 '
@@ -376,7 +376,7 @@ test_expect_success "clone with promisor.advertise set to 'true' but don't delet
376
377 test_expect_success "setup for subsequent fetches" '
378 # Generate new commit with large blob
379 - test-tool genrandom bar 10240 >template/bar &&
379 + test-tool genrandom bar 10k >template/bar &&
380 git -C template add bar &&
381 git -C template commit -m bar &&
382
t/t7700-repack.sh
+3 -3
@@ -319,7 +319,7 @@ test_expect_success 'no bitmaps created if .keep files present' '
319
320 test_expect_success 'auto-bitmaps do not complain if unavailable' '
321 test_config -C bare.git pack.packSizeLimit 1M &&
322 - blob=$(test-tool genrandom big $((1024*1024)) |
322 + blob=$(test-tool genrandom big 1m |
323 git -C bare.git hash-object -w --stdin) &&
324 git -C bare.git update-ref refs/tags/big $blob &&
325
@@ -495,9 +495,9 @@ test_expect_success '--filter works with --max-pack-size' '
495 cd max-pack-size &&
496 test_commit base &&
497 # two blobs which exceed the maximum pack size
498 - test-tool genrandom foo 1048576 >foo &&
498 + test-tool genrandom foo 1m >foo &&
499 git hash-object -w foo &&
500 - test-tool genrandom bar 1048576 >bar &&
500 + test-tool genrandom bar 1m >bar &&
501 git hash-object -w bar &&
502 git add foo bar &&
503 git commit -m "adding foo and bar"