pkger/pkging/pkgtest/suite.go

473 lines
8.5 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"
"strings"
"testing"
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-03 18:29:28 +03:00
pkging.Pkger
2019-09-01 05:45:22 +03:00
}
2019-09-03 18:29:28 +03:00
func NewSuite(yourpkging pkging.Pkger) (Suite, error) {
2019-09-02 00:41:03 +03:00
suite := Suite{
2019-09-03 18:29:28 +03:00
Pkger: yourpkging,
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 {
t.Fatalf("something went wrong wrong with %s %T", s, s)
}
for i := 0; i < rt.NumMethod(); i++ {
m := rt.Method(i)
if !strings.HasPrefix(m.Name, "Test_") {
continue
}
s.sub(t, m)
}
}
2019-09-02 00:41:03 +03:00
func (s Suite) sub(t *testing.T, m reflect.Method) {
2019-09-01 05:45:22 +03:00
name := strings.TrimPrefix(m.Name, "Test_")
2019-09-03 18:29:28 +03:00
name = fmt.Sprintf("%T_%s", s.Pkger, name)
2019-09-01 05:45:22 +03:00
t.Run(name, func(st *testing.T) {
defer func() {
if err := recover(); err != nil {
st.Fatal(err)
}
}()
cleaner := func() {
if err := s.Clean(); err != nil {
st.Fatal(err)
}
}
cleaner()
defer cleaner()
m.Func.Call([]reflect.Value{
reflect.ValueOf(s),
reflect.ValueOf(st),
})
})
}
2019-09-02 00:41:03 +03:00
func (s Suite) Clean() error {
2019-09-01 22:42:22 +03:00
pt, err := s.Parse("/")
2019-09-01 05:45:22 +03:00
if err != nil {
return err
}
2019-09-01 22:42:22 +03:00
_ = pt
2019-09-01 22:59:42 +03:00
if err := s.RemoveAll(pt.Name); err != nil {
return err
}
2019-09-01 23:00:18 +03:00
if _, err := s.Stat(pt.Name); err == nil {
return fmt.Errorf("expected %q to be, you know, not there any more", pt)
}
2019-09-01 05:45:22 +03:00
return nil
}
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-02 05:20:02 +03:00
cur, err := s.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},
{in: filepath.Dir(mould)},
{in: ":" + filepath.Dir(mould)},
{in: ip + ":" + filepath.Dir(mould)},
}
2019-09-01 05:45:22 +03:00
2019-09-02 05:20:02 +03:00
for _, tt := range table {
t.Run(tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
r.NoError(s.MkdirAll(filepath.Dir(pt.Name), 0755))
f, err := s.Create(pt.Name)
r.NoError(err)
r.Equal(pt.Name, f.Name())
fi, err := f.Stat()
r.NoError(err)
2019-09-01 05:45:22 +03:00
2019-09-02 05:20:02 +03:00
r.Equal(pt.Name, fi.Name())
r.Equal(os.FileMode(0644), fi.Mode())
r.NotZero(fi.ModTime())
r.NoError(s.RemoveAll(pt.String()))
})
}
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)
cur, err := s.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 {
t.Run(tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
_, err = s.Create(pt.Name)
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-01 06:29:25 +03:00
info, err := s.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)
cur, err := s.Current()
r.NoError(err)
info, err := s.Info(cur.ImportPath)
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)
cur, err := s.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 {
t.Run(tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
dir := filepath.Dir(pt.Name)
r.NoError(s.MkdirAll(dir, 0755))
fi, err := s.Stat(dir)
r.NoError(err)
r.Equal(dir, fi.Name())
r.Equal(os.FileMode(0755), fi.Mode().Perm())
r.NotZero(fi.ModTime())
r.NoError(s.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)
cur, err := s.Current()
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
}{
{in: mould},
{in: ":" + mould},
{in: ip + ":" + mould},
{in: hart},
}
for _, tt := range table {
t.Run(tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
2019-09-02 05:20:02 +03:00
r.NoError(s.MkdirAll(filepath.Dir(pt.Name), 0755))
2019-09-02 05:07:52 +03:00
body := "!" + pt.String()
2019-09-03 18:47:19 +03:00
pkgutil.WriteFile(s, tt.in, []byte(body), 0644)
2019-09-02 05:07:52 +03:00
2019-09-03 18:47:19 +03:00
f, err := s.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-03 18:47:19 +03:00
b, err = pkgutil.ReadFile(s, 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)
cur, err := s.Current()
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
2019-09-02 01:02:45 +03:00
exp pkging.Path
}{
2019-09-02 01:02:45 +03:00
{in: mould, exp: pkging.Path{Pkg: ip, Name: mould}},
{in: filepath.Join(cur.Dir, mould), exp: pkging.Path{Pkg: ip, Name: mould}},
{in: ":" + mould, exp: pkging.Path{Pkg: ip, Name: mould}},
{in: ip + ":" + mould, exp: pkging.Path{Pkg: ip, Name: mould}},
{in: ip, exp: pkging.Path{Pkg: ip, Name: "/"}},
{in: ":", exp: pkging.Path{Pkg: ip, Name: "/"}},
{in: husker + ":" + mould, exp: pkging.Path{Pkg: husker, Name: mould}},
{in: husker, exp: pkging.Path{Pkg: husker, Name: "/"}},
{in: husker + ":", exp: pkging.Path{Pkg: husker, Name: "/"}},
}
for _, tt := range table {
t.Run(tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.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)
cur, err := s.Current()
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 {
t.Run(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-01 23:27:31 +03:00
pt, err := s.Parse(tt.in)
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
2019-09-02 00:35:38 +03:00
_, err = s.Stat(tt.in)
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-02 00:35:38 +03:00
cur, err := s.Current()
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 {
t.Run(tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
r.NoError(s.MkdirAll(pt.Name, 0755))
info, err := s.Stat(tt.in)
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)
cur, err := s.Current()
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
}{
{in: mould},
{in: ":" + mould},
{in: ip + ":" + mould},
{in: hart},
}
for _, tt := range table {
t.Run(tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
2019-09-02 05:20:02 +03:00
r.NoError(s.MkdirAll(filepath.Dir(pt.Name), 0755))
2019-09-01 20:21:34 +03:00
2019-09-01 22:59:42 +03:00
f, err := s.Create(tt.in)
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
info, err := s.Stat(tt.in)
r.NoError(err)
r.Equal(pt.Name, info.Name())
})
}
2019-09-01 05:45:22 +03:00
}
2019-09-02 00:41:03 +03:00
func (s Suite) Test_Walk(t *testing.T) {
2019-09-01 05:45:22 +03:00
panic("not implemented")
}
2019-09-02 19:57:18 +03:00
2019-09-03 18:29:28 +03:00
func (s Suite) Test_Remove(t *testing.T) {
panic("not implemented")
}
func (s Suite) Test_HTTP_Open(t *testing.T) {
panic("not implemented")
}
func (s Suite) Test_HTTP_Readdir(t *testing.T) {
panic("not implemented")
}
2019-09-02 19:57:18 +03:00
func (s Suite) Test_ReadFile(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
}{
{in: mould},
{in: ":" + mould},
{in: ip + ":" + mould},
{in: hart},
}
for _, tt := range table {
t.Run(tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
r.NoError(s.MkdirAll(filepath.Dir(pt.Name), 0755))
f, err := s.Create(tt.in)
r.NoError(err)
body := "!" + pt.String()
_, err = io.Copy(f, strings.NewReader(body))
r.NoError(err)
r.NoError(f.Close())
2019-09-03 18:29:28 +03:00
b, err := pkgutil.ReadFile(s, tt.in)
2019-09-02 19:57:18 +03:00
r.NoError(err)
r.Equal(body, string(b))
})
}
}