]> git.ipfire.org Git - thirdparty/git.git/commitdiff
unit-tests: show location of checks outside of tests
authorRené Scharfe <l.s.r@web.de>
Tue, 30 Jul 2024 14:07:00 +0000 (16:07 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 30 Jul 2024 20:32:43 +0000 (13:32 -0700)
Checks outside of tests are caught at runtime and reported like this:

 Assertion failed: (ctx.running), function test_assert, file test-lib.c, line 267.

The assert() call aborts the unit test and doesn't reveal the location
or even the type of the offending check, as test_assert() is called by
all of them.

Handle it like the opposite case, a test without any checks: Don't
abort, but report the location of the actual check, along with a message
explaining the situation.  The output for example above becomes:

 # BUG: check outside of test at t/helper/test-example-tap.c:75

... and the unit test program continues and indicates the error in its
exit code at the end.

Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/helper/test-example-tap.c
t/t0080-unit-test-output.sh
t/unit-tests/test-lib.c

index d072ad559f64f71823f25462832eb7bc53864828..79c12b01cdb76f188eaf84eae28590ba59a25af1 100644 (file)
@@ -72,6 +72,8 @@ static void t_empty(void)
 
 int cmd__example_tap(int argc, const char **argv)
 {
+       check(1);
+
        test_res = TEST(check_res = check_int(1, ==, 1), "passing test");
        TEST(t_res(1), "passing test and assertion return 1");
        test_res = TEST(check_res = check_int(1, ==, 2), "failing test");
index 9ec47b7360eaea4638e070bcf9a52be9e9a7c852..fe221f3bdbca51d157452634848a9466f48b3fd2 100755 (executable)
@@ -7,9 +7,10 @@ TEST_PASSES_SANITIZE_LEAK=true
 
 test_expect_success 'TAP output from unit tests' - <<\EOT
        cat >expect <<-EOF &&
+       # BUG: check outside of test at t/helper/test-example-tap.c:75
        ok 1 - passing test
        ok 2 - passing test and assertion return 1
-       # check "1 == 2" failed at t/helper/test-example-tap.c:77
+       # check "1 == 2" failed at t/helper/test-example-tap.c:79
        #    left: 1
        #   right: 2
        not ok 3 - failing test
@@ -46,7 +47,7 @@ test_expect_success 'TAP output from unit tests' - <<\EOT
        #    left: '\\\\'
        #   right: '\\''
        not ok 17 - messages from failing string and char comparison
-       # BUG: test has no checks at t/helper/test-example-tap.c:92
+       # BUG: test has no checks at t/helper/test-example-tap.c:94
        not ok 18 - test with no checks
        ok 19 - test with no checks returns 0
        1..19
index 3c513ce59a3e0d7dcbc16c1c7268a928f4d8352e..989dc758e6f60e3bd50c7d2c0628e641cc0fd995 100644 (file)
@@ -264,7 +264,12 @@ static void test_todo(void)
 
 int test_assert(const char *location, const char *check, int ok)
 {
-       assert(ctx.running);
+       if (!ctx.running) {
+               test_msg("BUG: check outside of test at %s",
+                        make_relative(location));
+               ctx.failed = 1;
+               return 0;
+       }
 
        if (ctx.result == RESULT_SKIP) {
                test_msg("skipping check '%s' at %s", check,