diff --git a/pkging/costello/costello.go b/pkging/costello/costello.go index 9acb6aa..d5486ad 100644 --- a/pkging/costello/costello.go +++ b/pkging/costello/costello.go @@ -2,7 +2,9 @@ package costello import ( "fmt" + "os" "testing" + "time" "github.com/markbates/pkger/pkging" "github.com/stretchr/testify/require" @@ -16,12 +18,15 @@ 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, - "CurrentTest": CurrentTest, - "InfoTest": InfoTest, - "MkdirAll": MkdirAllTest, + "Open": OpenTest, + "Stat": StatTest, + "Create": CreateTest, + "Current": CurrentTest, + "Info": InfoTest, + "MkdirAll": MkdirAllTest, + "Remove": RemoveTest, + "RemoveAll": RemoveAllTest, + "Walk": WalkTest, } pkg, err := fn(ref) @@ -38,3 +43,13 @@ func All(t *testing.T, ref *Ref, fn AllFn) { } } + +func cmpFileInfo(t *testing.T, a os.FileInfo, b os.FileInfo) { + t.Helper() + r := require.New(t) + r.Equal(a.IsDir(), b.IsDir()) + r.Equal(a.ModTime().Format(time.RFC3339), b.ModTime().Format(time.RFC3339)) + r.Equal(a.Mode(), b.Mode()) + r.Equal(a.Name(), b.Name()) + r.Equal(a.Size(), b.Size()) +} diff --git a/pkging/costello/open.go b/pkging/costello/open.go index 9319181..6c33b62 100644 --- a/pkging/costello/open.go +++ b/pkging/costello/open.go @@ -6,7 +6,6 @@ import ( "os" "path/filepath" "testing" - "time" "github.com/markbates/pkger/pkging" "github.com/stretchr/testify/require" @@ -33,11 +32,7 @@ func openTest(name string, t *testing.T, ref *Ref, pkg pkging.Pkger) { psi, err := pf.Stat() r.NoError(err) - 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)) + cmpFileInfo(t, osi, psi) if osi.IsDir() { return diff --git a/pkging/costello/remove.go b/pkging/costello/remove.go index 15b8b0f..45cd0fc 100644 --- a/pkging/costello/remove.go +++ b/pkging/costello/remove.go @@ -1 +1,24 @@ package costello + +import ( + "testing" + + "github.com/markbates/pkger/pkging" + "github.com/stretchr/testify/require" +) + +func RemoveTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { + r := require.New(t) + + r.NoError(LoadRef(ref, pkg)) + + name := "/go.mod" + + _, err := pkg.Stat(name) + r.NoError(err) + + r.NoError(pkg.Remove(name)) + + _, err = pkg.Stat(name) + r.Error(err) +} diff --git a/pkging/costello/remove_all.go b/pkging/costello/remove_all.go index 15b8b0f..9df1705 100644 --- a/pkging/costello/remove_all.go +++ b/pkging/costello/remove_all.go @@ -1 +1,24 @@ package costello + +import ( + "testing" + + "github.com/markbates/pkger/pkging" + "github.com/stretchr/testify/require" +) + +func RemoveAllTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { + r := require.New(t) + + r.NoError(LoadRef(ref, pkg)) + + name := "/public/assets" + + _, err := pkg.Stat(name) + r.NoError(err) + + r.NoError(pkg.RemoveAll(name)) + + _, err = pkg.Stat(name) + r.Error(err) +} diff --git a/pkging/costello/walk.go b/pkging/costello/walk.go index 15b8b0f..8ca9fb2 100644 --- a/pkging/costello/walk.go +++ b/pkging/costello/walk.go @@ -1 +1,48 @@ package costello + +import ( + "os" + "path/filepath" + "testing" + + "github.com/markbates/pkger/pkging" + "github.com/stretchr/testify/require" +) + +func WalkTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { + r := require.New(t) + + r.NoError(LoadRef(ref, pkg)) + + name := "public" + + fp := filepath.Join(ref.Dir, name) + + var exp []os.FileInfo + err := filepath.Walk(fp, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + exp = append(exp, info) + return nil + }) + r.NoError(err) + + var act []os.FileInfo + err = pkg.Walk("/"+name, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + act = append(act, info) + return nil + }) + + r.NoError(err) + + r.Len(act, len(exp)) + + for i, info := range exp { + cmpFileInfo(t, info, act[i]) + } +} diff --git a/pkging/mem/mkdirall_test.go b/pkging/mem/mkdirall_test.go new file mode 100644 index 0000000..f98634e --- /dev/null +++ b/pkging/mem/mkdirall_test.go @@ -0,0 +1,20 @@ +package mem + +import ( + "testing" + + "github.com/markbates/pkger/pkging/costello" + "github.com/stretchr/testify/require" +) + +func Test_Pkger_MkdirAll(t *testing.T) { + r := require.New(t) + + ref, err := costello.NewRef() + r.NoError(err) + + pkg, err := New(ref.Info) + r.NoError(err) + + costello.MkdirAllTest(t, ref, pkg) +} diff --git a/pkging/mem/remove_test.go b/pkging/mem/remove_test.go new file mode 100644 index 0000000..c62a1bc --- /dev/null +++ b/pkging/mem/remove_test.go @@ -0,0 +1,20 @@ +package mem + +import ( + "testing" + + "github.com/markbates/pkger/pkging/costello" + "github.com/stretchr/testify/require" +) + +func Test_Pkger_Remove(t *testing.T) { + r := require.New(t) + + ref, err := costello.NewRef() + r.NoError(err) + + pkg, err := New(ref.Info) + r.NoError(err) + + costello.RemoveTest(t, ref, pkg) +} diff --git a/pkging/mem/removeall_test.go b/pkging/mem/removeall_test.go new file mode 100644 index 0000000..da06e5e --- /dev/null +++ b/pkging/mem/removeall_test.go @@ -0,0 +1,20 @@ +package mem + +import ( + "testing" + + "github.com/markbates/pkger/pkging/costello" + "github.com/stretchr/testify/require" +) + +func Test_Pkger_RemoveAll(t *testing.T) { + r := require.New(t) + + ref, err := costello.NewRef() + r.NoError(err) + + pkg, err := New(ref.Info) + r.NoError(err) + + costello.RemoveAllTest(t, ref, pkg) +} diff --git a/pkging/mem/walk_test.go b/pkging/mem/walk_test.go new file mode 100644 index 0000000..c4e76c7 --- /dev/null +++ b/pkging/mem/walk_test.go @@ -0,0 +1,20 @@ +package mem + +import ( + "testing" + + "github.com/markbates/pkger/pkging/costello" + "github.com/stretchr/testify/require" +) + +func Test_Pkger_Walk(t *testing.T) { + r := require.New(t) + + ref, err := costello.NewRef() + r.NoError(err) + + pkg, err := New(ref.Info) + r.NoError(err) + + costello.WalkTest(t, ref, pkg) +}