don't mind

This commit is contained in:
Mark Bates 2019-09-03 12:41:21 -04:00
parent aff26c9df7
commit c129ccbfe0
6 changed files with 181 additions and 33 deletions

View File

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"path/filepath"
"strings" "strings"
"time" "time"
@ -149,10 +150,13 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) {
if pt.Name == f.parent.Name { if pt.Name == f.parent.Name {
return nil return nil
} }
// if f.parent.Name != "/" {
info = pkging.WithName(strings.TrimPrefix(info.Name(), f.parent.Name), info) info = pkging.WithName(strings.TrimPrefix(info.Name(), f.parent.Name), info)
// }
infos = append(infos, info) infos = append(infos, info)
if info.IsDir() && path != root {
return filepath.SkipDir
}
return nil return nil
}) })

View File

@ -86,26 +86,12 @@ func (fx *Pkger) RemoveAll(name string) error {
return err return err
} }
return fx.Walk("/", func(path string, info os.FileInfo, err error) error { fx.files.Range(func(key pkging.Path, file pkging.File) bool {
if err != nil { if strings.HasPrefix(key.Name, pt.Name) {
return err fx.files.Delete(key)
} }
return true
if !strings.HasPrefix(path, pt.String()) {
return nil
}
ph, err := fx.Parse(path)
if err != nil {
return err
}
fx.files.Delete(ph)
return nil
}) })
if _, ok := fx.files.Load(pt); !ok {
return &os.PathError{"remove", pt.String(), fmt.Errorf("no such file or directory")}
}
fx.files.Delete(pt)
return nil return nil
} }

View File

@ -15,10 +15,16 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
if err != nil { if err != nil {
return err return err
} }
skip := "!"
for _, k := range keys { for _, k := range keys {
if !strings.HasPrefix(k.Name, pt.Name) { if !strings.HasPrefix(k.Name, pt.Name) {
continue continue
} }
if strings.HasPrefix(k.Name, skip) {
continue
}
fl, ok := f.files.Load(k) fl, ok := f.files.Load(k)
if !ok { if !ok {
return fmt.Errorf("could not find %s", k) return fmt.Errorf("could not find %s", k)
@ -30,6 +36,12 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
fi = pkging.WithName(strings.TrimPrefix(k.Name, pt.Name), fi) fi = pkging.WithName(strings.TrimPrefix(k.Name, pt.Name), fi)
err = wf(k.String(), fi, nil) err = wf(k.String(), fi, nil)
if err == filepath.SkipDir {
skip = k.Name
continue
}
if err != nil { if err != nil {
return err return err
} }

View File

@ -33,23 +33,13 @@ func (s Suite) Test_File_Info(t *testing.T) {
f, err := s.Open(tt.in) f, err := s.Open(tt.in)
r.NoError(err) r.NoError(err)
r.Equal(mould, f.Name())
r.Equal(cur.ImportPath, f.Info().ImportPath) r.Equal(cur.ImportPath, f.Info().ImportPath)
r.NoError(f.Close())
}) })
} }
} }
func (s Suite) Test_File_Name(t *testing.T) {
panic("not implemented")
}
func (s Suite) Test_File_Open(t *testing.T) {
panic("not implemented")
}
func (s Suite) Test_File_Path(t *testing.T) {
panic("not implemented")
}
func (s Suite) Test_File_Read(t *testing.T) { func (s Suite) Test_File_Read(t *testing.T) {
panic("not implemented") panic("not implemented")
} }

140
pkging/pkgtest/http.go Normal file
View File

@ -0,0 +1,140 @@
package pkgtest
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"github.com/markbates/pkger/pkging/pkgutil"
"github.com/stretchr/testify/require"
)
// examples/app
// ├── Dockerfile
// ├── Makefile
// ├── go.mod
// ├── go.sum
// ├── main.go
// ├── public
// │   ├── images
// │   │   ├── mark-small.png
// │   │   ├── mark.png
// │   │   ├── mark_250px.png
// │   │   └── mark_400px.png
// │   └── index.html
// └── templates
// ├── a.txt
// └── b
// └── b.txt
func (s Suite) LoadFolder() error {
files := []string{
"/main.go",
"/public/images/mark.png",
"/public/index.html",
"/templates/a.txt",
"/templates/b/b.txt",
}
for _, f := range files {
if err := s.MkdirAll(filepath.Dir(f), 0755); err != nil {
return err
}
if err := pkgutil.WriteFile(s, f, []byte("!"+f), 0644); err != nil {
return err
}
}
return nil
}
func (s Suite) Test_HTTP_Dir(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
r.NoError(err)
ip := cur.ImportPath
r.NoError(s.LoadFolder())
table := []struct {
in string
req string
exp string
}{
{in: "/", req: "/", exp: `>/public/</a`},
{in: ":" + "/", req: "/", exp: `>/public/</a`},
{in: ip + ":" + "/", req: "/", exp: `>/public/</a`},
}
for _, tt := range table {
dir, err := s.Open(tt.in)
r.NoError(err)
ts := httptest.NewServer(http.FileServer(dir))
defer ts.Close()
res, err := http.Get(ts.URL + tt.req)
r.NoError(err)
r.Equal(200, res.StatusCode)
b, err := ioutil.ReadAll(res.Body)
r.NoError(err)
r.Contains(string(b), tt.exp)
r.NotContains(string(b), "mark.png")
}
}
// func (s Suite) Test_HTTP_File_Memory(t *testing.T) {
// r := require.New(t)
//
// fs := NewPkger()
// r.NoError(Folder.Create(fs))
//
// dir, err := fs.Open("/")
// r.NoError(err)
// ts := httptest.NewServer(http.FileServer(dir))
// defer ts.Close()
//
// res, err := http.Get(ts.URL + "/public/images/mark.png")
// r.NoError(err)
// r.Equal(200, res.StatusCode)
//
// b, err := ioutil.ReadAll(res.Body)
// r.NoError(err)
// r.Contains(string(b), `!/public/images/mark.png`)
// }
//
// func (s Suite) Test_HTTP_Dir_Memory_StripPrefix(t *testing.T) {
// r := require.New(t)
//
// fs := NewPkger()
// r.NoError(Folder.Create(fs))
//
// dir, err := fs.Open("/public")
// r.NoError(err)
// defer dir.Close()
//
// ts := httptest.NewServer(http.StripPrefix("/assets/", http.FileServer(dir)))
// defer ts.Close()
//
// res, err := http.Get(ts.URL + "/assets/images/mark.png")
// r.NoError(err)
// r.Equal(200, res.StatusCode)
//
// b, _ := ioutil.ReadAll(res.Body)
// // r.NoError(err)
// r.Contains(string(b), "!/public/images/mark.png")
//
// res, err = http.Get(ts.URL + "/assets/images/")
// r.NoError(err)
// r.Equal(200, res.StatusCode)
//
// b, _ = ioutil.ReadAll(res.Body)
// // r.NoError(err)
// r.Contains(string(b), `<a href="/mark.png">/mark.png</a>`)
// r.NotContains(string(b), `/public`)
// r.NotContains(string(b), `/images`)
// r.NotContains(string(b), `/go.mod`)
// }

View File

@ -18,6 +18,22 @@ type File struct {
pkging pkging.Pkger pkging pkging.Pkger
} }
type HTTPFile struct {
http.File
}
func (f *HTTPFile) Readdir(n int) ([]os.FileInfo, error) {
infos, err := f.File.Readdir(n)
if err != nil {
return nil, err
}
for i, info := range infos {
infos[i] = pkging.NewFileInfo(info)
}
return infos, nil
}
func NewFile(fx pkging.Pkger, osf *os.File) (*File, error) { func NewFile(fx pkging.Pkger, osf *os.File) (*File, error) {
pt, err := fx.Parse(osf.Name()) pt, err := fx.Parse(osf.Name())
@ -62,7 +78,7 @@ func (f *File) Name() string {
} }
func (f *File) Open(name string) (http.File, error) { func (f *File) Open(name string) (http.File, error) {
return f.File, nil return &HTTPFile{f.File}, nil
} }
func (f *File) Path() pkging.Path { func (f *File) Path() pkging.Path {