]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t/u-odb-inmemory: implement wrapper for writing objects
authorPatrick Steinhardt <ps@pks.im>
Fri, 17 Jul 2026 09:32:10 +0000 (11:32 +0200)
committerJunio C Hamano <gitster@pobox.com>
Sun, 19 Jul 2026 02:03:48 +0000 (19:03 -0700)
In the next commit we're about to change how objects are being written
into the object database source. Prepare for this refactoring by
introducing a wrapper function into our unit tests so that we don't have
to adjust all callsites.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/unit-tests/u-odb-inmemory.c

index 6844bfc37ccfdc9177c26bc8654c8b5693819cfe..2dbc3ab1df689a9035afa03230fbf6162306a940 100644 (file)
@@ -1,5 +1,6 @@
 #include "unit-test.h"
 #include "hex.h"
+#include "object-file.h"
 #include "odb/source-inmemory.h"
 #include "odb/streaming.h"
 #include "oidset.h"
@@ -36,6 +37,16 @@ static void cl_assert_object_info(struct odb_source_inmemory *source,
        free(actual_content);
 }
 
+static void cl_assert_write_object(struct odb_source_inmemory *source,
+                                  const char *content,
+                                  enum object_type type,
+                                  struct object_id *oid)
+{
+       size_t content_len = strlen(content);
+       cl_must_pass(odb_source_write_object(&source->base, content, content_len,
+                                            type, oid, NULL, 0));
+}
+
 void test_odb_inmemory__initialize(void)
 {
        odb = odb_new(&repo, "", "");
@@ -78,8 +89,7 @@ void test_odb_inmemory__read_written_object(void)
        const char data[] = "foobar";
        struct object_id written_oid;
 
-       cl_must_pass(odb_source_write_object(&source->base, data, strlen(data),
-                                            OBJ_BLOB, &written_oid, NULL, 0));
+       cl_assert_write_object(source, data, OBJ_BLOB, &written_oid);
        cl_assert_equal_s(oid_to_hex(&written_oid), FOOBAR_OID);
        cl_assert_object_info(source, &written_oid, OBJ_BLOB, "foobar");
 
@@ -94,8 +104,7 @@ void test_odb_inmemory__read_stream_object(void)
        const char data[] = "foobar";
        char buf[3] = { 0 };
 
-       cl_must_pass(odb_source_write_object(&source->base, data, strlen(data),
-                                            OBJ_BLOB, &written_oid, NULL, 0));
+       cl_assert_write_object(source, data, OBJ_BLOB, &written_oid);
 
        cl_must_pass(odb_source_read_object_stream(&stream, &source->base,
                                                   &written_oid));
@@ -141,8 +150,7 @@ void test_odb_inmemory__for_each_object(void)
                strbuf_reset(&buf);
                strbuf_addf(&buf, "%d", i);
 
-               cl_must_pass(odb_source_write_object(&source->base, buf.buf, buf.len,
-                                                    OBJ_BLOB, &written_oid, NULL, 0));
+               cl_assert_write_object(source, buf.buf, OBJ_BLOB, &written_oid);
                cl_must_pass(oidset_insert(&expected_oids, &written_oid));
        }
 
@@ -174,12 +182,9 @@ void test_odb_inmemory__for_each_object_can_abort_iteration(void)
        struct object_id written_oid;
        unsigned counter = 0;
 
-       cl_must_pass(odb_source_write_object(&source->base, "1", 1,
-                                            OBJ_BLOB, &written_oid, NULL, 0));
-       cl_must_pass(odb_source_write_object(&source->base, "2", 1,
-                                            OBJ_BLOB, &written_oid, NULL, 0));
-       cl_must_pass(odb_source_write_object(&source->base, "3", 1,
-                                            OBJ_BLOB, &written_oid, NULL, 0));
+       cl_assert_write_object(source, "1", OBJ_BLOB, &written_oid);
+       cl_assert_write_object(source, "2", OBJ_BLOB, &written_oid);
+       cl_assert_write_object(source, "3", OBJ_BLOB, &written_oid);
 
        cl_assert_equal_i(odb_source_for_each_object(&source->base, NULL,
                                                     abort_after_two_objects,
@@ -199,12 +204,9 @@ void test_odb_inmemory__count_objects(void)
        cl_must_pass(odb_source_count_objects(&source->base, 0, &count));
        cl_assert_equal_u(count, 0);
 
-       cl_must_pass(odb_source_write_object(&source->base, "1", 1,
-                                            OBJ_BLOB, &written_oid, NULL, 0));
-       cl_must_pass(odb_source_write_object(&source->base, "2", 1,
-                                            OBJ_BLOB, &written_oid, NULL, 0));
-       cl_must_pass(odb_source_write_object(&source->base, "3", 1,
-                                            OBJ_BLOB, &written_oid, NULL, 0));
+       cl_assert_write_object(source, "1", OBJ_BLOB, &written_oid);
+       cl_assert_write_object(source, "2", OBJ_BLOB, &written_oid);
+       cl_assert_write_object(source, "3", OBJ_BLOB, &written_oid);
 
        cl_must_pass(odb_source_count_objects(&source->base, 0, &count));
        cl_assert_equal_u(count, 3);
@@ -228,8 +230,7 @@ void test_odb_inmemory__find_abbrev_len(void)
         *
         * With only one blob written we expect a length of 4.
         */
-       cl_must_pass(odb_source_write_object(&source->base, "368317", strlen("368317"),
-                                            OBJ_BLOB, &oid1, NULL, 0));
+       cl_assert_write_object(source, "368317", OBJ_BLOB, &oid1);
        cl_must_pass(odb_source_find_abbrev_len(&source->base, &oid1, 4,
                                                &abbrev_len));
        cl_assert_equal_u(abbrev_len, 4);
@@ -238,8 +239,7 @@ void test_odb_inmemory__find_abbrev_len(void)
         * With both objects present, the shared 10-character prefix means we
         * need at least 11 characters to uniquely identify either object.
         */
-       cl_must_pass(odb_source_write_object(&source->base, "514796", strlen("514796"),
-                                            OBJ_BLOB, &oid2, NULL, 0));
+       cl_assert_write_object(source, "514796", OBJ_BLOB, &oid2);
        cl_must_pass(odb_source_find_abbrev_len(&source->base, &oid1, 4,
                                                &abbrev_len));
        cl_assert_equal_u(abbrev_len, 11);
@@ -257,9 +257,7 @@ void test_odb_inmemory__freshen_object(void)
        cl_must_pass(parse_oid_hex_algop(RANDOM_OID, &oid, &end, repo.hash_algo));
        cl_assert_equal_i(odb_source_freshen_object(&source->base, &oid), 0);
 
-       cl_must_pass(odb_source_write_object(&source->base, "foobar",
-                                            strlen("foobar"), OBJ_BLOB,
-                                            &written_oid, NULL, 0));
+       cl_assert_write_object(source, "foobar", OBJ_BLOB, &written_oid);
        cl_assert_equal_i(odb_source_freshen_object(&source->base,
                                                    &written_oid), 1);