From 88c40da8a1a4296961cc36ce75892e6f3c635f60 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sat, 21 Sep 2019 14:05:59 -0400 Subject: [PATCH] working for a living --- pkging/mem/file.go | 4 ++++ pkging/pkgtest/file.go | 45 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/pkging/mem/file.go b/pkging/mem/file.go index 79bce9e..8bb1c07 100644 --- a/pkging/mem/file.go +++ b/pkging/mem/file.go @@ -174,6 +174,10 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) { return io.EOF } + if root == path { + return nil + } + pt, err := f.pkging.Parse(path) if err != nil { return err diff --git a/pkging/pkgtest/file.go b/pkging/pkgtest/file.go index 72d7fa6..313160b 100644 --- a/pkging/pkgtest/file.go +++ b/pkging/pkgtest/file.go @@ -2,6 +2,7 @@ package pkgtest import ( "path/filepath" + "sort" "testing" "github.com/markbates/pkger/pkging/pkgutil" @@ -48,9 +49,47 @@ func (s Suite) Test_File_Info(t *testing.T) { // panic("not implemented") // } // -// func (s Suite) Test_File_Readdir(t *testing.T) { -// panic("not implemented") -// } +func (s Suite) Test_File_Readdir(t *testing.T) { + r := require.New(t) + + pkg, err := s.Make() + r.NoError(err) + + cur, err := pkg.Current() + r.NoError(err) + + ip := cur.ImportPath + table := []struct { + in string + }{ + {in: ":/public"}, + {in: ip + ":/public"}, + } + + r.NoError(s.LoadFolder(pkg)) + + for _, tt := range table { + s.Run(t, tt.in, func(st *testing.T) { + r := require.New(st) + + dir, err := pkg.Open(tt.in) + r.NoError(err) + defer dir.Close() + + infos, err := dir.Readdir(-1) + r.NoError(err) + r.Len(infos, 2) + + sort.Slice(infos, func(i, j int) bool { + return infos[i].Name() < infos[j].Name() + }) + + r.Equal("images", infos[0].Name()) + r.Equal("index.html", infos[1].Name()) + }) + } +} + // // func (s Suite) Test_File_Seek(t *testing.T) { // panic("not implemented")