summaryrefslogtreecommitdiff
path: root/t/unit-tests
diff options
context:
space:
mode:
authorJiang Xin <worldhello.net@gmail.com>2024-02-15 09:48:25 +0800
committerJiang Xin <worldhello.net@gmail.com>2024-02-15 09:48:25 +0800
commitf98643fcb247a3613291d70f6f8e6f80b359c56b (patch)
treea518d7335830f5772ece84931c93910063cb25bf /t/unit-tests
parent6032aee65ec5c68c2f43761d1df4d227f7b5966e (diff)
parent4fc51f00ef18d2c0174ab2fd39d0ee473fd144bd (diff)
Merge branch 'master' of github.com:git/git
* 'master' of github.com:git/git: (51 commits) Hopefully the last batch of fixes before 2.44 final Git 2.43.2 A few more fixes before -rc1 write-or-die: fix the polarity of GIT_FLUSH environment variable A few more topics before -rc1 completion: add and use __git_compute_second_level_config_vars_for_section completion: add and use __git_compute_first_level_config_vars_for_section completion: complete 'submodule.*' config variables completion: add space after config variable names also in Bash 3 receive-pack: use find_commit_header() in check_nonce() ci(linux32): add a note about Actions that must not be updated ci: bump remaining outdated Actions versions unit-tests: do show relative file paths on non-Windows, too receive-pack: use find_commit_header() in check_cert_push_options() prune: mark rebase autostash and orig-head as reachable sequencer: unset GIT_CHERRY_PICK_HELP for 'exec' commands ref-filter.c: sort formatted dates by byte value ssh signing: signal an error with a negative return value bisect: document command line arguments for "bisect start" bisect: document "terms" subcommand more fully ...
Diffstat (limited to 't/unit-tests')
-rw-r--r--t/unit-tests/test-lib.c61
1 files changed, 47 insertions, 14 deletions
diff --git a/t/unit-tests/test-lib.c b/t/unit-tests/test-lib.c
index 7bf9dfdb95..66d6980ffb 100644
--- a/t/unit-tests/test-lib.c
+++ b/t/unit-tests/test-lib.c
@@ -21,12 +21,11 @@ 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.
+ * There are other compilers that do the same, and are not for
+ * Windows.
*/
#include "dir.h"
@@ -34,32 +33,66 @@ static const char *make_relative(const char *location)
{
static char prefix[] = __FILE__, buf[PATH_MAX], *p;
static size_t prefix_len;
+ static int need_bs_to_fs = -1;
- if (!prefix_len) {
+ /* one-time preparation */
+ if (need_bs_to_fs < 0) {
size_t len = strlen(prefix);
- const char *needle = "\\t\\unit-tests\\test-lib.c";
+ char needle[] = "t\\unit-tests\\test-lib.c";
size_t needle_len = strlen(needle);
- if (len < needle_len || strcmp(needle, prefix + len - needle_len))
- die("unexpected suffix of '%s'", prefix);
+ if (len < needle_len)
+ die("unexpected prefix '%s'", prefix);
+
+ /*
+ * The path could be relative (t/unit-tests/test-lib.c)
+ * or full (/home/user/git/t/unit-tests/test-lib.c).
+ * Check the slash between "t" and "unit-tests".
+ */
+ prefix_len = len - needle_len;
+ if (prefix[prefix_len + 1] == '/') {
+ /* Oh, we're not Windows */
+ for (size_t i = 0; i < needle_len; i++)
+ if (needle[i] == '\\')
+ needle[i] = '/';
+ need_bs_to_fs = 0;
+ } else {
+ need_bs_to_fs = 1;
+ }
- /* let it end in a directory separator */
- prefix_len = len - needle_len + 1;
+ /*
+ * prefix_len == 0 if the compiler gives paths relative
+ * to the root of the working tree. Otherwise, we want
+ * to see that we did find the needle[] at a directory
+ * boundary. Again we rely on that needle[] begins with
+ * "t" followed by the directory separator.
+ */
+ if (fspathcmp(needle, prefix + prefix_len) ||
+ (prefix_len && prefix[prefix_len - 1] != needle[1]))
+ die("unexpected suffix of '%s'", prefix);
}
- /* Does it not start with the expected prefix? */
- if (fspathncmp(location, prefix, prefix_len))
+ /*
+ * Does it not start with the expected prefix?
+ * Return it as-is without making it worse.
+ */
+ if (prefix_len && fspathncmp(location, prefix, prefix_len))
return location;
- strlcpy(buf, location + prefix_len, sizeof(buf));
+ /*
+ * If we do not need to munge directory separator, we can return
+ * the substring at the tail of the location.
+ */
+ if (!need_bs_to_fs)
+ return location + prefix_len;
+
/* convert backslashes to forward slashes */
+ strlcpy(buf, location + prefix_len, sizeof(buf));
for (p = buf; *p; p++)
if (*p == '\\')
*p = '/';
-
return buf;
}
-#endif
static void msg_with_prefix(const char *prefix, const char *format, va_list ap)
{