Skip to content

Commit

Permalink
fix File.DirAndName for FS with prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Mar 13, 2024
1 parent 2c7cd32 commit b518cbe
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 37 deletions.
2 changes: 1 addition & 1 deletion dropboxfs/dropboxfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (*fileSystem) MatchAnyPattern(name string, patterns []string) (bool, error)
return fsimpl.MatchAnyPattern(name, patterns)
}

func (dbfs *fileSystem) SplitDirAndName(filePath string) (dir, name string) {
func (*fileSystem) SplitDirAndName(filePath string) (dir, name string) {
return fsimpl.SplitDirAndName(filePath, 0, Separator)
}

Expand Down
2 changes: 1 addition & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (file File) Dir() File {
func (file File) DirAndName() (dir File, name string) {
fileSystem, path := file.ParseRawURI()
dirPath, name := fileSystem.SplitDirAndName(path)
return File(dirPath), name
return fileSystem.JoinCleanFile(dirPath), name
}

// VolumeName returns the name of the volume at the beginning of the file path,
Expand Down
43 changes: 20 additions & 23 deletions fsimpl/fsimpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,36 @@ import (
"github.com/stretchr/testify/require"
)

func Test_DirAndName(t *testing.T) {
func TestSplitDirAndName(t *testing.T) {
refTable := map[string][2]string{
"/": {"/", ""},
"./": {".", "."},
".": {".", "."},
// "/.": {"/", ""},
"hello": {".", "hello"},
"./hello": {".", "hello"},
"hello/": {".", "hello"},
"./hello/": {".", "hello"},
"/hello/world": {"/hello", "world"},
"hello/world": {"hello", "world"},
"/hello/world/": {"/hello", "world"},
"hello/world/": {"hello", "world"},
"/": {"/", ""},
"./": {".", "."},
".": {".", "."},
"/.": {"/", "."},
"hello": {".", "hello"},
"./hello": {".", "hello"},
"hello/": {".", "hello"},
"./hello/": {".", "hello"},
"/hello/world": {"/hello", "world"},
"hello/world": {"hello", "world"},
"/hello/world/": {"/hello", "world"},
"hello/world/": {"hello", "world"},
"http://example.com/dir": {"http://example.com", "dir"},
"sftp://example.com/dir/subdir": {"sftp://example.com/dir", "subdir"},
}

for filePath, dirAndName := range refTable {
dir, name := SplitDirAndName(filePath, 0, "/")
assert.Equal(t, dir, dirAndName[0], "filePath(%#v): %#v, %#v", filePath, dir, name)
assert.Equal(t, name, dirAndName[1], "filePath(%#v): %#v, %#v", filePath, dir, name)
assert.Equalf(t, dirAndName[0], dir, "SplitDirAndName(%#v) = %#v, %#v", filePath, dir, name)
assert.Equalf(t, dirAndName[1], name, "SplitDirAndName(%#v) = %#v, %#v", filePath, dir, name)
}
}

func Test_RandomString(t *testing.T) {
str := RandomString()
assert.Equal(t, 20, len(str))
func TestRandomString(t *testing.T) {
require.Len(t, RandomString(), 20, "RandomString length should be 20")
}

func Test_ReadonlyFileBuffer(t *testing.T) {
func TestReadonlyFileBuffer(t *testing.T) {
out := make([]byte, 0)
b := NewReadonlyFileBuffer(nil, nil)
n, err := b.Read(out)
Expand Down Expand Up @@ -132,7 +133,3 @@ func TestJoinCleanPath(t *testing.T) {
})
}
}

func TestRandomString(t *testing.T) {
require.Len(t, RandomString(), 20, "RandomString length should be 20")
}
2 changes: 1 addition & 1 deletion ftpfs/ftpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (f *fileSystem) AbsPath(filePath string) string {
return Prefix + strings.TrimPrefix(filePath, Separator)
}

func (f *fileSystem) SplitDirAndName(filePath string) (dir, name string) {
func (*fileSystem) SplitDirAndName(filePath string) (dir, name string) {
return fsimpl.SplitDirAndName(filePath, 0, Separator)
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ go 1.21

require (
github.com/fsnotify/fsnotify v1.7.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
2 changes: 1 addition & 1 deletion httpfs/httpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (f *fileSystem) AbsPath(filePath string) string {
return f.prefix + strings.TrimPrefix(filePath, Separator)
}

func (f *fileSystem) SplitDirAndName(filePath string) (dir, name string) {
func (*fileSystem) SplitDirAndName(filePath string) (dir, name string) {
return fsimpl.SplitDirAndName(filePath, 0, Separator)
}

Expand Down
2 changes: 2 additions & 0 deletions httpfs/httpfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func TestStat(t *testing.T) {

file := fs.File("https://raw.githubusercontent.com/ungerik/go-fs/master/README.md")
info := file.Info()

// Note that this can fail when the file time changes between the two calls
assert.Equal(t, fs.NewFileInfo(file, osInfo, false), info)
}

Expand Down
2 changes: 1 addition & 1 deletion localfilesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (local *LocalFileSystem) MatchAnyPattern(name string, patterns []string) (b
return false, nil
}

func (local *LocalFileSystem) SplitDirAndName(filePath string) (dir, name string) {
func (*LocalFileSystem) SplitDirAndName(filePath string) (dir, name string) {
filePath = expandTilde(filePath)
return fsimpl.SplitDirAndName(filePath, len(filepath.VolumeName(filePath)), Separator)
}
Expand Down
2 changes: 1 addition & 1 deletion localfilesystem_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const localRoot = `/`

var extraDirPermissions Permissions = AllExecute

func hasLocalFileAttributeHidden(path string) (bool, error) {
func hasLocalFileAttributeHidden(string) (bool, error) {
return false, nil
}

Expand Down
2 changes: 1 addition & 1 deletion memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (f MemFile) Size() int64 {
// It's valid to call this method on a nil pointer,
// will return false in this case.
func (f MemFile) Exists() bool {
return &f != nil && f.FileName != ""
return f.FileName != ""
}

// CheckExists return an ErrDoesNotExist error
Expand Down
6 changes: 3 additions & 3 deletions memfilesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (fs *MemFileSystem) MakeDir(dirPath string, perm []Permissions) error {
return fs.makeDir(dirPath, perm)
}

func (fs *MemFileSystem) makeDir(dirPath string, perm []Permissions) error {
func (fs *MemFileSystem) makeDir(dirPath string, _ []Permissions) error {
if dirPath == "" {
return ErrEmptyPath
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func (fs *MemFileSystem) MakeAllDirs(dirPath string, perm []Permissions) error {
return fs.makeAllDirs(dirPath, perm)
}

func (fs *MemFileSystem) makeAllDirs(dirPath string, perm []Permissions) error {
func (fs *MemFileSystem) makeAllDirs(dirPath string, _ []Permissions) error {
if dirPath == "" {
return ErrEmptyPath
}
Expand Down Expand Up @@ -341,7 +341,7 @@ func (*MemFileSystem) MatchAnyPattern(name string, patterns []string) (bool, err
}

func (fs *MemFileSystem) SplitDirAndName(filePath string) (dir, name string) {
return fs.SplitDirAndName(filePath)
return fsimpl.SplitDirAndName(filePath, 0, "/")
}

func (fs *MemFileSystem) VolumeName(filePath string) string {
Expand Down
2 changes: 1 addition & 1 deletion s3fs/s3fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (s *fileSystem) MatchAnyPattern(name string, patterns []string) (bool, erro
return fsimpl.MatchAnyPattern(name, patterns)
}

func (s *fileSystem) SplitDirAndName(filePath string) (dir, name string) {
func (*fileSystem) SplitDirAndName(filePath string) (dir, name string) {
return fsimpl.SplitDirAndName(filePath, 0, Separator)
}

Expand Down
3 changes: 2 additions & 1 deletion sftpfs/sftpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func (*fileSystem) URL(cleanPath string) string {
}

func (f *fileSystem) JoinCleanFile(uriParts ...string) fs.File {
println("sftpfs.JoinCleanFile", f.prefix, f.JoinCleanPath(uriParts...))
return fs.File(f.prefix + f.JoinCleanPath(uriParts...))
}

Expand All @@ -291,7 +292,7 @@ func (f *fileSystem) AbsPath(filePath string) string {
return Prefix + strings.TrimPrefix(filePath, Separator)
}

func (f *fileSystem) SplitDirAndName(filePath string) (dir, name string) {
func (*fileSystem) SplitDirAndName(filePath string) (dir, name string) {
return fsimpl.SplitDirAndName(filePath, 0, Separator)
}

Expand Down

0 comments on commit b518cbe

Please sign in to comment.