unit-tests: do show relative file paths

Visual C interpolates `__FILE__` with the absolute _Windows_ path of the source file. GCC interpolates it with the relative path, and the tests even verify that. So let's make sure that the unit tests only emit such paths. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Sep 25, 2023 at 11:20 UTC a2c5e294dbb095023e6c0d53b302c4363021f1b2
1 file changed +48 -4
t/unit-tests/test-lib.c
+48 -4
@@ -21,6 +21,46 @@ static struct {
21 .result = RESULT_NONE,
22 };
23
24 +#ifndef _MSC_VER
25 +#define make_relative(location) location
26 +#else
27 +/*
28 + * Visual C interpolates the absolute Windows path for `__FILE__`,
29 + * but we want to see relative paths, as verified by t0080.
30 + */
31 +#include "dir.h"
32 +
33 +static const char *make_relative(const char *location)
34 +{
35 + static char prefix[] = __FILE__, buf[PATH_MAX], *p;
36 + static size_t prefix_len;
37 +
38 + if (!prefix_len) {
39 + size_t len = strlen(prefix);
40 + const char *needle = "\\t\\unit-tests\\test-lib.c";
41 + size_t needle_len = strlen(needle);
42 +
43 + if (len < needle_len || strcmp(needle, prefix + len - needle_len))
44 + die("unexpected suffix of '%s'", prefix);
45 +
46 + /* let it end in a directory separator */
47 + prefix_len = len - needle_len + 1;
48 + }
49 +
50 + /* Does it not start with the expected prefix? */
51 + if (fspathncmp(location, prefix, prefix_len))
52 + return location;
53 +
54 + strlcpy(buf, location + prefix_len, sizeof(buf));
55 + /* convert backslashes to forward slashes */
56 + for (p = buf; *p; p++)
57 + if (*p == '\\')
58 + *p = '/';
59 +
60 + return buf;
61 +}
62 +#endif
63 +
64 static void msg_with_prefix(const char *prefix, const char *format, va_list ap)
65 {
66 fflush(stderr);
@@ -147,7 +187,8 @@ int test__run_end(int was_run UNUSED, const char *location, const char *format,
187 break;
188
189 case RESULT_NONE:
150 - test_msg("BUG: test has no checks at %s", location);
190 + test_msg("BUG: test has no checks at %s",
191 + make_relative(location));
192 printf("not ok %d", ctx.count);
193 print_description(format, ap);
194 ctx.result = RESULT_FAILURE;
@@ -193,14 +234,16 @@ int test_assert(const char *location, const char *check, int ok)
234 assert(ctx.running);
235
236 if (ctx.result == RESULT_SKIP) {
196 - test_msg("skipping check '%s' at %s", check, location);
237 + test_msg("skipping check '%s' at %s", check,
238 + make_relative(location));
239 return 1;
240 }
241 if (!ctx.todo) {
242 if (ok) {
243 test_pass();
244 } else {
203 - test_msg("check \"%s\" failed at %s", check, location);
245 + test_msg("check \"%s\" failed at %s", check,
246 + make_relative(location));
247 test_fail();
248 }
249 }
@@ -225,7 +268,8 @@ int test__todo_end(const char *location, const char *check, int res)
268 if (ctx.result == RESULT_SKIP)
269 return 1;
270 if (res) {
228 - test_msg("todo check '%s' succeeded at %s", check, location);
271 + test_msg("todo check '%s' succeeded at %s", check,
272 + make_relative(location));
273 test_fail();
274 } else {
275 test_todo();