dancing in the dark

This commit is contained in:
Mark Bates 2019-09-03 11:29:28 -04:00
parent c3ae1d1bda
commit b6440c5fec
26 changed files with 105 additions and 89 deletions

View File

@ -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

View File

@ -1,4 +1,4 @@
package memware
package mem
// func Test_Create(t *testing.T) {
// r := require.New(t)

View File

@ -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) {

View File

@ -1,4 +1,4 @@
package memware
package mem
// func Test_File_Read_Memory(t *testing.T) {
// r := require.New(t)

1
pkging/mem/http.go Normal file
View File

@ -0,0 +1 @@
package mem

View File

@ -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")

View File

@ -1,4 +1,4 @@
package memware
package mem
import (
"encoding/json"

View File

@ -1,4 +1,4 @@
package memware
package mem
// func Test_File_JSON(t *testing.T) {
// r := require.New(t)

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -1,4 +1,4 @@
package memware
package mem
import (
"os"

View File

@ -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

View File

@ -1,4 +1,4 @@
package memware
package mem
// func Test_Open(t *testing.T) {
// r := require.New(t)

View File

@ -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

View File

@ -1,4 +1,4 @@
package memware
package mem
// func Test_Stat(t *testing.T) {
// r := require.New(t)

View File

@ -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)

View File

@ -1,4 +1,4 @@
package memware
package mem
// func Test_Walk(t *testing.T) {
// r := require.New(t)

View File

@ -1 +0,0 @@
package memware

View File

@ -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)

View File

@ -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))
})

17
pkging/pkgutil/file.go Normal file
View File

@ -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)
}

View File

@ -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 {

View File

@ -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

View File

@ -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)

View File

@ -1 +0,0 @@
package waretest