From c129ccbfe07a43d7790c50aae1519967d280e97b Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Tue, 3 Sep 2019 12:41:21 -0400 Subject: [PATCH] don't mind --- pkging/mem/file.go | 8 ++- pkging/mem/mem.go | 22 ++----- pkging/mem/walk.go | 12 ++++ pkging/pkgtest/file.go | 14 +---- pkging/pkgtest/http.go | 140 +++++++++++++++++++++++++++++++++++++++++ pkging/stdos/file.go | 18 +++++- 6 files changed, 181 insertions(+), 33 deletions(-) create mode 100644 pkging/pkgtest/http.go diff --git a/pkging/mem/file.go b/pkging/mem/file.go index 6f32073..af9d829 100644 --- a/pkging/mem/file.go +++ b/pkging/mem/file.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "path" + "path/filepath" "strings" "time" @@ -149,10 +150,13 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) { if pt.Name == f.parent.Name { return nil } - // if f.parent.Name != "/" { + info = pkging.WithName(strings.TrimPrefix(info.Name(), f.parent.Name), info) - // } infos = append(infos, info) + if info.IsDir() && path != root { + return filepath.SkipDir + } + return nil }) diff --git a/pkging/mem/mem.go b/pkging/mem/mem.go index d0bf18c..2eac9ff 100644 --- a/pkging/mem/mem.go +++ b/pkging/mem/mem.go @@ -86,26 +86,12 @@ func (fx *Pkger) RemoveAll(name string) error { return err } - return fx.Walk("/", func(path string, info os.FileInfo, err error) error { - if err != nil { - return err + fx.files.Range(func(key pkging.Path, file pkging.File) bool { + if strings.HasPrefix(key.Name, pt.Name) { + fx.files.Delete(key) } - - if !strings.HasPrefix(path, pt.String()) { - return nil - } - - ph, err := fx.Parse(path) - if err != nil { - return err - } - fx.files.Delete(ph) - return nil + return true }) - 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 } diff --git a/pkging/mem/walk.go b/pkging/mem/walk.go index 96f7421..f608328 100644 --- a/pkging/mem/walk.go +++ b/pkging/mem/walk.go @@ -15,10 +15,16 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error { if err != nil { return err } + + skip := "!" + for _, k := range keys { if !strings.HasPrefix(k.Name, pt.Name) { continue } + if strings.HasPrefix(k.Name, skip) { + continue + } fl, ok := f.files.Load(k) if !ok { 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) err = wf(k.String(), fi, nil) + if err == filepath.SkipDir { + + skip = k.Name + continue + } + if err != nil { return err } diff --git a/pkging/pkgtest/file.go b/pkging/pkgtest/file.go index ca537a6..11e5a80 100644 --- a/pkging/pkgtest/file.go +++ b/pkging/pkgtest/file.go @@ -33,23 +33,13 @@ func (s Suite) Test_File_Info(t *testing.T) { f, err := s.Open(tt.in) r.NoError(err) + r.Equal(mould, f.Name()) 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) { panic("not implemented") } diff --git a/pkging/pkgtest/http.go b/pkging/pkgtest/http.go new file mode 100644 index 0000000..292dcb4 --- /dev/null +++ b/pkging/pkgtest/http.go @@ -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//public//public//mark.png`) +// r.NotContains(string(b), `/public`) +// r.NotContains(string(b), `/images`) +// r.NotContains(string(b), `/go.mod`) +// } diff --git a/pkging/stdos/file.go b/pkging/stdos/file.go index fd64c9d..56bc051 100644 --- a/pkging/stdos/file.go +++ b/pkging/stdos/file.go @@ -18,6 +18,22 @@ type File struct { 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) { pt, err := fx.Parse(osf.Name()) @@ -62,7 +78,7 @@ func (f *File) Name() string { } func (f *File) Open(name string) (http.File, error) { - return f.File, nil + return &HTTPFile{f.File}, nil } func (f *File) Path() pkging.Path {