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 committedFeb 23, 2026 at 17:00 UTC26fc7b59cd00ee4042494b0a01afbda62c9d5b1a
8 files changed+16-13
t/helper/test-genrandom.c
+4-1
index 51b67f2f87..d681961abb 100644--- a/t/helper/test-genrandom.c+++ b/t/helper/test-genrandom.c@@ -6,6 +6,7 @@ #include "test-tool.h" #include "git-compat-util.h"+#include "parse.h" int cmd__genrandom(int argc, const char **argv) {@@ -22,7 +23,9 @@ int cmd__genrandom(int argc, const char **argv) next = next * 11 + *c; } while (*c++);- count = (argc == 3) ? strtoul(argv[2], NULL, 0) : ULONG_MAX;+ count = ULONG_MAX;+ if (argc == 3 && !git_parse_ulong(argv[2], &count))+ return error_errno("cannot parse argument '%s'", argv[2]); while (count--) { next = next * 1103515245 + 12345;
t/t1006-cat-file.sh
+1-1
index 0eee3bb878..5499be8dc9 100755--- a/t/t1006-cat-file.sh+++ b/t/t1006-cat-file.sh@@ -643,7 +643,7 @@ test_expect_success 'object reference via commit text search' ' ' test_expect_success 'setup blobs which are likely to delta' '- test-tool genrandom foo 10240 >foo &&+ test-tool genrandom foo 10k >foo && { cat foo && echo plus; } >foo-plus && git add foo foo-plus && git commit -m foo &&
t/t1050-large.sh
+3-3
index 5be273611a..7d40d08521 100755--- a/t/t1050-large.sh+++ b/t/t1050-large.sh@@ -104,9 +104,9 @@ test_expect_success 'packsize limit' ' # mid1 and mid2 will fit within 256k limit but # appending mid3 will bust the limit and will # result in a separate packfile.- test-tool genrandom "a" $(( 66 * 1024 )) >mid1 &&- test-tool genrandom "b" $(( 80 * 1024 )) >mid2 &&- test-tool genrandom "c" $(( 128 * 1024 )) >mid3 &&+ test-tool genrandom "a" 66k >mid1 &&+ test-tool genrandom "b" 80k >mid2 &&+ test-tool genrandom "c" 128k >mid3 && git add mid1 mid2 mid3 && count=0 &&
t/t1450-fsck.sh
+1-1
index 3fae05f9d9..8fb79b3e5d 100755--- a/t/t1450-fsck.sh+++ b/t/t1450-fsck.sh@@ -918,7 +918,7 @@ test_expect_success 'fsck detects trailing loose garbage (large blob)' ' test_expect_success 'fsck detects truncated loose object' ' # make it big enough that we know we will truncate in the data # portion, not the header- test-tool genrandom truncate 4096 >file &&+ test-tool genrandom truncate 4k >file && blob=$(git hash-object -w file) && file=$(sha1_file $blob) && test_when_finished "remove_object $blob" &&
t/t5301-sliding-window.sh
+1-1
index ff6b5159a3..3c3666b278 100755--- a/t/t5301-sliding-window.sh+++ b/t/t5301-sliding-window.sh@@ -12,7 +12,7 @@ test_expect_success 'setup' ' for i in a b c do echo $i >$i &&- test-tool genrandom "$i" 32768 >>$i &&+ test-tool genrandom "$i" 32k >>$i && git update-index --add $i || return 1 done && echo d >d && cat c >>d && git update-index --add d &&