Skip to content

Commit 6024f32

Browse files
committed
Merge branch 'sk/unit-test-oid'
Convert a few unit tests to the clar framework. * sk/unit-test-oid: t/unit-tests: convert oidtree test to use clar test framework t/unit-tests: convert oidmap test to use clar test framework t/unit-tests: convert oid-array test to use clar test framework t/unit-tests: implement clar specific oid helper functions
2 parents feffb34 + 1495850 commit 6024f32

File tree

11 files changed

+399
-461
lines changed

11 files changed

+399
-461
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,9 @@ CLAR_TEST_SUITES += u-example-decorate
13561356
CLAR_TEST_SUITES += u-hash
13571357
CLAR_TEST_SUITES += u-hashmap
13581358
CLAR_TEST_SUITES += u-mem-pool
1359+
CLAR_TEST_SUITES += u-oid-array
1360+
CLAR_TEST_SUITES += u-oidmap
1361+
CLAR_TEST_SUITES += u-oidtree
13591362
CLAR_TEST_SUITES += u-prio-queue
13601363
CLAR_TEST_SUITES += u-reftable-tree
13611364
CLAR_TEST_SUITES += u-strbuf
@@ -1365,10 +1368,8 @@ CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
13651368
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
13661369
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
13671370
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
1371+
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
13681372

1369-
UNIT_TEST_PROGRAMS += t-oid-array
1370-
UNIT_TEST_PROGRAMS += t-oidmap
1371-
UNIT_TEST_PROGRAMS += t-oidtree
13721373
UNIT_TEST_PROGRAMS += t-reftable-basics
13731374
UNIT_TEST_PROGRAMS += t-reftable-block
13741375
UNIT_TEST_PROGRAMS += t-reftable-merged
@@ -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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ clar_test_suites = [
44
'unit-tests/u-hash.c',
55
'unit-tests/u-hashmap.c',
66
'unit-tests/u-mem-pool.c',
7+
'unit-tests/u-oid-array.c',
8+
'unit-tests/u-oidmap.c',
9+
'unit-tests/u-oidtree.c',
710
'unit-tests/u-prio-queue.c',
811
'unit-tests/u-reftable-tree.c',
912
'unit-tests/u-strbuf.c',
@@ -14,6 +17,7 @@ clar_test_suites = [
1417
clar_sources = [
1518
'unit-tests/clar/clar.c',
1619
'unit-tests/unit-test.c',
20+
'unit-tests/lib-oid.c'
1721
]
1822

1923
clar_decls_h = custom_target(
@@ -48,9 +52,6 @@ clar_unit_tests = executable('unit-tests',
4852
test('unit-tests', clar_unit_tests)
4953

5054
unit_test_programs = [
51-
'unit-tests/t-oid-array.c',
52-
'unit-tests/t-oidmap.c',
53-
'unit-tests/t-oidtree.c',
5455
'unit-tests/t-reftable-basics.c',
5556
'unit-tests/t-reftable-block.c',
5657
'unit-tests/t-reftable-merged.c',
@@ -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/t-oid-array.c

Lines changed: 0 additions & 126 deletions
This file was deleted.

0 commit comments

Comments
 (0)