Skip to content

Commit a2c5e29

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0df903d commit a2c5e29

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

t/unit-tests/test-lib.c

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,46 @@ static struct {
2121
.result = RESULT_NONE,
2222
};
2323

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+
2464
static void msg_with_prefix(const char *prefix, const char *format, va_list ap)
2565
{
2666
fflush(stderr);
@@ -147,7 +187,8 @@ int test__run_end(int was_run UNUSED, const char *location, const char *format,
147187
break;
148188

149189
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));
151192
printf("not ok %d", ctx.count);
152193
print_description(format, ap);
153194
ctx.result = RESULT_FAILURE;
@@ -193,14 +234,16 @@ int test_assert(const char *location, const char *check, int ok)
193234
assert(ctx.running);
194235

195236
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));
197239
return 1;
198240
}
199241
if (!ctx.todo) {
200242
if (ok) {
201243
test_pass();
202244
} else {
203-
test_msg("check \"%s\" failed at %s", check, location);
245+
test_msg("check \"%s\" failed at %s", check,
246+
make_relative(location));
204247
test_fail();
205248
}
206249
}
@@ -225,7 +268,8 @@ int test__todo_end(const char *location, const char *check, int res)
225268
if (ctx.result == RESULT_SKIP)
226269
return 1;
227270
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));
229273
test_fail();
230274
} else {
231275
test_todo();

0 commit comments

Comments
 (0)