pkger/pkging/pkgtest/suite.go

572 lines
9.8 KiB
Go
Raw Normal View History

2019-09-03 18:29:28 +03:00
package pkgtest
2019-09-01 05:45:22 +03:00
import (
"fmt"
2019-09-01 22:59:42 +03:00
"io"
2019-09-02 05:07:52 +03:00
"io/ioutil"
2019-09-01 05:45:22 +03:00
"os"
2019-09-01 22:42:22 +03:00
"path/filepath"
2019-09-01 05:45:22 +03:00
"reflect"
2019-09-20 22:40:55 +03:00
"runtime"
2019-09-01 05:45:22 +03:00
"strings"
"testing"
2019-10-09 20:21:54 +03:00
"github.com/markbates/pkger/here"
2019-09-02 01:02:45 +03:00
"github.com/markbates/pkger/pkging"
2019-09-03 18:29:28 +03:00
"github.com/markbates/pkger/pkging/pkgutil"
2019-09-01 05:45:22 +03:00
"github.com/stretchr/testify/require"
)
2019-09-02 00:35:38 +03:00
const mould = "/easy/listening/sugar.file"
const hart = "/easy/listening/grant.hart"
const husker = "github.com/husker/du"
2019-09-02 00:41:03 +03:00
type Suite struct {
2019-09-12 04:29:39 +03:00
Name string
gen func() (pkging.Pkger, error)
2019-09-01 05:45:22 +03:00
}
2019-09-12 04:29:39 +03:00
func (s Suite) Make() (pkging.Pkger, error) {
if s.gen == nil {
return nil, fmt.Errorf("missing generator function")
}
return s.gen()
}
func NewSuite(name string, fn func() (pkging.Pkger, error)) (Suite, error) {
2019-09-02 00:41:03 +03:00
suite := Suite{
2019-09-12 04:29:39 +03:00
Name: name,
gen: fn,
2019-09-01 05:45:22 +03:00
}
return suite, nil
}
2019-09-02 00:41:03 +03:00
func (s Suite) Test(t *testing.T) {
2019-09-01 05:45:22 +03:00
rv := reflect.ValueOf(s)
rt := rv.Type()
if rt.NumMethod() == 0 {
2019-09-12 04:29:39 +03:00
t.Fatalf("something went wrong wrong with %s", s.Name)
2019-09-01 05:45:22 +03:00
}
for i := 0; i < rt.NumMethod(); i++ {
m := rt.Method(i)
if !strings.HasPrefix(m.Name, "Test_") {
continue
}
2019-09-12 04:29:39 +03:00
2019-09-01 05:45:22 +03:00
s.sub(t, m)
}
}
2019-09-03 23:42:47 +03:00
func (s Suite) Run(t *testing.T, name string, fn func(t *testing.T)) {
2019-09-01 05:45:22 +03:00
t.Run(name, func(st *testing.T) {
2019-09-03 23:42:47 +03:00
fn(st)
})
}
func (s Suite) sub(t *testing.T, m reflect.Method) {
2019-09-12 04:29:39 +03:00
name := fmt.Sprintf("%s/%s", s.Name, m.Name)
2019-09-03 23:42:47 +03:00
s.Run(t, name, func(st *testing.T) {
2019-09-01 05:45:22 +03:00
m.Func.Call([]reflect.Value{
reflect.ValueOf(s),
reflect.ValueOf(st),
})
})
}
2019-09-02 00:41:03 +03:00
func (s Suite) Test_Create(t *testing.T) {
2019-09-01 05:45:22 +03:00
r := require.New(t)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
2019-09-01 05:45:22 +03:00
r.NoError(err)
2019-09-01 06:29:25 +03:00
2019-09-02 05:20:02 +03:00
ip := cur.ImportPath
table := []struct {
in string
}{
{in: mould},
{in: ":" + mould},
{in: ip + ":" + mould},
}
2019-09-01 05:45:22 +03:00
2019-09-02 05:20:02 +03:00
for _, tt := range table {
2019-09-03 23:42:47 +03:00
s.Run(t, tt.in, func(st *testing.T) {
2019-09-02 05:20:02 +03:00
r := require.New(st)
2019-09-12 04:29:39 +03:00
pt, err := pkg.Parse(tt.in)
2019-09-02 05:20:02 +03:00
r.NoError(err)
2019-09-12 04:29:39 +03:00
r.NoError(pkg.MkdirAll(filepath.Dir(pt.Name), 0755))
2019-09-02 05:20:02 +03:00
2019-09-12 04:29:39 +03:00
f, err := pkg.Create(pt.Name)
2019-09-02 05:20:02 +03:00
r.NoError(err)
r.Equal(pt.Name, f.Name())
fi, err := f.Stat()
r.NoError(err)
2019-09-20 22:40:55 +03:00
r.NoError(f.Close())
2019-09-01 05:45:22 +03:00
2019-09-02 05:20:02 +03:00
r.Equal(pt.Name, fi.Name())
r.NotZero(fi.ModTime())
2019-09-12 04:29:39 +03:00
r.NoError(pkg.RemoveAll(pt.String()))
2019-09-02 05:20:02 +03:00
})
}
2019-09-01 05:45:22 +03:00
}
2019-09-02 05:25:53 +03:00
func (s Suite) Test_Create_No_MkdirAll(t *testing.T) {
r := require.New(t)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
2019-09-02 05:25:53 +03:00
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
}{
{in: mould},
{in: ":" + mould},
{in: ip + ":" + mould},
{in: filepath.Dir(mould)},
{in: ":" + filepath.Dir(mould)},
{in: ip + ":" + filepath.Dir(mould)},
}
for _, tt := range table {
2019-09-03 23:42:47 +03:00
s.Run(t, tt.in, func(st *testing.T) {
2019-09-02 05:25:53 +03:00
r := require.New(st)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
2019-09-02 05:25:53 +03:00
r.NoError(err)
2019-09-12 04:29:39 +03:00
pt, err := pkg.Parse(tt.in)
r.NoError(err)
2019-09-02 05:25:53 +03:00
2019-09-12 04:29:39 +03:00
_, err = pkg.Create(pt.Name)
2019-09-02 05:25:53 +03:00
r.Error(err)
})
}
}
2019-09-01 05:45:22 +03:00
2019-09-02 00:41:03 +03:00
func (s Suite) Test_Current(t *testing.T) {
2019-09-01 05:47:23 +03:00
r := require.New(t)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
info, err := pkg.Current()
2019-09-01 05:47:23 +03:00
r.NoError(err)
r.NotZero(info)
2019-09-01 05:45:22 +03:00
}
2019-09-02 00:41:03 +03:00
func (s Suite) Test_Info(t *testing.T) {
2019-09-01 06:29:25 +03:00
r := require.New(t)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
2019-09-01 06:29:25 +03:00
r.NoError(err)
2019-09-12 04:29:39 +03:00
info, err := pkg.Info(cur.ImportPath)
2019-09-01 06:29:25 +03:00
r.NoError(err)
r.NotZero(info)
2019-09-01 05:45:22 +03:00
}
2019-09-02 00:41:03 +03:00
func (s Suite) Test_MkdirAll(t *testing.T) {
r := require.New(t)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
}{
{in: mould},
{in: ":" + mould},
{in: ip + ":" + mould},
{in: filepath.Dir(mould)},
{in: ":" + filepath.Dir(mould)},
{in: ip + ":" + filepath.Dir(mould)},
}
for _, tt := range table {
2019-09-03 23:42:47 +03:00
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
2019-09-12 04:29:39 +03:00
pt, err := pkg.Parse(tt.in)
r.NoError(err)
dir := filepath.Dir(pt.Name)
2019-09-12 04:29:39 +03:00
r.NoError(pkg.MkdirAll(dir, 0755))
2019-09-12 04:29:39 +03:00
fi, err := pkg.Stat(dir)
r.NoError(err)
2019-09-20 22:40:55 +03:00
if runtime.GOOS == "windows" {
dir = strings.Replace(dir, "\\", "/", -1)
}
r.Equal(dir, fi.Name())
r.NotZero(fi.ModTime())
2019-09-12 04:29:39 +03:00
r.NoError(pkg.RemoveAll(pt.String()))
})
}
2019-09-01 05:45:22 +03:00
}
2019-09-02 05:07:52 +03:00
func (s Suite) Test_Open_File(t *testing.T) {
r := require.New(t)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
2019-09-02 05:07:52 +03:00
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
}{
{in: mould},
{in: ":" + mould},
{in: ip + ":" + mould},
{in: hart},
}
for _, tt := range table {
2019-09-03 23:42:47 +03:00
s.Run(t, tt.in, func(st *testing.T) {
2019-09-02 05:07:52 +03:00
r := require.New(st)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
2019-09-02 05:07:52 +03:00
r.NoError(err)
2019-09-12 04:29:39 +03:00
pt, err := pkg.Parse(tt.in)
r.NoError(err)
r.NoError(pkg.RemoveAll(pt.String()))
r.NoError(pkg.MkdirAll(filepath.Dir(pt.Name), 0755))
2019-09-02 05:07:52 +03:00
body := "!" + pt.String()
2019-09-12 04:29:39 +03:00
pkgutil.WriteFile(pkg, tt.in, []byte(body), 0644)
2019-09-02 05:07:52 +03:00
2019-09-12 04:29:39 +03:00
f, err := pkg.Open(tt.in)
2019-09-02 05:07:52 +03:00
r.NoError(err)
2019-09-03 18:47:19 +03:00
r.Equal(pt.Name, f.Path().Name)
b, err := ioutil.ReadAll(f)
2019-09-02 05:07:52 +03:00
r.NoError(err)
2019-09-03 18:47:19 +03:00
r.Equal(body, string(b))
2019-09-02 05:07:52 +03:00
2019-09-12 04:29:39 +03:00
b, err = pkgutil.ReadFile(pkg, tt.in)
2019-09-02 05:07:52 +03:00
r.NoError(err)
r.Equal(body, string(b))
2019-09-03 18:47:19 +03:00
r.NoError(f.Close())
2019-09-02 05:07:52 +03:00
})
}
2019-09-01 05:45:22 +03:00
}
2019-09-02 00:41:03 +03:00
func (s Suite) Test_Parse(t *testing.T) {
r := require.New(t)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
2019-10-09 20:21:54 +03:00
exp here.Path
}{
2019-10-09 20:21:54 +03:00
{in: mould, exp: here.Path{Pkg: ip, Name: mould}},
{in: filepath.Join(cur.Dir, mould), exp: here.Path{Pkg: ip, Name: mould}},
{in: ":" + mould, exp: here.Path{Pkg: ip, Name: mould}},
{in: ip + ":" + mould, exp: here.Path{Pkg: ip, Name: mould}},
{in: ip, exp: here.Path{Pkg: ip, Name: "/"}},
{in: ":", exp: here.Path{Pkg: ip, Name: "/"}},
{in: husker + ":" + mould, exp: here.Path{Pkg: husker, Name: mould}},
{in: husker, exp: here.Path{Pkg: husker, Name: "/"}},
{in: husker + ":", exp: here.Path{Pkg: husker, Name: "/"}},
}
for _, tt := range table {
2019-09-03 23:45:32 +03:00
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
2019-09-12 04:29:39 +03:00
pt, err := pkg.Parse(tt.in)
r.NoError(err)
r.Equal(tt.exp, pt)
})
}
2019-09-01 05:45:22 +03:00
}
2019-09-02 00:41:03 +03:00
func (s Suite) Test_Stat_Error(t *testing.T) {
2019-09-01 20:21:34 +03:00
r := require.New(t)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
2019-09-01 20:21:34 +03:00
r.NoError(err)
ip := cur.ImportPath
2019-09-02 00:35:38 +03:00
2019-09-01 20:21:34 +03:00
table := []struct {
2019-09-02 00:35:38 +03:00
in string
2019-09-01 20:21:34 +03:00
}{
2019-09-02 00:35:38 +03:00
{in: hart},
{in: ":" + hart},
{in: ip},
{in: ip + ":"},
{in: ip + ":" + hart},
2019-09-01 20:21:34 +03:00
}
for _, tt := range table {
2019-09-03 23:45:32 +03:00
s.Run(t, tt.in, func(st *testing.T) {
2019-09-01 23:27:31 +03:00
2019-09-01 20:21:34 +03:00
r := require.New(st)
2019-09-12 04:29:39 +03:00
pt, err := pkg.Parse(tt.in)
2019-09-01 23:27:31 +03:00
r.NoError(err)
2019-09-12 04:29:39 +03:00
r.NoError(pkg.RemoveAll(pt.String()))
2019-09-01 23:27:31 +03:00
2019-09-12 04:29:39 +03:00
_, err = pkg.Stat(tt.in)
2019-09-02 00:35:38 +03:00
r.Error(err)
})
}
}
2019-09-01 20:21:34 +03:00
2019-09-02 00:41:03 +03:00
func (s Suite) Test_Stat_Dir(t *testing.T) {
2019-09-02 00:35:38 +03:00
r := require.New(t)
2019-09-01 23:27:31 +03:00
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
2019-09-02 00:35:38 +03:00
r.NoError(err)
dir := filepath.Dir(mould)
ip := cur.ImportPath
table := []struct {
in string
}{
{in: ip},
{in: dir},
{in: ":" + dir},
{in: ip + ":" + dir},
}
for _, tt := range table {
2019-09-03 23:45:32 +03:00
s.Run(t, tt.in, func(st *testing.T) {
2019-09-02 00:35:38 +03:00
r := require.New(st)
2019-09-12 04:29:39 +03:00
pt, err := pkg.Parse(tt.in)
2019-09-02 00:35:38 +03:00
r.NoError(err)
2019-09-12 04:29:39 +03:00
r.NoError(pkg.RemoveAll(pt.String()))
2019-09-02 00:35:38 +03:00
2019-09-12 04:29:39 +03:00
r.NoError(pkg.MkdirAll(pt.Name, 0755))
info, err := pkg.Stat(tt.in)
2019-09-02 00:35:38 +03:00
r.NoError(err)
r.Equal(pt.Name, info.Name())
})
}
}
2019-09-02 00:41:03 +03:00
func (s Suite) Test_Stat_File(t *testing.T) {
2019-09-02 00:35:38 +03:00
r := require.New(t)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
2019-09-02 00:35:38 +03:00
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
}{
{in: mould},
{in: ":" + mould},
{in: ip + ":" + mould},
{in: hart},
}
for _, tt := range table {
2019-09-03 23:45:32 +03:00
s.Run(t, tt.in, func(st *testing.T) {
2019-09-02 00:35:38 +03:00
r := require.New(st)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
2019-09-02 00:35:38 +03:00
r.NoError(err)
2019-09-12 04:29:39 +03:00
pt, err := pkg.Parse(tt.in)
r.NoError(err)
r.NoError(pkg.RemoveAll(pt.String()))
r.NoError(pkg.MkdirAll(filepath.Dir(pt.Name), 0755))
2019-09-01 20:21:34 +03:00
2019-09-12 04:29:39 +03:00
f, err := pkg.Create(tt.in)
2019-09-01 22:59:42 +03:00
r.NoError(err)
2019-09-02 05:20:02 +03:00
2019-09-01 22:59:42 +03:00
_, err = io.Copy(f, strings.NewReader("!"+pt.String()))
r.NoError(err)
r.NoError(f.Close())
2019-09-01 20:21:34 +03:00
2019-09-12 04:29:39 +03:00
info, err := pkg.Stat(tt.in)
2019-09-01 20:21:34 +03:00
r.NoError(err)
r.Equal(pt.Name, info.Name())
})
}
2019-09-01 05:45:22 +03:00
}
2019-09-20 17:56:26 +03:00
func (s Suite) Test_Walk(t *testing.T) {
r := require.New(t)
2019-09-12 04:29:39 +03:00
2019-09-20 17:56:26 +03:00
pkg, err := s.Make()
r.NoError(err)
2019-10-18 19:01:48 +03:00
2019-09-20 17:56:26 +03:00
r.NoError(s.LoadFolder(pkg))
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
}{
{in: ip},
{in: "/"},
{in: ":/"},
{in: ip + ":/"},
}
for _, tt := range table {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
var act []string
err := pkg.Walk(tt.in, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
act = append(act, path)
return nil
})
r.NoError(err)
exp := []string{
2019-10-18 19:01:48 +03:00
"app:/",
"app:/main.go",
"app:/public",
"app:/public/images",
2019-10-18 19:24:51 +03:00
"app:/public/images/img1.png",
2019-10-18 19:01:48 +03:00
"app:/public/index.html",
"app:/templates",
"app:/templates/a.txt",
"app:/templates/b",
"app:/templates/b/b.txt",
2019-09-20 17:56:26 +03:00
}
r.Equal(exp, act)
})
}
}
func (s Suite) Test_Remove(t *testing.T) {
r := require.New(t)
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
}{
2019-10-18 19:24:51 +03:00
{in: "/public/images/img1.png"},
{in: ":/public/images/img1.png"},
{in: ip + ":/public/images/img1.png"},
2019-09-20 17:56:26 +03:00
}
for _, tt := range table {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
pkg, err := s.Make()
r.NoError(err)
r.NoError(s.LoadFolder(pkg))
_, err = pkg.Stat(tt.in)
r.NoError(err)
r.NoError(pkg.Remove(tt.in))
_, err = pkg.Stat(tt.in)
r.Error(err)
r.Error(pkg.Remove("unknown"))
})
}
}
func (s Suite) Test_RemoveAll(t *testing.T) {
r := require.New(t)
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
}{
{in: "/public"},
{in: ":/public"},
{in: ip + ":/public"},
}
for _, tt := range table {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
pkg, err := s.Make()
r.NoError(err)
r.NoError(s.LoadFolder(pkg))
_, err = pkg.Stat(tt.in)
r.NoError(err)
r.NoError(pkg.RemoveAll(tt.in))
_, err = pkg.Stat(tt.in)
r.Error(err)
})
}
}