like lambs

This commit is contained in:
Mark Bates 2019-09-21 17:07:49 -04:00
parent 175eee3600
commit e8a8d77764
3 changed files with 54 additions and 9 deletions

View File

@ -65,23 +65,18 @@ func NewFileInfo(info os.FileInfo) *FileInfo {
}
func WithName(name string, info os.FileInfo) *FileInfo {
s := cleanName(name)
if !strings.HasPrefix(s, "/") {
s = "/" + s
}
fo := NewFileInfo(info)
fo.Details.Name = cleanName(name)
return fo
}
func WithRelName(name string, info os.FileInfo) *FileInfo {
fo := NewFileInfo(info)
s := cleanName(name)
s = strings.TrimPrefix(s, "/")
fo := NewFileInfo(info)
fo.Details.Name = cleanName(s)
fo.Details.Name = s
return fo
}
@ -89,5 +84,8 @@ func cleanName(s string) string {
if strings.Contains(s, "\\") {
s = strings.Replace(s, "\\", "/", -1)
}
if !strings.HasPrefix(s, "/") {
s = "/" + s
}
return s
}

47
pkging/info_test.go Normal file
View File

@ -0,0 +1,47 @@
package pkging
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
func Test_NewFileInfo(t *testing.T) {
const exp = "/public/images/mark.png"
in := []string{
"/public/images/mark.png",
"public/images/mark.png",
"/public\\images/mark.png",
"public/images\\mark.png",
"\\public\\images\\mark.png",
"public\\images\\mark.png",
"\\public/images\\mark.png",
"public\\images/mark.png",
"\\public\\images\\mark.png",
}
for _, n := range in {
t.Run(n, func(st *testing.T) {
r := require.New(st)
f1 := &FileInfo{
Details: Details{
Name: n,
Size: 42,
Mode: os.FileMode(0644),
IsDir: true,
},
}
f2 := NewFileInfo(f1)
r.Equal(exp, f2.Name())
r.Equal(f1.Size(), f2.Size())
r.Equal(f1.Mode(), f2.Mode())
r.Equal(f1.IsDir(), f2.IsDir())
})
}
}

View File

@ -74,7 +74,7 @@ func (f *HTTP) Readdir(count int) ([]os.FileInfo, error) {
infos := make([]os.FileInfo, len(osinfos))
for i, info := range osinfos {
infos[i] = pkging.WithName(info.Name(), info)
infos[i] = pkging.WithRelName(info.Name(), info)
}
return infos, err
}