2019-09-02 01:02:45 +03:00
|
|
|
package waretest
|
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-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-02 01:02:45 +03:00
|
|
|
pkging.Warehouse
|
2019-09-01 05:45:22 +03:00
|
|
|
}
|
|
|
|
|
2019-09-02 01:02:45 +03:00
|
|
|
func NewSuite(yourpkging pkging.Warehouse) (Suite, error) {
|
2019-09-02 00:41:03 +03:00
|
|
|
suite := Suite{
|
2019-09-02 01:02:45 +03:00
|
|
|
Warehouse: 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-02 00:47:10 +03:00
|
|
|
name = fmt.Sprintf("%T_%s", s.Warehouse, 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 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) {
|
2019-09-01 05:45:22 +03:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
f1, err := s.Create(tt.in)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
_, err = io.Copy(f1, strings.NewReader(body))
|
|
|
|
r.NoError(err)
|
|
|
|
r.NoError(f1.Close())
|
|
|
|
|
|
|
|
f2, err := s.Open(tt.in)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
r.Equal(pt.Name, f2.Path().Name)
|
|
|
|
b, err := ioutil.ReadAll(f2)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(body, string(b))
|
|
|
|
|
|
|
|
r.NoError(f2.Close())
|
|
|
|
})
|
|
|
|
}
|
2019-09-01 05:45:22 +03:00
|
|
|
}
|
|
|
|
|
2019-09-02 00:41:03 +03:00
|
|
|
func (s Suite) Test_Parse(t *testing.T) {
|
2019-09-01 07:03:01 +03:00
|
|
|
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-01 07:03:01 +03:00
|
|
|
}{
|
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: "/"}},
|
2019-09-01 07:03:01 +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.Equal(tt.exp, pt)
|
|
|
|
})
|
|
|
|
}
|
2019-09-01 05:45:22 +03:00
|
|
|
}
|
|
|
|
|
2019-09-02 00:41:03 +03:00
|
|
|
func (s Suite) Test_ReadFile(t *testing.T) {
|
2019-09-01 05:45:22 +03:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|