Skip to content

Commit cc72a4e

Browse files
committed
Merge branch 'ps/clar-unit-test' into seen
* ps/clar-unit-test: t/unit-tests: convert ctype tests to use clar t/unit-tests: convert strvec tests to use clar Makefile: wire up the clar unit testing framework Makefile: do not use sparse on third-party sources Makefile: make hdr-check depend on generated headers Makefile: fix sparse dependency on GENERATED_H t/clar: fix compatibility with NonStop t: import the clar unit testing framework t: do not pass GIT_TEST_OPTS to unit tests with prove
2 parents 25a7c95 + 8330c5d commit cc72a4e

30 files changed

+3345
-235
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/GIT-PYTHON-VARS
1010
/GIT-SCRIPT-DEFINES
1111
/GIT-SPATCH-DEFINES
12+
/GIT-TEST-SUITES
1213
/GIT-USER-AGENT
1314
/GIT-VERSION-FILE
1415
/bin-wrappers/

Documentation/technical/unit-tests.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ GitHub / GitLab stars to estimate this.
203203
:criterion: https://github.com/Snaipe/Criterion[Criterion]
204204
:c-tap: https://github.com/rra/c-tap-harness/[C TAP]
205205
:check: https://libcheck.github.io/check/[Check]
206+
:clar: https://github.com/clar-test/clar[Clar]
206207

207208
[format="csv",options="header",width="33%",subs="specialcharacters,attributes,quotes,macros"]
208209
|=====
@@ -212,6 +213,7 @@ Framework,"<<license,License>>","<<vendorable-or-ubiquitous,Vendorable or ubiqui
212213
{criterion},{mit},{false},{partial},{true},{true},{true},{true},{true},{false},{true},19,1800
213214
{c-tap},{expat},{true},{partial},{partial},{true},{false},{true},{false},{false},{false},4,33
214215
{check},{lgpl},{false},{partial},{true},{true},{true},{false},{false},{false},{true},17,973
216+
{clar},{isc},{false},{partial},{true},{true},{true},{true},{false},{false},{true},1,192
215217
|=====
216218

217219
=== Additional framework candidates

Makefile

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,8 @@ REFTABLE_TEST_LIB = reftable/libreftable_test.a
915915
GENERATED_H += command-list.h
916916
GENERATED_H += config-list.h
917917
GENERATED_H += hook-list.h
918+
GENERATED_H += $(UNIT_TEST_DIR)/clar-decls.h
919+
GENERATED_H += $(UNIT_TEST_DIR)/clar.suite
918920

919921
.PHONY: generated-hdrs
920922
generated-hdrs: $(GENERATED_H)
@@ -1334,9 +1336,17 @@ THIRD_PARTY_SOURCES += compat/poll/%
13341336
THIRD_PARTY_SOURCES += compat/regex/%
13351337
THIRD_PARTY_SOURCES += sha1collisiondetection/%
13361338
THIRD_PARTY_SOURCES += sha1dc/%
1339+
THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/%
1340+
THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/%
1341+
1342+
UNIT_TESTS_SUITES += ctype
1343+
UNIT_TESTS_SUITES += strvec
1344+
UNIT_TESTS_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
1345+
UNIT_TESTS_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TESTS_SUITES))
1346+
UNIT_TESTS_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
1347+
UNIT_TESTS_OBJS += $(UNIT_TEST_DIR)/unit-test.o
13371348

13381349
UNIT_TEST_PROGRAMS += t-apply
1339-
UNIT_TEST_PROGRAMS += t-ctype
13401350
UNIT_TEST_PROGRAMS += t-example-decorate
13411351
UNIT_TEST_PROGRAMS += t-hash
13421352
UNIT_TEST_PROGRAMS += t-hashmap
@@ -1353,7 +1363,6 @@ UNIT_TEST_PROGRAMS += t-reftable-record
13531363
UNIT_TEST_PROGRAMS += t-reftable-tree
13541364
UNIT_TEST_PROGRAMS += t-strbuf
13551365
UNIT_TEST_PROGRAMS += t-strcmp-offset
1356-
UNIT_TEST_PROGRAMS += t-strvec
13571366
UNIT_TEST_PROGRAMS += t-trailer
13581367
UNIT_TEST_PROGRAMS += t-urlmatch-normalization
13591368
UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
@@ -2719,6 +2728,7 @@ OBJECTS += $(FUZZ_OBJS)
27192728
OBJECTS += $(REFTABLE_OBJS) $(REFTABLE_TEST_OBJS)
27202729
OBJECTS += $(UNIT_TEST_OBJS)
27212730
OBJECTS += contrib/cgit-rs/cgit-sys/public_symbol_export.o
2731+
OBJECTS += $(UNIT_TESTS_OBJS)
27222732

27232733
ifndef NO_CURL
27242734
OBJECTS += http.o http-walker.o remote-curl.o
@@ -3221,7 +3231,7 @@ endif
32213231

32223232
test_bindir_programs := $(patsubst %,bin-wrappers/%,$(BINDIR_PROGRAMS_NEED_X) $(BINDIR_PROGRAMS_NO_X) $(TEST_PROGRAMS_NEED_X))
32233233

3224-
all:: $(TEST_PROGRAMS) $(test_bindir_programs) $(UNIT_TEST_PROGS)
3234+
all:: $(TEST_PROGRAMS) $(test_bindir_programs) $(UNIT_TEST_PROGS) $(UNIT_TESTS_PROG)
32253235

32263236
bin-wrappers/%: wrap-for-bin.sh
32273237
$(call mkdir_p_parent_template)
@@ -3257,9 +3267,10 @@ t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS) $(REFTABLE_TEST_LIB)
32573267
check-sha1:: t/helper/test-tool$X
32583268
t/helper/test-sha1.sh
32593269

3260-
SP_OBJ = $(patsubst %.o,%.sp,$(OBJECTS))
3270+
SP_SRC = $(filter-out $(THIRD_PARTY_SOURCES),$(patsubst %.o,%.c,$(OBJECTS)))
3271+
SP_OBJ = $(patsubst %.c,%.sp,$(SP_SRC))
32613272

3262-
$(SP_OBJ): %.sp: %.c %.o
3273+
$(SP_OBJ): %.sp: %.c %.o $(GENERATED_H)
32633274
$(QUIET_SP)cgcc -no-compile $(ALL_CFLAGS) $(EXTRA_CPPFLAGS) \
32643275
-Wsparse-error \
32653276
$(SPARSE_FLAGS) $(SP_EXTRA_FLAGS) $< && \
@@ -3268,7 +3279,7 @@ $(SP_OBJ): %.sp: %.c %.o
32683279
.PHONY: sparse
32693280
sparse: $(SP_OBJ)
32703281

3271-
EXCEPT_HDRS := $(GENERATED_H) unicode-width.h compat/% xdiff/%
3282+
EXCEPT_HDRS := $(GENERATED_H) unicode-width.h compat/% xdiff/% $(UNIT_TEST_DIR)/clar/% $(UNIT_TEST_DIR)/clar/clar/%
32723283
ifndef OPENSSL_SHA1
32733284
EXCEPT_HDRS += sha1/openssl.h
32743285
endif
@@ -3289,7 +3300,7 @@ HCC = $(HCO:hco=hcc)
32893300
@echo '#include "git-compat-util.h"' >$@
32903301
@echo '#include "$<"' >>$@
32913302

3292-
$(HCO): %.hco: %.hcc FORCE
3303+
$(HCO): %.hco: %.hcc $(GENERATED_H) FORCE
32933304
$(QUIET_HDR)$(CC) $(ALL_CFLAGS) -o /dev/null -c -xc $<
32943305

32953306
.PHONY: hdr-check $(HCO)
@@ -3300,7 +3311,7 @@ style:
33003311
git clang-format --style file --diff --extensions c,h
33013312

33023313
.PHONY: check
3303-
check: $(GENERATED_H)
3314+
check:
33043315
@if sparse; \
33053316
then \
33063317
echo >&2 "Use 'make sparse' instead"; \
@@ -3652,7 +3663,7 @@ endif
36523663

36533664
artifacts-tar:: $(ALL_COMMANDS_TO_INSTALL) $(SCRIPT_LIB) $(OTHER_PROGRAMS) \
36543665
GIT-BUILD-OPTIONS $(TEST_PROGRAMS) $(test_bindir_programs) \
3655-
$(UNIT_TEST_PROGS) $(MOFILES)
3666+
$(UNIT_TEST_PROGS) $(UNIT_TESTS_PROG) $(MOFILES)
36563667
$(QUIET_SUBDIR0)templates $(QUIET_SUBDIR1) \
36573668
SHELL_PATH='$(SHELL_PATH_SQ)' PERL_PATH='$(PERL_PATH_SQ)'
36583669
test -n "$(ARTIFACTS_DIRECTORY)"
@@ -3708,6 +3719,7 @@ cocciclean:
37083719

37093720
clean: profile-clean coverage-clean cocciclean
37103721
$(RM) -r .build $(UNIT_TEST_BIN)
3722+
$(RM) GIT-TEST-SUITES
37113723
$(RM) po/git.pot po/git-core.pot
37123724
$(RM) git.res
37133725
$(RM) $(OBJECTS)
@@ -3868,9 +3880,28 @@ $(UNIT_TEST_PROGS): $(UNIT_TEST_BIN)/%$X: $(UNIT_TEST_DIR)/%.o \
38683880
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) \
38693881
$(filter %.o,$^) $(filter %.a,$^) $(LIBS)
38703882

3883+
GIT-TEST-SUITES: FORCE
3884+
@FLAGS='$(UNIT_TESTS_SUITES)'; \
3885+
if test x"$$FLAGS" != x"`cat GIT-TEST-SUITES 2>/dev/null`" ; then \
3886+
echo >&2 " * new test suites"; \
3887+
echo "$$FLAGS" >GIT-TEST-SUITES; \
3888+
fi
3889+
3890+
$(UNIT_TEST_DIR)/clar-decls.h: $(patsubst %,$(UNIT_TEST_DIR)/%.c,$(UNIT_TESTS_SUITES)) GIT-TEST-SUITES
3891+
$(QUIET_GEN)for suite in $(UNIT_TESTS_SUITES); do \
3892+
sed -ne "s/^\(void test_$${suite}__[a-zA-Z_0-9][a-zA-Z_0-9]*(void)$$\)/extern \1;/p" $(UNIT_TEST_DIR)/$$suite.c; \
3893+
done >$@
3894+
$(UNIT_TEST_DIR)/clar.suite: $(UNIT_TEST_DIR)/clar-decls.h
3895+
$(QUIET_GEN)awk -f $(UNIT_TEST_DIR)/clar-generate.awk $< >$(UNIT_TEST_DIR)/clar.suite
3896+
$(UNIT_TESTS_OBJS): $(UNIT_TEST_DIR)/clar-decls.h
3897+
$(UNIT_TESTS_OBJS): EXTRA_CPPFLAGS = -I$(UNIT_TEST_DIR)
3898+
$(UNIT_TESTS_PROG): $(UNIT_TEST_DIR)/clar.suite $(UNIT_TESTS_OBJS) $(GITLIBS) GIT-LDFLAGS
3899+
$(call mkdir_p_parent_template)
3900+
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
3901+
38713902
.PHONY: build-unit-tests unit-tests
3872-
build-unit-tests: $(UNIT_TEST_PROGS)
3873-
unit-tests: $(UNIT_TEST_PROGS) t/helper/test-tool$X
3903+
build-unit-tests: $(UNIT_TEST_PROGS) $(UNIT_TESTS_PROG)
3904+
unit-tests: $(UNIT_TEST_PROGS) $(UNIT_TESTS_PROG) t/helper/test-tool$X
38743905
$(MAKE) -C t/ unit-tests
38753906

38763907
contrib/cgit-rs/cgit-sys/partial_symbol_export.o: contrib/cgit-rs/cgit-sys/public_symbol_export.o libgit.a reftable/libreftable.a xdiff/lib.a

t/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ CHAINLINTTESTS = $(sort $(patsubst chainlint/%.test,%,$(wildcard chainlint/*.tes
4848
CHAINLINT = '$(PERL_PATH_SQ)' chainlint.pl
4949
UNIT_TEST_SOURCES = $(wildcard unit-tests/t-*.c)
5050
UNIT_TEST_PROGRAMS = $(patsubst unit-tests/%.c,unit-tests/bin/%$(X),$(UNIT_TEST_SOURCES))
51+
UNIT_TEST_PROGRAMS += unit-tests/bin/unit-tests$(X)
5152
UNIT_TESTS = $(sort $(UNIT_TEST_PROGRAMS))
5253
UNIT_TESTS_NO_DIR = $(notdir $(UNIT_TESTS))
5354

@@ -68,7 +69,8 @@ failed:
6869
test -z "$$failed" || $(MAKE) $$failed
6970

7071
prove: pre-clean check-chainlint $(TEST_LINT)
71-
@echo "*** prove (shell & unit tests) ***"; $(CHAINLINTSUPPRESS) TEST_SHELL_PATH='$(TEST_SHELL_PATH_SQ)' $(PROVE) --exec ./run-test.sh $(GIT_PROVE_OPTS) $(T) $(UNIT_TESTS) :: $(GIT_TEST_OPTS)
72+
@echo "*** prove (shell & unit tests) ***"
73+
@$(CHAINLINTSUPPRESS) TEST_OPTIONS='$(GIT_TEST_OPTS)' TEST_SHELL_PATH='$(TEST_SHELL_PATH_SQ)' $(PROVE) --exec ./run-test.sh $(GIT_PROVE_OPTS) $(T) $(UNIT_TESTS)
7274
$(MAKE) clean-except-prove-cache
7375

7476
$(T):

t/run-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ case "$1" in
1010
echo >&2 "ERROR: TEST_SHELL_PATH is empty or not set"
1111
exit 1
1212
fi
13-
exec "${TEST_SHELL_PATH}" "$@"
13+
exec "${TEST_SHELL_PATH}" "$@" ${TEST_OPTIONS}
1414
;;
1515
*)
1616
exec "$@"

t/unit-tests/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/bin
2+
/clar.suite
3+
/clar-decls.h

t/unit-tests/clar-generate.awk

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function add_suite(suite, initialize, cleanup, count) {
2+
if (!suite) return
3+
suite_count++
4+
callback_count += count
5+
suites = suites " {\n"
6+
suites = suites " \"" suite "\",\n"
7+
suites = suites " " initialize ",\n"
8+
suites = suites " " cleanup ",\n"
9+
suites = suites " _clar_cb_" suite ", " count ", 1\n"
10+
suites = suites " },\n"
11+
}
12+
13+
BEGIN {
14+
suites = "static struct clar_suite _clar_suites[] = {\n"
15+
}
16+
17+
{
18+
print
19+
name = $3; sub(/\(.*$/, "", name)
20+
suite = name; sub(/^test_/, "", suite); sub(/__.*$/, "", suite)
21+
short_name = name; sub(/^.*__/, "", short_name)
22+
cb = "{ \"" short_name "\", &" name " }"
23+
if (suite != prev_suite) {
24+
add_suite(prev_suite, initialize, cleanup, count)
25+
if (callbacks) callbacks = callbacks "};\n"
26+
callbacks = callbacks "static const struct clar_func _clar_cb_" suite "[] = {\n"
27+
initialize = "{ NULL, NULL }"
28+
cleanup = "{ NULL, NULL }"
29+
count = 0
30+
prev_suite = suite
31+
}
32+
if (short_name == "initialize") {
33+
initialize = cb
34+
} else if (short_name == "cleanup") {
35+
cleanup = cb
36+
} else {
37+
callbacks = callbacks " " cb ",\n"
38+
count++
39+
}
40+
}
41+
42+
END {
43+
add_suite(suite, initialize, cleanup, count)
44+
suites = suites "};"
45+
if (callbacks) callbacks = callbacks "};"
46+
print callbacks
47+
print suites
48+
print "static const size_t _clar_suite_count = " suite_count ";"
49+
print "static const size_t _clar_callback_count = " callback_count ";"
50+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
strategy:
12+
matrix:
13+
os: [ ubuntu-latest, macos-latest ]
14+
15+
runs-on: ${{ matrix.os }}
16+
17+
steps:
18+
- name: Check out
19+
uses: actions/checkout@v2
20+
- name: Build
21+
run: |
22+
cd test
23+
make

t/unit-tests/clar/COPYING

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ISC License
2+
3+
Copyright (c) 2011-2015 Vicent Marti
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

0 commit comments

Comments
 (0)