t/unit-tests: implement clar specific oid helper functions

`get_oid_arbitrary_hex()` and `init_hash_algo()` are both required for oid-related tests to run without errors. In the current implementation, both functions are defined and declared in the `t/unit-tests/lib-oid.{c,h}` which is utilized by oid-related tests in the homegrown unit tests structure. Adapt functions in lib-oid.{c,h} to use clar. Both these functions become available for oid-related test files implemented using the clar testing framework, which requires them. This will be used by subsequent commits. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Seyi Kuforiji committed Feb 25, 2025 at 11:10 UTC a16a2ee312b088ae6ac2ab3302ed191d19a71718
5 files changed +21 -26
Makefile
+1 -1
@@ -1365,6 +1365,7 @@ CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
1365 CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
1366 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
1367 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
1368 +CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
1369
1370 UNIT_TEST_PROGRAMS += t-oid-array
1371 UNIT_TEST_PROGRAMS += t-oidmap
@@ -1381,7 +1382,6 @@ UNIT_TEST_PROGRAMS += t-trailer
1382 UNIT_TEST_PROGRAMS += t-urlmatch-normalization
1383 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
1384 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
1384 -UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
1385 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-reftable.o
1386
1387 # xdiff and reftable libs may in turn depend on what is in libgit.a
t/meson.build
+1 -1
@@ -14,6 +14,7 @@ clar_test_suites = [
14 clar_sources = [
15 'unit-tests/clar/clar.c',
16 'unit-tests/unit-test.c',
17 + 'unit-tests/lib-oid.c'
18 ]
19
20 clar_decls_h = custom_target(
@@ -68,7 +69,6 @@ foreach unit_test_program : unit_test_programs
69 unit_test = executable(unit_test_name,
70 sources: [
71 'unit-tests/test-lib.c',
71 - 'unit-tests/lib-oid.c',
72 'unit-tests/lib-reftable.c',
73 unit_test_program,
74 ],
t/unit-tests/lib-oid.c
+11 -21
@@ -1,9 +1,9 @@
1 -#include "test-lib.h"
1 +#include "unit-test.h"
2 #include "lib-oid.h"
3 #include "strbuf.h"
4 #include "hex.h"
5
6 -int init_hash_algo(void)
6 +int cl_setup_hash_algo(void)
7 {
8 static int algo = -1;
9
@@ -11,42 +11,32 @@ int init_hash_algo(void)
11 const char *algo_name = getenv("GIT_TEST_DEFAULT_HASH");
12 algo = algo_name ? hash_algo_by_name(algo_name) : GIT_HASH_SHA1;
13
14 - if (!check(algo != GIT_HASH_UNKNOWN))
15 - test_msg("BUG: invalid GIT_TEST_DEFAULT_HASH value ('%s')",
16 - algo_name);
14 + cl_assert(algo != GIT_HASH_UNKNOWN);
15 }
16 return algo;
17 }
18
21 -static int get_oid_arbitrary_hex_algop(const char *hex, struct object_id *oid,
19 +static void cl_parse_oid(const char *hex, struct object_id *oid,
20 const struct git_hash_algo *algop)
21 {
24 - int ret;
22 size_t sz = strlen(hex);
23 struct strbuf buf = STRBUF_INIT;
24
28 - if (!check(sz <= algop->hexsz)) {
29 - test_msg("BUG: hex string (%s) bigger than maximum allowed (%lu)",
30 - hex, (unsigned long)algop->hexsz);
31 - return -1;
32 - }
25 + cl_assert(sz <= algop->hexsz);
26
27 strbuf_add(&buf, hex, sz);
28 strbuf_addchars(&buf, '0', algop->hexsz - sz);
29
37 - ret = get_oid_hex_algop(buf.buf, oid, algop);
38 - if (!check_int(ret, ==, 0))
39 - test_msg("BUG: invalid hex input (%s) provided", hex);
30 + cl_assert_equal_i(get_oid_hex_algop(buf.buf, oid, algop), 0);
31
32 strbuf_release(&buf);
42 - return ret;
33 }
34
45 -int get_oid_arbitrary_hex(const char *hex, struct object_id *oid)
35 +
36 +void cl_parse_any_oid(const char *hex, struct object_id *oid)
37 {
47 - int hash_algo = init_hash_algo();
38 + int hash_algo = cl_setup_hash_algo();
39
49 - if (!check_int(hash_algo, !=, GIT_HASH_UNKNOWN))
50 - return -1;
51 - return get_oid_arbitrary_hex_algop(hex, oid, &hash_algos[hash_algo]);
40 + cl_assert(hash_algo != GIT_HASH_UNKNOWN);
41 + cl_parse_oid(hex, oid, &hash_algos[hash_algo]);
42 }
t/unit-tests/lib-oid.h
+6 -3
@@ -5,6 +5,7 @@
5
6 /*
7 * Convert arbitrary hex string to object_id.
8 + *
9 * For example, passing "abc12" will generate
10 * "abc1200000000000000000000000000000000000" hex of length 40 for SHA-1 and
11 * create object_id with that.
@@ -12,14 +13,16 @@
13 * algo is not allowed. The hash algo is decided based on GIT_TEST_DEFAULT_HASH
14 * environment variable.
15 */
15 -int get_oid_arbitrary_hex(const char *s, struct object_id *oid);
16 +
17 +void cl_parse_any_oid (const char *s, struct object_id *oid);
18 /*
19 * Returns one of GIT_HASH_{SHA1, SHA256, UNKNOWN} based on the value of
20 * GIT_TEST_DEFAULT_HASH environment variable. The fallback value in the
21 * absence of GIT_TEST_DEFAULT_HASH is GIT_HASH_SHA1. It also uses
20 - * check(algo != GIT_HASH_UNKNOWN) before returning to verify if the
22 + * cl_assert(algo != GIT_HASH_UNKNOWN) before returning to verify if the
23 * GIT_TEST_DEFAULT_HASH's value is valid or not.
24 */
23 -int init_hash_algo(void);
25 +
26 +int cl_setup_hash_algo(void);
27
28 #endif /* LIB_OID_H */
t/unit-tests/unit-test.c
+2
@@ -1,5 +1,7 @@
1 #include "unit-test.h"
2 +#include "hex.h"
3 #include "parse-options.h"
4 +#include "strbuf.h"
5 #include "string-list.h"
6 #include "strvec.h"
7