Skip to content

Commit b7a1d47

Browse files
committed
Merge branch 'js/unit-test-suite-runner'
The "test-tool" has been taught to run testsuite tests in parallel, bypassing the need to use the "prove" tool. * js/unit-test-suite-runner: cmake: let `test-tool` run the unit tests, too ci: use test-tool as unit test runner on Windows t/Makefile: run unit tests alongside shell tests unit tests: add rule for running with test-tool test-tool run-command testsuite: support unit tests test-tool run-command testsuite: remove hardcoded filter test-tool run-command testsuite: get shell from env t0080: turn t-basic unit test into a helper
2 parents 83f1add + 9511056 commit b7a1d47

File tree

11 files changed

+74
-30
lines changed

11 files changed

+74
-30
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ TEST_BUILTINS_OBJS += test-dump-split-index.o
794794
TEST_BUILTINS_OBJS += test-dump-untracked-cache.o
795795
TEST_BUILTINS_OBJS += test-env-helper.o
796796
TEST_BUILTINS_OBJS += test-example-decorate.o
797+
TEST_BUILTINS_OBJS += test-example-tap.o
797798
TEST_BUILTINS_OBJS += test-find-pack.o
798799
TEST_BUILTINS_OBJS += test-fsmonitor-client.o
799800
TEST_BUILTINS_OBJS += test-genrandom.o
@@ -1333,7 +1334,6 @@ THIRD_PARTY_SOURCES += compat/regex/%
13331334
THIRD_PARTY_SOURCES += sha1collisiondetection/%
13341335
THIRD_PARTY_SOURCES += sha1dc/%
13351336

1336-
UNIT_TEST_PROGRAMS += t-basic
13371337
UNIT_TEST_PROGRAMS += t-mem-pool
13381338
UNIT_TEST_PROGRAMS += t-strbuf
13391339
UNIT_TEST_PROGRAMS += t-ctype
@@ -3235,7 +3235,7 @@ perf: all
32353235

32363236
.PRECIOUS: $(TEST_OBJS)
32373237

3238-
t/helper/test-tool$X: $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
3238+
t/helper/test-tool$X: $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS)) $(UNIT_TEST_DIR)/test-lib.o
32393239

32403240
t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS) $(REFTABLE_TEST_LIB)
32413241
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(filter %.a,$^) $(LIBS)
@@ -3888,5 +3888,5 @@ $(UNIT_TEST_PROGS): $(UNIT_TEST_BIN)/%$X: $(UNIT_TEST_DIR)/%.o $(UNIT_TEST_DIR)/
38883888

38893889
.PHONY: build-unit-tests unit-tests
38903890
build-unit-tests: $(UNIT_TEST_PROGS)
3891-
unit-tests: $(UNIT_TEST_PROGS)
3891+
unit-tests: $(UNIT_TEST_PROGS) t/helper/test-tool$X
38923892
$(MAKE) -C t/ unit-tests

ci/run-build-and-tests.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ if test -n "$run_tests"
5353
then
5454
group "Run tests" make test ||
5555
handle_failed_tests
56-
group "Run unit tests" \
57-
make DEFAULT_UNIT_TEST_TARGET=unit-tests-prove unit-tests
5856
fi
5957
check_unignored_build_artifacts
6058

ci/run-test-slice.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ handle_failed_tests
1717

1818
# We only have one unit test at the moment, so run it in the first slice
1919
if [ "$1" == "0" ] ; then
20-
group "Run unit tests" make --quiet -C t unit-tests-prove
20+
group "Run unit tests" make --quiet -C t unit-tests-test-tool
2121
fi
2222

2323
check_unignored_build_artifacts

contrib/buildsystems/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,10 +1005,11 @@ endforeach()
10051005

10061006
#test-tool
10071007
parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")
1008+
add_library(test-lib OBJECT ${CMAKE_SOURCE_DIR}/t/unit-tests/test-lib.c)
10081009

10091010
list(TRANSFORM test-tool_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/t/helper/")
10101011
add_executable(test-tool ${CMAKE_SOURCE_DIR}/t/helper/test-tool.c ${test-tool_SOURCES} ${test-reftable_SOURCES})
1011-
target_link_libraries(test-tool common-main)
1012+
target_link_libraries(test-tool test-lib common-main)
10121013

10131014
set_target_properties(test-fake-ssh test-tool
10141015
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/helper)

t/Makefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ 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_TESTS = $(sort $(filter-out unit-tests/bin/t-basic%,$(UNIT_TEST_PROGRAMS)))
51+
UNIT_TESTS = $(sort $(UNIT_TEST_PROGRAMS))
52+
UNIT_TESTS_NO_DIR = $(notdir $(UNIT_TESTS))
5253

5354
# `test-chainlint` (which is a dependency of `test-lint`, `test` and `prove`)
5455
# checks all tests in all scripts via a single invocation, so tell individual
@@ -67,7 +68,7 @@ failed:
6768
test -z "$$failed" || $(MAKE) $$failed
6869

6970
prove: pre-clean check-chainlint $(TEST_LINT)
70-
@echo "*** prove ***"; $(CHAINLINTSUPPRESS) $(PROVE) --exec '$(TEST_SHELL_PATH_SQ)' $(GIT_PROVE_OPTS) $(T) :: $(GIT_TEST_OPTS)
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)
7172
$(MAKE) clean-except-prove-cache
7273

7374
$(T):
@@ -76,14 +77,21 @@ $(T):
7677
$(UNIT_TESTS):
7778
@echo "*** $@ ***"; $@
7879

79-
.PHONY: unit-tests unit-tests-raw unit-tests-prove
80+
.PHONY: unit-tests unit-tests-raw unit-tests-prove unit-tests-test-tool
8081
unit-tests: $(DEFAULT_UNIT_TEST_TARGET)
8182

8283
unit-tests-raw: $(UNIT_TESTS)
8384

8485
unit-tests-prove:
8586
@echo "*** prove - unit tests ***"; $(PROVE) $(GIT_PROVE_OPTS) $(UNIT_TESTS)
8687

88+
unit-tests-test-tool:
89+
@echo "*** test-tool - unit tests **"
90+
( \
91+
cd unit-tests/bin && \
92+
../../helper/test-tool$X run-command testsuite $(UNIT_TESTS_NO_DIR)\
93+
)
94+
8795
pre-clean:
8896
$(RM) -r '$(TEST_RESULTS_DIRECTORY_SQ)'
8997

t/unit-tests/t-basic.c renamed to t/helper/test-example-tap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include "test-lib.h"
1+
#include "test-tool.h"
2+
#include "t/unit-tests/test-lib.h"
23

34
/*
45
* The purpose of this "unit test" is to verify a few invariants of the unit
@@ -69,7 +70,7 @@ static void t_empty(void)
6970
; /* empty */
7071
}
7172

72-
int cmd_main(int argc, const char **argv)
73+
int cmd__example_tap(int argc, const char **argv)
7374
{
7475
test_res = TEST(check_res = check_int(1, ==, 1), "passing test");
7576
TEST(t_res(1), "passing test and assertion return 1");

t/helper/test-run-command.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct testsuite {
6565
struct string_list tests, failed;
6666
int next;
6767
int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
68+
const char *shell_path;
6869
};
6970
#define TESTSUITE_INIT { \
7071
.tests = STRING_LIST_INIT_DUP, \
@@ -80,7 +81,9 @@ static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
8081
return 0;
8182

8283
test = suite->tests.items[suite->next++].string;
83-
strvec_pushl(&cp->args, "sh", test, NULL);
84+
if (suite->shell_path)
85+
strvec_push(&cp->args, suite->shell_path);
86+
strvec_push(&cp->args, test);
8487
if (suite->quiet)
8588
strvec_push(&cp->args, "--quiet");
8689
if (suite->immediate)
@@ -155,33 +158,45 @@ static int testsuite(int argc, const char **argv)
155158
.task_finished = test_finished,
156159
.data = &suite,
157160
};
161+
struct strbuf progpath = STRBUF_INIT;
162+
size_t path_prefix_len;
158163

159164
argc = parse_options(argc, argv, NULL, options,
160165
testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
161166

162167
if (max_jobs <= 0)
163168
max_jobs = online_cpus();
164169

170+
/*
171+
* If we run without a shell, execute the programs directly from CWD.
172+
*/
173+
suite.shell_path = getenv("TEST_SHELL_PATH");
174+
if (!suite.shell_path)
175+
strbuf_addstr(&progpath, "./");
176+
path_prefix_len = progpath.len;
177+
165178
dir = opendir(".");
166179
if (!dir)
167180
die("Could not open the current directory");
168181
while ((d = readdir(dir))) {
169182
const char *p = d->d_name;
170183

171-
if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
172-
!isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
173-
!ends_with(p, ".sh"))
184+
if (!strcmp(p, ".") || !strcmp(p, ".."))
174185
continue;
175186

176187
/* No pattern: match all */
177188
if (!argc) {
178-
string_list_append(&suite.tests, p);
189+
strbuf_setlen(&progpath, path_prefix_len);
190+
strbuf_addstr(&progpath, p);
191+
string_list_append(&suite.tests, progpath.buf);
179192
continue;
180193
}
181194

182195
for (i = 0; i < argc; i++)
183196
if (!wildmatch(argv[i], p, 0)) {
184-
string_list_append(&suite.tests, p);
197+
strbuf_setlen(&progpath, path_prefix_len);
198+
strbuf_addstr(&progpath, p);
199+
string_list_append(&suite.tests, progpath.buf);
185200
break;
186201
}
187202
}
@@ -208,6 +223,7 @@ static int testsuite(int argc, const char **argv)
208223

209224
string_list_clear(&suite.tests, 0);
210225
string_list_clear(&suite.failed, 0);
226+
strbuf_release(&progpath);
211227

212228
return ret;
213229
}

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static struct test_cmd cmds[] = {
3030
{ "dump-untracked-cache", cmd__dump_untracked_cache },
3131
{ "env-helper", cmd__env_helper },
3232
{ "example-decorate", cmd__example_decorate },
33+
{ "example-tap", cmd__example_tap },
3334
{ "find-pack", cmd__find_pack },
3435
{ "fsmonitor-client", cmd__fsmonitor_client },
3536
{ "genrandom", cmd__genrandom },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ int cmd__dump_untracked_cache(int argc, const char **argv);
2424
int cmd__dump_reftable(int argc, const char **argv);
2525
int cmd__env_helper(int argc, const char **argv);
2626
int cmd__example_decorate(int argc, const char **argv);
27+
int cmd__example_tap(int argc, const char **argv);
2728
int cmd__find_pack(int argc, const char **argv);
2829
int cmd__fsmonitor_client(int argc, const char **argv);
2930
int cmd__genrandom(int argc, const char **argv);

t/run-test.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
3+
# A simple wrapper to run shell tests via TEST_SHELL_PATH,
4+
# or exec unit tests directly.
5+
6+
case "$1" in
7+
*.sh)
8+
if test -z "${TEST_SHELL_PATH}"
9+
then
10+
echo >&2 "ERROR: TEST_SHELL_PATH is empty or not set"
11+
exit 1
12+
fi
13+
exec "${TEST_SHELL_PATH}" "$@"
14+
;;
15+
*)
16+
exec "$@"
17+
;;
18+
esac

t/t0080-unit-test-output.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,50 @@ test_expect_success 'TAP output from unit tests' '
99
cat >expect <<-EOF &&
1010
ok 1 - passing test
1111
ok 2 - passing test and assertion return 1
12-
# check "1 == 2" failed at t/unit-tests/t-basic.c:76
12+
# check "1 == 2" failed at t/helper/test-example-tap.c:77
1313
# left: 1
1414
# right: 2
1515
not ok 3 - failing test
1616
ok 4 - failing test and assertion return 0
1717
not ok 5 - passing TEST_TODO() # TODO
1818
ok 6 - passing TEST_TODO() returns 1
19-
# todo check ${SQ}check(x)${SQ} succeeded at t/unit-tests/t-basic.c:25
19+
# todo check ${SQ}check(x)${SQ} succeeded at t/helper/test-example-tap.c:26
2020
not ok 7 - failing TEST_TODO()
2121
ok 8 - failing TEST_TODO() returns 0
22-
# check "0" failed at t/unit-tests/t-basic.c:30
22+
# check "0" failed at t/helper/test-example-tap.c:31
2323
# skipping test - missing prerequisite
24-
# skipping check ${SQ}1${SQ} at t/unit-tests/t-basic.c:32
24+
# skipping check ${SQ}1${SQ} at t/helper/test-example-tap.c:33
2525
ok 9 - test_skip() # SKIP
2626
ok 10 - skipped test returns 1
2727
# skipping test - missing prerequisite
2828
ok 11 - test_skip() inside TEST_TODO() # SKIP
2929
ok 12 - test_skip() inside TEST_TODO() returns 1
30-
# check "0" failed at t/unit-tests/t-basic.c:48
30+
# check "0" failed at t/helper/test-example-tap.c:49
3131
not ok 13 - TEST_TODO() after failing check
3232
ok 14 - TEST_TODO() after failing check returns 0
33-
# check "0" failed at t/unit-tests/t-basic.c:56
33+
# check "0" failed at t/helper/test-example-tap.c:57
3434
not ok 15 - failing check after TEST_TODO()
3535
ok 16 - failing check after TEST_TODO() returns 0
36-
# check "!strcmp("\thello\\\\", "there\"\n")" failed at t/unit-tests/t-basic.c:61
36+
# check "!strcmp("\thello\\\\", "there\"\n")" failed at t/helper/test-example-tap.c:62
3737
# left: "\011hello\\\\"
3838
# right: "there\"\012"
39-
# check "!strcmp("NULL", NULL)" failed at t/unit-tests/t-basic.c:62
39+
# check "!strcmp("NULL", NULL)" failed at t/helper/test-example-tap.c:63
4040
# left: "NULL"
4141
# right: NULL
42-
# check "${SQ}a${SQ} == ${SQ}\n${SQ}" failed at t/unit-tests/t-basic.c:63
42+
# check "${SQ}a${SQ} == ${SQ}\n${SQ}" failed at t/helper/test-example-tap.c:64
4343
# left: ${SQ}a${SQ}
4444
# right: ${SQ}\012${SQ}
45-
# check "${SQ}\\\\${SQ} == ${SQ}\\${SQ}${SQ}" failed at t/unit-tests/t-basic.c:64
45+
# check "${SQ}\\\\${SQ} == ${SQ}\\${SQ}${SQ}" failed at t/helper/test-example-tap.c:65
4646
# left: ${SQ}\\\\${SQ}
4747
# right: ${SQ}\\${SQ}${SQ}
4848
not ok 17 - messages from failing string and char comparison
49-
# BUG: test has no checks at t/unit-tests/t-basic.c:91
49+
# BUG: test has no checks at t/helper/test-example-tap.c:92
5050
not ok 18 - test with no checks
5151
ok 19 - test with no checks returns 0
5252
1..19
5353
EOF
5454
55-
! "$GIT_BUILD_DIR"/t/unit-tests/bin/t-basic >actual &&
55+
! test-tool example-tap >actual &&
5656
test_cmp expect actual
5757
'
5858

0 commit comments

Comments
 (0)