From e8a8d77764989949a052b98d75b24d2ccf1481c0 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 21 Sep 2019 17:07:49 -0400 Subject: [PATCH] like lambs --- pkging/info.go | 14 ++++++------- pkging/info_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ pkging/stdos/file.go | 2 +- 3 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 pkging/info_test.go diff --git a/pkging/info.go b/pkging/info.go index 8ec4e3b..418292d 100644 --- a/pkging/info.go +++ b/pkging/info.go @@ -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 } diff --git a/pkging/info_test.go b/pkging/info_test.go new file mode 100644 index 0000000..bde8c0f --- /dev/null +++ b/pkging/info_test.go @@ -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()) + }) + } + +} diff --git a/pkging/stdos/file.go b/pkging/stdos/file.go index 249c277..7ab1619 100644 --- a/pkging/stdos/file.go +++ b/pkging/stdos/file.go @@ -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 }