t/unit-tests: implement clar specific reftable test helper functions

Helper functions defined in `t/unit-tests/lib-reftable.{c,h}` are required for the reftable-related test files to run. In the current implementation these functions are designed to conform with our homegrown unit-testing structure. So in other to convert the reftable test files, there is need for a clar specific implementation of these helper functions. Implement equivalent helper functions in `lib-reftable-clar.{c,h}` to use clar. These functions conform with the clar testing framework and become available for all reftable-related test files implemented using the clar testing framework, which requires them. This will be used by subsequent commits. Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Seyi Kuforiji committed Jul 24, 2025 at 15:28 UTC 5dd5c4e34561ccbc982512c27926a21ddca5e1e5
4 files changed +124 -1
Makefile
+1
@@ -1375,6 +1375,7 @@ CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
1375 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
1376 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
1377 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
1378 +CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-reftable-clar.o
1379
1380 UNIT_TEST_PROGRAMS += t-reftable-basics
1381 UNIT_TEST_PROGRAMS += t-reftable-block
t/meson.build
+2 -1
@@ -19,7 +19,8 @@ clar_test_suites = [
19 clar_sources = [
20 'unit-tests/clar/clar.c',
21 'unit-tests/unit-test.c',
22 - 'unit-tests/lib-oid.c'
22 + 'unit-tests/lib-oid.c',
23 + 'unit-tests/lib-reftable-clar.c'
24 ]
25
26 clar_decls_h = custom_target(
t/unit-tests/lib-reftable-clar.c new
+101
@@ -0,0 +1,101 @@
1 +#include "unit-test.h"
2 +#include "lib-reftable-clar.h"
3 +#include "hex.h"
4 +#include "parse-options.h"
5 +#include "reftable/constants.h"
6 +#include "reftable/writer.h"
7 +#include "strbuf.h"
8 +#include "string-list.h"
9 +#include "strvec.h"
10 +
11 +void cl_reftable_set_hash(uint8_t *p, int i, enum reftable_hash id)
12 +{
13 + memset(p, (uint8_t)i, hash_size(id));
14 +}
15 +
16 +static ssize_t strbuf_writer_write(void *b, const void *data, size_t sz)
17 +{
18 + strbuf_add(b, data, sz);
19 + return sz;
20 +}
21 +
22 +static int strbuf_writer_flush(void *arg UNUSED)
23 +{
24 + return 0;
25 +}
26 +
27 +struct reftable_writer *cl_reftable_strbuf_writer(struct reftable_buf *buf,
28 + struct reftable_write_options *opts)
29 +{
30 + struct reftable_writer *writer;
31 + int ret = reftable_writer_new(&writer, &strbuf_writer_write, &strbuf_writer_flush,
32 + buf, opts);
33 + cl_assert(!ret);
34 + return writer;
35 +}
36 +
37 +void cl_reftable_write_to_buf(struct reftable_buf *buf,
38 + struct reftable_ref_record *refs,
39 + size_t nrefs,
40 + struct reftable_log_record *logs,
41 + size_t nlogs,
42 + struct reftable_write_options *_opts)
43 +{
44 + struct reftable_write_options opts = { 0 };
45 + const struct reftable_stats *stats;
46 + struct reftable_writer *writer;
47 + uint64_t min = 0xffffffff;
48 + uint64_t max = 0;
49 + int ret;
50 +
51 + if (_opts)
52 + opts = *_opts;
53 +
54 + for (size_t i = 0; i < nrefs; i++) {
55 + uint64_t ui = refs[i].update_index;
56 + if (ui > max)
57 + max = ui;
58 + if (ui < min)
59 + min = ui;
60 + }
61 + for (size_t i = 0; i < nlogs; i++) {
62 + uint64_t ui = logs[i].update_index;
63 + if (ui > max)
64 + max = ui;
65 + if (ui < min)
66 + min = ui;
67 + }
68 +
69 + writer = cl_reftable_strbuf_writer(buf, &opts);
70 + ret = reftable_writer_set_limits(writer, min, max);
71 + cl_assert_equal_i(ret, 0);
72 +
73 + if (nrefs) {
74 + ret = reftable_writer_add_refs(writer, refs, nrefs);
75 + cl_assert_equal_i(ret, 0);
76 + }
77 +
78 + if (nlogs) {
79 + ret = reftable_writer_add_logs(writer, logs, nlogs);
80 + cl_assert_equal_i(ret, 0);
81 + }
82 +
83 + ret = reftable_writer_close(writer);
84 + cl_assert_equal_i(ret, 0);
85 +
86 + stats = reftable_writer_stats(writer);
87 + for (size_t i = 0; i < (size_t)stats->ref_stats.blocks; i++) {
88 + size_t off = i * (opts.block_size ? opts.block_size
89 + : DEFAULT_BLOCK_SIZE);
90 + if (!off)
91 + off = header_size(opts.hash_id == REFTABLE_HASH_SHA256 ? 2 : 1);
92 + cl_assert(buf->buf[off] == 'r');
93 + }
94 +
95 + if (nrefs)
96 + cl_assert(stats->ref_stats.blocks > 0);
97 + if (nlogs)
98 + cl_assert(stats->log_stats.blocks > 0);
99 +
100 + reftable_writer_free(writer);
101 +}
t/unit-tests/lib-reftable-clar.h new
+20
@@ -0,0 +1,20 @@
1 +#include "git-compat-util.h"
2 +#include "clar/clar.h"
3 +#include "clar-decls.h"
4 +#include "git-compat-util.h"
5 +#include "reftable/reftable-writer.h"
6 +#include "strbuf.h"
7 +
8 +struct reftable_buf;
9 +
10 +void cl_reftable_set_hash(uint8_t *p, int i, enum reftable_hash id);
11 +
12 +struct reftable_writer *cl_reftable_strbuf_writer(struct reftable_buf *buf,
13 + struct reftable_write_options *opts);
14 +
15 +void cl_reftable_write_to_buf(struct reftable_buf *buf,
16 + struct reftable_ref_record *refs,
17 + size_t nrecords,
18 + struct reftable_log_record *logs,
19 + size_t nlogs,
20 + struct reftable_write_options *opts);