Skip to content

Commit

Permalink
Merge branch 'js/doc-unit-tests-with-cmake' into next
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gitster committed Nov 9, 2023
2 parents 7d00ffd + 694e89b commit b4503c9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3614,7 +3614,7 @@ rpm::
.PHONY: rpm

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

artifacts-tar:: $(ALL_COMMANDS_TO_INSTALL) $(SCRIPT_LIB) $(OTHER_PROGRAMS) \
Expand Down
38 changes: 34 additions & 4 deletions contrib/buildsystems/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,35 @@ target_link_libraries(test-fake-ssh common-main)
parse_makefile_for_sources(test-reftable_SOURCES "REFTABLE_TEST_OBJS")
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
parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")

Expand Down Expand Up @@ -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/)
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
foreach(tsh ${test_scipts})
add_test(NAME ${tsh}
foreach(tsh ${test_scripts})
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
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/t)
endforeach()

# 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
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
2 changes: 1 addition & 1 deletion t/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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))
CHAINLINTTESTS = $(sort $(patsubst chainlint/%.test,%,$(wildcard chainlint/*.test)))
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`)
# checks all tests in all scripts via a single invocation, so tell individual
Expand Down
52 changes: 48 additions & 4 deletions t/unit-tests/test-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,46 @@ static struct {
.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)
{
fflush(stderr);
Expand Down Expand Up @@ -147,7 +187,8 @@ int test__run_end(int was_run UNUSED, const char *location, const char *format,
break;

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);
print_description(format, ap);
ctx.result = RESULT_FAILURE;
Expand Down Expand Up @@ -193,14 +234,16 @@ int test_assert(const char *location, const char *check, int ok)
assert(ctx.running);

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;
}
if (!ctx.todo) {
if (ok) {
test_pass();
} else {
test_msg("check \"%s\" failed at %s", check, location);
test_msg("check \"%s\" failed at %s", check,
make_relative(location));
test_fail();
}
}
Expand All @@ -225,7 +268,8 @@ int test__todo_end(const char *location, const char *check, int res)
if (ctx.result == RESULT_SKIP)
return 1;
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();
} else {
test_todo();
Expand Down

0 comments on commit b4503c9

Please sign in to comment.