Skip to content

Commit 712177e

Browse files
committed
Merge branch 'js/doc-unit-tests-with-cmake'
Update the base topic to work with CMake builds. * js/doc-unit-tests-with-cmake: cmake: handle also unit tests cmake: use test names instead of full paths cmake: fix typo in variable name artifacts-tar: when including `.dll` files, don't forget the unit-tests unit-tests: do show relative file paths unit-tests: do not mistake `.pdb` files for being executable cmake: also build unit tests
2 parents 8bf6fbd + 694e89b commit 712177e

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3614,7 +3614,7 @@ rpm::
36143614
.PHONY: rpm
36153615

36163616
ifneq ($(INCLUDE_DLLS_IN_ARTIFACTS),)
3617-
OTHER_PROGRAMS += $(shell echo *.dll t/helper/*.dll)
3617+
OTHER_PROGRAMS += $(shell echo *.dll t/helper/*.dll t/unit-tests/bin/*.dll)
36183618
endif
36193619

36203620
artifacts-tar:: $(ALL_COMMANDS_TO_INSTALL) $(SCRIPT_LIB) $(OTHER_PROGRAMS) \

contrib/buildsystems/CMakeLists.txt

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,35 @@ target_link_libraries(test-fake-ssh common-main)
974974
parse_makefile_for_sources(test-reftable_SOURCES "REFTABLE_TEST_OBJS")
975975
list(TRANSFORM test-reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
976976

977+
#unit-tests
978+
add_library(unit-test-lib OBJECT ${CMAKE_SOURCE_DIR}/t/unit-tests/test-lib.c)
979+
980+
parse_makefile_for_scripts(unit_test_PROGRAMS "UNIT_TEST_PROGRAMS" "")
981+
foreach(unit_test ${unit_test_PROGRAMS})
982+
add_executable("${unit_test}" "${CMAKE_SOURCE_DIR}/t/unit-tests/${unit_test}.c")
983+
target_link_libraries("${unit_test}" unit-test-lib common-main)
984+
set_target_properties("${unit_test}"
985+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
986+
if(MSVC)
987+
set_target_properties("${unit_test}"
988+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
989+
set_target_properties("${unit_test}"
990+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
991+
endif()
992+
list(APPEND PROGRAMS_BUILT "${unit_test}")
993+
994+
# t-basic intentionally fails tests, to validate the unit-test infrastructure.
995+
# Therefore, it should only be run as part of t0080, which verifies that it
996+
# fails only in the expected ways.
997+
#
998+
# All other unit tests should be run.
999+
if(NOT ${unit_test} STREQUAL "t-basic")
1000+
add_test(NAME "t.unit-tests.${unit_test}"
1001+
COMMAND "./${unit_test}"
1002+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/t/unit-tests/bin)
1003+
endif()
1004+
endforeach()
1005+
9771006
#test-tool
9781007
parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")
9791008

@@ -1093,17 +1122,18 @@ if(NOT ${CMAKE_BINARY_DIR}/CMakeCache.txt STREQUAL ${CACHE_PATH})
10931122
file(COPY ${CMAKE_SOURCE_DIR}/contrib/completion/git-completion.bash DESTINATION ${CMAKE_BINARY_DIR}/contrib/completion/)
10941123
endif()
10951124

1096-
file(GLOB test_scipts "${CMAKE_SOURCE_DIR}/t/t[0-9]*.sh")
1125+
file(GLOB test_scripts "${CMAKE_SOURCE_DIR}/t/t[0-9]*.sh")
10971126

10981127
#test
1099-
foreach(tsh ${test_scipts})
1100-
add_test(NAME ${tsh}
1128+
foreach(tsh ${test_scripts})
1129+
string(REGEX REPLACE ".*/(.*)\\.sh" "\\1" test_name ${tsh})
1130+
add_test(NAME "t.suite.${test_name}"
11011131
COMMAND ${SH_EXE} ${tsh} --no-bin-wrappers --no-chain-lint -vx
11021132
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/t)
11031133
endforeach()
11041134

11051135
# This test script takes an extremely long time and is known to time out even
11061136
# on fast machines because it requires in excess of one hour to run
1107-
set_tests_properties("${CMAKE_SOURCE_DIR}/t/t7112-reset-submodule.sh" PROPERTIES TIMEOUT 4000)
1137+
set_tests_properties("t.suite.t7112-reset-submodule" PROPERTIES TIMEOUT 4000)
11081138

11091139
endif()#BUILD_TESTING

t/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ TPERF = $(sort $(wildcard perf/p[0-9][0-9][0-9][0-9]-*.sh))
4242
TINTEROP = $(sort $(wildcard interop/i[0-9][0-9][0-9][0-9]-*.sh))
4343
CHAINLINTTESTS = $(sort $(patsubst chainlint/%.test,%,$(wildcard chainlint/*.test)))
4444
CHAINLINT = '$(PERL_PATH_SQ)' chainlint.pl
45-
UNIT_TESTS = $(sort $(filter-out unit-tests/bin/t-basic%,$(wildcard unit-tests/bin/t-*)))
45+
UNIT_TESTS = $(sort $(filter-out %.pdb unit-tests/bin/t-basic%,$(wildcard unit-tests/bin/t-*)))
4646

4747
# `test-chainlint` (which is a dependency of `test-lint`, `test` and `prove`)
4848
# checks all tests in all scripts via a single invocation, so tell individual

t/unit-tests/test-lib.c

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,46 @@ static struct {
2121
.result = RESULT_NONE,
2222
};
2323

24+
#ifndef _MSC_VER
25+
#define make_relative(location) location
26+
#else
27+
/*
28+
* Visual C interpolates the absolute Windows path for `__FILE__`,
29+
* but we want to see relative paths, as verified by t0080.
30+
*/
31+
#include "dir.h"
32+
33+
static const char *make_relative(const char *location)
34+
{
35+
static char prefix[] = __FILE__, buf[PATH_MAX], *p;
36+
static size_t prefix_len;
37+
38+
if (!prefix_len) {
39+
size_t len = strlen(prefix);
40+
const char *needle = "\\t\\unit-tests\\test-lib.c";
41+
size_t needle_len = strlen(needle);
42+
43+
if (len < needle_len || strcmp(needle, prefix + len - needle_len))
44+
die("unexpected suffix of '%s'", prefix);
45+
46+
/* let it end in a directory separator */
47+
prefix_len = len - needle_len + 1;
48+
}
49+
50+
/* Does it not start with the expected prefix? */
51+
if (fspathncmp(location, prefix, prefix_len))
52+
return location;
53+
54+
strlcpy(buf, location + prefix_len, sizeof(buf));
55+
/* convert backslashes to forward slashes */
56+
for (p = buf; *p; p++)
57+
if (*p == '\\')
58+
*p = '/';
59+
60+
return buf;
61+
}
62+
#endif
63+
2464
static void msg_with_prefix(const char *prefix, const char *format, va_list ap)
2565
{
2666
fflush(stderr);
@@ -147,7 +187,8 @@ int test__run_end(int was_run UNUSED, const char *location, const char *format,
147187
break;
148188

149189
case RESULT_NONE:
150-
test_msg("BUG: test has no checks at %s", location);
190+
test_msg("BUG: test has no checks at %s",
191+
make_relative(location));
151192
printf("not ok %d", ctx.count);
152193
print_description(format, ap);
153194
ctx.result = RESULT_FAILURE;
@@ -193,14 +234,16 @@ int test_assert(const char *location, const char *check, int ok)
193234
assert(ctx.running);
194235

195236
if (ctx.result == RESULT_SKIP) {
196-
test_msg("skipping check '%s' at %s", check, location);
237+
test_msg("skipping check '%s' at %s", check,
238+
make_relative(location));
197239
return 1;
198240
}
199241
if (!ctx.todo) {
200242
if (ok) {
201243
test_pass();
202244
} else {
203-
test_msg("check \"%s\" failed at %s", check, location);
245+
test_msg("check \"%s\" failed at %s", check,
246+
make_relative(location));
204247
test_fail();
205248
}
206249
}
@@ -225,7 +268,8 @@ int test__todo_end(const char *location, const char *check, int res)
225268
if (ctx.result == RESULT_SKIP)
226269
return 1;
227270
if (res) {
228-
test_msg("todo check '%s' succeeded at %s", check, location);
271+
test_msg("todo check '%s' succeeded at %s", check,
272+
make_relative(location));
229273
test_fail();
230274
} else {
231275
test_todo();

0 commit comments

Comments
 (0)