]> Cypherpunks repositories - gostls13.git/commitdiff
os: don't fail TestGetwdDeepWithPWDSet if TMPDIR has a symlink
authorIan Lance Taylor <iant@golang.org>
Wed, 4 Sep 2024 22:57:51 +0000 (15:57 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 4 Sep 2024 23:26:25 +0000 (23:26 +0000)
When testing with PWD set, it's possible for the stat of PWD to fail
with ENAMETOOLONG, and for syscall.Getwd to fail for the same reason.
If PWD contains symlinks, the fallback code won't know about them.
If Getwd returns the same result as PWD with resolved symlinks,
the test should not fail.

Change-Id: I39587ddb826d4e18339e185aad0cdd60167b1079
Reviewed-on: https://go-review.googlesource.com/c/go/+/610759
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Reviewed-by: Tim King <taking@google.com>
src/os/getwd_unix_test.go

index 084344735cf2ca30a7558f180bd2d67f891a4d54..f5265445c26309ab97c5ee442af99b3884553f19 100644 (file)
@@ -9,6 +9,7 @@ package os_test
 import (
        "errors"
        . "os"
+       "path/filepath"
        "runtime"
        "strings"
        "syscall"
@@ -26,7 +27,9 @@ func TestGetwdDeepWithPWDSet(t *testing.T) {
 // testGetwdDeep checks that os.Getwd is able to return paths
 // longer than syscall.PathMax (with or without PWD set).
 func testGetwdDeep(t *testing.T, setPWD bool) {
-       dir := t.TempDir()
+       tempDir := t.TempDir()
+
+       dir := tempDir
        t.Chdir(dir)
 
        if setPWD {
@@ -66,7 +69,23 @@ func testGetwdDeep(t *testing.T, setPWD bool) {
                        t.Fatal(err)
                }
                if setPWD && wd != dir {
-                       t.Fatalf("Getwd: want same value as $PWD: %q, got %q", dir, wd)
+                       // It's possible for the stat of PWD to fail
+                       // with ENAMETOOLONG, and for getwd to fail for
+                       // the same reason, and it's possible for $TMPDIR
+                       // to contain a symlink. In that case the fallback
+                       // code will not return the same directory.
+                       if len(dir) > 1000 {
+                               symDir, err := filepath.EvalSymlinks(tempDir)
+                               if err == nil && symDir != tempDir {
+                                       t.Logf("EvalSymlinks(%q) = %q", tempDir, symDir)
+                                       if strings.Replace(dir, tempDir, symDir, 1) == wd {
+                                               // Symlink confusion is OK.
+                                               break
+                                       }
+                               }
+                       }
+
+                       t.Fatalf("Getwd: got %q, want same value as $PWD: %q", wd, dir)
                }
                // Ideally the success criterion should be len(wd) > syscall.PathMax,
                // but the latter is not public for some platforms, so use Stat(wd).