minimize usage of internal/fs in tests

This commit is contained in:
Michael Eischer 2024-07-21 15:58:41 +02:00
parent 65a7157383
commit cc7f99125a
11 changed files with 23 additions and 57 deletions

View file

@ -846,7 +846,7 @@ func TestArchiverSaveDir(t *testing.T) {
back := rtest.Chdir(t, chdir) back := rtest.Chdir(t, chdir)
defer back() defer back()
fi, err := fs.Lstat(test.target) fi, err := os.Lstat(test.target)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -920,7 +920,7 @@ func TestArchiverSaveDirIncremental(t *testing.T) {
arch.runWorkers(ctx, wg) arch.runWorkers(ctx, wg)
arch.summary = &Summary{} arch.summary = &Summary{}
fi, err := fs.Lstat(tempdir) fi, err := os.Lstat(tempdir)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -95,17 +95,17 @@ func TestCreateFiles(t testing.TB, target string, dir TestDir) {
t.Fatal(err) t.Fatal(err)
} }
case TestSymlink: case TestSymlink:
err := fs.Symlink(filepath.FromSlash(it.Target), targetPath) err := os.Symlink(filepath.FromSlash(it.Target), targetPath)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
case TestHardlink: 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
case TestDir: case TestDir:
err := fs.Mkdir(targetPath, 0755) err := os.Mkdir(targetPath, 0755)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -157,7 +157,7 @@ func TestEnsureFiles(t testing.TB, target string, dir TestDir) {
// first, test that all items are there // first, test that all items are there
TestWalkFiles(t, target, dir, func(path string, item interface{}) error { TestWalkFiles(t, target, dir, func(path string, item interface{}) error {
fi, err := fs.Lstat(path) fi, err := os.Lstat(path)
if err != nil { if err != nil {
return err return err
} }
@ -188,7 +188,7 @@ func TestEnsureFiles(t testing.TB, target string, dir TestDir) {
return nil return nil
} }
target, err := fs.Readlink(path) target, err := os.Readlink(path)
if err != nil { if err != nil {
return err return err
} }

View file

@ -54,7 +54,7 @@ func (t *MockT) Errorf(msg string, args ...interface{}) {
func createFilesAt(t testing.TB, targetdir string, files map[string]interface{}) { func createFilesAt(t testing.TB, targetdir string, files map[string]interface{}) {
for name, item := range files { for name, item := range files {
target := filepath.Join(targetdir, filepath.FromSlash(name)) target := filepath.Join(targetdir, filepath.FromSlash(name))
err := fs.MkdirAll(filepath.Dir(target), 0700) err := os.MkdirAll(filepath.Dir(target), 0700)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -66,7 +66,7 @@ func createFilesAt(t testing.TB, targetdir string, files map[string]interface{})
t.Fatal(err) t.Fatal(err)
} }
case TestSymlink: case TestSymlink:
err := fs.Symlink(filepath.FromSlash(it.Target), target) err := os.Symlink(filepath.FromSlash(it.Target), target)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -105,7 +105,7 @@ func TestTestCreateFiles(t *testing.T) {
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
tempdir := filepath.Join(tempdir, fmt.Sprintf("test-%d", i)) tempdir := filepath.Join(tempdir, fmt.Sprintf("test-%d", i))
err := fs.MkdirAll(tempdir, 0700) err := os.MkdirAll(tempdir, 0700)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -114,7 +114,7 @@ func TestTestCreateFiles(t *testing.T) {
for name, item := range test.files { for name, item := range test.files {
targetPath := filepath.Join(tempdir, filepath.FromSlash(name)) targetPath := filepath.Join(tempdir, filepath.FromSlash(name))
fi, err := fs.Lstat(targetPath) fi, err := os.Lstat(targetPath)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
continue continue
@ -142,7 +142,7 @@ func TestTestCreateFiles(t *testing.T) {
continue continue
} }
target, err := fs.Readlink(targetPath) target, err := os.Readlink(targetPath)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
continue continue
@ -455,7 +455,7 @@ func TestTestEnsureSnapshot(t *testing.T) {
tempdir := rtest.TempDir(t) tempdir := rtest.TempDir(t)
targetDir := filepath.Join(tempdir, "target") targetDir := filepath.Join(tempdir, "target")
err := fs.Mkdir(targetDir, 0700) err := os.Mkdir(targetDir, 0700)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -13,7 +13,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test" 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) return fmt.Errorf("foldernames must end with separator got %v", hdr.Name)
} }
case tar.TypeSymlink: case tar.TypeSymlink:
target, err := fs.Readlink(matchPath) target, err := os.Readlink(matchPath)
if err != nil { if err != nil {
return err return err
} }

View file

@ -9,8 +9,6 @@ import (
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/restic/restic/internal/fs"
) )
func TestWriteZip(t *testing.T) { 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) return fmt.Errorf("foldernames must end with separator got %v", f.Name)
} }
case f.Mode()&os.ModeSymlink != 0: case f.Mode()&os.ModeSymlink != 0:
target, err := fs.Readlink(matchPath) target, err := os.Readlink(matchPath)
if err != nil { if err != nil {
return err return err
} }

View file

@ -3,15 +3,8 @@ package fs
import ( import (
"fmt" "fmt"
"os" "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, // 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 // 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, // 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) 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. // Remove removes the named file or directory.
// If there is an error, it will be of type *PathError. // If there is an error, it will be of type *PathError.
func Remove(name string) error { func Remove(name string) error {
@ -40,20 +27,6 @@ func RemoveAll(path string) error {
return os.RemoveAll(fixpath(path)) 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. // Link creates newname as a hard link to oldname.
// If there is an error, it will be of type *LinkError. // If there is an error, it will be of type *LinkError.
func Link(oldname, newname string) error { func Link(oldname, newname string) error {
@ -68,11 +41,6 @@ func Lstat(name string) (os.FileInfo, error) {
return os.Lstat(fixpath(name)) 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 // OpenFile is the generalized open call; most users will use Open
// or Create instead. It opens the named file with specified flag // or Create instead. It opens the named file with specified flag
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, // (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,

View file

@ -80,7 +80,7 @@ func nodeFillExtra(node *restic.Node, path string, fi os.FileInfo, ignoreXattrLi
case restic.NodeTypeDir: case restic.NodeTypeDir:
case restic.NodeTypeSymlink: case restic.NodeTypeSymlink:
var err error var err error
node.LinkTarget, err = Readlink(path) node.LinkTarget, err = os.Readlink(fixpath(path))
node.Links = uint64(stat.nlink()) node.Links = uint64(stat.nlink())
if err != nil { if err != nil {
return errors.WithStack(err) return errors.WithStack(err)
@ -212,7 +212,7 @@ func NodeCreateAt(node *restic.Node, path string) error {
} }
func nodeCreateDirAt(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) { if err != nil && !os.IsExist(err) {
return errors.WithStack(err) return errors.WithStack(err)
} }
@ -234,7 +234,7 @@ func nodeCreateFileAt(path string) error {
} }
func nodeCreateSymlinkAt(node *restic.Node, 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) return errors.WithStack(err)
} }

View file

@ -1,6 +1,7 @@
package fs package fs
import ( import (
"os"
"path/filepath" "path/filepath"
"syscall" "syscall"
@ -10,7 +11,7 @@ import (
) )
func nodeRestoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error { 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 { if err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }

View file

@ -58,7 +58,7 @@ func lchown(_ string, _ int, _ int) (err error) {
// restoreSymlinkTimestamps restores timestamps for symlinks // restoreSymlinkTimestamps restores timestamps for symlinks
func nodeRestoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error { func nodeRestoreSymlinkTimestamps(path string, utimes [2]syscall.Timespec) error {
// tweaked version of UtimesNano from go/src/syscall/syscall_windows.go // 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 { if e != nil {
return e return e
} }

View file

@ -19,7 +19,7 @@ func TestNoatime(t *testing.T) {
defer func() { defer func() {
_ = f.Close() _ = f.Close()
err = Remove(f.Name()) err = os.Remove(f.Name())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -1223,7 +1223,7 @@ func TestRestorerOverwriteSpecial(t *testing.T) {
} }
} }
for filename, target := range links { 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.OK(t, err)
rtest.Equals(t, link, target, "wrong symlink target") rtest.Equals(t, link, target, "wrong symlink target")
} }