Skip to content

Commit a4f50bb

Browse files
pks-tgitster
authored andcommitted
t/unit-tests: introduce reftable library
We have recently migrated all of the reftable unit tests that were part of the reftable library into our own unit testing framework. As part of that migration we have duplicated some of the functionality that was part of the reftable test framework into each of the migrated test suites. This was a sensible decision to not have all of the migrations dependent on each other, but now that the migration is done it makes sense to deduplicate the functionality again. Introduce a new reftable test library that hosts some shared code and adapt tests to use it. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 428672a commit a4f50bb

File tree

6 files changed

+177
-179
lines changed

6 files changed

+177
-179
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,7 @@ UNIT_TEST_PROGRAMS += t-urlmatch-normalization
13581358
UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
13591359
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
13601360
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
1361+
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-reftable.o
13611362

13621363
# xdiff and reftable libs may in turn depend on what is in libgit.a
13631364
GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(LIB_FILE)

t/unit-tests/lib-reftable.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include "lib-reftable.h"
2+
#include "test-lib.h"
3+
#include "reftable/constants.h"
4+
#include "reftable/writer.h"
5+
6+
void t_reftable_set_hash(uint8_t *p, int i, uint32_t id)
7+
{
8+
memset(p, (uint8_t)i, hash_size(id));
9+
}
10+
11+
static ssize_t strbuf_writer_write(void *b, const void *data, size_t sz)
12+
{
13+
strbuf_add(b, data, sz);
14+
return sz;
15+
}
16+
17+
static int strbuf_writer_flush(void *arg UNUSED)
18+
{
19+
return 0;
20+
}
21+
22+
struct reftable_writer *t_reftable_strbuf_writer(struct strbuf *buf,
23+
struct reftable_write_options *opts)
24+
{
25+
return reftable_new_writer(&strbuf_writer_write,
26+
&strbuf_writer_flush,
27+
buf, opts);
28+
}
29+
30+
void t_reftable_write_to_buf(struct strbuf *buf,
31+
struct reftable_ref_record *refs,
32+
size_t nrefs,
33+
struct reftable_log_record *logs,
34+
size_t nlogs,
35+
struct reftable_write_options *_opts)
36+
{
37+
struct reftable_write_options opts = { 0 };
38+
const struct reftable_stats *stats;
39+
struct reftable_writer *writer;
40+
uint64_t min = 0xffffffff;
41+
uint64_t max = 0;
42+
int ret;
43+
44+
if (_opts)
45+
opts = *_opts;
46+
47+
for (size_t i = 0; i < nrefs; i++) {
48+
uint64_t ui = refs[i].update_index;
49+
if (ui > max)
50+
max = ui;
51+
if (ui < min)
52+
min = ui;
53+
}
54+
for (size_t i = 0; i < nlogs; i++) {
55+
uint64_t ui = logs[i].update_index;
56+
if (ui > max)
57+
max = ui;
58+
if (ui < min)
59+
min = ui;
60+
}
61+
62+
writer = t_reftable_strbuf_writer(buf, &opts);
63+
reftable_writer_set_limits(writer, min, max);
64+
65+
if (nrefs) {
66+
ret = reftable_writer_add_refs(writer, refs, nrefs);
67+
check_int(ret, ==, 0);
68+
}
69+
70+
if (nlogs) {
71+
ret = reftable_writer_add_logs(writer, logs, nlogs);
72+
check_int(ret, ==, 0);
73+
}
74+
75+
ret = reftable_writer_close(writer);
76+
check_int(ret, ==, 0);
77+
78+
stats = reftable_writer_stats(writer);
79+
for (size_t i = 0; i < stats->ref_stats.blocks; i++) {
80+
size_t off = i * (opts.block_size ? opts.block_size
81+
: DEFAULT_BLOCK_SIZE);
82+
if (!off)
83+
off = header_size(opts.hash_id == GIT_SHA256_FORMAT_ID ? 2 : 1);
84+
check_char(buf->buf[off], ==, 'r');
85+
}
86+
87+
if (nrefs)
88+
check_int(stats->ref_stats.blocks, >, 0);
89+
if (nlogs)
90+
check_int(stats->log_stats.blocks, >, 0);
91+
92+
reftable_writer_free(writer);
93+
}

t/unit-tests/lib-reftable.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef LIB_REFTABLE_H
2+
#define LIB_REFTABLE_H
3+
4+
#include "git-compat-util.h"
5+
#include "strbuf.h"
6+
#include "reftable/reftable-writer.h"
7+
8+
void t_reftable_set_hash(uint8_t *p, int i, uint32_t id);
9+
10+
struct reftable_writer *t_reftable_strbuf_writer(struct strbuf *buf,
11+
struct reftable_write_options *opts);
12+
13+
void t_reftable_write_to_buf(struct strbuf *buf,
14+
struct reftable_ref_record *refs,
15+
size_t nrecords,
16+
struct reftable_log_record *logs,
17+
size_t nlogs,
18+
struct reftable_write_options *opts);
19+
20+
#endif

t/unit-tests/t-reftable-merged.c

Lines changed: 11 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ license that can be found in the LICENSE file or at
77
*/
88

99
#include "test-lib.h"
10+
#include "lib-reftable.h"
1011
#include "reftable/blocksource.h"
1112
#include "reftable/constants.h"
1213
#include "reftable/merged.h"
@@ -15,91 +16,23 @@ license that can be found in the LICENSE file or at
1516
#include "reftable/reftable-merged.h"
1617
#include "reftable/reftable-writer.h"
1718

18-
static ssize_t strbuf_add_void(void *b, const void *data, const size_t sz)
19-
{
20-
strbuf_add(b, data, sz);
21-
return sz;
22-
}
23-
24-
static int noop_flush(void *arg UNUSED)
25-
{
26-
return 0;
27-
}
28-
29-
static void write_test_table(struct strbuf *buf,
30-
struct reftable_ref_record refs[], const size_t n)
31-
{
32-
uint64_t min = 0xffffffff;
33-
uint64_t max = 0;
34-
size_t i;
35-
int err;
36-
37-
struct reftable_write_options opts = {
38-
.block_size = 256,
39-
};
40-
struct reftable_writer *w = NULL;
41-
for (i = 0; i < n; i++) {
42-
uint64_t ui = refs[i].update_index;
43-
if (ui > max)
44-
max = ui;
45-
if (ui < min)
46-
min = ui;
47-
}
48-
49-
w = reftable_new_writer(&strbuf_add_void, &noop_flush, buf, &opts);
50-
reftable_writer_set_limits(w, min, max);
51-
52-
for (i = 0; i < n; i++) {
53-
uint64_t before = refs[i].update_index;
54-
int n = reftable_writer_add_ref(w, &refs[i]);
55-
check_int(n, ==, 0);
56-
check_int(before, ==, refs[i].update_index);
57-
}
58-
59-
err = reftable_writer_close(w);
60-
check(!err);
61-
62-
reftable_writer_free(w);
63-
}
64-
65-
static void write_test_log_table(struct strbuf *buf, struct reftable_log_record logs[],
66-
const size_t n, const uint64_t update_index)
67-
{
68-
int err;
69-
70-
struct reftable_write_options opts = {
71-
.block_size = 256,
72-
.exact_log_message = 1,
73-
};
74-
struct reftable_writer *w = NULL;
75-
w = reftable_new_writer(&strbuf_add_void, &noop_flush, buf, &opts);
76-
reftable_writer_set_limits(w, update_index, update_index);
77-
78-
for (size_t i = 0; i < n; i++) {
79-
int err = reftable_writer_add_log(w, &logs[i]);
80-
check(!err);
81-
}
82-
83-
err = reftable_writer_close(w);
84-
check(!err);
85-
86-
reftable_writer_free(w);
87-
}
88-
8919
static struct reftable_merged_table *
9020
merged_table_from_records(struct reftable_ref_record **refs,
9121
struct reftable_block_source **source,
9222
struct reftable_reader ***readers, const size_t *sizes,
9323
struct strbuf *buf, const size_t n)
9424
{
9525
struct reftable_merged_table *mt = NULL;
26+
struct reftable_write_options opts = {
27+
.block_size = 256,
28+
};
9629
int err;
9730

9831
REFTABLE_CALLOC_ARRAY(*readers, n);
9932
REFTABLE_CALLOC_ARRAY(*source, n);
10033

10134
for (size_t i = 0; i < n; i++) {
102-
write_test_table(&buf[i], refs[i], sizes[i]);
35+
t_reftable_write_to_buf(&buf[i], refs[i], sizes[i], NULL, 0, &opts);
10336
block_source_from_strbuf(&(*source)[i], &buf[i]);
10437

10538
err = reftable_reader_new(&(*readers)[i], &(*source)[i],
@@ -268,13 +201,17 @@ merged_table_from_log_records(struct reftable_log_record **logs,
268201
struct strbuf *buf, const size_t n)
269202
{
270203
struct reftable_merged_table *mt = NULL;
204+
struct reftable_write_options opts = {
205+
.block_size = 256,
206+
.exact_log_message = 1,
207+
};
271208
int err;
272209

273210
REFTABLE_CALLOC_ARRAY(*readers, n);
274211
REFTABLE_CALLOC_ARRAY(*source, n);
275212

276213
for (size_t i = 0; i < n; i++) {
277-
write_test_log_table(&buf[i], logs[i], sizes[i], i + 1);
214+
t_reftable_write_to_buf(&buf[i], NULL, 0, logs[i], sizes[i], &opts);
278215
block_source_from_strbuf(&(*source)[i], &buf[i]);
279216

280217
err = reftable_reader_new(&(*readers)[i], &(*source)[i],
@@ -402,9 +339,7 @@ static void t_default_write_opts(void)
402339
{
403340
struct reftable_write_options opts = { 0 };
404341
struct strbuf buf = STRBUF_INIT;
405-
struct reftable_writer *w =
406-
reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
407-
342+
struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
408343
struct reftable_ref_record rec = {
409344
.refname = (char *) "master",
410345
.update_index = 1,

0 commit comments

Comments
 (0)