Skip to content

Commit

Permalink
unit-tests: do show relative file paths
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dscho committed Aug 30, 2023
1 parent 3a1163a commit 035f2bb
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions t/unit-tests/test-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,42 @@ static struct {
.result = RESULT_NONE,
};

#ifndef _MSC_VER
#define make_relative(location) location
#else
/*
* Visual C interpolates the absolute Windows path for `__FILE__`,
* but we want to see relative paths, as verified by t0080.
*/
#include "strbuf.h"
#include "dir.h"

static const char *make_relative(const char *location)
{
static const char *prefix;
static size_t prefix_len;
static struct strbuf buf = STRBUF_INIT;

if (!prefix) {
strbuf_addstr(&buf, __FILE__);
if (!strbuf_strip_suffix(&buf, "\\t\\unit-tests\\test-lib.c"))
die("unexpected suffix of '%s'");
strbuf_complete(&buf, '\\');
prefix = strbuf_detach(&buf, &prefix_len);
}

/* Does it not start with the expected prefix? */
if (fspathncmp(location, prefix, prefix_len))
return location;

strbuf_reset(&buf);
strbuf_addstr(&buf, location + prefix_len);
convert_slashes(buf.buf);

return buf.buf;
}
#endif

static void msg_with_prefix(const char *prefix, const char *format, va_list ap)
{
fflush(stderr);
Expand Down Expand Up @@ -147,7 +183,8 @@ int test__run_end(int was_run UNUSED, const char *location, const char *format,
break;

case RESULT_NONE:
test_msg("BUG: test has no checks at %s", location);
test_msg("BUG: test has no checks at %s",
make_relative(location));
printf("not ok %d", ctx.count);
print_description(format, ap);
ctx.result = RESULT_FAILURE;
Expand Down Expand Up @@ -193,13 +230,15 @@ int test_assert(const char *location, const char *check, int ok)
assert(ctx.running);

if (ctx.result == RESULT_SKIP) {
test_msg("skipping check '%s' at %s", check, location);
test_msg("skipping check '%s' at %s", check,
make_relative(location));
return 0;
} else if (!ctx.todo) {
if (ok) {
test_pass();
} else {
test_msg("check \"%s\" failed at %s", check, location);
test_msg("check \"%s\" failed at %s", check,
make_relative(location));
test_fail();
}
}
Expand All @@ -224,7 +263,8 @@ int test__todo_end(const char *location, const char *check, int res)
if (ctx.result == RESULT_SKIP)
return 0;
if (!res) {
test_msg("todo check '%s' succeeded at %s", check, location);
test_msg("todo check '%s' succeeded at %s", check,
make_relative(location));
test_fail();
} else {
test_todo();
Expand Down

0 comments on commit 035f2bb

Please sign in to comment.