From b01fb2af9ed6a3b90dd89d548ceef38b4ec1fc94 Mon Sep 17 00:00:00 2001 From: DrGo Date: Mon, 4 Jan 2021 11:26:46 +0000 Subject: testing/fstest: fix typo in error message Change-Id: Iac59f5271c79c46b39733fdf0eb4bf9b0fc0bdca GitHub-Last-Rev: 03f96e32a81d1516a9307b6578c930434783e3d3 GitHub-Pull-Request: golang/go#43450 Reviewed-on: https://go-review.googlesource.com/c/go/+/280953 Trust: Tobias Klauser Run-TryBot: Tobias Klauser TryBot-Result: Go Bot Reviewed-by: Tobias Klauser Reviewed-by: Ian Lance Taylor --- src/testing/fstest/testfs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/testing') diff --git a/src/testing/fstest/testfs.go b/src/testing/fstest/testfs.go index 2602bdf0cc..4da6f04eed 100644 --- a/src/testing/fstest/testfs.go +++ b/src/testing/fstest/testfs.go @@ -121,7 +121,7 @@ func (t *fsTester) openDir(dir string) fs.ReadDirFile { d, ok := f.(fs.ReadDirFile) if !ok { f.Close() - t.errorf("%s: Open returned File type %T, not a io.ReadDirFile", dir, f) + t.errorf("%s: Open returned File type %T, not a fs.ReadDirFile", dir, f) return nil } return d -- cgit v1.3-5-g9baa From 0c5afc4fb7e3349ec4efdce6554f83554e3d087c Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 7 Jan 2021 17:50:14 -0500 Subject: testing/fstest,os: clarify racy behavior of TestFS The testing.TestFS function assumes that the file system it's testing doesn't change under it. Clarify this in the documentation and fix the use of os.TestDirFS that's currently susceptible to this race. Fixes #42637. Change-Id: Ia7792380726177f8953d150ee87381b66cb01cb3 Reviewed-on: https://go-review.googlesource.com/c/go/+/282452 Trust: Austin Clements Run-TryBot: Austin Clements TryBot-Result: Go Bot Reviewed-by: Bryan C. Mills --- src/os/os_test.go | 2 +- src/os/testdata/dirfs/a | 0 src/os/testdata/dirfs/b | 0 src/os/testdata/dirfs/dir/x | 0 src/testing/fstest/testfs.go | 1 + 5 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 src/os/testdata/dirfs/a create mode 100644 src/os/testdata/dirfs/b create mode 100644 src/os/testdata/dirfs/dir/x (limited to 'src/testing') diff --git a/src/os/os_test.go b/src/os/os_test.go index 765797f5fb..d2e8ed5d82 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -2687,7 +2687,7 @@ func TestOpenFileKeepsPermissions(t *testing.T) { } func TestDirFS(t *testing.T) { - if err := fstest.TestFS(DirFS("./signal"), "signal.go", "internal/pty/pty.go"); err != nil { + if err := fstest.TestFS(DirFS("./testdata/dirfs"), "a", "b", "dir/x"); err != nil { t.Fatal(err) } } diff --git a/src/os/testdata/dirfs/a b/src/os/testdata/dirfs/a new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/os/testdata/dirfs/b b/src/os/testdata/dirfs/b new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/os/testdata/dirfs/dir/x b/src/os/testdata/dirfs/dir/x new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/testing/fstest/testfs.go b/src/testing/fstest/testfs.go index 4da6f04eed..a7f8007333 100644 --- a/src/testing/fstest/testfs.go +++ b/src/testing/fstest/testfs.go @@ -24,6 +24,7 @@ import ( // It also checks that the file system contains at least the expected files. // As a special case, if no expected files are listed, fsys must be empty. // Otherwise, fsys must only contain at least the listed files: it can also contain others. +// The contents of fsys must not change concurrently with TestFS. // // If TestFS finds any misbehaviors, it returns an error reporting all of them. // The error text spans multiple lines, one per detected misbehavior. -- cgit v1.3-5-g9baa From 4516afebedd18692c6dc70cbdee16a049c26024b Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 5 Feb 2021 10:55:12 -0500 Subject: testing/fstest: avoid symlink-induced failures in tester Do not require directory entry and Stat result to match for symlinks, because they won't (Stat dereferences the symlink). Fixes #44113. Change-Id: Ifc6dbce5719906e2f42254a7172f1ef787464a9e Reviewed-on: https://go-review.googlesource.com/c/go/+/290009 Trust: Russ Cox Run-TryBot: Russ Cox Reviewed-by: Bryan C. Mills --- src/testing/fstest/testfs.go | 25 ++++++++++++++++++------- src/testing/fstest/testfs_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 src/testing/fstest/testfs_test.go (limited to 'src/testing') diff --git a/src/testing/fstest/testfs.go b/src/testing/fstest/testfs.go index a7f8007333..8fc8acaaf3 100644 --- a/src/testing/fstest/testfs.go +++ b/src/testing/fstest/testfs.go @@ -403,9 +403,10 @@ func (t *fsTester) checkStat(path string, entry fs.DirEntry) { return } fentry := formatEntry(entry) - finfo := formatInfoEntry(info) - if fentry != finfo { - t.errorf("%s: mismatch:\n\tentry = %s\n\tfile.Stat() = %s", path, fentry, finfo) + fientry := formatInfoEntry(info) + // Note: mismatch here is OK for symlink, because Open dereferences symlink. + if fentry != fientry && entry.Type()&fs.ModeSymlink == 0 { + t.errorf("%s: mismatch:\n\tentry = %s\n\tfile.Stat() = %s", path, fentry, fientry) } einfo, err := entry.Info() @@ -413,12 +414,22 @@ func (t *fsTester) checkStat(path string, entry fs.DirEntry) { t.errorf("%s: entry.Info: %v", path, err) return } - fentry = formatInfo(einfo) - finfo = formatInfo(info) - if fentry != finfo { - t.errorf("%s: mismatch:\n\tentry.Info() = %s\n\tfile.Stat() = %s\n", path, fentry, finfo) + finfo := formatInfo(info) + if entry.Type()&fs.ModeSymlink != 0 { + // For symlink, just check that entry.Info matches entry on common fields. + // Open deferences symlink, so info itself may differ. + feentry := formatInfoEntry(einfo) + if fentry != feentry { + t.errorf("%s: mismatch\n\tentry = %s\n\tentry.Info() = %s\n", path, fentry, feentry) + } + } else { + feinfo := formatInfo(einfo) + if feinfo != finfo { + t.errorf("%s: mismatch:\n\tentry.Info() = %s\n\tfile.Stat() = %s\n", path, feinfo, finfo) + } } + // Stat should be the same as Open+Stat, even for symlinks. info2, err := fs.Stat(t.fsys, path) if err != nil { t.errorf("%s: fs.Stat: %v", path, err) diff --git a/src/testing/fstest/testfs_test.go b/src/testing/fstest/testfs_test.go new file mode 100644 index 0000000000..5b8813c343 --- /dev/null +++ b/src/testing/fstest/testfs_test.go @@ -0,0 +1,31 @@ +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fstest + +import ( + "internal/testenv" + "os" + "path/filepath" + "testing" +) + +func TestSymlink(t *testing.T) { + testenv.MustHaveSymlink(t) + + tmp := t.TempDir() + tmpfs := os.DirFS(tmp) + + if err := os.WriteFile(filepath.Join(tmp, "hello"), []byte("hello, world\n"), 0644); err != nil { + t.Fatal(err) + } + + if err := os.Symlink(filepath.Join(tmp, "hello"), filepath.Join(tmp, "hello.link")); err != nil { + t.Fatal(err) + } + + if err := TestFS(tmpfs, "hello", "hello.link"); err != nil { + t.Fatal(err) + } +} -- cgit v1.3-5-g9baa