]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t/helper: improve "genrandom" test helper
authorPatrick Steinhardt <ps@pks.im>
Mon, 23 Feb 2026 16:00:06 +0000 (17:00 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Feb 2026 21:19:00 +0000 (13:19 -0800)
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>
t/helper/test-genrandom.c
t/t1006-cat-file.sh
t/t1050-large.sh
t/t1450-fsck.sh
t/t5301-sliding-window.sh
t/t5310-pack-bitmaps.sh
t/t5710-promisor-remote-capability.sh
t/t7700-repack.sh

index 51b67f2f87469471a40288e369d9c784eb2ccba0..d681961abbbc37f7a0567a370d0f0f79fc64a6af 100644 (file)
@@ -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;
index 0eee3bb8781b304967f2dd1bf038b7ddf0f5e2a9..5499be8dc955599993f4a5f80bf18f0b577c8e83 100755 (executable)
@@ -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 &&
index 5be273611ad850f62a24dfccd5aa89b0742c7e16..7d40d0852166b50b7a26769fba4ea6b6776d509e 100755 (executable)
@@ -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 &&
index 3fae05f9d9f805d86d04ac3ecebd60b16cce80f6..8fb79b3e5d6ed4282209106e63916311e1e3e199 100755 (executable)
@@ -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" &&
index ff6b5159a312bb307f0c2afbcfa707a414e16f9c..3c3666b2781946011f6171630f8c2a08cd8c0a72 100755 (executable)
@@ -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 &&
index 6718fb98c0574fd6158a54ae830ffca0caf83188..3e3366f57d68a6bdb815a6fc3037560f09171fce 100755 (executable)
@@ -242,7 +242,7 @@ test_bitmap_cases () {
        '
 
        test_expect_success 'splitting packs does not generate bogus bitmaps' '
-               test-tool genrandom foo $((1024 * 1024)) >rand &&
+               test-tool genrandom foo 1m >rand &&
                git add rand &&
                git commit -m "commit with big file" &&
                git -c pack.packSizeLimit=500k repack -adb &&
index 023735d6a84ea8158896dafc132c43af6f4c38eb..66af84cd56d35c0747ca68a52e1c43b3a1e7165e 100755 (executable)
@@ -20,7 +20,7 @@ test_expect_success 'setup: create "template" repository' '
        test_commit -C template 1 &&
        test_commit -C template 2 &&
        test_commit -C template 3 &&
-       test-tool genrandom foo 10240 >template/foo &&
+       test-tool genrandom foo 10k >template/foo &&
        git -C template add foo &&
        git -C template commit -m foo
 '
@@ -376,7 +376,7 @@ test_expect_success "clone with promisor.advertise set to 'true' but don't delet
 
 test_expect_success "setup for subsequent fetches" '
        # Generate new commit with large blob
-       test-tool genrandom bar 10240 >template/bar &&
+       test-tool genrandom bar 10k >template/bar &&
        git -C template add bar &&
        git -C template commit -m bar &&
 
index 73b78bdd887d80706e2f65dc9c3c25cb81027c71..439ab24d2390f75534a772be8cbde5a201b1acee 100755 (executable)
@@ -319,7 +319,7 @@ test_expect_success 'no bitmaps created if .keep files present' '
 
 test_expect_success 'auto-bitmaps do not complain if unavailable' '
        test_config -C bare.git pack.packSizeLimit 1M &&
-       blob=$(test-tool genrandom big $((1024*1024)) |
+       blob=$(test-tool genrandom big 1m |
               git -C bare.git hash-object -w --stdin) &&
        git -C bare.git update-ref refs/tags/big $blob &&
 
@@ -495,9 +495,9 @@ test_expect_success '--filter works with --max-pack-size' '
                cd max-pack-size &&
                test_commit base &&
                # two blobs which exceed the maximum pack size
-               test-tool genrandom foo 1048576 >foo &&
+               test-tool genrandom foo 1m >foo &&
                git hash-object -w foo &&
-               test-tool genrandom bar 1048576 >bar &&
+               test-tool genrandom bar 1m >bar &&
                git hash-object -w bar &&
                git add foo bar &&
                git commit -m "adding foo and bar"