From 2e1702c1e55be1ffe3b486fa9e34ce9fc45de822 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Tue, 3 Sep 2019 15:24:16 -0400 Subject: [PATCH] troubled times --- pkging/info.go | 22 ++++++++++++++++------ pkging/mem/file.go | 6 +----- pkging/pkgtest/http.go | 13 ++++++------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/pkging/info.go b/pkging/info.go index 3e6e97a..9d5f9b5 100644 --- a/pkging/info.go +++ b/pkging/info.go @@ -65,9 +65,22 @@ func NewFileInfo(info os.FileInfo) *FileInfo { } func WithName(name string, info os.FileInfo) *FileInfo { - if ft, ok := info.(*FileInfo); ok { - ft.Details.Name = cleanName(name) - return ft + 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 { + s := cleanName(name) + + if !strings.HasPrefix(s, "/") { + s = "/" + s } fo := NewFileInfo(info) @@ -79,8 +92,5 @@ 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/mem/file.go b/pkging/mem/file.go index 803daed..c0c1548 100644 --- a/pkging/mem/file.go +++ b/pkging/mem/file.go @@ -151,11 +151,7 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) { return nil } - info = pkging.WithName(strings.TrimPrefix(info.Name(), f.parent.Name), info) - if minf, ok := info.(*pkging.FileInfo); ok { - minf.Details.Name = strings.TrimPrefix(info.Name(), "/") - info = minf - } + info = pkging.WithRelName(strings.TrimPrefix(info.Name(), f.parent.Name), info) infos = append(infos, info) if info.IsDir() && path != root { return filepath.SkipDir diff --git a/pkging/pkgtest/http.go b/pkging/pkgtest/http.go index 9f80810..b6bf1b1 100644 --- a/pkging/pkgtest/http.go +++ b/pkging/pkgtest/http.go @@ -5,7 +5,6 @@ import ( "net/http" "net/http/httptest" "path/filepath" - "strings" "testing" "github.com/markbates/pkger/pkging/pkgutil" @@ -64,9 +63,9 @@ func (s Suite) Test_HTTP_Dir(t *testing.T) { req string exp string }{ - {in: "/", req: "/", exp: `>/public//public//public/public/public/public/