From 296799efbc52f8eeb133d89d9ca306b074331db0 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Wed, 30 Oct 2019 17:15:49 -0400 Subject: [PATCH] even though I've seen the movie --- pkging/costello/costello.go | 16 ++++++++++---- pkging/costello/current.go | 15 +++++++++++++ pkging/costello/info.go | 16 ++++++++++++++ pkging/costello/mkdir_all.go | 41 ++++++++++++++++++++++++++++++++++++ pkging/costello/open.go | 22 ++++++++++++------- pkging/mem/current_test.go | 20 ++++++++++++++++++ pkging/mem/info_test.go | 20 ++++++++++++++++++ 7 files changed, 138 insertions(+), 12 deletions(-) create mode 100644 pkging/mem/current_test.go create mode 100644 pkging/mem/info_test.go diff --git a/pkging/costello/costello.go b/pkging/costello/costello.go index d4a2fc8..9acb6aa 100644 --- a/pkging/costello/costello.go +++ b/pkging/costello/costello.go @@ -1,6 +1,7 @@ package costello import ( + "fmt" "testing" "github.com/markbates/pkger/pkging" @@ -15,13 +16,20 @@ func All(t *testing.T, ref *Ref, fn AllFn) { type tf func(*testing.T, *Ref, pkging.Pkger) tests := map[string]tf{ - "OpenTest": OpenTest, - "StatTest": StatTest, - "CreateTest": CreateTest, + "OpenTest": OpenTest, + "StatTest": StatTest, + "CreateTest": CreateTest, + "CurrentTest": CurrentTest, + "InfoTest": InfoTest, + "MkdirAll": MkdirAllTest, } + pkg, err := fn(ref) + r.NoError(err) + for n, tt := range tests { - t.Run(n, func(st *testing.T) { + t.Run(fmt.Sprintf("%T/%s", pkg, n), func(st *testing.T) { + st.Parallel() pkg, err := fn(ref) r.NoError(err) diff --git a/pkging/costello/current.go b/pkging/costello/current.go index 15b8b0f..b3bbfbd 100644 --- a/pkging/costello/current.go +++ b/pkging/costello/current.go @@ -1 +1,16 @@ package costello + +import ( + "testing" + + "github.com/markbates/pkger/pkging" + "github.com/stretchr/testify/require" +) + +func CurrentTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { + r := require.New(t) + + cur, err := pkg.Current() + r.NoError(err) + r.Equal(ref.Info, cur) +} diff --git a/pkging/costello/info.go b/pkging/costello/info.go index 15b8b0f..d361e31 100644 --- a/pkging/costello/info.go +++ b/pkging/costello/info.go @@ -1 +1,17 @@ package costello + +import ( + "testing" + + "github.com/markbates/pkger/pkging" + "github.com/stretchr/testify/require" +) + +func InfoTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { + r := require.New(t) + + info, err := pkg.Info("app") + r.NoError(err) + + r.Equal(ref.Info, info) +} diff --git a/pkging/costello/mkdir_all.go b/pkging/costello/mkdir_all.go index 15b8b0f..2bca6e5 100644 --- a/pkging/costello/mkdir_all.go +++ b/pkging/costello/mkdir_all.go @@ -1 +1,42 @@ package costello + +import ( + "os" + "path" + "path/filepath" + "strings" + "testing" + + "github.com/markbates/pkger/pkging" + "github.com/stretchr/testify/require" +) + +func MkdirAllTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { + r := require.New(t) + + parts := []string{"all", "this", "useless", "beauty"} + + fp := ref.Dir + for _, part := range parts { + fp = filepath.Join(fp, part) + } + + os.RemoveAll(fp) + defer os.RemoveAll(fp) + + _, err := os.Stat(fp) + r.Error(err) + + name := path.Join(parts...) + if !strings.HasPrefix(name, "/") { + name = "/" + name + } + + _, err = pkg.Stat(name) + r.Error(err) + + r.NoError(os.MkdirAll(fp, 0755)) + r.NoError(pkg.MkdirAll(name, 0755)) + + openTest(name, t, ref, pkg) +} diff --git a/pkging/costello/open.go b/pkging/costello/open.go index 4eebb0e..9319181 100644 --- a/pkging/costello/open.go +++ b/pkging/costello/open.go @@ -25,10 +25,6 @@ func openTest(name string, t *testing.T, ref *Ref, pkg pkging.Pkger) { osi, err := osf.Stat() r.NoError(err) - osb, err := ioutil.ReadAll(osf) - r.NoError(err) - r.NoError(osf.Close()) - r.NoError(LoadRef(ref, pkg)) pf, err := pkg.Open(fmt.Sprintf("/%s", name)) @@ -37,13 +33,23 @@ func openTest(name string, t *testing.T, ref *Ref, pkg pkging.Pkger) { psi, err := pf.Stat() r.NoError(err) - psb, err := ioutil.ReadAll(pf) - r.NoError(err) - r.NoError(pf.Close()) - + r.Equal(osi.IsDir(), psi.IsDir()) 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)) + + if osi.IsDir() { + return + } + + osb, err := ioutil.ReadAll(osf) + r.NoError(err) + r.NoError(osf.Close()) + + psb, err := ioutil.ReadAll(pf) + r.NoError(err) + r.NoError(pf.Close()) + r.Equal(osb, psb) } diff --git a/pkging/mem/current_test.go b/pkging/mem/current_test.go new file mode 100644 index 0000000..f1afc1d --- /dev/null +++ b/pkging/mem/current_test.go @@ -0,0 +1,20 @@ +package mem + +import ( + "testing" + + "github.com/markbates/pkger/pkging/costello" + "github.com/stretchr/testify/require" +) + +func Test_Pkger_Current(t *testing.T) { + r := require.New(t) + + ref, err := costello.NewRef() + r.NoError(err) + + pkg, err := New(ref.Info) + r.NoError(err) + + costello.CurrentTest(t, ref, pkg) +} diff --git a/pkging/mem/info_test.go b/pkging/mem/info_test.go new file mode 100644 index 0000000..3752db8 --- /dev/null +++ b/pkging/mem/info_test.go @@ -0,0 +1,20 @@ +package mem + +import ( + "testing" + + "github.com/markbates/pkger/pkging/costello" + "github.com/stretchr/testify/require" +) + +func Test_Pkger_Info(t *testing.T) { + r := require.New(t) + + ref, err := costello.NewRef() + r.NoError(err) + + pkg, err := New(ref.Info) + r.NoError(err) + + costello.InfoTest(t, ref, pkg) +}