test-tool: help verifying BUG() code paths

When we call BUG(), we signal via SIGABRT that something bad happened, dumping cores if so configured. In some setups these coredumps are redirected to some central place such as /proc/sys/kernel/core_pattern, which is a good thing. However, when we try to verify in our test suite that bugs are caught in certain code paths, we do *not* want to clutter such a central place with unnecessary coredumps. So let's special-case the test helpers (which we use to verify such code paths) so that the BUG() calls will *not* call abort() but exit with a special-purpose exit code instead. Helped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed May 2, 2018 at 11:38 UTC a86303cb5d5772364a3a5080d97be6f1a577be4c
2 files changed +7
t/helper/test-tool.c
+2
@@ -47,7 +47,9 @@ static struct test_cmd cmds[] = {
47 int cmd_main(int argc, const char **argv)
48 {
49 int i;
50 + extern int BUG_exit_code;
51
52 + BUG_exit_code = 99;
53 if (argc < 2)
54 die("I need a test name!");
55
usage.c
+5
@@ -210,6 +210,9 @@ void warning(const char *warn, ...)
210 va_end(params);
211 }
212
213 +/* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
214 +int BUG_exit_code;
215 +
216 static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
217 {
218 char prefix[256];
@@ -221,6 +224,8 @@ static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_lis
224 snprintf(prefix, sizeof(prefix), "BUG: ");
225
226 vreportf(prefix, fmt, params);
227 + if (BUG_exit_code)
228 + exit(BUG_exit_code);
229 abort();
230 }
231