2019-09-01 05:45:22 +03:00
|
|
|
package fstest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-09-01 22:59:42 +03:00
|
|
|
"io"
|
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"
|
|
|
|
|
|
|
|
"github.com/markbates/pkger/fs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FileSystem struct {
|
2019-09-01 06:29:25 +03:00
|
|
|
fs.FileSystem
|
2019-09-01 05:45:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileSystem(yourfs fs.FileSystem) (*FileSystem, error) {
|
|
|
|
suite := &FileSystem{
|
2019-09-01 06:29:25 +03:00
|
|
|
FileSystem: yourfs,
|
2019-09-01 05:45:22 +03:00
|
|
|
}
|
|
|
|
return suite, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystem) Test(t *testing.T) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystem) sub(t *testing.T, m reflect.Method) {
|
|
|
|
name := strings.TrimPrefix(m.Name, "Test_")
|
2019-09-01 06:29:25 +03:00
|
|
|
name = fmt.Sprintf("%T_%s", s.FileSystem, 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),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystem) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystem) Test_Create(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
2019-09-01 22:42:22 +03:00
|
|
|
pt, err := s.Parse("/i/want/candy.song")
|
2019-09-01 05:45:22 +03:00
|
|
|
r.NoError(err)
|
2019-09-01 06:29:25 +03:00
|
|
|
|
|
|
|
f, err := s.Create(pt.Name)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(pt.Name, f.Name())
|
2019-09-01 05:45:22 +03:00
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
r.NoError(err)
|
|
|
|
|
2019-09-01 06:29:25 +03:00
|
|
|
r.Equal(pt.Name, fi.Name())
|
2019-09-01 05:45:22 +03:00
|
|
|
r.Equal(os.FileMode(0644), fi.Mode())
|
|
|
|
r.NotZero(fi.ModTime())
|
2019-09-01 22:42:22 +03:00
|
|
|
r.NoError(s.RemoveAll(pt.String()))
|
2019-09-01 05:45:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystem) 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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystem) 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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystem) Test_MkdirAll(t *testing.T) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystem) Test_Open(t *testing.T) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystem) 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
|
|
|
|
exp fs.Path
|
|
|
|
}{
|
|
|
|
{in: "/foo.go", exp: fs.Path{Pkg: ip, Name: "/foo.go"}},
|
2019-09-01 22:42:22 +03:00
|
|
|
{in: filepath.Join(cur.Dir, "foo.go"), exp: fs.Path{Pkg: ip, Name: "/foo.go"}},
|
2019-09-01 07:03:01 +03:00
|
|
|
{in: ":/foo.go", exp: fs.Path{Pkg: ip, Name: "/foo.go"}},
|
|
|
|
{in: ip + ":/foo.go", exp: fs.Path{Pkg: ip, Name: "/foo.go"}},
|
|
|
|
{in: ip, exp: fs.Path{Pkg: ip, Name: "/"}},
|
2019-09-01 20:21:34 +03:00
|
|
|
{in: ":", exp: fs.Path{Pkg: ip, Name: "/"}},
|
|
|
|
{in: "github.com/old/97s:/foo.go", exp: fs.Path{Pkg: "github.com/old/97s", Name: "/foo.go"}},
|
|
|
|
{in: "github.com/old/97s", exp: fs.Path{Pkg: "github.com/old/97s", Name: "/"}},
|
|
|
|
{in: "github.com/old/97s:", exp: fs.Path{Pkg: "github.com/old/97s", 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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystem) Test_ReadFile(t *testing.T) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystem) Test_Stat(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
|
|
|
|
table := []struct {
|
|
|
|
in string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{in: "/foo.go", err: false},
|
|
|
|
{in: ":/foo.go", err: false},
|
|
|
|
{in: ip + ":/foo.go", err: false},
|
|
|
|
{in: ip, err: false},
|
|
|
|
{in: "/no.go", err: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01 20:21:34 +03:00
|
|
|
if tt.err {
|
|
|
|
_, err := s.Stat(tt.in)
|
|
|
|
r.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-01 23:27:31 +03:00
|
|
|
isDir := filepath.Ext(pt.Name) == ""
|
|
|
|
|
|
|
|
if isDir {
|
|
|
|
r.NoError(s.MkdirAll(pt.Name, 0755))
|
|
|
|
info, err := s.Stat(tt.in)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(pt.Name, info.Name())
|
|
|
|
return
|
|
|
|
}
|
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)
|
|
|
|
_, 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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystem) Test_Walk(t *testing.T) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|