it's not your punch then it's your pout

This commit is contained in:
Mark Bates 2019-10-31 11:06:20 -04:00
parent 6638215c4d
commit 5c07a3dede
18 changed files with 275 additions and 92 deletions

View File

@ -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)
}

View File

@ -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())
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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())
}

View File

@ -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(),
},
},
}

View File

@ -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)
}

View File

@ -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)
})
}

View File

@ -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)
}

View File

@ -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)
}

20
pkging/stdos/info_test.go Normal file
View File

@ -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)
}

View File

@ -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)
}

20
pkging/stdos/open_test.go Normal file
View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

20
pkging/stdos/stat_test.go Normal file
View File

@ -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)
}

View File

@ -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)
})
}

20
pkging/stdos/walk_test.go Normal file
View File

@ -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)
}