From f79eafd2579ed3623ba1a943b59425af2b0c592c Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Wed, 30 Oct 2019 16:49:55 -0400 Subject: [PATCH] his suntan lotion and his castanets --- pkging/costello/costello.go | 5 +++-- pkging/costello/create.go | 41 +++++++++++++++++++++++++++++++++++-- pkging/costello/open.go | 9 ++++++-- pkging/mem/create_test.go | 20 ++++++++++++++++++ 4 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 pkging/mem/create_test.go diff --git a/pkging/costello/costello.go b/pkging/costello/costello.go index ebad4ed..ebe1b2e 100644 --- a/pkging/costello/costello.go +++ b/pkging/costello/costello.go @@ -14,8 +14,9 @@ func All(t *testing.T, fn AllFn) { type tf func(*testing.T, pkging.Pkger) tests := map[string]tf{ - "OpenTest": OpenTest, - "StatTest": StatTest, + "OpenTest": OpenTest, + "StatTest": StatTest, + "CreateTest": CreateTest, } ref, err := NewRef() diff --git a/pkging/costello/create.go b/pkging/costello/create.go index d994f38..0a95463 100644 --- a/pkging/costello/create.go +++ b/pkging/costello/create.go @@ -1,11 +1,48 @@ package costello import ( + "fmt" + "os" + "path/filepath" + "strings" "testing" "github.com/markbates/pkger/pkging" + "github.com/stretchr/testify/require" ) -func Create_Test(t *testing.T, pkg pkging.Pkger) error { - return nil +func CreateTest(t *testing.T, pkg pkging.Pkger) { + r := require.New(t) + + ref, err := NewRef() + r.NoError(err) + + const name = "create.test" + + fp := filepath.Join(ref.Dir, name) + os.RemoveAll(fp) + defer os.RemoveAll(fp) + + _, err = os.Stat(fp) + r.Error(err) + + _, err = pkg.Stat(name) + r.Error(err) + + data := []byte(strings.ToUpper(name)) + + osf, err := os.Create(fp) + r.NoError(err) + + _, err = osf.Write(data) + r.NoError(err) + r.NoError(osf.Close()) + + psf, err := pkg.Create(fmt.Sprintf("/%s", name)) + r.NoError(err) + + _, err = psf.Write(data) + r.NoError(err) + r.NoError(psf.Close()) + openTest(name, t, pkg) } diff --git a/pkging/costello/open.go b/pkging/costello/open.go index ea12c82..bf632c4 100644 --- a/pkging/costello/open.go +++ b/pkging/costello/open.go @@ -1,6 +1,7 @@ package costello import ( + "fmt" "io/ioutil" "os" "path/filepath" @@ -12,12 +13,16 @@ import ( ) func OpenTest(t *testing.T, pkg pkging.Pkger) { + openTest("go.mod", t, pkg) +} + +func openTest(name string, t *testing.T, pkg pkging.Pkger) { r := require.New(t) ref, err := NewRef() r.NoError(err) - osf, err := os.Open(filepath.Join(ref.Dir, "go.mod")) + osf, err := os.Open(filepath.Join(ref.Dir, name)) r.NoError(err) osi, err := osf.Stat() @@ -29,7 +34,7 @@ func OpenTest(t *testing.T, pkg pkging.Pkger) { r.NoError(LoadRef(ref, pkg)) - pf, err := pkg.Open("/go.mod") + pf, err := pkg.Open(fmt.Sprintf("/%s", name)) r.NoError(err) psi, err := pf.Stat() diff --git a/pkging/mem/create_test.go b/pkging/mem/create_test.go new file mode 100644 index 0000000..7205a08 --- /dev/null +++ b/pkging/mem/create_test.go @@ -0,0 +1,20 @@ +package mem + +import ( + "testing" + + "github.com/markbates/pkger/pkging/costello" + "github.com/stretchr/testify/require" +) + +func Test_Pkger_Create(t *testing.T) { + r := require.New(t) + + ref, err := costello.NewRef() + r.NoError(err) + + pkg, err := New(ref.Info) + r.NoError(err) + + costello.CreateTest(t, pkg) +}