diff --git a/pkging/memware/create.go b/pkging/mem/create.go similarity index 87% rename from pkging/memware/create.go rename to pkging/mem/create.go index b9a34ee..d8ae1ca 100644 --- a/pkging/memware/create.go +++ b/pkging/mem/create.go @@ -1,4 +1,4 @@ -package memware +package mem import ( "path/filepath" @@ -8,7 +8,7 @@ import ( ) // no such file or directory -func (fx *Warehouse) Create(name string) (pkging.File, error) { +func (fx *Pkger) Create(name string) (pkging.File, error) { pt, err := fx.Parse(name) if err != nil { return nil, err diff --git a/pkging/memware/create_test.go b/pkging/mem/create_test.go similarity index 99% rename from pkging/memware/create_test.go rename to pkging/mem/create_test.go index bcab262..ed92d10 100644 --- a/pkging/memware/create_test.go +++ b/pkging/mem/create_test.go @@ -1,4 +1,4 @@ -package memware +package mem // func Test_Create(t *testing.T) { // r := require.New(t) diff --git a/pkging/memware/file.go b/pkging/mem/file.go similarity index 98% rename from pkging/memware/file.go rename to pkging/mem/file.go index b5b89f5..6f32073 100644 --- a/pkging/memware/file.go +++ b/pkging/mem/file.go @@ -1,4 +1,4 @@ -package memware +package mem import ( "bytes" @@ -27,7 +27,7 @@ type File struct { parent pkging.Path writer *bytes.Buffer reader io.Reader - pkging pkging.Warehouse + pkging pkging.Pkger } func (f *File) Seek(ofpkginget int64, whence int) (int64, error) { diff --git a/pkging/memware/file_test.go b/pkging/mem/file_test.go similarity index 98% rename from pkging/memware/file_test.go rename to pkging/mem/file_test.go index 87c636f..2903001 100644 --- a/pkging/memware/file_test.go +++ b/pkging/mem/file_test.go @@ -1,4 +1,4 @@ -package memware +package mem // func Test_File_Read_Memory(t *testing.T) { // r := require.New(t) diff --git a/pkging/mem/http.go b/pkging/mem/http.go new file mode 100644 index 0000000..6a9fb31 --- /dev/null +++ b/pkging/mem/http.go @@ -0,0 +1 @@ +package mem diff --git a/pkging/memware/http_test.go b/pkging/mem/http_test.go similarity index 95% rename from pkging/memware/http_test.go rename to pkging/mem/http_test.go index a075de8..6719aa1 100644 --- a/pkging/memware/http_test.go +++ b/pkging/mem/http_test.go @@ -1,9 +1,9 @@ -package memware +package mem // func Test_HTTP_Dir(t *testing.T) { // r := require.New(t) // -// fs := NewWarehouse() +// fs := NewPkger() // // r.NoError(Folder.Create(fs)) // @@ -24,7 +24,7 @@ package memware // func Test_HTTP_File_Memory(t *testing.T) { // r := require.New(t) // -// fs := NewWarehouse() +// fs := NewPkger() // r.NoError(Folder.Create(fs)) // // dir, err := fs.Open("/") @@ -44,7 +44,7 @@ package memware // func Test_HTTP_Dir_Memory_StripPrefix(t *testing.T) { // r := require.New(t) // -// fs := NewWarehouse() +// fs := NewPkger() // r.NoError(Folder.Create(fs)) // // dir, err := fs.Open("/public") diff --git a/pkging/memware/json.go b/pkging/mem/json.go similarity index 98% rename from pkging/memware/json.go rename to pkging/mem/json.go index 717d830..55a626b 100644 --- a/pkging/memware/json.go +++ b/pkging/mem/json.go @@ -1,4 +1,4 @@ -package memware +package mem import ( "encoding/json" diff --git a/pkging/memware/json_test.go b/pkging/mem/json_test.go similarity index 97% rename from pkging/memware/json_test.go rename to pkging/mem/json_test.go index 585f109..f258cf4 100644 --- a/pkging/memware/json_test.go +++ b/pkging/mem/json_test.go @@ -1,4 +1,4 @@ -package memware +package mem // func Test_File_JSON(t *testing.T) { // r := require.New(t) diff --git a/pkging/memware/warehouse.go b/pkging/mem/mem.go similarity index 66% rename from pkging/memware/warehouse.go rename to pkging/mem/mem.go index 1a66846..d0bf18c 100644 --- a/pkging/memware/warehouse.go +++ b/pkging/mem/mem.go @@ -1,8 +1,7 @@ -package memware +package mem import ( "fmt" - "io/ioutil" "os" "strings" @@ -11,16 +10,16 @@ import ( "github.com/markbates/pkger/pkging" ) -var _ pkging.Warehouse = &Warehouse{} +var _ pkging.Pkger = &Pkger{} -func WithInfo(fx *Warehouse, infos ...here.Info) { +func WithInfo(fx *Pkger, infos ...here.Info) { for _, info := range infos { fx.infos.Store(info.ImportPath, info) } } -func New(info here.Info) (*Warehouse, error) { - f := &Warehouse{ +func New(info here.Info) (*Pkger, error) { + f := &Pkger{ infos: &maps.Infos{}, paths: &maps.Paths{ Current: info, @@ -31,14 +30,14 @@ func New(info here.Info) (*Warehouse, error) { return f, nil } -type Warehouse struct { +type Pkger struct { infos *maps.Infos paths *maps.Paths files *maps.Files current here.Info } -func (f *Warehouse) Abs(p string) (string, error) { +func (f *Pkger) Abs(p string) (string, error) { pt, err := f.Parse(p) if err != nil { return "", err @@ -46,15 +45,15 @@ func (f *Warehouse) Abs(p string) (string, error) { return f.AbsPath(pt) } -func (f *Warehouse) AbsPath(pt pkging.Path) (string, error) { +func (f *Pkger) AbsPath(pt pkging.Path) (string, error) { return pt.String(), nil } -func (f *Warehouse) Current() (here.Info, error) { +func (f *Pkger) Current() (here.Info, error) { return f.current, nil } -func (f *Warehouse) Info(p string) (here.Info, error) { +func (f *Pkger) Info(p string) (here.Info, error) { info, ok := f.infos.Load(p) if !ok { return info, fmt.Errorf("no such package %q", p) @@ -63,20 +62,11 @@ func (f *Warehouse) Info(p string) (here.Info, error) { return info, nil } -func (f *Warehouse) Parse(p string) (pkging.Path, error) { +func (f *Pkger) Parse(p string) (pkging.Path, error) { return f.paths.Parse(p) } -func (fx *Warehouse) ReadFile(s string) ([]byte, error) { - f, err := fx.Open(s) - if err != nil { - return nil, err - } - defer f.Close() - return ioutil.ReadAll(f) -} - -func (fx *Warehouse) Remove(name string) error { +func (fx *Pkger) Remove(name string) error { pt, err := fx.Parse(name) if err != nil { return err @@ -90,7 +80,7 @@ func (fx *Warehouse) Remove(name string) error { return nil } -func (fx *Warehouse) RemoveAll(name string) error { +func (fx *Pkger) RemoveAll(name string) error { pt, err := fx.Parse(name) if err != nil { return err diff --git a/pkging/memware/warehouse_test.go b/pkging/mem/mem_test.go similarity index 67% rename from pkging/memware/warehouse_test.go rename to pkging/mem/mem_test.go index 9eb1032..925b26b 100644 --- a/pkging/memware/warehouse_test.go +++ b/pkging/mem/mem_test.go @@ -1,14 +1,14 @@ -package memware +package mem import ( "testing" "github.com/markbates/pkger/here" - "github.com/markbates/pkger/pkging/waretest" + "github.com/markbates/pkger/pkging/pkgtest" "github.com/stretchr/testify/require" ) -func Test_Warehouse(t *testing.T) { +func Test_Pkger(t *testing.T) { r := require.New(t) info, err := here.Current() @@ -20,7 +20,7 @@ func Test_Warehouse(t *testing.T) { WithInfo(wh, info) - suite, err := waretest.NewSuite(wh) + suite, err := pkgtest.NewSuite(wh) r.NoError(err) suite.Test(t) diff --git a/pkging/memware/mkdirall.go b/pkging/mem/mkdirall.go similarity index 91% rename from pkging/memware/mkdirall.go rename to pkging/mem/mkdirall.go index a3efc18..eb17709 100644 --- a/pkging/memware/mkdirall.go +++ b/pkging/mem/mkdirall.go @@ -1,4 +1,4 @@ -package memware +package mem import ( "os" @@ -8,7 +8,7 @@ import ( "github.com/markbates/pkger/pkging" ) -func (fx *Warehouse) MkdirAll(p string, perm os.FileMode) error { +func (fx *Pkger) MkdirAll(p string, perm os.FileMode) error { path, err := fx.Parse(p) if err != nil { return err diff --git a/pkging/memware/mkdirall_test.go b/pkging/mem/mkdirall_test.go similarity index 96% rename from pkging/memware/mkdirall_test.go rename to pkging/mem/mkdirall_test.go index fa3f4df..d8f4299 100644 --- a/pkging/memware/mkdirall_test.go +++ b/pkging/mem/mkdirall_test.go @@ -1,4 +1,4 @@ -package memware +package mem import ( "os" diff --git a/pkging/memware/open.go b/pkging/mem/open.go similarity index 85% rename from pkging/memware/open.go rename to pkging/mem/open.go index 62ff730..cd56aac 100644 --- a/pkging/memware/open.go +++ b/pkging/mem/open.go @@ -1,4 +1,4 @@ -package memware +package mem import ( "fmt" @@ -6,7 +6,7 @@ import ( "github.com/markbates/pkger/pkging" ) -func (fx *Warehouse) Open(name string) (pkging.File, error) { +func (fx *Pkger) Open(name string) (pkging.File, error) { pt, err := fx.Parse(name) if err != nil { return nil, err diff --git a/pkging/memware/open_test.go b/pkging/mem/open_test.go similarity index 96% rename from pkging/memware/open_test.go rename to pkging/mem/open_test.go index 288fd73..dfd8651 100644 --- a/pkging/memware/open_test.go +++ b/pkging/mem/open_test.go @@ -1,4 +1,4 @@ -package memware +package mem // func Test_Open(t *testing.T) { // r := require.New(t) diff --git a/pkging/memware/stat.go b/pkging/mem/stat.go similarity index 71% rename from pkging/memware/stat.go rename to pkging/mem/stat.go index 2711a3e..1906418 100644 --- a/pkging/memware/stat.go +++ b/pkging/mem/stat.go @@ -1,11 +1,11 @@ -package memware +package mem import ( "fmt" "os" ) -func (fx *Warehouse) Stat(name string) (os.FileInfo, error) { +func (fx *Pkger) Stat(name string) (os.FileInfo, error) { pt, err := fx.Parse(name) if err != nil { return nil, err diff --git a/pkging/memware/stat_test.go b/pkging/mem/stat_test.go similarity index 95% rename from pkging/memware/stat_test.go rename to pkging/mem/stat_test.go index b4dd5c2..4a2817c 100644 --- a/pkging/memware/stat_test.go +++ b/pkging/mem/stat_test.go @@ -1,4 +1,4 @@ -package memware +package mem // func Test_Stat(t *testing.T) { // r := require.New(t) diff --git a/pkging/memware/walk.go b/pkging/mem/walk.go similarity index 87% rename from pkging/memware/walk.go rename to pkging/mem/walk.go index 577fb48..96f7421 100644 --- a/pkging/memware/walk.go +++ b/pkging/mem/walk.go @@ -1,4 +1,4 @@ -package memware +package mem import ( "fmt" @@ -8,7 +8,7 @@ import ( "github.com/markbates/pkger/pkging" ) -func (f *Warehouse) Walk(p string, wf filepath.WalkFunc) error { +func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error { keys := f.files.Keys() pt, err := f.Parse(p) diff --git a/pkging/memware/walk_test.go b/pkging/mem/walk_test.go similarity index 98% rename from pkging/memware/walk_test.go rename to pkging/mem/walk_test.go index 0d45c10..e36d03f 100644 --- a/pkging/memware/walk_test.go +++ b/pkging/mem/walk_test.go @@ -1,4 +1,4 @@ -package memware +package mem // func Test_Walk(t *testing.T) { // r := require.New(t) diff --git a/pkging/memware/http.go b/pkging/memware/http.go deleted file mode 100644 index 2ed2560..0000000 --- a/pkging/memware/http.go +++ /dev/null @@ -1 +0,0 @@ -package memware diff --git a/pkging/warehouse.go b/pkging/pkger.go similarity index 87% rename from pkging/warehouse.go rename to pkging/pkger.go index 63a63e9..19757b5 100644 --- a/pkging/warehouse.go +++ b/pkging/pkger.go @@ -7,7 +7,7 @@ import ( "github.com/markbates/pkger/here" ) -type Warehouse interface { +type Pkger interface { Parse(p string) (Path, error) Abs(string) (string, error) AbsPath(Path) (string, error) @@ -15,9 +15,6 @@ type Warehouse interface { Current() (here.Info, error) Info(p string) (here.Info, error) - // ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported. - ReadFile(s string) ([]byte, error) - // Create creates the named file with mode 0666 (before umask) - It's actually 0644, truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR. Create(name string) (File, error) diff --git a/pkging/waretest/suite.go b/pkging/pkgtest/suite.go similarity index 94% rename from pkging/waretest/suite.go rename to pkging/pkgtest/suite.go index 2ee141c..af4fe78 100644 --- a/pkging/waretest/suite.go +++ b/pkging/pkgtest/suite.go @@ -1,4 +1,4 @@ -package waretest +package pkgtest import ( "fmt" @@ -11,6 +11,7 @@ import ( "testing" "github.com/markbates/pkger/pkging" + "github.com/markbates/pkger/pkging/pkgutil" "github.com/stretchr/testify/require" ) @@ -19,12 +20,12 @@ const hart = "/easy/listening/grant.hart" const husker = "github.com/husker/du" type Suite struct { - pkging.Warehouse + pkging.Pkger } -func NewSuite(yourpkging pkging.Warehouse) (Suite, error) { +func NewSuite(yourpkging pkging.Pkger) (Suite, error) { suite := Suite{ - Warehouse: yourpkging, + Pkger: yourpkging, } return suite, nil } @@ -46,7 +47,7 @@ func (s Suite) Test(t *testing.T) { func (s Suite) sub(t *testing.T, m reflect.Method) { name := strings.TrimPrefix(m.Name, "Test_") - name = fmt.Sprintf("%T_%s", s.Warehouse, name) + name = fmt.Sprintf("%T_%s", s.Pkger, name) t.Run(name, func(st *testing.T) { defer func() { if err := recover(); err != nil { @@ -417,6 +418,18 @@ func (s Suite) Test_Walk(t *testing.T) { panic("not implemented") } +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") +} + func (s Suite) Test_ReadFile(t *testing.T) { r := require.New(t) @@ -452,7 +465,7 @@ func (s Suite) Test_ReadFile(t *testing.T) { r.NoError(err) r.NoError(f.Close()) - b, err := s.ReadFile(tt.in) + b, err := pkgutil.ReadFile(s, tt.in) r.NoError(err) r.Equal(body, string(b)) }) diff --git a/pkging/pkgutil/file.go b/pkging/pkgutil/file.go new file mode 100644 index 0000000..b7505a7 --- /dev/null +++ b/pkging/pkgutil/file.go @@ -0,0 +1,17 @@ +package pkgutil + +import ( + "io/ioutil" + + "github.com/markbates/pkger/pkging" +) + +// ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported. +func ReadFile(pkg pkging.Pkger, s string) ([]byte, error) { + f, err := pkg.Open(s) + if err != nil { + return nil, err + } + defer f.Close() + return ioutil.ReadAll(f) +} diff --git a/pkging/osware/file.go b/pkging/stdos/file.go similarity index 92% rename from pkging/osware/file.go rename to pkging/stdos/file.go index cbc335e..fd64c9d 100644 --- a/pkging/osware/file.go +++ b/pkging/stdos/file.go @@ -1,4 +1,4 @@ -package hdware +package stdos import ( "net/http" @@ -15,10 +15,10 @@ type File struct { info *pkging.FileInfo her here.Info path pkging.Path - pkging pkging.Warehouse + pkging pkging.Pkger } -func NewFile(fx pkging.Warehouse, osf *os.File) (*File, error) { +func NewFile(fx pkging.Pkger, osf *os.File) (*File, error) { pt, err := fx.Parse(osf.Name()) if err != nil { diff --git a/pkging/osware/warehouse.go b/pkging/stdos/stdos.go similarity index 72% rename from pkging/osware/warehouse.go rename to pkging/stdos/stdos.go index 9ad054e..44ffa0e 100644 --- a/pkging/osware/warehouse.go +++ b/pkging/stdos/stdos.go @@ -1,4 +1,4 @@ -package hdware +package stdos import ( "fmt" @@ -12,15 +12,15 @@ import ( "github.com/markbates/pkger/pkging" ) -var _ pkging.Warehouse = &Warehouse{} +var _ pkging.Pkger = &Pkger{} -type Warehouse struct { +type Pkger struct { infos *maps.Infos paths *maps.Paths current here.Info } -func (f *Warehouse) Abs(p string) (string, error) { +func (f *Pkger) Abs(p string) (string, error) { pt, err := f.Parse(p) if err != nil { return "", err @@ -28,7 +28,7 @@ func (f *Warehouse) Abs(p string) (string, error) { return f.AbsPath(pt) } -func (f *Warehouse) AbsPath(pt pkging.Path) (string, error) { +func (f *Pkger) AbsPath(pt pkging.Path) (string, error) { if pt.Pkg == f.current.ImportPath { return filepath.Join(f.current.Dir, pt.Name), nil } @@ -39,12 +39,12 @@ func (f *Warehouse) AbsPath(pt pkging.Path) (string, error) { return filepath.Join(info.Dir, pt.Name), nil } -func New() (*Warehouse, error) { +func New() (*Pkger, error) { info, err := here.Current() if err != nil { return nil, err } - return &Warehouse{ + return &Pkger{ infos: &maps.Infos{}, paths: &maps.Paths{ Current: info, @@ -53,7 +53,7 @@ func New() (*Warehouse, error) { }, nil } -func (fx *Warehouse) Create(name string) (pkging.File, error) { +func (fx *Pkger) Create(name string) (pkging.File, error) { name, err := fx.Abs(name) if err != nil { return nil, err @@ -65,11 +65,11 @@ func (fx *Warehouse) Create(name string) (pkging.File, error) { return NewFile(fx, f) } -func (f *Warehouse) Current() (here.Info, error) { +func (f *Pkger) Current() (here.Info, error) { return f.current, nil } -func (f *Warehouse) Info(p string) (here.Info, error) { +func (f *Pkger) Info(p string) (here.Info, error) { info, ok := f.infos.Load(p) if ok { return info, nil @@ -83,7 +83,7 @@ func (f *Warehouse) Info(p string) (here.Info, error) { return info, nil } -func (f *Warehouse) MkdirAll(p string, perm os.FileMode) error { +func (f *Pkger) MkdirAll(p string, perm os.FileMode) error { p, err := f.Abs(p) if err != nil { return err @@ -91,7 +91,7 @@ func (f *Warehouse) MkdirAll(p string, perm os.FileMode) error { return os.MkdirAll(p, perm) } -func (fx *Warehouse) Open(name string) (pkging.File, error) { +func (fx *Pkger) Open(name string) (pkging.File, error) { name, err := fx.Abs(name) if err != nil { return nil, err @@ -103,11 +103,11 @@ func (fx *Warehouse) Open(name string) (pkging.File, error) { return NewFile(fx, f) } -func (f *Warehouse) Parse(p string) (pkging.Path, error) { +func (f *Pkger) Parse(p string) (pkging.Path, error) { return f.paths.Parse(p) } -func (f *Warehouse) ReadFile(s string) ([]byte, error) { +func (f *Pkger) ReadFile(s string) ([]byte, error) { s, err := f.Abs(s) if err != nil { return nil, err @@ -115,7 +115,7 @@ func (f *Warehouse) ReadFile(s string) ([]byte, error) { return ioutil.ReadFile(s) } -func (f *Warehouse) Stat(name string) (os.FileInfo, error) { +func (f *Pkger) Stat(name string) (os.FileInfo, error) { pt, err := f.Parse(name) if err != nil { return nil, err @@ -136,7 +136,7 @@ func (f *Warehouse) Stat(name string) (os.FileInfo, error) { return info, nil } -func (f *Warehouse) Walk(p string, wf filepath.WalkFunc) error { +func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error { fp, err := f.Abs(p) if err != nil { return err @@ -162,7 +162,7 @@ func (f *Warehouse) Walk(p string, wf filepath.WalkFunc) error { return err } -func (fx *Warehouse) Remove(name string) error { +func (fx *Pkger) Remove(name string) error { name, err := fx.Abs(name) if err != nil { return err @@ -170,7 +170,7 @@ func (fx *Warehouse) Remove(name string) error { return os.Remove(name) } -func (fx *Warehouse) RemoveAll(name string) error { +func (fx *Pkger) RemoveAll(name string) error { name, err := fx.Abs(name) if err != nil { return err diff --git a/pkging/osware/warehouse_test.go b/pkging/stdos/stdos_test.go similarity index 51% rename from pkging/osware/warehouse_test.go rename to pkging/stdos/stdos_test.go index 44324db..e61edc0 100644 --- a/pkging/osware/warehouse_test.go +++ b/pkging/stdos/stdos_test.go @@ -1,23 +1,23 @@ -package hdware +package stdos import ( "path/filepath" "testing" - "github.com/markbates/pkger/pkging/waretest" + "github.com/markbates/pkger/pkging/pkgtest" "github.com/stretchr/testify/require" ) -func Test_Warehouse(t *testing.T) { +func Test_Pkger(t *testing.T) { r := require.New(t) mypkging, err := New() r.NoError(err) - mypkging.current.Dir = filepath.Join(mypkging.current.Dir, ".waretest") + mypkging.current.Dir = filepath.Join(mypkging.current.Dir, ".pkgtest") mypkging.paths.Current = mypkging.current - suite, err := waretest.NewSuite(mypkging) + suite, err := pkgtest.NewSuite(mypkging) r.NoError(err) suite.Test(t) diff --git a/pkging/waretest/waretest.go b/pkging/waretest/waretest.go deleted file mode 100644 index 92b37c8..0000000 --- a/pkging/waretest/waretest.go +++ /dev/null @@ -1 +0,0 @@ -package waretest