All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Phillip Wood <phillip.wood123@gmail.com>,
	Johannes Schindelin <johannes.schindelin@gmx.de>,
	Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH v3 3/7] unit-tests: do show relative file paths
Date: Mon, 25 Sep 2023 11:20:32 +0000	[thread overview]
Message-ID: <f0b804129e8a21449cbb6f346473d3570182ddfa.1695640837.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1579.v3.git.1695640836.gitgitgadget@gmail.com>

From: Johannes Schindelin <johannes.schindelin@gmx.de>

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>
---
 t/unit-tests/test-lib.c | 52 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 48 insertions(+), 4 deletions(-)

diff --git a/t/unit-tests/test-lib.c b/t/unit-tests/test-lib.c
index 70030d587ff..744e223ee98 100644
--- a/t/unit-tests/test-lib.c
+++ b/t/unit-tests/test-lib.c
@@ -21,6 +21,46 @@ 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 "dir.h"
+
+static const char *make_relative(const char *location)
+{
+	static char prefix[] = __FILE__, buf[PATH_MAX], *p;
+	static size_t prefix_len;
+
+	if (!prefix_len) {
+		size_t len = strlen(prefix);
+		const 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);
+
+		/* let it end in a directory separator */
+		prefix_len = len - needle_len + 1;
+	}
+
+	/* Does it not start with the expected prefix? */
+	if (fspathncmp(location, prefix, prefix_len))
+		return location;
+
+	strlcpy(buf, location + prefix_len, sizeof(buf));
+	/* convert backslashes to forward slashes */
+	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)
 {
 	fflush(stderr);
@@ -147,7 +187,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;
@@ -193,13 +234,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();
 		}
 	}
@@ -224,7 +267,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();
-- 
gitgitgadget


  parent reply	other threads:[~2023-09-25 11:20 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-31  6:15 [PATCH 0/4] CMake(Visual C) support for js/doc-unit-tests Johannes Schindelin via GitGitGadget
2023-08-31  6:15 ` [PATCH 1/4] cmake: also build unit tests Johannes Schindelin via GitGitGadget
2023-09-11 13:23   ` Phillip Wood
2023-09-18 20:58     ` Johannes Schindelin
2023-08-31  6:15 ` [PATCH 2/4] unit-tests: do not mistake `.pdb` files for being executable Johannes Schindelin via GitGitGadget
2023-08-31  6:15 ` [PATCH 3/4] unit-tests: do show relative file paths Johannes Schindelin via GitGitGadget
2023-09-11 13:25   ` Phillip Wood
2023-09-18 21:00     ` Johannes Schindelin
2023-08-31  6:15 ` [PATCH 4/4] artifacts-tar: when including `.dll` files, don't forget the unit-tests Johannes Schindelin via GitGitGadget
2023-09-18 20:54 ` [PATCH v2 0/7] CMake(Visual C) support for js/doc-unit-tests Johannes Schindelin via GitGitGadget
2023-09-18 20:54   ` [PATCH v2 1/7] cmake: also build unit tests Johannes Schindelin via GitGitGadget
2023-09-18 20:54   ` [PATCH v2 2/7] unit-tests: do not mistake `.pdb` files for being executable Johannes Schindelin via GitGitGadget
2023-09-18 20:54   ` [PATCH v2 3/7] unit-tests: do show relative file paths Johannes Schindelin via GitGitGadget
2023-09-22 14:35     ` Phillip Wood
2023-09-18 20:54   ` [PATCH v2 4/7] artifacts-tar: when including `.dll` files, don't forget the unit-tests Johannes Schindelin via GitGitGadget
2023-09-18 20:54   ` [PATCH v2 5/7] cmake: fix typo in variable name Johannes Schindelin via GitGitGadget
2023-09-18 20:54   ` [PATCH v2 6/7] cmake: use test names instead of full paths Johannes Schindelin via GitGitGadget
2023-09-22 14:37     ` Phillip Wood
2023-09-25  9:06       ` Johannes Schindelin
2023-09-18 20:54   ` [PATCH v2 7/7] cmake: handle also unit tests Johannes Schindelin via GitGitGadget
2023-09-22 14:39     ` Phillip Wood
2023-09-25 11:20   ` [PATCH v3 0/7] CMake(Visual C) support for js/doc-unit-tests Johannes Schindelin via GitGitGadget
2023-09-25 11:20     ` [PATCH v3 1/7] cmake: also build unit tests Johannes Schindelin via GitGitGadget
2023-09-25 11:20     ` [PATCH v3 2/7] unit-tests: do not mistake `.pdb` files for being executable Johannes Schindelin via GitGitGadget
2023-09-25 11:20     ` Johannes Schindelin via GitGitGadget [this message]
2023-09-25 11:20     ` [PATCH v3 4/7] artifacts-tar: when including `.dll` files, don't forget the unit-tests Johannes Schindelin via GitGitGadget
2023-09-25 11:20     ` [PATCH v3 5/7] cmake: fix typo in variable name Johannes Schindelin via GitGitGadget
2023-09-25 11:20     ` [PATCH v3 6/7] cmake: use test names instead of full paths Johannes Schindelin via GitGitGadget
2023-09-25 11:20     ` [PATCH v3 7/7] cmake: handle also unit tests Johannes Schindelin via GitGitGadget
2023-09-25 19:09     ` [PATCH v3 0/7] CMake(Visual C) support for js/doc-unit-tests Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f0b804129e8a21449cbb6f346473d3570182ddfa.1695640837.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=johannes.schindelin@gmx.de \
    --cc=phillip.wood123@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.