Skip to content

Commit 1f452d6

Browse files
rscharfegitster
authored andcommitted
unit-tests: show location of checks outside of tests
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4575ba6 commit 1f452d6

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

t/helper/test-example-tap.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ static void t_empty(void)
7272

7373
int cmd__example_tap(int argc, const char **argv)
7474
{
75+
check(1);
76+
7577
test_res = TEST(check_res = check_int(1, ==, 1), "passing test");
7678
TEST(t_res(1), "passing test and assertion return 1");
7779
test_res = TEST(check_res = check_int(1, ==, 2), "failing test");

t/t0080-unit-test-output.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ TEST_PASSES_SANITIZE_LEAK=true
77

88
test_expect_success 'TAP output from unit tests' - <<\EOT
99
cat >expect <<-EOF &&
10+
# BUG: check outside of test at t/helper/test-example-tap.c:75
1011
ok 1 - passing test
1112
ok 2 - passing test and assertion return 1
12-
# check "1 == 2" failed at t/helper/test-example-tap.c:77
13+
# check "1 == 2" failed at t/helper/test-example-tap.c:79
1314
# left: 1
1415
# right: 2
1516
not ok 3 - failing test
@@ -46,7 +47,7 @@ test_expect_success 'TAP output from unit tests' - <<\EOT
4647
# left: '\\\\'
4748
# right: '\\''
4849
not ok 17 - messages from failing string and char comparison
49-
# BUG: test has no checks at t/helper/test-example-tap.c:92
50+
# BUG: test has no checks at t/helper/test-example-tap.c:94
5051
not ok 18 - test with no checks
5152
ok 19 - test with no checks returns 0
5253
1..19

t/unit-tests/test-lib.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,12 @@ static void test_todo(void)
264264

265265
int test_assert(const char *location, const char *check, int ok)
266266
{
267-
assert(ctx.running);
267+
if (!ctx.running) {
268+
test_msg("BUG: check outside of test at %s",
269+
make_relative(location));
270+
ctx.failed = 1;
271+
return 0;
272+
}
268273

269274
if (ctx.result == RESULT_SKIP) {
270275
test_msg("skipping check '%s' at %s", check,

0 commit comments

Comments
 (0)