diff --git a/pkging/costello/costello.go b/pkging/costello/costello.go index d5486ad..0342a2c 100644 --- a/pkging/costello/costello.go +++ b/pkging/costello/costello.go @@ -4,15 +4,15 @@ import ( "fmt" "os" "testing" - "time" + "github.com/markbates/pkger/here" "github.com/markbates/pkger/pkging" "github.com/stretchr/testify/require" ) type AllFn func(ref *Ref) (pkging.Pkger, error) -func All(t *testing.T, ref *Ref, fn AllFn) { +func All(t *testing.T, fn AllFn) { r := require.New(t) type tf func(*testing.T, *Ref, pkging.Pkger) @@ -29,12 +29,19 @@ func All(t *testing.T, ref *Ref, fn AllFn) { "Walk": WalkTest, } + ref, err := NewRef() + r.NoError(err) + pkg, err := fn(ref) r.NoError(err) for n, tt := range tests { t.Run(fmt.Sprintf("%T/%s", pkg, n), func(st *testing.T) { st.Parallel() + + ref, err := NewRef() + r.NoError(err) + pkg, err := fn(ref) r.NoError(err) @@ -46,10 +53,31 @@ 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.Mode().String(), b.Mode().String()) r.Equal(a.Name(), b.Name()) r.Equal(a.Size(), b.Size()) + r.NotZero(b.ModTime()) +} + +func cmpHereInfo(t *testing.T, a here.Info, b here.Info) { + t.Helper() + + r := require.New(t) + + r.NotZero(a) + r.NotZero(b) + + r.Equal(a.ImportPath, b.ImportPath) + r.Equal(a.Name, b.Name) + + am := a.Module + bm := b.Module + + r.Equal(am.Path, bm.Path) + r.Equal(am.Main, bm.Main) + r.Equal(am.GoVersion, bm.GoVersion) } diff --git a/pkging/costello/create.go b/pkging/costello/create.go index 0c5df6e..9b5bd69 100644 --- a/pkging/costello/create.go +++ b/pkging/costello/create.go @@ -1,9 +1,7 @@ package costello import ( - "fmt" - "os" - "path/filepath" + "io/ioutil" "strings" "testing" @@ -14,32 +12,31 @@ import ( func CreateTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { r := require.New(t) - const name = "create.test" + 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) + _, err := pkg.Stat(name) r.Error(err) data := []byte(strings.ToUpper(name)) - osf, err := os.Create(fp) + f, err := pkg.Create(name) r.NoError(err) - _, err = osf.Write(data) + _, err = f.Write(data) r.NoError(err) - r.NoError(osf.Close()) + r.NoError(f.Close()) - psf, err := pkg.Create(fmt.Sprintf("/%s", name)) + f, err = pkg.Open(name) r.NoError(err) - _, err = psf.Write(data) + info, err := f.Stat() r.NoError(err) - r.NoError(psf.Close()) - openTest(name, t, ref, pkg) + + b, err := ioutil.ReadAll(f) + r.NoError(err) + r.NoError(f.Close()) + + r.Equal(data, b) + r.Equal("create.test", info.Name()) + } diff --git a/pkging/costello/current.go b/pkging/costello/current.go index b3bbfbd..f16be92 100644 --- a/pkging/costello/current.go +++ b/pkging/costello/current.go @@ -4,13 +4,13 @@ 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) - + // panic("ref") cur, err := pkg.Current() - r.NoError(err) - r.Equal(ref.Info, cur) + if err != nil { + t.Fatal(err) + } + cmpHereInfo(t, ref.Info, cur) } diff --git a/pkging/costello/info.go b/pkging/costello/info.go index d361e31..d998f0d 100644 --- a/pkging/costello/info.go +++ b/pkging/costello/info.go @@ -13,5 +13,5 @@ func InfoTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { info, err := pkg.Info("app") r.NoError(err) - r.Equal(ref.Info, info) + cmpHereInfo(t, ref.Info, info) } diff --git a/pkging/costello/mkdir_all.go b/pkging/costello/mkdir_all.go index 2bca6e5..d9c0407 100644 --- a/pkging/costello/mkdir_all.go +++ b/pkging/costello/mkdir_all.go @@ -1,10 +1,6 @@ package costello import ( - "os" - "path" - "path/filepath" - "strings" "testing" "github.com/markbates/pkger/pkging" @@ -14,29 +10,19 @@ import ( func MkdirAllTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { r := require.New(t) - parts := []string{"all", "this", "useless", "beauty"} + name := "/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) + _, err := pkg.Stat(name) 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) + f, err := pkg.Open(name) + r.NoError(err) + + info, err := f.Stat() + r.NoError(err) + + r.Equal("app:"+name, f.Name()) + r.Equal("beauty", info.Name()) } diff --git a/pkging/costello/ref.go b/pkging/costello/ref.go index 0e789d4..95ae879 100644 --- a/pkging/costello/ref.go +++ b/pkging/costello/ref.go @@ -3,6 +3,7 @@ package costello import ( "os" "path/filepath" + "runtime" "github.com/markbates/pkger/here" ) @@ -33,9 +34,13 @@ func NewRef() (*Ref, error) { Info: here.Info{ ImportPath: "app", Dir: dir, + Name: "app", Module: here.Module{ - Path: "app", - Dir: dir, + Main: true, + Path: "app", + Dir: dir, + GoMod: filepath.Join(dir, "go.mod"), + GoVersion: runtime.Version(), }, }, } diff --git a/pkging/costello/stat.go b/pkging/costello/stat.go index dbc2fa2..6e2243f 100644 --- a/pkging/costello/stat.go +++ b/pkging/costello/stat.go @@ -4,7 +4,6 @@ import ( "os" "path/filepath" "testing" - "time" "github.com/markbates/pkger/pkging" "github.com/stretchr/testify/require" @@ -20,8 +19,5 @@ func StatTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { 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)) + cmpFileInfo(t, osi, psi) } diff --git a/pkging/mem/mem_test.go b/pkging/mem/mem_test.go index 90ed9bc..e18817f 100644 --- a/pkging/mem/mem_test.go +++ b/pkging/mem/mem_test.go @@ -9,11 +9,7 @@ import ( ) func Test_Pkger(t *testing.T) { - ref, err := costello.NewRef() - if err != nil { - t.Fatal(err) - } - costello.All(t, ref, func(ref *costello.Ref) (pkging.Pkger, error) { + costello.All(t, func(ref *costello.Ref) (pkging.Pkger, error) { return mem.New(ref.Info) }) } diff --git a/pkging/stdos/create_test.go b/pkging/stdos/create_test.go new file mode 100644 index 0000000..9a49049 --- /dev/null +++ b/pkging/stdos/create_test.go @@ -0,0 +1,20 @@ +package stdos + +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 := NewTemp(ref) + r.NoError(err) + + costello.CreateTest(t, ref, pkg) +} diff --git a/pkging/stdos/current_test.go b/pkging/stdos/current_test.go new file mode 100644 index 0000000..41af520 --- /dev/null +++ b/pkging/stdos/current_test.go @@ -0,0 +1,20 @@ +package stdos + +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 := NewTemp(ref) + r.NoError(err) + + costello.CurrentTest(t, ref, pkg) +} diff --git a/pkging/stdos/info_test.go b/pkging/stdos/info_test.go new file mode 100644 index 0000000..119fee4 --- /dev/null +++ b/pkging/stdos/info_test.go @@ -0,0 +1,20 @@ +package stdos + +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 := NewTemp(ref) + r.NoError(err) + + costello.InfoTest(t, ref, pkg) +} diff --git a/pkging/stdos/mkdirall_test.go b/pkging/stdos/mkdirall_test.go new file mode 100644 index 0000000..315b6a4 --- /dev/null +++ b/pkging/stdos/mkdirall_test.go @@ -0,0 +1,20 @@ +package stdos + +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 := NewTemp(ref) + r.NoError(err) + + costello.MkdirAllTest(t, ref, pkg) +} diff --git a/pkging/stdos/open_test.go b/pkging/stdos/open_test.go new file mode 100644 index 0000000..fc92f35 --- /dev/null +++ b/pkging/stdos/open_test.go @@ -0,0 +1,20 @@ +package stdos + +import ( + "testing" + + "github.com/markbates/pkger/pkging/costello" + "github.com/stretchr/testify/require" +) + +func Test_Pkger_Open(t *testing.T) { + r := require.New(t) + + ref, err := costello.NewRef() + r.NoError(err) + + pkg, err := NewTemp(ref) + r.NoError(err) + + costello.OpenTest(t, ref, pkg) +} diff --git a/pkging/stdos/remove_test.go b/pkging/stdos/remove_test.go new file mode 100644 index 0000000..d0f16d4 --- /dev/null +++ b/pkging/stdos/remove_test.go @@ -0,0 +1,20 @@ +package stdos + +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 := NewTemp(ref) + r.NoError(err) + + costello.RemoveTest(t, ref, pkg) +} diff --git a/pkging/stdos/removeall_test.go b/pkging/stdos/removeall_test.go new file mode 100644 index 0000000..86370ea --- /dev/null +++ b/pkging/stdos/removeall_test.go @@ -0,0 +1,20 @@ +package stdos + +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 := NewTemp(ref) + r.NoError(err) + + costello.RemoveAllTest(t, ref, pkg) +} diff --git a/pkging/stdos/stat_test.go b/pkging/stdos/stat_test.go new file mode 100644 index 0000000..9a97c0a --- /dev/null +++ b/pkging/stdos/stat_test.go @@ -0,0 +1,20 @@ +package stdos + +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 := NewTemp(ref) + r.NoError(err) + + costello.StatTest(t, ref, pkg) +} diff --git a/pkging/stdos/stdos_test.go b/pkging/stdos/stdos_test.go index 582955a..d951890 100644 --- a/pkging/stdos/stdos_test.go +++ b/pkging/stdos/stdos_test.go @@ -1,39 +1,34 @@ -package stdos_test +package stdos import ( "io/ioutil" + "path/filepath" "testing" + "github.com/markbates/pkger/here" "github.com/markbates/pkger/pkging" - "github.com/markbates/pkger/pkging/pkgtest" - "github.com/markbates/pkger/pkging/stdos" + "github.com/markbates/pkger/pkging/costello" ) -func Test_Pkger(t *testing.T) { - t.SkipNow() - suite, err := pkgtest.NewSuite("stdos", func() (pkging.Pkger, error) { - app, err := pkgtest.App() - if err != nil { - return nil, err - } - - dir, err := ioutil.TempDir("", "stdos") - if err != nil { - return nil, err - } - - app.Dir = dir - - mypkging, err := stdos.New(app.Info) - if err != nil { - return nil, err - } - - return mypkging, nil - }) +func NewTemp(ref *costello.Ref) (pkging.Pkger, error) { + dir, err := ioutil.TempDir("", "stdos") if err != nil { - t.Fatal(err) + return nil, err } - suite.Test(t) + info := here.Info{ + Module: ref.Module, + ImportPath: ref.ImportPath, + Name: ref.Name, + Dir: dir, + } + info.Module.Dir = dir + info.Module.GoMod = filepath.Join(dir, "go.mod") + return New(info) +} + +func Test_Pkger(t *testing.T) { + costello.All(t, func(ref *costello.Ref) (pkging.Pkger, error) { + return NewTemp(ref) + }) } diff --git a/pkging/stdos/walk_test.go b/pkging/stdos/walk_test.go new file mode 100644 index 0000000..79a0765 --- /dev/null +++ b/pkging/stdos/walk_test.go @@ -0,0 +1,20 @@ +package stdos + +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 := NewTemp(ref) + r.NoError(err) + + costello.WalkTest(t, ref, pkg) +}