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

[PATCH 2/3] test-tool: add `getuid` subcommand

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Nov 27, 2020, 07:50 UTC
Message-ID
<20201127075054.31174-3-sunshine@sunshineco.com>
In-Reply-To
<20201127075054.31174-1-sunshine@sunshineco.com>

It is difficult, if not impossible, for a test to obtain the user ID in a portable fashion on all platforms and be guaranteed that it is the same user ID Git itself sees when invoking getuid(). This is especially true on Microsoft Windows for which getuid() in C code and $(id -u) in a shell script may return entirely different values. Sidestep this problem by adding a `getuid` subcommand to test-tool which is guaranteed to provide tests with the same value as getuid() returns in Git code.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 Makefile               | 1 +
 t/helper/test-getuid.c | 7 +++++++
 t/helper/test-tool.c   | 1 +
 t/helper/test-tool.h   | 1 +
 4 files changed, 10 insertions(+)
 create mode 100644 t/helper/test-getuid.c
diff --git a/Makefile b/Makefile
index c39b39bd7d..a5c38aad5e 100644
--- a/Makefile
+++ b/Makefile
@@ -703,6 +703,7 @@ TEST_BUILTINS_OBJS += test-dump-untracked-cache.o
 TEST_BUILTINS_OBJS += test-example-decorate.o
 TEST_BUILTINS_OBJS += test-genrandom.o
 TEST_BUILTINS_OBJS += test-genzeros.o
+TEST_BUILTINS_OBJS += test-getuid.o
 TEST_BUILTINS_OBJS += test-hash-speed.o
 TEST_BUILTINS_OBJS += test-hash.o
 TEST_BUILTINS_OBJS += test-hashmap.o
diff --git a/t/helper/test-getuid.c b/t/helper/test-getuid.c
new file mode 100644
index 0000000000..d741302461
--- /dev/null
+++ b/t/helper/test-getuid.c
@@ -0,0 +1,7 @@
+#include "test-tool.h"
+
+int cmd__getuid(int argc, const char **argv)
+{
+	printf("%d\n", getuid());
+	return 0;
+}
diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c
index 432b49d948..ad5f681e68 100644
--- a/t/helper/test-tool.c
+++ b/t/helper/test-tool.c
@@ -31,6 +31,7 @@ static struct test_cmd cmds[] = {
 	{ "example-decorate", cmd__example_decorate },
 	{ "genrandom", cmd__genrandom },
 	{ "genzeros", cmd__genzeros },
+	{ "getuid", cmd__getuid },
 	{ "hashmap", cmd__hashmap },
 	{ "hash-speed", cmd__hash_speed },
 	{ "index-version", cmd__index_version },
diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h
index 7c3281e071..765976e1eb 100644
--- a/t/helper/test-tool.h
+++ b/t/helper/test-tool.h
@@ -21,6 +21,7 @@ int cmd__dump_untracked_cache(int argc, const char **argv);
 int cmd__example_decorate(int argc, const char **argv);
 int cmd__genrandom(int argc, const char **argv);
 int cmd__genzeros(int argc, const char **argv);
+int cmd__getuid(int argc, const char **argv);
 int cmd__hashmap(int argc, const char **argv);
 int cmd__hash_speed(int argc, const char **argv);
 int cmd__index_version(int argc, const char **argv);
-- 
2.29.2.576.ga3fc446d84
Previous: Eric SunshineNext: Eric Sunshine
Message 3 of 6 in “make macOS `git maintenance` test work on Windows”
  1. 0/3 make macOS `git maintenance` test work on WindowsEric Sunshine, Nov 27, 2020
  2. 1/3 t7900: fix test failures when invoked individually via --runEric Sunshine, Nov 27, 2020
  3. 2/3 test-tool: add `getuid` subcommandEric Sunshine, Nov 27, 2020
  4. 3/3 t7900: make macOS-specific test work on WindowsEric Sunshine, Nov 27, 2020
  5. Ævar Arnfjörð BjarmasonNov 27, 2020
  6. Eric SunshineNov 28, 2020

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.