From da03471d96a7433fa2c9efcb17550d2d6667c14f Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Wed, 30 Oct 2019 16:39:05 -0400 Subject: [PATCH] juliet was waiting with a safety net --- pkging/costello/costello.go | 57 +++++++-------------- pkging/costello/load_ref.go | 52 +++++++++++++++++++ pkging/costello/open.go | 6 ++- pkging/costello/stat.go | 29 +++++++++++ pkging/mem/mem_test.go | 22 ++------ pkging/mem/{create_test.go => open_test.go} | 7 +-- pkging/mem/stat_test.go | 20 ++++++++ 7 files changed, 129 insertions(+), 64 deletions(-) create mode 100644 pkging/costello/load_ref.go rename pkging/mem/{create_test.go => open_test.go} (69%) create mode 100644 pkging/mem/stat_test.go diff --git a/pkging/costello/costello.go b/pkging/costello/costello.go index eda283e..ebad4ed 100644 --- a/pkging/costello/costello.go +++ b/pkging/costello/costello.go @@ -1,52 +1,33 @@ package costello import ( - "io" - "os" - "path/filepath" - "strings" + "testing" "github.com/markbates/pkger/pkging" + "github.com/stretchr/testify/require" ) -func LoadRef(ref *Ref, pkg pkging.Pkger) error { - return filepath.Walk(ref.Dir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - of, err := os.Open(path) - if err != nil { - return err - } - defer of.Close() +type AllFn func(ref *Ref) (pkging.Pkger, error) - if a, ok := pkg.(pkging.Adder); ok { - return a.Add(of) - } +func All(t *testing.T, fn AllFn) { + r := require.New(t) + type tf func(*testing.T, pkging.Pkger) - path = strings.TrimPrefix(path, ref.Dir) + tests := map[string]tf{ + "OpenTest": OpenTest, + "StatTest": StatTest, + } - pt, err := pkg.Parse(path) - if err != nil { - return err - } + ref, err := NewRef() + r.NoError(err) - if err := pkg.MkdirAll(filepath.Dir(pt.Name), 0755); err != nil { - return err - } + for n, tt := range tests { + t.Run(n, func(st *testing.T) { + pkg, err := fn(ref) + r.NoError(err) - if info.IsDir() { - return nil - } - f, err := pkg.Create(pt.String()) - if err != nil { - return err - } - defer f.Close() + tt(st, pkg) + }) + } - if _, err := io.Copy(f, of); err != nil { - return err - } - return nil - }) } diff --git a/pkging/costello/load_ref.go b/pkging/costello/load_ref.go new file mode 100644 index 0000000..eda283e --- /dev/null +++ b/pkging/costello/load_ref.go @@ -0,0 +1,52 @@ +package costello + +import ( + "io" + "os" + "path/filepath" + "strings" + + "github.com/markbates/pkger/pkging" +) + +func LoadRef(ref *Ref, pkg pkging.Pkger) error { + return filepath.Walk(ref.Dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + of, err := os.Open(path) + if err != nil { + return err + } + defer of.Close() + + if a, ok := pkg.(pkging.Adder); ok { + return a.Add(of) + } + + path = strings.TrimPrefix(path, ref.Dir) + + pt, err := pkg.Parse(path) + if err != nil { + return err + } + + if err := pkg.MkdirAll(filepath.Dir(pt.Name), 0755); err != nil { + return err + } + + if info.IsDir() { + return nil + } + f, err := pkg.Create(pt.String()) + if err != nil { + return err + } + defer f.Close() + + if _, err := io.Copy(f, of); err != nil { + return err + } + return nil + }) +} diff --git a/pkging/costello/open.go b/pkging/costello/open.go index 563b3eb..ea12c82 100644 --- a/pkging/costello/open.go +++ b/pkging/costello/open.go @@ -14,10 +14,10 @@ import ( func OpenTest(t *testing.T, pkg pkging.Pkger) { r := require.New(t) - app, err := NewRef() + ref, err := NewRef() r.NoError(err) - osf, err := os.Open(filepath.Join(app.Dir, "go.mod")) + osf, err := os.Open(filepath.Join(ref.Dir, "go.mod")) r.NoError(err) osi, err := osf.Stat() @@ -27,6 +27,8 @@ func OpenTest(t *testing.T, pkg pkging.Pkger) { r.NoError(err) r.NoError(osf.Close()) + r.NoError(LoadRef(ref, pkg)) + pf, err := pkg.Open("/go.mod") r.NoError(err) diff --git a/pkging/costello/stat.go b/pkging/costello/stat.go index 15b8b0f..442788b 100644 --- a/pkging/costello/stat.go +++ b/pkging/costello/stat.go @@ -1 +1,30 @@ package costello + +import ( + "os" + "path/filepath" + "testing" + "time" + + "github.com/markbates/pkger/pkging" + "github.com/stretchr/testify/require" +) + +func StatTest(t *testing.T, pkg pkging.Pkger) { + r := require.New(t) + + ref, err := NewRef() + r.NoError(err) + + osi, err := os.Stat(filepath.Join(ref.Dir, "go.mod")) + r.NoError(err) + + r.NoError(LoadRef(ref, pkg)) + psi, err := pkg.Stat("/go.mod") + r.NoError(err) + + r.Equal(osi.Name(), psi.Name()) + r.Equal(osi.Mode(), psi.Mode()) + r.Equal(osi.Size(), psi.Size()) + r.Equal(osi.ModTime().Format(time.RFC3339), psi.ModTime().Format(time.RFC3339)) +} diff --git a/pkging/mem/mem_test.go b/pkging/mem/mem_test.go index 2082dc0..e18817f 100644 --- a/pkging/mem/mem_test.go +++ b/pkging/mem/mem_test.go @@ -4,28 +4,12 @@ import ( "testing" "github.com/markbates/pkger/pkging" + "github.com/markbates/pkger/pkging/costello" "github.com/markbates/pkger/pkging/mem" - "github.com/markbates/pkger/pkging/pkgtest" ) func Test_Pkger(t *testing.T) { - t.SkipNow() - suite, err := pkgtest.NewSuite("memos", func() (pkging.Pkger, error) { - app, err := pkgtest.App() - if err != nil { - return nil, err - } - - pkg, err := mem.New(app.Info) - if err != nil { - return nil, err - } - - return pkg, nil + costello.All(t, func(ref *costello.Ref) (pkging.Pkger, error) { + return mem.New(ref.Info) }) - if err != nil { - t.Fatal(err) - } - - suite.Test(t) } diff --git a/pkging/mem/create_test.go b/pkging/mem/open_test.go similarity index 69% rename from pkging/mem/create_test.go rename to pkging/mem/open_test.go index 4ee879b..d3f25a5 100644 --- a/pkging/mem/create_test.go +++ b/pkging/mem/open_test.go @@ -10,13 +10,10 @@ import ( func Test_Pkger_Open(t *testing.T) { r := require.New(t) - app, err := costello.NewRef() + ref, err := costello.NewRef() r.NoError(err) - pkg, err := New(app.Info) - r.NoError(err) - - err = costello.LoadRef(app, pkg) + pkg, err := New(ref.Info) r.NoError(err) costello.OpenTest(t, pkg) diff --git a/pkging/mem/stat_test.go b/pkging/mem/stat_test.go new file mode 100644 index 0000000..4bf9e89 --- /dev/null +++ b/pkging/mem/stat_test.go @@ -0,0 +1,20 @@ +package mem + +import ( + "testing" + + "github.com/markbates/pkger/pkging/costello" + "github.com/stretchr/testify/require" +) + +func Test_Pkger_Stat(t *testing.T) { + r := require.New(t) + + ref, err := costello.NewRef() + r.NoError(err) + + pkg, err := New(ref.Info) + r.NoError(err) + + costello.StatTest(t, pkg) +}