git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/4] unit-tests: do show relative file paths

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Sep 11, 2023, 13:25 UTC
Message-ID
<69f6f263-06e1-4fef-abd9-d6c03ae0c148@gmail.com>
In-Reply-To
<2b4e36c05c9e01b1e489100531fd01515b0786ab.1693462532.git.gitgitgadget@gmail.com>
Hi Johannes
On 31/08/2023 07:15, Johannes Schindelin via GitGitGadget wrote:
Show 5 quoted lines
> 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.
Oh, that's a pain
> So let's make sure that the unit tests only emit such paths.
Makes sense
Show 15 quoted lines
> +#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;

So far test-lib.c avoids using things like struct strbuf that it will be used to test. In this instance we're only using it on one particular compiler so it may not matter so much. We could avoid it but I'm not sure it is worth the extra complexity. One thing I noted in this patch is that prefix is leaked but I'm not sure if you run any leak checkers on the msvc build.

static const char *make_relative(const char *location)
{
	static char prefix[] = __FILE__
	static size_t *prefix_len = (size_t)-1;
	static char buf[PATH_MAX];
	if (prefix == (size_t)-1) {
		const char *path = "\\t\unit-tests\\test-lib.c";
		size_t path_len = strlen(path);
		
		prefix_len = strlen(prefix);
		if (prefix_len < path_len) ||
		    memcmp(prefix + prefix_len - path_len, path, path_len)
			die(...);
		prefix_len -= path_len - 1; /* keep trailing '\\' */
		prefix[prefix_len] = '\0';
	}
	/* Does it not start with the expected prefix? */
	if (fspathncmp(location, prefix, prefix_len))
		return location;
	if (strlen(location) - prefix_len > sizeof(buf) - 1)
		die(...)
	/* +1 to copy NUL terminator */
	memcpy(buf, location + prefix_len, strlen(location) - prefix_len + 1);
	convert_slashes(buf);
	return buf;
}
Best Wishes
Phillip
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin
Message 6 of 30 in “CMake(Visual C) support for js/doc-unit-tests”
  1. 0/4 CMake(Visual C) support for js/doc-unit-testsJohannes Schindelin via GitGitGadget, Aug 31, 2023
  2. 1/4 cmake: also build unit testsJohannes Schindelin via GitGitGadget, Aug 31, 2023
  3. Phillip WoodSep 11, 2023
  4. Johannes SchindelinSep 18, 2023
  5. 3/4 unit-tests: do show relative file pathsJohannes Schindelin via GitGitGadget, Aug 31, 2023
  6. Phillip WoodSep 11, 2023
  7. Johannes SchindelinSep 18, 2023
  8. 2/4 unit-tests: do not mistake `.pdb` files for being executableJohannes Schindelin via GitGitGadget, Aug 31, 2023
  9. 4/4 artifacts-tar: when including `.dll` files, don't forget the unit-testsJohannes Schindelin via GitGitGadget, Aug 31, 2023
  10. 0/7 CMake(Visual C) support for js/doc-unit-testsJohannes Schindelin via GitGitGadget, Sep 18, 2023
  11. 1/7 cmake: also build unit testsJohannes Schindelin via GitGitGadget, Sep 18, 2023
  12. 2/7 unit-tests: do not mistake `.pdb` files for being executableJohannes Schindelin via GitGitGadget, Sep 18, 2023
  13. 3/7 unit-tests: do show relative file pathsJohannes Schindelin via GitGitGadget, Sep 18, 2023
  14. Phillip WoodSep 22, 2023
  15. 4/7 artifacts-tar: when including `.dll` files, don't forget the unit-testsJohannes Schindelin via GitGitGadget, Sep 18, 2023
  16. 5/7 cmake: fix typo in variable nameJohannes Schindelin via GitGitGadget, Sep 18, 2023
  17. 6/7 cmake: use test names instead of full pathsJohannes Schindelin via GitGitGadget, Sep 18, 2023
  18. Phillip WoodSep 22, 2023
  19. Johannes SchindelinSep 25, 2023
  20. 7/7 cmake: handle also unit testsJohannes Schindelin via GitGitGadget, Sep 18, 2023
  21. Phillip WoodSep 22, 2023
  22. 0/7 CMake(Visual C) support for js/doc-unit-testsJohannes Schindelin via GitGitGadget, Sep 25, 2023
  23. 2/7 unit-tests: do not mistake `.pdb` files for being executableJohannes Schindelin via GitGitGadget, Sep 25, 2023
  24. 1/7 cmake: also build unit testsJohannes Schindelin via GitGitGadget, Sep 25, 2023
  25. 3/7 unit-tests: do show relative file pathsJohannes Schindelin via GitGitGadget, Sep 25, 2023
  26. 4/7 artifacts-tar: when including `.dll` files, don't forget the unit-testsJohannes Schindelin via GitGitGadget, Sep 25, 2023
  27. 5/7 cmake: fix typo in variable nameJohannes Schindelin via GitGitGadget, Sep 25, 2023
  28. 7/7 cmake: handle also unit testsJohannes Schindelin via GitGitGadget, Sep 25, 2023
  29. 6/7 cmake: use test names instead of full pathsJohannes Schindelin via GitGitGadget, Sep 25, 2023
  30. Junio C HamanoSep 25, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.