unit-tests: do show relative file paths on non-Windows, too

There are compilers other than Visual C that want to show absolute paths. Generalize the helper introduced by a2c5e294 (unit-tests: do show relative file paths, 2023-09-25) so that it can also work with a path that uses slash as the directory separator, and becomes almost no-op once one-time preparation finds out that we are using a compiler that already gives relative paths. Incidentally, this also should do the right thing on Windows with a compiler that shows relative paths but with backslash as the directory separator (if such a thing exists and is used to build git). Reported-by: Randall S. Becker <rsbecker@nexbridge.com> Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Feb 11, 2024 at 07:58 UTC f66286364f7875f9b3e3ac63733dd98e396fb7b8
1 file changed +47 -14
t/unit-tests/test-lib.c
+47 -14
@@ -21,12 +21,11 @@ static struct {
21 .result = RESULT_NONE,
22 };
23
24 -#ifndef _MSC_VER
25 -#define make_relative(location) location
26 -#else
24 /*
25 * Visual C interpolates the absolute Windows path for `__FILE__`,
26 * but we want to see relative paths, as verified by t0080.
27 + * There are other compilers that do the same, and are not for
28 + * Windows.
29 */
30 #include "dir.h"
31
@@ -34,32 +33,66 @@ static const char *make_relative(const char *location)
33 {
34 static char prefix[] = __FILE__, buf[PATH_MAX], *p;
35 static size_t prefix_len;
36 + static int need_bs_to_fs = -1;
37
38 - if (!prefix_len) {
38 + /* one-time preparation */
39 + if (need_bs_to_fs < 0) {
40 size_t len = strlen(prefix);
40 - const char *needle = "\\t\\unit-tests\\test-lib.c";
41 + char needle[] = "t\\unit-tests\\test-lib.c";
42 size_t needle_len = strlen(needle);
43
43 - if (len < needle_len || strcmp(needle, prefix + len - needle_len))
44 - die("unexpected suffix of '%s'", prefix);
44 + if (len < needle_len)
45 + die("unexpected prefix '%s'", prefix);
46 +
47 + /*
48 + * The path could be relative (t/unit-tests/test-lib.c)
49 + * or full (/home/user/git/t/unit-tests/test-lib.c).
50 + * Check the slash between "t" and "unit-tests".
51 + */
52 + prefix_len = len - needle_len;
53 + if (prefix[prefix_len + 1] == '/') {
54 + /* Oh, we're not Windows */
55 + for (size_t i = 0; i < needle_len; i++)
56 + if (needle[i] == '\\')
57 + needle[i] = '/';
58 + need_bs_to_fs = 0;
59 + } else {
60 + need_bs_to_fs = 1;
61 + }
62
46 - /* let it end in a directory separator */
47 - prefix_len = len - needle_len + 1;
63 + /*
64 + * prefix_len == 0 if the compiler gives paths relative
65 + * to the root of the working tree. Otherwise, we want
66 + * to see that we did find the needle[] at a directory
67 + * boundary. Again we rely on that needle[] begins with
68 + * "t" followed by the directory separator.
69 + */
70 + if (fspathcmp(needle, prefix + prefix_len) ||
71 + (prefix_len && prefix[prefix_len - 1] != needle[1]))
72 + die("unexpected suffix of '%s'", prefix);
73 }
74
50 - /* Does it not start with the expected prefix? */
51 - if (fspathncmp(location, prefix, prefix_len))
75 + /*
76 + * Does it not start with the expected prefix?
77 + * Return it as-is without making it worse.
78 + */
79 + if (prefix_len && fspathncmp(location, prefix, prefix_len))
80 return location;
81
54 - strlcpy(buf, location + prefix_len, sizeof(buf));
82 + /*
83 + * If we do not need to munge directory separator, we can return
84 + * the substring at the tail of the location.
85 + */
86 + if (!need_bs_to_fs)
87 + return location + prefix_len;
88 +
89 /* convert backslashes to forward slashes */
90 + strlcpy(buf, location + prefix_len, sizeof(buf));
91 for (p = buf; *p; p++)
92 if (*p == '\\')
93 *p = '/';
59 -
94 return buf;
95 }
62 -#endif
96
97 static void msg_with_prefix(const char *prefix, const char *format, va_list ap)
98 {