feat: Add rest of tests for symlink behaviour of File.Stat
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -43,7 +44,7 @@ type File struct {
|
|||||||
getFileBuffer func() (cache.WriteCache, func() error, error)
|
getFileBuffer func() (cache.WriteCache, func() error, error)
|
||||||
|
|
||||||
name string
|
name string
|
||||||
info os.FileInfo
|
info *FileInfo
|
||||||
|
|
||||||
ioLock *sync.Mutex
|
ioLock *sync.Mutex
|
||||||
|
|
||||||
@@ -72,7 +73,7 @@ func NewFile(
|
|||||||
ioLock *sync.Mutex,
|
ioLock *sync.Mutex,
|
||||||
|
|
||||||
name string,
|
name string,
|
||||||
info os.FileInfo,
|
info *FileInfo,
|
||||||
|
|
||||||
onHeader func(hdr *config.Header),
|
onHeader func(hdr *config.Header),
|
||||||
log logging.StructuredLogger,
|
log logging.StructuredLogger,
|
||||||
@@ -394,6 +395,14 @@ func (f *File) Stat() (os.FileInfo, error) {
|
|||||||
f.ioLock.Lock()
|
f.ioLock.Lock()
|
||||||
defer f.ioLock.Unlock()
|
defer f.ioLock.Unlock()
|
||||||
|
|
||||||
|
if f.link != "" {
|
||||||
|
info := f.info
|
||||||
|
|
||||||
|
info.name = path.Base(f.link)
|
||||||
|
|
||||||
|
return info, nil
|
||||||
|
}
|
||||||
|
|
||||||
return f.info, nil
|
return f.info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -270,7 +270,7 @@ var fileStatTests = []struct {
|
|||||||
name string
|
name string
|
||||||
open string
|
open string
|
||||||
wantErr bool
|
wantErr bool
|
||||||
prepare func(afero.Fs) error
|
prepare func(symFs) error
|
||||||
check func(os.FileInfo) error
|
check func(os.FileInfo) error
|
||||||
withCache bool
|
withCache bool
|
||||||
withOsFs bool
|
withOsFs bool
|
||||||
@@ -279,7 +279,7 @@ var fileStatTests = []struct {
|
|||||||
"Can stat /",
|
"Can stat /",
|
||||||
"/",
|
"/",
|
||||||
false,
|
false,
|
||||||
func(f afero.Fs) error { return nil },
|
func(f symFs) error { return nil },
|
||||||
func(f os.FileInfo) error {
|
func(f os.FileInfo) error {
|
||||||
dir, _ := path.Split(f.Name())
|
dir, _ := path.Split(f.Name())
|
||||||
if !(dir == "/" || dir == "") {
|
if !(dir == "/" || dir == "") {
|
||||||
@@ -300,7 +300,7 @@ var fileStatTests = []struct {
|
|||||||
"Can stat /test.txt",
|
"Can stat /test.txt",
|
||||||
"/test.txt",
|
"/test.txt",
|
||||||
false,
|
false,
|
||||||
func(f afero.Fs) error {
|
func(f symFs) error {
|
||||||
if _, err := f.Create("/test.txt"); err != nil {
|
if _, err := f.Create("/test.txt"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -331,7 +331,7 @@ var fileStatTests = []struct {
|
|||||||
"Can stat system-specific properties of /test.txt",
|
"Can stat system-specific properties of /test.txt",
|
||||||
"/test.txt",
|
"/test.txt",
|
||||||
false,
|
false,
|
||||||
func(f afero.Fs) error {
|
func(f symFs) error {
|
||||||
if _, err := f.Create("/test.txt"); err != nil {
|
if _, err := f.Create("/test.txt"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -385,7 +385,7 @@ var fileStatTests = []struct {
|
|||||||
"Can stat /mydir/test.txt",
|
"Can stat /mydir/test.txt",
|
||||||
"/mydir/test.txt",
|
"/mydir/test.txt",
|
||||||
false,
|
false,
|
||||||
func(f afero.Fs) error {
|
func(f symFs) error {
|
||||||
if err := f.Mkdir("/mydir", os.ModePerm); err != nil {
|
if err := f.Mkdir("/mydir", os.ModePerm); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -420,7 +420,7 @@ var fileStatTests = []struct {
|
|||||||
"Can stat system-specific properties of /mydir/test.txt",
|
"Can stat system-specific properties of /mydir/test.txt",
|
||||||
"/mydir/test.txt",
|
"/mydir/test.txt",
|
||||||
false,
|
false,
|
||||||
func(f afero.Fs) error {
|
func(f symFs) error {
|
||||||
if err := f.Mkdir("/mydir", os.ModePerm); err != nil {
|
if err := f.Mkdir("/mydir", os.ModePerm); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -474,6 +474,114 @@ var fileStatTests = []struct {
|
|||||||
true,
|
true,
|
||||||
false, // HACK: OsFs uses umask, which yields unexpected permission bits (see https://github.com/golang/go/issues/38282)
|
false, // HACK: OsFs uses umask, which yields unexpected permission bits (see https://github.com/golang/go/issues/38282)
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"Can stat symlink to /test.txt",
|
||||||
|
"/existingsymlink",
|
||||||
|
false,
|
||||||
|
func(f symFs) error {
|
||||||
|
if _, err := f.Create("/test.txt"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := f.SymlinkIfPossible("/test.txt", "/existingsymlink"); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
func(f os.FileInfo) error {
|
||||||
|
wantName := "existingsymlink"
|
||||||
|
gotName := f.Name()
|
||||||
|
|
||||||
|
if wantName != gotName {
|
||||||
|
return fmt.Errorf("invalid name, got %v, want %v", gotName, wantName)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Can stat symlink to /mydir",
|
||||||
|
"/existingsymlink",
|
||||||
|
false,
|
||||||
|
func(f symFs) error {
|
||||||
|
if err := f.Mkdir("/mydir", os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := f.SymlinkIfPossible("/mydir", "/existingsymlink"); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
func(f os.FileInfo) error {
|
||||||
|
wantName := "existingsymlink"
|
||||||
|
gotName := f.Name()
|
||||||
|
|
||||||
|
if wantName != gotName {
|
||||||
|
return fmt.Errorf("invalid name, got %v, want %v", gotName, wantName)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Can stat symlink to /mydir/nesteddir",
|
||||||
|
"/existingsymlink",
|
||||||
|
false,
|
||||||
|
func(f symFs) error {
|
||||||
|
if err := f.MkdirAll("/mydir/nesteddir", os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := f.SymlinkIfPossible("/mydir/nesteddir", "/existingsymlink"); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
func(f os.FileInfo) error {
|
||||||
|
wantName := "existingsymlink"
|
||||||
|
gotName := f.Name()
|
||||||
|
|
||||||
|
if wantName != gotName {
|
||||||
|
return fmt.Errorf("invalid name, got %v, want %v", gotName, wantName)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Can stat symlink to symlink to root",
|
||||||
|
"/existingsymlink",
|
||||||
|
false,
|
||||||
|
func(f symFs) error {
|
||||||
|
if err := f.SymlinkIfPossible("/", "/existingsymlink"); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
func(f os.FileInfo) error {
|
||||||
|
wantName := "existingsymlink"
|
||||||
|
gotName := f.Name()
|
||||||
|
|
||||||
|
if wantName != gotName {
|
||||||
|
return fmt.Errorf("invalid name, got %v, want %v", gotName, wantName)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFile_Stat(t *testing.T) {
|
func TestFile_Stat(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user