my hands are tied

This commit is contained in:
Mark Bates 2019-08-31 23:29:25 -04:00
parent ffdb70686a
commit 915694eb9d
8 changed files with 139 additions and 136 deletions

View File

@ -9,4 +9,4 @@ type TestFile struct {
Path fs.Path Path fs.Path
} }
type TestFiles map[string]TestFile type TestFiles map[fs.Path]TestFile

View File

@ -1,7 +1,6 @@
package fstest package fstest
import ( import (
"fmt"
"path" "path"
"strings" "strings"
@ -9,29 +8,29 @@ import (
) )
func Files(fx fs.FileSystem) (TestFiles, error) { func Files(fx fs.FileSystem) (TestFiles, error) {
info, err := fx.Current()
if err != nil {
return nil, err
}
tf := TestFiles{} tf := TestFiles{}
for _, f := range fileList { for _, f := range fileList {
name := Path(fx, f) pt, err := Path(fx, f)
tf[name] = TestFile{ if err != nil {
Name: name, return tf, err
Path: fs.Path{ }
Pkg: info.ImportPath, tf[pt] = TestFile{
Name: name, Name: pt.Name,
}, Path: pt,
} }
} }
return tf, nil return tf, nil
} }
func Path(fx fs.FileSystem, ps ...string) string { func Path(fx fs.FileSystem, ps ...string) (fs.Path, error) {
name := path.Join("/.fstest", fmt.Sprintf("%T", fx)) name := path.Join(ps...)
name = path.Join(name, strings.Join(ps, "/")) name = path.Join(".fstest", name)
return name if !strings.HasPrefix(name, "/") {
name = "/" + name
}
return fx.Parse(name)
} }
var fileList = []string{ var fileList = []string{

View File

@ -12,12 +12,12 @@ import (
) )
type FileSystem struct { type FileSystem struct {
FS fs.FileSystem fs.FileSystem
} }
func NewFileSystem(yourfs fs.FileSystem) (*FileSystem, error) { func NewFileSystem(yourfs fs.FileSystem) (*FileSystem, error) {
suite := &FileSystem{ suite := &FileSystem{
FS: yourfs, FileSystem: yourfs,
} }
return suite, nil return suite, nil
} }
@ -39,7 +39,7 @@ func (s *FileSystem) Test(t *testing.T) {
func (s *FileSystem) sub(t *testing.T, m reflect.Method) { func (s *FileSystem) sub(t *testing.T, m reflect.Method) {
name := strings.TrimPrefix(m.Name, "Test_") name := strings.TrimPrefix(m.Name, "Test_")
name = fmt.Sprintf("%T_%s", s.FS, name) name = fmt.Sprintf("%T_%s", s.FileSystem, name)
t.Run(name, func(st *testing.T) { t.Run(name, func(st *testing.T) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
@ -63,30 +63,35 @@ func (s *FileSystem) sub(t *testing.T, m reflect.Method) {
} }
func (s *FileSystem) Clean() error { func (s *FileSystem) Clean() error {
name := Path(s.FS) pt, err := Path(s)
err := s.FS.RemoveAll(name)
if err != nil { if err != nil {
return err return err
} }
if _, err := s.FS.Stat(name); err == nil { if err := s.RemoveAll(pt.Name); err != nil {
return fmt.Errorf("expected %q to be, you know, not there any more", name) return err
}
if _, err := s.Stat(pt.Name); err == nil {
return fmt.Errorf("expected %q to be, you know, not there any more", pt)
} }
return nil return nil
} }
func (s *FileSystem) Test_Create(t *testing.T) { func (s *FileSystem) Test_Create(t *testing.T) {
r := require.New(t) r := require.New(t)
name := Path(s.FS, "i", "want", "candy.song")
f, err := s.FS.Create(name) pt, err := Path(s, "i", "want", "candy.song")
r.NoError(err) r.NoError(err)
r.Equal(name, f.Name())
f, err := s.Create(pt.Name)
r.NoError(err)
r.Equal(pt.Name, f.Name())
fi, err := f.Stat() fi, err := f.Stat()
r.NoError(err) r.NoError(err)
r.Equal(name, fi.Name()) r.Equal(pt.Name, fi.Name())
r.Equal(os.FileMode(0644), fi.Mode()) r.Equal(os.FileMode(0644), fi.Mode())
r.NotZero(fi.ModTime()) r.NotZero(fi.ModTime())
} }
@ -94,13 +99,21 @@ func (s *FileSystem) Test_Create(t *testing.T) {
func (s *FileSystem) Test_Current(t *testing.T) { func (s *FileSystem) Test_Current(t *testing.T) {
r := require.New(t) r := require.New(t)
info, err := s.FS.Current() info, err := s.Current()
r.NoError(err) r.NoError(err)
r.NotZero(info) r.NotZero(info)
} }
func (s *FileSystem) Test_Info(t *testing.T) { func (s *FileSystem) Test_Info(t *testing.T) {
panic("not implemented") r := require.New(t)
cur, err := s.Current()
r.NoError(err)
info, err := s.Info(cur.ImportPath)
r.NoError(err)
r.NotZero(info)
} }
func (s *FileSystem) Test_MkdirAll(t *testing.T) { func (s *FileSystem) Test_MkdirAll(t *testing.T) {

View File

@ -21,14 +21,17 @@ type FS struct {
} }
func New() (*FS, error) { func New() (*FS, error) {
f := &FS{ info, err := here.Current()
infos: &maps.Infos{}, if err != nil {
paths: &maps.Paths{}, return nil, err
} }
return &FS{
var err error infos: &maps.Infos{},
f.current, err = here.Current() paths: &maps.Paths{
return f, err Current: info,
},
current: info,
}, nil
} }
func (fx *FS) Create(name string) (fs.File, error) { func (fx *FS) Create(name string) (fs.File, error) {

View File

@ -13,10 +13,18 @@ import (
var _ fs.FileSystem = &FS{} var _ fs.FileSystem = &FS{}
func WithInfo(fx *FS, infos ...here.Info) {
for _, info := range infos {
fx.infos.Store(info.ImportPath, info)
}
}
func New(info here.Info) (*FS, error) { func New(info here.Info) (*FS, error) {
f := &FS{ f := &FS{
infos: &maps.Infos{}, infos: &maps.Infos{},
paths: &maps.Paths{}, paths: &maps.Paths{
Current: info,
},
files: &maps.Files{}, files: &maps.Files{},
current: info, current: info,
} }
@ -36,15 +44,10 @@ func (f *FS) Current() (here.Info, error) {
func (f *FS) Info(p string) (here.Info, error) { func (f *FS) Info(p string) (here.Info, error) {
info, ok := f.infos.Load(p) info, ok := f.infos.Load(p)
if ok { if !ok {
return info, nil return info, fmt.Errorf("no such package %q", p)
} }
info, err := here.Package(p)
if err != nil {
return info, err
}
f.infos.Store(p, info)
return info, nil return info, nil
} }

View File

@ -13,10 +13,13 @@ func Test_FS(t *testing.T) {
info, err := here.Current() info, err := here.Current()
r.NoError(err) r.NoError(err)
r.NotZero(info)
myfs, err := New(info) myfs, err := New(info)
r.NoError(err) r.NoError(err)
WithInfo(myfs, info)
suite, err := fstest.NewFileSystem(myfs) suite, err := fstest.NewFileSystem(myfs)
r.NoError(err) r.NoError(err)

View File

@ -1,25 +1,18 @@
package memfs package memfs
import ( // func Test_Stat(t *testing.T) {
"testing" // r := require.New(t)
//
"github.com/markbates/pkger/here" // fs, err := New(here.Info{})
"github.com/stretchr/testify/require" // r.NoError(err)
) // _, err = fs.Stat("/i.dont.exist")
// r.Error(err)
func Test_Stat(t *testing.T) { //
r := require.New(t) // f, err := fs.Create("/i.exist")
// r.NoError(err)
fs, err := New(here.Info{}) // r.NoError(f.Close())
r.NoError(err) //
_, err = fs.Stat("/i.dont.exist") // fi, err := fs.Stat("/i.exist")
r.Error(err) // r.NoError(err)
// r.Equal("/i.exist", fi.Name())
f, err := fs.Create("/i.exist") // }
r.NoError(err)
r.NoError(f.Close())
fi, err := fs.Stat("/i.exist")
r.NoError(err)
r.Equal("/i.exist", fi.Name())
}

View File

@ -1,69 +1,58 @@
package memfs package memfs
import ( // func Test_Walk(t *testing.T) {
"io" // r := require.New(t)
"os" //
"sort" // files := []struct {
"strings" // name string
"testing" // body string
// }{
"github.com/markbates/pkger/here" // {name: "/a/a.txt", body: "A"},
"github.com/stretchr/testify/require" // {name: "/a/a.md", body: "Amd"},
) // {name: "/b/c/d.txt", body: "B"},
// {name: "/f.txt", body: "F"},
func Test_Walk(t *testing.T) { // }
r := require.New(t) //
// sort.Slice(files, func(a, b int) bool {
files := []struct { // return files[a].name < files[b].name
name string // })
body string //
}{ // fs, err := New(here.Info{})
{name: "/a/a.txt", body: "A"}, // r.NoError(err)
{name: "/a/a.md", body: "Amd"}, //
{name: "/b/c/d.txt", body: "B"}, // for _, file := range files {
{name: "/f.txt", body: "F"}, // f, err := fs.Create(file.name)
} // r.NoError(err)
// _, err = io.Copy(f, strings.NewReader(file.body))
sort.Slice(files, func(a, b int) bool { // r.NoError(err)
return files[a].name < files[b].name // r.NoError(f.Close())
}) // }
//
fs, err := New(here.Info{}) // var found []string
r.NoError(err) // err = fs.Walk("/", func(path string, info os.FileInfo, err error) error {
// if err != nil {
for _, file := range files { // return err
f, err := fs.Create(file.name) // }
r.NoError(err) //
_, err = io.Copy(f, strings.NewReader(file.body)) // found = append(found, path)
r.NoError(err) // return nil
r.NoError(f.Close()) // })
} // r.NoError(err)
//
var found []string // expected := []string{":/", ":/a", ":/a/a.md", ":/a/a.txt", ":/b", ":/b/c", ":/b/c/d.txt", ":/f.txt"}
err = fs.Walk("/", func(path string, info os.FileInfo, err error) error { // r.Equal(expected, found)
if err != nil { //
return err // found = []string{}
} // err = fs.Walk("/a/", func(path string, info os.FileInfo, err error) error {
// if err != nil {
found = append(found, path) // return err
return nil // }
}) //
r.NoError(err) // found = append(found, path)
// return nil
expected := []string{":/", ":/a", ":/a/a.md", ":/a/a.txt", ":/b", ":/b/c", ":/b/c/d.txt", ":/f.txt"} // })
r.Equal(expected, found) // r.NoError(err)
//
found = []string{} // expected = []string{":/a/a.md", ":/a/a.txt"}
err = fs.Walk("/a/", func(path string, info os.FileInfo, err error) error { // r.Equal(expected, found)
if err != nil { // }
return err
}
found = append(found, path)
return nil
})
r.NoError(err)
expected = []string{":/a/a.md", ":/a/a.txt"}
r.Equal(expected, found)
}