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
maint
Junio C Hamano 2023-12-09 16:37:47 -08:00
commit 712177ed04
4 changed files with 84 additions and 10 deletions

View File

@ -3614,7 +3614,7 @@ rpm::
.PHONY: rpm .PHONY: rpm


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


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

View File

@ -974,6 +974,35 @@ target_link_libraries(test-fake-ssh common-main)
parse_makefile_for_sources(test-reftable_SOURCES "REFTABLE_TEST_OBJS") parse_makefile_for_sources(test-reftable_SOURCES "REFTABLE_TEST_OBJS")
list(TRANSFORM test-reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/") list(TRANSFORM test-reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")


#unit-tests
add_library(unit-test-lib OBJECT ${CMAKE_SOURCE_DIR}/t/unit-tests/test-lib.c)

parse_makefile_for_scripts(unit_test_PROGRAMS "UNIT_TEST_PROGRAMS" "")
foreach(unit_test ${unit_test_PROGRAMS})
add_executable("${unit_test}" "${CMAKE_SOURCE_DIR}/t/unit-tests/${unit_test}.c")
target_link_libraries("${unit_test}" unit-test-lib common-main)
set_target_properties("${unit_test}"
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
if(MSVC)
set_target_properties("${unit_test}"
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
set_target_properties("${unit_test}"
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/t/unit-tests/bin)
endif()
list(APPEND PROGRAMS_BUILT "${unit_test}")

# t-basic intentionally fails tests, to validate the unit-test infrastructure.
# Therefore, it should only be run as part of t0080, which verifies that it
# fails only in the expected ways.
#
# All other unit tests should be run.
if(NOT ${unit_test} STREQUAL "t-basic")
add_test(NAME "t.unit-tests.${unit_test}"
COMMAND "./${unit_test}"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/t/unit-tests/bin)
endif()
endforeach()

#test-tool #test-tool
parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS") parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")


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


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


#test #test
foreach(tsh ${test_scipts}) foreach(tsh ${test_scripts})
add_test(NAME ${tsh} string(REGEX REPLACE ".*/(.*)\\.sh" "\\1" test_name ${tsh})
add_test(NAME "t.suite.${test_name}"
COMMAND ${SH_EXE} ${tsh} --no-bin-wrappers --no-chain-lint -vx COMMAND ${SH_EXE} ${tsh} --no-bin-wrappers --no-chain-lint -vx
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/t) WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/t)
endforeach() endforeach()


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


endif()#BUILD_TESTING endif()#BUILD_TESTING

View File

@ -42,7 +42,7 @@ TPERF = $(sort $(wildcard perf/p[0-9][0-9][0-9][0-9]-*.sh))
TINTEROP = $(sort $(wildcard interop/i[0-9][0-9][0-9][0-9]-*.sh)) TINTEROP = $(sort $(wildcard interop/i[0-9][0-9][0-9][0-9]-*.sh))
CHAINLINTTESTS = $(sort $(patsubst chainlint/%.test,%,$(wildcard chainlint/*.test))) CHAINLINTTESTS = $(sort $(patsubst chainlint/%.test,%,$(wildcard chainlint/*.test)))
CHAINLINT = '$(PERL_PATH_SQ)' chainlint.pl CHAINLINT = '$(PERL_PATH_SQ)' chainlint.pl
UNIT_TESTS = $(sort $(filter-out unit-tests/bin/t-basic%,$(wildcard unit-tests/bin/t-*))) UNIT_TESTS = $(sort $(filter-out %.pdb unit-tests/bin/t-basic%,$(wildcard unit-tests/bin/t-*)))


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

View File

@ -21,6 +21,46 @@ static struct {
.result = RESULT_NONE, .result = RESULT_NONE,
}; };


#ifndef _MSC_VER
#define make_relative(location) location
#else
/*
* Visual C interpolates the absolute Windows path for `__FILE__`,
* but we want to see relative paths, as verified by t0080.
*/
#include "dir.h"

static const char *make_relative(const char *location)
{
static char prefix[] = __FILE__, buf[PATH_MAX], *p;
static size_t prefix_len;

if (!prefix_len) {
size_t len = strlen(prefix);
const char *needle = "\\t\\unit-tests\\test-lib.c";
size_t needle_len = strlen(needle);

if (len < needle_len || strcmp(needle, prefix + len - needle_len))
die("unexpected suffix of '%s'", prefix);

/* let it end in a directory separator */
prefix_len = len - needle_len + 1;
}

/* Does it not start with the expected prefix? */
if (fspathncmp(location, prefix, prefix_len))
return location;

strlcpy(buf, location + prefix_len, sizeof(buf));
/* convert backslashes to forward slashes */
for (p = buf; *p; p++)
if (*p == '\\')
*p = '/';

return buf;
}
#endif

static void msg_with_prefix(const char *prefix, const char *format, va_list ap) static void msg_with_prefix(const char *prefix, const char *format, va_list ap)
{ {
fflush(stderr); fflush(stderr);
@ -147,7 +187,8 @@ int test__run_end(int was_run UNUSED, const char *location, const char *format,
break; break;


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


if (ctx.result == RESULT_SKIP) { if (ctx.result == RESULT_SKIP) {
test_msg("skipping check '%s' at %s", check, location); test_msg("skipping check '%s' at %s", check,
make_relative(location));
return 1; return 1;
} }
if (!ctx.todo) { if (!ctx.todo) {
if (ok) { if (ok) {
test_pass(); test_pass();
} else { } else {
test_msg("check \"%s\" failed at %s", check, location); test_msg("check \"%s\" failed at %s", check,
make_relative(location));
test_fail(); test_fail();
} }
} }
@ -225,7 +268,8 @@ int test__todo_end(const char *location, const char *check, int res)
if (ctx.result == RESULT_SKIP) if (ctx.result == RESULT_SKIP)
return 1; return 1;
if (res) { if (res) {
test_msg("todo check '%s' succeeded at %s", check, location); test_msg("todo check '%s' succeeded at %s", check,
make_relative(location));
test_fail(); test_fail();
} else { } else {
test_todo(); test_todo();