From cc7f99125a7cc25b0da073c29f787c8ba1be30c9 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 21 Jul 2024 15:58:41 +0200 Subject: [PATCH] minimize usage of internal/fs in tests --- internal/archiver/archiver_test.go | 4 ++-- internal/archiver/testing.go | 10 +++++----- internal/archiver/testing_test.go | 12 +++++------ internal/dump/tar_test.go | 3 +-- internal/dump/zip_test.go | 4 +--- internal/fs/file.go | 32 ------------------------------ internal/fs/node.go | 6 +++--- internal/fs/node_linux.go | 3 ++- internal/fs/node_windows.go | 2 +- internal/fs/setflags_linux_test.go | 2 +- internal/restorer/restorer_test.go | 2 +- 11 files changed, 23 insertions(+), 57 deletions(-) diff --git a/internal/archiver/archiver_test.go b/internal/archiver/archiver_test.go index 04d0b876b..b95947a2e 100644 --- a/internal/archiver/archiver_test.go +++ b/internal/archiver/archiver_test.go @@ -846,7 +846,7 @@ func TestArchiverSaveDir(t *testing.T) { back := rtest.Chdir(t, chdir) defer back() - fi, err := fs.Lstat(test.target) + fi, err := os.Lstat(test.target) if err != nil { t.Fatal(err) } @@ -920,7 +920,7 @@ func TestArchiverSaveDirIncremental(t *testing.T) { arch.runWorkers(ctx, wg) arch.summary = &Summary{} - fi, err := fs.Lstat(tempdir) + fi, err := os.Lstat(tempdir) if err != nil { t.Fatal(err) } diff --git a/internal/archiver/testing.go b/internal/archiver/testing.go index 97bada644..e555a70d6 100644 --- a/internal/archiver/testing.go +++ b/internal/archiver/testing.go @@ -95,17 +95,17 @@ func TestCreateFiles(t testing.TB, target string, dir TestDir) { t.Fatal(err) } case TestSymlink: - err := fs.Symlink(filepath.FromSlash(it.Target), targetPath) + err := os.Symlink(filepath.FromSlash(it.Target), targetPath) if err != nil { t.Fatal(err) } case TestHardlink: - err := fs.Link(filepath.Join(target, filepath.FromSlash(it.Target)), targetPath) + err := os.Link(filepath.Join(target, filepath.FromSlash(it.Target)), targetPath) if err != nil { t.Fatal(err) } case TestDir: - err := fs.Mkdir(targetPath, 0755) + err := os.Mkdir(targetPath, 0755) if err != nil { t.Fatal(err) } @@ -157,7 +157,7 @@ func TestEnsureFiles(t testing.TB, target string, dir TestDir) { // first, test that all items are there TestWalkFiles(t, target, dir, func(path string, item interface{}) error { - fi, err := fs.Lstat(path) + fi, err := os.Lstat(path) if err != nil { return err } @@ -188,7 +188,7 @@ func TestEnsureFiles(t testing.TB, target string, dir TestDir) { return nil } - target, err := fs.Readlink(path) + target, err := os.Readlink(path) if err != nil { return err } diff --git a/internal/archiver/testing_test.go b/internal/archiver/testing_test.go index bb4b63a82..a217abe25 100644 --- a/internal/archiver/testing_test.go +++ b/internal/archiver/testing_test.go @@ -54,7 +54,7 @@ func (t *MockT) Errorf(msg string, args ...interface{}) { func createFilesAt(t testing.TB, targetdir string, files map[string]interface{}) { for name, item := range files { target := filepath.Join(targetdir, filepath.FromSlash(name)) - err := fs.MkdirAll(filepath.Dir(target), 0700) + err := os.MkdirAll(filepath.Dir(target), 0700) if err != nil { t.Fatal(err) } @@ -66,7 +66,7 @@ func createFilesAt(t testing.TB, targetdir string, files map[string]interface{}) t.Fatal(err) } case TestSymlink: - err := fs.Symlink(filepath.FromSlash(it.Target), target) + err := os.Symlink(filepath.FromSlash(it.Target), target) if err != nil { t.Fatal(err) } @@ -105,7 +105,7 @@ func TestTestCreateFiles(t *testing.T) { t.Run("", func(t *testing.T) { tempdir := filepath.Join(tempdir, fmt.Sprintf("test-%d", i)) - err := fs.MkdirAll(tempdir, 0700) + err := os.MkdirAll(tempdir, 0700) if err != nil { t.Fatal(err) } @@ -114,7 +114,7 @@ func TestTestCreateFiles(t *testing.T) { for name, item := range test.files { targetPath := filepath.Join(tempdir, filepath.FromSlash(name)) - fi, err := fs.Lstat(targetPath) + fi, err := os.Lstat(targetPath) if err != nil { t.Error(err) continue @@ -142,7 +142,7 @@ func TestTestCreateFiles(t *testing.T) { continue } - target, err := fs.Readlink(targetPath) + target, err := os.Readlink(targetPath) if err != nil { t.Error(err) continue @@ -455,7 +455,7 @@ func TestTestEnsureSnapshot(t *testing.T) { tempdir := rtest.TempDir(t) targetDir := filepath.Join(tempdir, "target") - err := fs.Mkdir(targetDir, 0700) + err := os.Mkdir(targetDir, 0700) if err != nil { t.Fatal(err) } diff --git a/internal/dump/tar_test.go b/internal/dump/tar_test.go index 0c97eae35..cb3cb08c4 100644 --- a/internal/dump/tar_test.go +++ b/internal/dump/tar_test.go @@ -13,7 +13,6 @@ import ( "testing" "time" - "github.com/restic/restic/internal/fs" "github.com/restic/restic/internal/restic" rtest "github.com/restic/restic/internal/test" ) @@ -83,7 +82,7 @@ func checkTar(t *testing.T, testDir string, srcTar *bytes.Buffer) error { return fmt.Errorf("foldernames must end with separator got %v", hdr.Name) } case tar.TypeSymlink: - target, err := fs.Readlink(matchPath) + target, err := os.Readlink(matchPath) if err != nil { return err } diff --git a/internal/dump/zip_test.go b/internal/dump/zip_test.go index 0c304d3da..6f5f60f54 100644 --- a/internal/dump/zip_test.go +++ b/internal/dump/zip_test.go @@ -9,8 +9,6 @@ import ( "strings" "testing" "time" - - "github.com/restic/restic/internal/fs" ) func TestWriteZip(t *testing.T) { @@ -91,7 +89,7 @@ func checkZip(t *testing.T, testDir string, srcZip *bytes.Buffer) error { return fmt.Errorf("foldernames must end with separator got %v", f.Name) } case f.Mode()&os.ModeSymlink != 0: - target, err := fs.Readlink(matchPath) + target, err := os.Readlink(matchPath) if err != nil { return err } diff --git a/internal/fs/file.go b/internal/fs/file.go index c1ebae081..8d60ed159 100644 --- a/internal/fs/file.go +++ b/internal/fs/file.go @@ -3,15 +3,8 @@ package fs import ( "fmt" "os" - "time" ) -// Mkdir creates a new directory with the specified name and permission bits. -// If there is an error, it will be of type *PathError. -func Mkdir(name string, perm os.FileMode) error { - return os.Mkdir(fixpath(name), perm) -} - // MkdirAll creates a directory named path, along with any necessary parents, // and returns nil, or else returns an error. The permission bits perm are used // for all directories that MkdirAll creates. If path is already a directory, @@ -20,12 +13,6 @@ func MkdirAll(path string, perm os.FileMode) error { return os.MkdirAll(fixpath(path), perm) } -// Readlink returns the destination of the named symbolic link. -// If there is an error, it will be of type *PathError. -func Readlink(name string) (string, error) { - return os.Readlink(fixpath(name)) -} - // Remove removes the named file or directory. // If there is an error, it will be of type *PathError. func Remove(name string) error { @@ -40,20 +27,6 @@ func RemoveAll(path string) error { return os.RemoveAll(fixpath(path)) } -// Rename renames (moves) oldpath to newpath. -// If newpath already exists, Rename replaces it. -// OS-specific restrictions may apply when oldpath and newpath are in different directories. -// If there is an error, it will be of type *LinkError. -func Rename(oldpath, newpath string) error { - return os.Rename(fixpath(oldpath), fixpath(newpath)) -} - -// Symlink creates newname as a symbolic link to oldname. -// If there is an error, it will be of type *LinkError. -func Symlink(oldname, newname string) error { - return os.Symlink(oldname, fixpath(newname)) -} - // Link creates newname as a hard link to oldname. // If there is an error, it will be of type *LinkError. func Link(oldname, newname string) error { @@ -68,11 +41,6 @@ func Lstat(name string) (os.FileInfo, error) { return os.Lstat(fixpath(name)) } -// Open opens a file for reading. -func Open(name string) (File, error) { - return os.Open(fixpath(name)) -} - // OpenFile is the generalized open call; most users will use Open // or Create instead. It opens the named file with specified flag // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, diff --git a/internal/fs/node.go b/internal/fs/node.go index e29011cc2..3945c426e 100644 --- a/internal/fs/node.go +++ b/internal/fs/node.go @@ -80,7 +80,7 @@ func nodeFillExtra(node *restic.Node, path string, fi os.FileInfo, ignoreXattrLi case restic.NodeTypeDir: case restic.NodeTypeSymlink: var err error - node.LinkTarget, err = Readlink(path) + node.LinkTarget, err = os.Readlink(fixpath(path)) node.Links = uint64(stat.nlink()) if err != nil { return errors.WithStack(err) @@ -212,7 +212,7 @@ func NodeCreateAt(node *restic.Node, path string) error { } func nodeCreateDirAt(node *restic.Node, path string) error { - err := Mkdir(path, node.Mode) + err := os.Mkdir(fixpath(path), node.Mode) if err != nil && !os.IsExist(err) { return errors.WithStack(err) } @@ -234,7 +234,7 @@ func nodeCreateFileAt(path string) error { } func nodeCreateSymlinkAt(node *restic.Node, path string) error { - if err := Symlink(node.LinkTarget, path); err != nil { + if err := os.Symlink(node.LinkTarget, fixpath(path)); err != nil { return errors.WithStack(err) } diff --git a/internal/fs/node_linux.go b/internal/fs/node_linux.go index 1cb4ee1ae..4cb9bdeaa 100644 --- a/internal/fs/node_linux.go +++ b/internal/fs/node_linux.go @@ -1,6 +1,7 @@ package fs import ( + "os" "path/filepath" "syscall" @@ -10,7 +11,7 @@ import ( ) func nodeRestoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error { - dir, err := Open(filepath.Dir(path)) + dir, err := os.Open(fixpath(filepath.Dir(path))) if err != nil { return errors.WithStack(err) } diff --git a/internal/fs/node_windows.go b/internal/fs/node_windows.go index f2a2001e9..d904c37fe 100644 --- a/internal/fs/node_windows.go +++ b/internal/fs/node_windows.go @@ -58,7 +58,7 @@ func lchown(_ string, _ int, _ int) (err error) { // restoreSymlinkTimestamps restores timestamps for symlinks func nodeRestoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error { // tweaked version of UtimesNano from go/src/syscall/syscall_windows.go - pathp, e := syscall.UTF16PtrFromString(path) + pathp, e := syscall.UTF16PtrFromString(fixpath(path)) if e != nil { return e } diff --git a/internal/fs/setflags_linux_test.go b/internal/fs/setflags_linux_test.go index b561a1009..8fe14a5a6 100644 --- a/internal/fs/setflags_linux_test.go +++ b/internal/fs/setflags_linux_test.go @@ -19,7 +19,7 @@ func TestNoatime(t *testing.T) { defer func() { _ = f.Close() - err = Remove(f.Name()) + err = os.Remove(f.Name()) if err != nil { t.Fatal(err) } diff --git a/internal/restorer/restorer_test.go b/internal/restorer/restorer_test.go index 8844435ca..e0306ce01 100644 --- a/internal/restorer/restorer_test.go +++ b/internal/restorer/restorer_test.go @@ -1223,7 +1223,7 @@ func TestRestorerOverwriteSpecial(t *testing.T) { } } for filename, target := range links { - link, err := fs.Readlink(filepath.Join(tempdir, filepath.FromSlash(filename))) + link, err := os.Readlink(filepath.Join(tempdir, filepath.FromSlash(filename))) rtest.OK(t, err) rtest.Equals(t, link, target, "wrong symlink target") }