troubled times

This commit is contained in:
Mark Bates 2019-09-03 15:24:16 -04:00
parent d453473dc0
commit 2e1702c1e5
3 changed files with 23 additions and 18 deletions

View File

@ -65,9 +65,22 @@ func NewFileInfo(info os.FileInfo) *FileInfo {
} }
func WithName(name string, info os.FileInfo) *FileInfo { func WithName(name string, info os.FileInfo) *FileInfo {
if ft, ok := info.(*FileInfo); ok { s := cleanName(name)
ft.Details.Name = cleanName(name)
return ft 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) fo := NewFileInfo(info)
@ -79,8 +92,5 @@ func cleanName(s string) string {
if strings.Contains(s, "\\") { if strings.Contains(s, "\\") {
s = strings.Replace(s, "\\", "/", -1) s = strings.Replace(s, "\\", "/", -1)
} }
if !strings.HasPrefix(s, "/") {
s = "/" + s
}
return s return s
} }

View File

@ -151,11 +151,7 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) {
return nil return nil
} }
info = pkging.WithName(strings.TrimPrefix(info.Name(), f.parent.Name), info) info = pkging.WithRelName(strings.TrimPrefix(info.Name(), f.parent.Name), info)
if minf, ok := info.(*pkging.FileInfo); ok {
minf.Details.Name = strings.TrimPrefix(info.Name(), "/")
info = minf
}
infos = append(infos, info) infos = append(infos, info)
if info.IsDir() && path != root { if info.IsDir() && path != root {
return filepath.SkipDir return filepath.SkipDir

View File

@ -5,7 +5,6 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/markbates/pkger/pkging/pkgutil" "github.com/markbates/pkger/pkging/pkgutil"
@ -64,9 +63,9 @@ func (s Suite) Test_HTTP_Dir(t *testing.T) {
req string req string
exp string exp string
}{ }{
{in: "/", req: "/", exp: `>/public/</a`}, {in: "/", req: "/", exp: `>public/</a`},
{in: ":" + "/", req: "/", exp: `>/public/</a`}, {in: ":" + "/", req: "/", exp: `>public/</a`},
{in: ip + ":" + "/", req: "/", exp: `>/public/</a`}, {in: ip + ":" + "/", req: "/", exp: `>public/</a`},
} }
for _, tt := range table { for _, tt := range table {
@ -105,7 +104,7 @@ func (s Suite) Test_HTTP_Dir_IndexHTML(t *testing.T) {
{in: ip + ":" + "/public", req: "/"}, {in: ip + ":" + "/public", req: "/"},
} }
exp := "!/public/index.html" exp := "index.html"
for _, tt := range table { for _, tt := range table {
t.Run(tt.in+exp, func(st *testing.T) { t.Run(tt.in+exp, func(st *testing.T) {
r := require.New(st) r := require.New(st)
@ -123,8 +122,8 @@ func (s Suite) Test_HTTP_Dir_IndexHTML(t *testing.T) {
b, err := ioutil.ReadAll(res.Body) b, err := ioutil.ReadAll(res.Body)
r.NoError(err) r.NoError(err)
body := strings.TrimSpace(string(b)) body := string(b)
r.Equal(exp, body) r.Contains(body, exp)
r.NotContains(body, "mark.png") r.NotContains(body, "mark.png")
}) })
} }