all: Grand test refactor (fixes #8779, fixes #8799)

This fixes various test issues with Go 1.20.

- Most tests rewritten to use fakefs where possible
- Some tests that were already skipped, or dubious (invasive,
  unmaintainable, unclear what they even tested) have been removed
- Some actual code rewritten to better support testing in fakefs

Co-authored-by: Eric P <eric@kastelo.net>
This commit is contained in:
Jakob Borg
2023-05-09 10:01:57 +00:00
co-authored by Eric P
parent ddce692f72
commit 1103a27337
54 changed files with 1133 additions and 1594 deletions
+9 -25
View File
@@ -8,14 +8,13 @@ package osutil_test
import (
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/osutil"
"github.com/syncthing/syncthing/lib/rand"
)
func TestIsDeleted(t *testing.T) {
@@ -41,9 +40,9 @@ func TestIsDeleted(t *testing.T) {
{filepath.Join("del", "del", "del"), true},
}
testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata")
testFs := fs.NewFilesystem(fs.FilesystemTypeFake, "testdata")
testFs.MkdirAll("dir", 0777)
testFs.MkdirAll("dir", 0o777)
for _, f := range []string{"file", "del.file", "dir.file", filepath.Join("dir", "file")} {
fd, err := testFs.Create(f)
if err != nil {
@@ -51,21 +50,9 @@ func TestIsDeleted(t *testing.T) {
}
fd.Close()
}
if !build.IsWindows {
// Can't create unreadable dir on windows
testFs.MkdirAll("inacc", 0777)
if err := testFs.Chmod("inacc", 0000); err == nil {
if _, err := testFs.Lstat(filepath.Join("inacc", "file")); fs.IsPermission(err) {
// May fail e.g. if tests are run as root -> just skip
cases = append(cases, tc{"inacc", false}, tc{filepath.Join("inacc", "file"), false})
}
}
}
for _, n := range []string{"Dir", "File", "Del"} {
if err := fs.DebugSymlinkForTestsOnly(testFs, testFs, strings.ToLower(n), "linkTo"+n); err != nil {
if build.IsWindows {
t.Skip("Symlinks aren't working")
}
if err := testFs.CreateSymlink(strings.ToLower(n), "linkTo"+n); err != nil {
t.Fatal(err)
}
}
@@ -75,13 +62,10 @@ func TestIsDeleted(t *testing.T) {
t.Errorf("IsDeleted(%v) != %v", c.path, c.isDel)
}
}
testFs.Chmod("inacc", 0777)
os.RemoveAll("testdata")
}
func TestRenameOrCopy(t *testing.T) {
sameFs := fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir())
sameFs := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true")
tests := []struct {
src fs.Filesystem
dst fs.Filesystem
@@ -93,13 +77,13 @@ func TestRenameOrCopy(t *testing.T) {
file: "file",
},
{
src: fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir()),
dst: fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir()),
src: fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true"),
dst: fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true"),
file: "file",
},
{
src: fs.NewFilesystem(fs.FilesystemTypeFake, `fake://fake/?files=1&seed=42`),
dst: fs.NewFilesystem(fs.FilesystemTypeBasic, t.TempDir()),
dst: fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32)+"?content=true"),
file: osutil.NativeFilename(`05/7a/4d52f284145b9fe8`),
},
}
+11 -23
View File
@@ -7,24 +7,18 @@
package osutil_test
import (
"os"
"path/filepath"
"testing"
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/osutil"
"github.com/syncthing/syncthing/lib/rand"
)
func TestTraversesSymlink(t *testing.T) {
tmpDir := t.TempDir()
testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir)
testFs.MkdirAll("a/b/c", 0755)
if err := fs.DebugSymlinkForTestsOnly(testFs, testFs, filepath.Join("a", "b"), filepath.Join("a", "l")); err != nil {
if build.IsWindows {
t.Skip("Symlinks aren't working")
}
testFs := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32))
testFs.MkdirAll("a/b/c", 0o755)
if err := testFs.CreateSymlink(filepath.Join("a", "b"), filepath.Join("a", "l")); err != nil {
t.Fatal(err)
}
@@ -66,14 +60,10 @@ func TestTraversesSymlink(t *testing.T) {
}
func TestIssue4875(t *testing.T) {
tmpDir := t.TempDir()
testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir)
testFs.MkdirAll(filepath.Join("a", "b", "c"), 0755)
if err := fs.DebugSymlinkForTestsOnly(testFs, testFs, filepath.Join("a", "b"), filepath.Join("a", "l")); err != nil {
if build.IsWindows {
t.Skip("Symlinks aren't working")
}
testFsPath := rand.String(32)
testFs := fs.NewFilesystem(fs.FilesystemTypeFake, testFsPath)
testFs.MkdirAll(filepath.Join("a", "b", "c"), 0o755)
if err := testFs.CreateSymlink(filepath.Join("a", "b"), filepath.Join("a", "l")); err != nil {
t.Fatal(err)
}
@@ -86,7 +76,7 @@ func TestIssue4875(t *testing.T) {
t.Fatal("error in setup, a/l/c should be a directory")
}
testFs = fs.NewFilesystem(fs.FilesystemTypeBasic, filepath.Join(tmpDir, "a/l"))
testFs = fs.NewFilesystem(fs.FilesystemTypeFake, filepath.Join(testFsPath, "a/l"))
if err := osutil.TraversesSymlink(testFs, "."); err != nil {
t.Error(`TraversesSymlink on filesystem with symlink at root returned error for ".":`, err)
}
@@ -95,10 +85,8 @@ func TestIssue4875(t *testing.T) {
var traversesSymlinkResult error
func BenchmarkTraversesSymlink(b *testing.B) {
os.RemoveAll("testdata")
defer os.RemoveAll("testdata")
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata")
fs.MkdirAll("a/b/c", 0755)
fs := fs.NewFilesystem(fs.FilesystemTypeFake, rand.String(32))
fs.MkdirAll("a/b/c", 0o755)
for i := 0; i < b.N; i++ {
traversesSymlinkResult = osutil.TraversesSymlink(fs, "a/b/c")