t0001: fix broken not-quite getcwd(3) test in bed67874e2

With a54e938e5b (strbuf: support long paths w/o read rights in strbuf_getcwd() on FreeBSD, 2017-03-26) we had t0001 break on systems like OpenBSD and AIX whose getcwd(3) has standard (but not like glibc et al) behavior. This was partially fixed in bed67874e2 (t0001: skip test with restrictive permissions if getpwd(3) respects them, 2017-08-07). The problem with that fix is that while its analysis of the problem is correct, it doesn't actually call getcwd(3), instead it invokes "pwd -P". There is no guarantee that "pwd -P" is going to call getcwd(3), as opposed to e.g. being a shell built-in. On AIX under both bash and ksh this test breaks because "pwd -P" will happily display the current working directory, but getcwd(3) called by the "git init" we're testing here will fail to get it. I checked whether clobbering the $PWD environment variable would affect it, and it didn't. Presumably these shells keep track of their working directory internally. There's possible follow-up work here in teaching strbuf_getcwd() to get the working directory with whatever method "pwd" uses on these platforms. See [1] for a discussion of that, but let's take the easy way out here and just skip these tests by fixing the GETCWD_IGNORES_PERMS prerequisite to match the limitations of strbuf_getcwd(). 1. https://lore.kernel.org/git/b650bef5-d739-d98d-e9f1-fa292b6ce982@web.de/ Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jul 30, 2021 at 18:18 UTC 482e1488a9b549cbf7466f4ee5396f93bb4588d6
5 files changed +33 -1
Makefile
+1
@@ -711,6 +711,7 @@ TEST_BUILTINS_OBJS += test-example-decorate.o
711 TEST_BUILTINS_OBJS += test-fast-rebase.o
712 TEST_BUILTINS_OBJS += test-genrandom.o
713 TEST_BUILTINS_OBJS += test-genzeros.o
714 +TEST_BUILTINS_OBJS += test-getcwd.o
715 TEST_BUILTINS_OBJS += test-hash-speed.o
716 TEST_BUILTINS_OBJS += test-hash.o
717 TEST_BUILTINS_OBJS += test-hashmap.o
t/helper/test-getcwd.c new
+26
@@ -0,0 +1,26 @@
1 +#include "test-tool.h"
2 +#include "git-compat-util.h"
3 +#include "parse-options.h"
4 +
5 +static const char *getcwd_usage[] = {
6 + "test-tool getcwd",
7 + NULL
8 +};
9 +
10 +int cmd__getcwd(int argc, const char **argv)
11 +{
12 + struct option options[] = {
13 + OPT_END()
14 + };
15 + char *cwd;
16 +
17 + argc = parse_options(argc, argv, "test-tools", options, getcwd_usage, 0);
18 + if (argc > 0)
19 + usage_with_options(getcwd_usage, options);
20 +
21 + cwd = xgetcwd();
22 + puts(cwd);
23 + free(cwd);
24 +
25 + return 0;
26 +}
t/helper/test-tool.c
+1
@@ -33,6 +33,7 @@ static struct test_cmd cmds[] = {
33 { "fast-rebase", cmd__fast_rebase },
34 { "genrandom", cmd__genrandom },
35 { "genzeros", cmd__genzeros },
36 + { "getcwd", cmd__getcwd },
37 { "hashmap", cmd__hashmap },
38 { "hash-speed", cmd__hash_speed },
39 { "index-version", cmd__index_version },
t/helper/test-tool.h
+1
@@ -23,6 +23,7 @@ int cmd__example_decorate(int argc, const char **argv);
23 int cmd__fast_rebase(int argc, const char **argv);
24 int cmd__genrandom(int argc, const char **argv);
25 int cmd__genzeros(int argc, const char **argv);
26 +int cmd__getcwd(int argc, const char **argv);
27 int cmd__hashmap(int argc, const char **argv);
28 int cmd__hash_speed(int argc, const char **argv);
29 int cmd__index_version(int argc, const char **argv);
t/t0001-init.sh
+4 -1
@@ -356,7 +356,10 @@ test_lazy_prereq GETCWD_IGNORES_PERMS '
356 chmod 100 $base ||
357 BUG "cannot prepare $base"
358
359 - (cd $base/dir && /bin/pwd -P)
359 + (
360 + cd $base/dir &&
361 + test-tool getcwd
362 + )
363 status=$?
364
365 chmod 700 $base &&