Git development
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Subject: [PATCH 5/8] t: run clar unit tests through test-lib.sh
Date: Tue, 27 Oct 2026 13:38:24 -0400	[thread overview]
Message-ID: <20261027173824.GE136740@coredump.intra.peff.net> (raw)
In-Reply-To: <20261027173205.GA97799@coredump.intra.peff.net>

We run the clar unit-test binary directly, without going through
test-lib.sh. This means it misses out on the setup done by the shell
tests, and is inconsistent with the rest of the test suite.

I've hit two specific cases in practice. One is that we accept but
ignore --verbose-log. This means that if unit-tests fail in CI (which
runs the tests under prove, eating their stdout), we never get to see
the failing test output at all!

Another is that test-lib.sh sets sanitizer options like LSAN_OPTIONS.
You can see the difference by creating an intentional leak like this:

  diff --git a/t/unit-tests/u-strbuf.c b/t/unit-tests/u-strbuf.c
  index caa5d78aa3..4d252902ed 100644
  --- a/t/unit-tests/u-strbuf.c
  +++ b/t/unit-tests/u-strbuf.c
  @@ -84,6 +84,8 @@ static void t_addstr(struct strbuf *buf, const void *data)
   	size_t orig_alloc = buf->alloc;
   	size_t orig_len = buf->len;

  +	write(2, strdup("foo"), 3);
  +
   	assert_sane_strbuf(buf);
   	strbuf_addstr(buf, text);
   	assert_sane_strbuf(buf);

Without fast_unwind_on_malloc=0, you get a fairly useless report. With
test-lib.sh's settings, we get the full stack trace.

Obviously we could port these features into the unit-test code. But by
wrapping them in a regular test script we get that for free, plus it
will let us get rid of some extra complexities in our prove invocations
(in subsequent patches).

We do have to forward any relevant options to the unit-test binary. We
should do that here in t0011 and not as a central test-lib.sh feature,
because the set of handled options may differ between external programs
(if we ever even get a second one). Right now we support --immediate and
nothing else. In theory we should be able to handle --run, but the
syntax is a bit different between the two systems. This is no worse than
the existing behavior, though, since run-test.sh only passes
GIT_TEST_OPTS to shell tests, not to the unit-test binary.

Likewise we do not support --write-junit-xml; the unit-tests will run
but the results won't be present in the XML output. It would be possible
to support this with some work, but for now we are not making anything
worse (the unit-tests are currently run separately and do not even
support the option).

One note on the implementation: we have to inform meson of the
dependency. Without that, running "meson test t0011-unit-tests" would
fail (but "meson compile" first would be OK, because it builds the unit
tests). The make system doesn't have the same problem, because it just
depends on "all" before testing (the meson equivalent would be depending
on "compile", but it is more fine-grained).

Signed-off-by: Jeff King <peff@peff.net>
---
 t/meson.build         |  3 +++
 t/t0011-unit-tests.sh | 20 ++++++++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100755 t/t0011-unit-tests.sh

diff --git a/t/meson.build b/t/meson.build
index 11b3752f00..fc628ad2d1 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -76,6 +76,8 @@ clar_unit_tests = executable('unit-tests',
 )
 test('unit-tests', clar_unit_tests, kwargs: test_kwargs)
 
+test_dependencies += clar_unit_tests
+
 subdir('helper')
 
 integration_tests = [
@@ -90,6 +92,7 @@ integration_tests = [
   't0008-ignores.sh',
   't0009-git-dir-validation.sh',
   't0010-racy-git.sh',
+  't0011-unit-tests.sh',
   't0012-help.sh',
   't0013-sha1dc.sh',
   't0014-alias.sh',
diff --git a/t/t0011-unit-tests.sh b/t/t0011-unit-tests.sh
new file mode 100755
index 0000000000..ac3d7ccda2
--- /dev/null
+++ b/t/t0011-unit-tests.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+test_description='run clar unit tests'
+test_external=t
+. ./test-lib.sh
+
+# We store the binary in different locations for make vs meson. Yuck.
+if test -d "$GIT_BUILD_DIR/t/unit-tests"
+then
+	UNIT_TESTS="$GIT_BUILD_DIR/t/unit-tests/bin/unit-tests"
+else
+	UNIT_TESTS="$GIT_BUILD_DIR/t/unit-tests"
+fi
+
+# Pass along any options that are meaningful to the unit-test binary here.
+"$UNIT_TESTS" \
+	${immediate:+-i} \
+	|| error "unit-test binary failed"
+
+test_done
-- 
2.56.0.559.g7c1a1d8168


  parent reply	other threads:[~2026-10-27 17:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27 17:32 [PATCH 0/8] running unit tests inside a test-lib script Jeff King
2026-10-27 17:32 ` [PATCH 1/8] unit-tests: report the correct unexpected argument Jeff King
2026-10-27 17:32 ` [PATCH 2/8] test-lib: drop unused test_external_has_tap variable Jeff King
2026-10-27 17:35 ` [PATCH 3/8] test-lib: stop inventing a JUnit testcase for empty suites Jeff King
2026-10-27 17:36 ` [PATCH 4/8] test-lib: allow an external program to supply TAP output Jeff King
2026-10-27 17:38 ` Jeff King [this message]
2026-10-27 17:40 ` [PATCH 6/8] t: drop redundant unit-test invocations Jeff King
2026-10-27 17:41 ` [RFC/PATCH 7/8] test-tool: drop the run-command testsuite mode Jeff King
2026-10-27 17:42 ` [RFC/PATCH 8/8] test-lib: drop the shell JUnit XML reporting code Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261027173824.GE136740@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox