Skip to content

Commit d772625

Browse files
committed
Merge branch 'js/doc-unit-tests-with-cmake' into seen
Update the base topic to work with CMake builds. * js/doc-unit-tests-with-cmake: 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 562eb0b + 65d09cd commit d772625

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3610,7 +3610,7 @@ rpm::
36103610
.PHONY: rpm
36113611

36123612
ifneq ($(INCLUDE_DLLS_IN_ARTIFACTS),)
3613-
OTHER_PROGRAMS += $(shell echo *.dll t/helper/*.dll)
3613+
OTHER_PROGRAMS += $(shell echo *.dll t/helper/*.dll t/unit-tests/*.dll)
36143614
endif
36153615

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

contrib/buildsystems/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,24 @@ 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)
986+
if(MSVC)
987+
set_target_properties("${unit_test}"
988+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/t/unit-tests)
989+
set_target_properties("${unit_test}"
990+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/t/unit-tests)
991+
endif()
992+
list(APPEND PROGRAMS_BUILT "${unit_test}")
993+
endforeach()
994+
977995
#test-tool
978996
parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")
979997

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 %.h %.c %.o unit-tests/t-basic%,$(wildcard unit-tests/t-*)))
45+
UNIT_TESTS = $(sort $(filter-out %.h %.c %.o %.pdb unit-tests/t-basic%,$(wildcard unit-tests/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: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,42 @@ 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 "strbuf.h"
32+
#include "dir.h"
33+
34+
static const char *make_relative(const char *location)
35+
{
36+
static const char *prefix;
37+
static size_t prefix_len;
38+
static struct strbuf buf = STRBUF_INIT;
39+
40+
if (!prefix) {
41+
strbuf_addstr(&buf, __FILE__);
42+
if (!strbuf_strip_suffix(&buf, "\\t\\unit-tests\\test-lib.c"))
43+
die("unexpected suffix of '%s'");
44+
strbuf_complete(&buf, '\\');
45+
prefix = strbuf_detach(&buf, &prefix_len);
46+
}
47+
48+
/* Does it not start with the expected prefix? */
49+
if (fspathncmp(location, prefix, prefix_len))
50+
return location;
51+
52+
strbuf_reset(&buf);
53+
strbuf_addstr(&buf, location + prefix_len);
54+
convert_slashes(buf.buf);
55+
56+
return buf.buf;
57+
}
58+
#endif
59+
2460
static void msg_with_prefix(const char *prefix, const char *format, va_list ap)
2561
{
2662
fflush(stderr);
@@ -147,7 +183,8 @@ int test__run_end(int was_run UNUSED, const char *location, const char *format,
147183
break;
148184

149185
case RESULT_NONE:
150-
test_msg("BUG: test has no checks at %s", location);
186+
test_msg("BUG: test has no checks at %s",
187+
make_relative(location));
151188
printf("not ok %d", ctx.count);
152189
print_description(format, ap);
153190
ctx.result = RESULT_FAILURE;
@@ -193,13 +230,15 @@ int test_assert(const char *location, const char *check, int ok)
193230
assert(ctx.running);
194231

195232
if (ctx.result == RESULT_SKIP) {
196-
test_msg("skipping check '%s' at %s", check, location);
233+
test_msg("skipping check '%s' at %s", check,
234+
make_relative(location));
197235
return 0;
198236
} else if (!ctx.todo) {
199237
if (ok) {
200238
test_pass();
201239
} else {
202-
test_msg("check \"%s\" failed at %s", check, location);
240+
test_msg("check \"%s\" failed at %s", check,
241+
make_relative(location));
203242
test_fail();
204243
}
205244
}
@@ -224,7 +263,8 @@ int test__todo_end(const char *location, const char *check, int res)
224263
if (ctx.result == RESULT_SKIP)
225264
return 0;
226265
if (!res) {
227-
test_msg("todo check '%s' succeeded at %s", check, location);
266+
test_msg("todo check '%s' succeeded at %s", check,
267+
make_relative(location));
228268
test_fail();
229269
} else {
230270
test_todo();

0 commit comments

Comments
 (0)