juliet was waiting with a safety net

This commit is contained in:
Mark Bates 2019-10-30 16:39:05 -04:00
parent 0edd9cf5cc
commit da03471d96
7 changed files with 129 additions and 64 deletions

View File

@ -1,52 +1,33 @@
package costello
import (
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/markbates/pkger/pkging"
"github.com/stretchr/testify/require"
)
func LoadRef(ref *Ref, pkg pkging.Pkger) error {
return filepath.Walk(ref.Dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
of, err := os.Open(path)
if err != nil {
return err
}
defer of.Close()
type AllFn func(ref *Ref) (pkging.Pkger, error)
if a, ok := pkg.(pkging.Adder); ok {
return a.Add(of)
}
func All(t *testing.T, fn AllFn) {
r := require.New(t)
type tf func(*testing.T, pkging.Pkger)
path = strings.TrimPrefix(path, ref.Dir)
tests := map[string]tf{
"OpenTest": OpenTest,
"StatTest": StatTest,
}
pt, err := pkg.Parse(path)
if err != nil {
return err
}
ref, err := NewRef()
r.NoError(err)
if err := pkg.MkdirAll(filepath.Dir(pt.Name), 0755); err != nil {
return err
}
for n, tt := range tests {
t.Run(n, func(st *testing.T) {
pkg, err := fn(ref)
r.NoError(err)
if info.IsDir() {
return nil
}
f, err := pkg.Create(pt.String())
if err != nil {
return err
}
defer f.Close()
tt(st, pkg)
})
}
if _, err := io.Copy(f, of); err != nil {
return err
}
return nil
})
}

View File

@ -0,0 +1,52 @@
package costello
import (
"io"
"os"
"path/filepath"
"strings"
"github.com/markbates/pkger/pkging"
)
func LoadRef(ref *Ref, pkg pkging.Pkger) error {
return filepath.Walk(ref.Dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
of, err := os.Open(path)
if err != nil {
return err
}
defer of.Close()
if a, ok := pkg.(pkging.Adder); ok {
return a.Add(of)
}
path = strings.TrimPrefix(path, ref.Dir)
pt, err := pkg.Parse(path)
if err != nil {
return err
}
if err := pkg.MkdirAll(filepath.Dir(pt.Name), 0755); err != nil {
return err
}
if info.IsDir() {
return nil
}
f, err := pkg.Create(pt.String())
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, of); err != nil {
return err
}
return nil
})
}

View File

@ -14,10 +14,10 @@ import (
func OpenTest(t *testing.T, pkg pkging.Pkger) {
r := require.New(t)
app, err := NewRef()
ref, err := NewRef()
r.NoError(err)
osf, err := os.Open(filepath.Join(app.Dir, "go.mod"))
osf, err := os.Open(filepath.Join(ref.Dir, "go.mod"))
r.NoError(err)
osi, err := osf.Stat()
@ -27,6 +27,8 @@ func OpenTest(t *testing.T, pkg pkging.Pkger) {
r.NoError(err)
r.NoError(osf.Close())
r.NoError(LoadRef(ref, pkg))
pf, err := pkg.Open("/go.mod")
r.NoError(err)

View File

@ -1 +1,30 @@
package costello
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/markbates/pkger/pkging"
"github.com/stretchr/testify/require"
)
func StatTest(t *testing.T, pkg pkging.Pkger) {
r := require.New(t)
ref, err := NewRef()
r.NoError(err)
osi, err := os.Stat(filepath.Join(ref.Dir, "go.mod"))
r.NoError(err)
r.NoError(LoadRef(ref, pkg))
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))
}

View File

@ -4,28 +4,12 @@ import (
"testing"
"github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/costello"
"github.com/markbates/pkger/pkging/mem"
"github.com/markbates/pkger/pkging/pkgtest"
)
func Test_Pkger(t *testing.T) {
t.SkipNow()
suite, err := pkgtest.NewSuite("memos", func() (pkging.Pkger, error) {
app, err := pkgtest.App()
if err != nil {
return nil, err
}
pkg, err := mem.New(app.Info)
if err != nil {
return nil, err
}
return pkg, nil
costello.All(t, func(ref *costello.Ref) (pkging.Pkger, error) {
return mem.New(ref.Info)
})
if err != nil {
t.Fatal(err)
}
suite.Test(t)
}

View File

@ -10,13 +10,10 @@ import (
func Test_Pkger_Open(t *testing.T) {
r := require.New(t)
app, err := costello.NewRef()
ref, err := costello.NewRef()
r.NoError(err)
pkg, err := New(app.Info)
r.NoError(err)
err = costello.LoadRef(app, pkg)
pkg, err := New(ref.Info)
r.NoError(err)
costello.OpenTest(t, pkg)

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

@ -0,0 +1,20 @@
package mem
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 := New(ref.Info)
r.NoError(err)
costello.StatTest(t, pkg)
}