Skip to content

Commit a16a2ee

Browse files
Seyi007gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Seyi Kuforiji <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2d2a71c commit a16a2ee

File tree

5 files changed

+21
-26
lines changed

5 files changed

+21
-26
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,7 @@ CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
13651365
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
13661366
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
13671367
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
1368+
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
13681369

13691370
UNIT_TEST_PROGRAMS += t-oid-array
13701371
UNIT_TEST_PROGRAMS += t-oidmap
@@ -1381,7 +1382,6 @@ UNIT_TEST_PROGRAMS += t-trailer
13811382
UNIT_TEST_PROGRAMS += t-urlmatch-normalization
13821383
UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
13831384
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
1384-
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
13851385
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-reftable.o
13861386

13871387
# xdiff and reftable libs may in turn depend on what is in libgit.a

t/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ clar_test_suites = [
1414
clar_sources = [
1515
'unit-tests/clar/clar.c',
1616
'unit-tests/unit-test.c',
17+
'unit-tests/lib-oid.c'
1718
]
1819

1920
clar_decls_h = custom_target(
@@ -68,7 +69,6 @@ foreach unit_test_program : unit_test_programs
6869
unit_test = executable(unit_test_name,
6970
sources: [
7071
'unit-tests/test-lib.c',
71-
'unit-tests/lib-oid.c',
7272
'unit-tests/lib-reftable.c',
7373
unit_test_program,
7474
],

t/unit-tests/lib-oid.c

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,42 @@
1-
#include "test-lib.h"
1+
#include "unit-test.h"
22
#include "lib-oid.h"
33
#include "strbuf.h"
44
#include "hex.h"
55

6-
int init_hash_algo(void)
6+
int cl_setup_hash_algo(void)
77
{
88
static int algo = -1;
99

1010
if (algo < 0) {
1111
const char *algo_name = getenv("GIT_TEST_DEFAULT_HASH");
1212
algo = algo_name ? hash_algo_by_name(algo_name) : GIT_HASH_SHA1;
1313

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);
1715
}
1816
return algo;
1917
}
2018

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,
2220
const struct git_hash_algo *algop)
2321
{
24-
int ret;
2522
size_t sz = strlen(hex);
2623
struct strbuf buf = STRBUF_INIT;
2724

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);
3326

3427
strbuf_add(&buf, hex, sz);
3528
strbuf_addchars(&buf, '0', algop->hexsz - sz);
3629

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);
4031

4132
strbuf_release(&buf);
42-
return ret;
4333
}
4434

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)
4637
{
47-
int hash_algo = init_hash_algo();
38+
int hash_algo = cl_setup_hash_algo();
4839

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]);
5242
}

t/unit-tests/lib-oid.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@
55

66
/*
77
* Convert arbitrary hex string to object_id.
8+
*
89
* For example, passing "abc12" will generate
910
* "abc1200000000000000000000000000000000000" hex of length 40 for SHA-1 and
1011
* create object_id with that.
1112
* WARNING: passing a string of length more than the hexsz of respective hash
1213
* algo is not allowed. The hash algo is decided based on GIT_TEST_DEFAULT_HASH
1314
* environment variable.
1415
*/
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);
1618
/*
1719
* Returns one of GIT_HASH_{SHA1, SHA256, UNKNOWN} based on the value of
1820
* GIT_TEST_DEFAULT_HASH environment variable. The fallback value in the
1921
* 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
2123
* GIT_TEST_DEFAULT_HASH's value is valid or not.
2224
*/
23-
int init_hash_algo(void);
25+
26+
int cl_setup_hash_algo(void);
2427

2528
#endif /* LIB_OID_H */

t/unit-tests/unit-test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "unit-test.h"
2+
#include "hex.h"
23
#include "parse-options.h"
4+
#include "strbuf.h"
35
#include "string-list.h"
46
#include "strvec.h"
57

0 commit comments

Comments
 (0)