t/u-odb-inmemory: implement wrapper for writing objects
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>
Patrick Steinhardt committed
Jul 17, 2026 at 11:32 UTC
cb5192ea9ad42f6f7aa539496a5c59d76b8eaf0f
1 file changed
+23
-25
t/unit-tests/u-odb-inmemory.c
+23
-25
@@ -1,5 +1,6 @@
1
#include "unit-test.h"
2
#include "hex.h"
3
+#include "object-file.h"
4
#include "odb/source-inmemory.h"
5
#include "odb/streaming.h"
6
#include "oidset.h"
@@ -36,6 +37,16 @@ static void cl_assert_object_info(struct odb_source_inmemory *source,
37
free(actual_content);
38
}
39
40
+static void cl_assert_write_object(struct odb_source_inmemory *source,
41
+ const char *content,
42
+ enum object_type type,
43
+ struct object_id *oid)
44
+{
45
+ size_t content_len = strlen(content);
46
+ cl_must_pass(odb_source_write_object(&source->base, content, content_len,
47
+ type, oid, NULL, 0));
48
+}
49
+
50
void test_odb_inmemory__initialize(void)
51
{
52
odb = odb_new(&repo, "", "");
@@ -78,8 +89,7 @@ void test_odb_inmemory__read_written_object(void)
89
const char data[] = "foobar";
90
struct object_id written_oid;
91
81
- cl_must_pass(odb_source_write_object(&source->base, data, strlen(data),
82
- OBJ_BLOB, &written_oid, NULL, 0));
92
+ cl_assert_write_object(source, data, OBJ_BLOB, &written_oid);
93
cl_assert_equal_s(oid_to_hex(&written_oid), FOOBAR_OID);
94
cl_assert_object_info(source, &written_oid, OBJ_BLOB, "foobar");
95
@@ -94,8 +104,7 @@ void test_odb_inmemory__read_stream_object(void)
104
const char data[] = "foobar";
105
char buf[3] = { 0 };
106
97
- cl_must_pass(odb_source_write_object(&source->base, data, strlen(data),
98
- OBJ_BLOB, &written_oid, NULL, 0));
107
+ cl_assert_write_object(source, data, OBJ_BLOB, &written_oid);
108
109
cl_must_pass(odb_source_read_object_stream(&stream, &source->base,
110
&written_oid));
@@ -141,8 +150,7 @@ void test_odb_inmemory__for_each_object(void)
150
strbuf_reset(&buf);
151
strbuf_addf(&buf, "%d", i);
152
144
- cl_must_pass(odb_source_write_object(&source->base, buf.buf, buf.len,
145
- OBJ_BLOB, &written_oid, NULL, 0));
153
+ cl_assert_write_object(source, buf.buf, OBJ_BLOB, &written_oid);
154
cl_must_pass(oidset_insert(&expected_oids, &written_oid));
155
}
156
@@ -174,12 +182,9 @@ void test_odb_inmemory__for_each_object_can_abort_iteration(void)
182
struct object_id written_oid;
183
unsigned counter = 0;
184
177
- cl_must_pass(odb_source_write_object(&source->base, "1", 1,
178
- OBJ_BLOB, &written_oid, NULL, 0));
179
- cl_must_pass(odb_source_write_object(&source->base, "2", 1,
180
- OBJ_BLOB, &written_oid, NULL, 0));
181
- cl_must_pass(odb_source_write_object(&source->base, "3", 1,
182
- OBJ_BLOB, &written_oid, NULL, 0));
185
+ cl_assert_write_object(source, "1", OBJ_BLOB, &written_oid);
186
+ cl_assert_write_object(source, "2", OBJ_BLOB, &written_oid);
187
+ cl_assert_write_object(source, "3", OBJ_BLOB, &written_oid);
188
189
cl_assert_equal_i(odb_source_for_each_object(&source->base, NULL,
190
abort_after_two_objects,
@@ -199,12 +204,9 @@ void test_odb_inmemory__count_objects(void)
204
cl_must_pass(odb_source_count_objects(&source->base, 0, &count));
205
cl_assert_equal_u(count, 0);
206
202
- cl_must_pass(odb_source_write_object(&source->base, "1", 1,
203
- OBJ_BLOB, &written_oid, NULL, 0));
204
- cl_must_pass(odb_source_write_object(&source->base, "2", 1,
205
- OBJ_BLOB, &written_oid, NULL, 0));
206
- cl_must_pass(odb_source_write_object(&source->base, "3", 1,
207
- OBJ_BLOB, &written_oid, NULL, 0));
207
+ cl_assert_write_object(source, "1", OBJ_BLOB, &written_oid);
208
+ cl_assert_write_object(source, "2", OBJ_BLOB, &written_oid);
209
+ cl_assert_write_object(source, "3", OBJ_BLOB, &written_oid);
210
211
cl_must_pass(odb_source_count_objects(&source->base, 0, &count));
212
cl_assert_equal_u(count, 3);
@@ -228,8 +230,7 @@ void test_odb_inmemory__find_abbrev_len(void)
230
*
231
* With only one blob written we expect a length of 4.
232
*/
231
- cl_must_pass(odb_source_write_object(&source->base, "368317", strlen("368317"),
232
- OBJ_BLOB, &oid1, NULL, 0));
233
+ cl_assert_write_object(source, "368317", OBJ_BLOB, &oid1);
234
cl_must_pass(odb_source_find_abbrev_len(&source->base, &oid1, 4,
235
&abbrev_len));
236
cl_assert_equal_u(abbrev_len, 4);
@@ -238,8 +239,7 @@ void test_odb_inmemory__find_abbrev_len(void)
239
* With both objects present, the shared 10-character prefix means we
240
* need at least 11 characters to uniquely identify either object.
241
*/
241
- cl_must_pass(odb_source_write_object(&source->base, "514796", strlen("514796"),
242
- OBJ_BLOB, &oid2, NULL, 0));
242
+ cl_assert_write_object(source, "514796", OBJ_BLOB, &oid2);
243
cl_must_pass(odb_source_find_abbrev_len(&source->base, &oid1, 4,
244
&abbrev_len));
245
cl_assert_equal_u(abbrev_len, 11);
@@ -257,9 +257,7 @@ void test_odb_inmemory__freshen_object(void)
257
cl_must_pass(parse_oid_hex_algop(RANDOM_OID, &oid, &end, repo.hash_algo));
258
cl_assert_equal_i(odb_source_freshen_object(&source->base, &oid), 0);
259
260
- cl_must_pass(odb_source_write_object(&source->base, "foobar",
261
- strlen("foobar"), OBJ_BLOB,
262
- &written_oid, NULL, 0));
260
+ cl_assert_write_object(source, "foobar", OBJ_BLOB, &written_oid);
261
cl_assert_equal_i(odb_source_freshen_object(&source->base,
262
&written_oid), 1);
263