hard candy

This commit is contained in:
Mark Bates 2019-09-11 21:29:39 -04:00
parent e45df3e5ac
commit bb509c2487
18 changed files with 375 additions and 254 deletions

View File

@ -13,7 +13,7 @@ build: tidy
make tidy
test: tidy
$(GO_BIN) test -cover -tags ${TAGS} -timeout 5s ./...
$(GO_BIN) test -cover -tags ${TAGS} -timeout 5s -race ./...
make tidy
cov:

View File

@ -1,6 +1,6 @@
module github.com/markbates/pkger/examples/app
go 1.12
go 1.13
require github.com/markbates/pkger v0.0.0

View File

@ -1,16 +1,6 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc=
github.com/markbates/pkger v0.0.0-20190830024022-c5e3a7de4d41 h1:bFEHpLBby7Qdvq92qw0TudeOfUKIZLcrweE/MMlNueI=
github.com/markbates/pkger v0.0.0-20190830024022-c5e3a7de4d41/go.mod h1:M9VeozwduQUCr6z54kJrK9JegpbOv4wiePSbgSbFOew=
github.com/markbates/pkger/examples/app v0.0.0-20190830175641-68f3a2a4571d/go.mod h1:cWIt8Tz1UjmFa2KRaVgTfWPfHP+gprnzxDxNpo4YDxo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@ -18,7 +8,5 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -8,34 +8,36 @@ import (
// Dir attempts to gather info for the requested directory.
func Dir(p string) (Info, error) {
var i Info
return Cache(p, func(p string) (Info, error) {
var i Info
fi, err := os.Stat(p)
if err != nil {
return i, err
}
fi, err := os.Stat(p)
if err != nil {
return i, err
}
if !fi.IsDir() {
p = filepath.Dir(p)
}
if !fi.IsDir() {
p = filepath.Dir(p)
}
pwd, err := os.Getwd()
if err != nil {
return i, err
}
pwd, err := os.Getwd()
if err != nil {
return i, err
}
defer os.Chdir(pwd)
defer os.Chdir(pwd)
os.Chdir(p)
os.Chdir(p)
b, err := run("go", "list", "-json")
if err != nil {
return i, err
}
b, err := run("go", "list", "-json")
if err != nil {
return i, err
}
if err := json.Unmarshal(b, &i); err != nil {
return i, err
}
if err := json.Unmarshal(b, &i); err != nil {
return i, err
}
return i, nil
return i, nil
})
}

View File

@ -4,8 +4,13 @@ import (
"bytes"
"os"
"os/exec"
"sync"
)
var cache = &infoMap{
data: &sync.Map{},
}
func run(n string, args ...string) ([]byte, error) {
c := exec.Command(n, args...)
@ -19,3 +24,16 @@ func run(n string, args ...string) ([]byte, error) {
return bb.Bytes(), nil
}
func Cache(p string, fn func(string) (Info, error)) (Info, error) {
i, ok := cache.Load(p)
if ok {
return i, nil
}
i, err := fn(p)
if err != nil {
return i, err
}
cache.Store(p, i)
return i, nil
}

87
here/info_map.go Normal file
View File

@ -0,0 +1,87 @@
// Code generated by github.com/gobuffalo/mapgen. DO NOT EDIT.
package here
import (
"sort"
"sync"
)
// infoMap wraps sync.Map and uses the following types:
// key: string
// value: Info
type infoMap struct {
data *sync.Map
}
// Delete the key from the map
func (m *infoMap) Delete(key string) {
m.data.Delete(key)
}
// Load the key from the map.
// Returns Info or bool.
// A false return indicates either the key was not found
// or the value is not of type Info
func (m *infoMap) Load(key string) (Info, bool) {
i, ok := m.data.Load(key)
if !ok {
return Info{}, false
}
s, ok := i.(Info)
return s, ok
}
// LoadOrStore will return an existing key or
// store the value if not already in the map
func (m *infoMap) LoadOrStore(key string, value Info) (Info, bool) {
i, _ := m.data.LoadOrStore(key, value)
s, ok := i.(Info)
return s, ok
}
// LoadOr will return an existing key or
// run the function and store the results
func (m *infoMap) LoadOr(key string, fn func(*infoMap) (Info, bool)) (Info, bool) {
i, ok := m.Load(key)
if ok {
return i, ok
}
i, ok = fn(m)
if ok {
m.Store(key, i)
return i, ok
}
return i, false
}
// Range over the Info values in the map
func (m *infoMap) Range(f func(key string, value Info) bool) {
m.data.Range(func(k, v interface{}) bool {
key, ok := k.(string)
if !ok {
return false
}
value, ok := v.(Info)
if !ok {
return false
}
return f(key, value)
})
}
// Store a Info in the map
func (m *infoMap) Store(key string, value Info) {
m.data.Store(key, value)
}
// Keys returns a list of keys in the map
func (m *infoMap) Keys() []string {
var keys []string
m.Range(func(key string, value Info) bool {
keys = append(keys, key)
return true
})
sort.Strings(keys)
return keys
}

View File

@ -1,6 +1,8 @@
package here
import "encoding/json"
import (
"encoding/json"
)
// Package attempts to gather info for the requested package.
//
@ -12,14 +14,16 @@ import "encoding/json"
// returned `Info` value and pass it to the `Dir(string) (Info, error)`
// function to return the complete data.
func Package(p string) (Info, error) {
var i Info
b, err := run("go", "list", "-json", "-find", p)
if err != nil {
return i, err
}
if err := json.Unmarshal(b, &i); err != nil {
return i, err
}
return Cache(p, func(p string) (Info, error) {
var i Info
b, err := run("go", "list", "-json", "-find", p)
if err != nil {
return i, err
}
if err := json.Unmarshal(b, &i); err != nil {
return i, err
}
return i, nil
return i, nil
})
}

View File

@ -85,29 +85,6 @@ func (m *Files) Load(key pkging.Path) (pkging.File, bool) {
return s, ok
}
// LoadOrStore will return an existing key or
// store the value if not already in the map
func (m *Files) LoadOrStore(key pkging.Path, value pkging.File) (pkging.File, bool) {
i, _ := m.Data().LoadOrStore(key, value)
s, ok := i.(pkging.File)
return s, ok
}
// LoadOr will return an existing key or
// run the function and store the results
func (m *Files) LoadOr(key pkging.Path, fn func(*Files) (pkging.File, bool)) (pkging.File, bool) {
i, ok := m.Load(key)
if ok {
return i, ok
}
i, ok = fn(m)
if ok {
m.Store(key, i)
return i, ok
}
return i, false
}
// Range over the pkging.File values in the map
func (m *Files) Range(f func(key pkging.Path, value pkging.File) bool) {
m.Data().Range(func(k, v interface{}) bool {

View File

@ -71,29 +71,6 @@ func (m *Infos) Load(key string) (here.Info, bool) {
return s, ok
}
// LoadOrStore will return an existing key or
// store the value if not already in the map
func (m *Infos) LoadOrStore(key string, value here.Info) (here.Info, bool) {
i, _ := m.Data().LoadOrStore(key, value)
s, ok := i.(here.Info)
return s, ok
}
// LoadOr will return an existing key or
// run the function and store the results
func (m *Infos) LoadOr(key string, fn func(*Infos) (here.Info, bool)) (here.Info, bool) {
i, ok := m.Load(key)
if ok {
return i, ok
}
i, ok = fn(m)
if ok {
m.Store(key, i)
return i, ok
}
return i, false
}
// Range over the here.Info values in the map
func (m *Infos) Range(f func(key string, value here.Info) bool) {
m.Data().Range(func(k, v interface{}) bool {

View File

@ -74,29 +74,6 @@ func (m *Paths) Load(key string) (pkging.Path, bool) {
return s, ok
}
// LoadOrStore will return an existing key or
// store the value if not already in the map
func (m *Paths) LoadOrStore(key string, value pkging.Path) (pkging.Path, bool) {
i, _ := m.Data().LoadOrStore(key, value)
s, ok := i.(pkging.Path)
return s, ok
}
// LoadOr will return an existing key or
// run the function and store the results
func (m *Paths) LoadOr(key string, fn func(*Paths) (pkging.Path, bool)) (pkging.Path, bool) {
i, ok := m.Load(key)
if ok {
return i, ok
}
i, ok = fn(m)
if ok {
m.Store(key, i)
return i, ok
}
return i, false
}
// Range over the Path values in the map
func (m *Paths) Range(f func(key string, value pkging.Path) bool) {
m.Data().Range(func(k, v interface{}) bool {

View File

@ -95,6 +95,12 @@ func (fx *Pkger) RemoveAll(name string) error {
return true
})
fx.paths.Range(func(key string, value pkging.Path) bool {
if strings.HasPrefix(key, pt.Name) {
fx.paths.Delete(key)
}
return true
})
return nil
}

View File

@ -4,24 +4,28 @@ import (
"testing"
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/pkgtest"
"github.com/stretchr/testify/require"
)
func Test_Pkger(t *testing.T) {
r := require.New(t)
suite, err := pkgtest.NewSuite("memos", func() (pkging.Pkger, error) {
info, err := here.Current()
if err != nil {
return nil, err
}
info, err := here.Current()
r.NoError(err)
r.NotZero(info)
wh, err := New(info)
if err != nil {
return nil, err
}
wh, err := New(info)
r.NoError(err)
WithInfo(wh, info)
suite, err := pkgtest.NewSuite(wh)
r.NoError(err)
WithInfo(wh, info)
return wh, nil
})
if err != nil {
t.Fatal(err)
}
suite.Test(t)
}

View File

@ -10,7 +10,11 @@ import (
func (s Suite) Test_File_Info(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
@ -26,12 +30,12 @@ func (s Suite) Test_File_Info(t *testing.T) {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
r.NoError(s.RemoveAll(mould))
r.NoError(s.MkdirAll(filepath.Dir(tt.in), 0755))
err := pkgutil.WriteFile(s, tt.in, []byte(mould), 0644)
r.NoError(pkg.RemoveAll(mould))
r.NoError(pkg.MkdirAll(filepath.Dir(tt.in), 0755))
err := pkgutil.WriteFile(pkg, tt.in, []byte(mould), 0644)
r.NoError(err)
f, err := s.Open(tt.in)
f, err := pkg.Open(tt.in)
r.NoError(err)
r.Equal(mould, f.Name())
r.Equal(cur.ImportPath, f.Info().ImportPath)

View File

@ -7,6 +7,7 @@ import (
"path/filepath"
"testing"
"github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/pkgutil"
"github.com/stretchr/testify/require"
)
@ -29,7 +30,7 @@ import (
// └── b
// └── b.txt
func (s Suite) LoadFolder() error {
func (s Suite) LoadFolder(pkg pkging.Pkger) error {
files := []string{
"/main.go",
"/public/images/mark.png",
@ -39,10 +40,10 @@ func (s Suite) LoadFolder() error {
}
for _, f := range files {
if err := s.MkdirAll(filepath.Dir(f), 0755); err != nil {
if err := pkg.MkdirAll(filepath.Dir(f), 0755); err != nil {
return err
}
if err := pkgutil.WriteFile(s, f, []byte("!"+f), 0644); err != nil {
if err := pkgutil.WriteFile(pkg, f, []byte("!"+f), 0644); err != nil {
return err
}
}
@ -52,7 +53,10 @@ func (s Suite) LoadFolder() error {
func (s Suite) Test_HTTP_Dir(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
@ -68,11 +72,13 @@ func (s Suite) Test_HTTP_Dir(t *testing.T) {
for _, tt := range table {
s.Run(t, tt.in+tt.req, func(st *testing.T) {
r := require.New(st)
r := require.New(t)
r.NoError(s.LoadFolder())
pkg, err := s.Make()
r.NoError(err)
r.NoError(s.LoadFolder(pkg))
dir, err := s.Open(tt.in)
dir, err := pkg.Open(tt.in)
r.NoError(err)
defer dir.Close()
@ -94,7 +100,10 @@ func (s Suite) Test_HTTP_Dir(t *testing.T) {
func (s Suite) Test_HTTP_Dir_IndexHTML(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
@ -112,9 +121,12 @@ func (s Suite) Test_HTTP_Dir_IndexHTML(t *testing.T) {
s.Run(t, tt.in+exp, func(st *testing.T) {
r := require.New(st)
r.NoError(s.LoadFolder())
pkg, err := s.Make()
r.NoError(err)
dir, err := s.Open(tt.in)
r.NoError(s.LoadFolder(pkg))
dir, err := pkg.Open(tt.in)
r.NoError(err)
defer dir.Close()
@ -138,7 +150,10 @@ func (s Suite) Test_HTTP_Dir_IndexHTML(t *testing.T) {
func (s Suite) Test_HTTP_File(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
@ -152,12 +167,14 @@ func (s Suite) Test_HTTP_File(t *testing.T) {
for _, tt := range table {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
r.NoError(s.LoadFolder())
pkg, err := s.Make()
r.NoError(err)
dir, err := s.Open(tt.in)
r.NoError(s.LoadFolder(pkg))
dir, err := pkg.Open(tt.in)
r.NoError(err)
defer dir.Close()

View File

@ -20,12 +20,21 @@ const hart = "/easy/listening/grant.hart"
const husker = "github.com/husker/du"
type Suite struct {
pkging.Pkger
Name string
gen func() (pkging.Pkger, error)
}
func NewSuite(yourpkging pkging.Pkger) (Suite, error) {
func (s Suite) Make() (pkging.Pkger, error) {
if s.gen == nil {
return nil, fmt.Errorf("missing generator function")
}
return s.gen()
}
func NewSuite(name string, fn func() (pkging.Pkger, error)) (Suite, error) {
suite := Suite{
Pkger: yourpkging,
Name: name,
gen: fn,
}
return suite, nil
}
@ -34,39 +43,60 @@ func (s Suite) 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)
t.Fatalf("something went wrong wrong with %s", s.Name)
}
for i := 0; i < rt.NumMethod(); i++ {
m := rt.Method(i)
if !strings.HasPrefix(m.Name, "Test_") {
continue
}
s.sub(t, m)
}
}
// func (s Suite) clone() (Suite, error) {
// if ns, ok := s.Pkger.(Newable); ok {
// pkg, err := ns.New()
// if err != nil {
// return s, err
// }
// s, err = NewSuite(pkg)
// if err != nil {
// return s, err
// }
// }
// if ns, ok := s.Pkger.(WithRootable); ok {
// dir, err := ioutil.TempDir("", "")
// if err != nil {
// return s, err
// }
// // defer opkg.RemoveAll(dir)
//
// pkg, err := ns.WithRoot(dir)
// if err != nil {
// return s, err
// }
// s, err = NewSuite(pkg)
// if err != nil {
// return s, err
// }
// }
// return s, nil
// }
func (s Suite) Run(t *testing.T, name string, fn func(t *testing.T)) {
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()
fn(st)
})
}
func (s Suite) sub(t *testing.T, m reflect.Method) {
name := fmt.Sprintf("%T/%s", s.Pkger, m.Name)
name := fmt.Sprintf("%s/%s", s.Name, m.Name)
// s, err := s.clone()
// if err != nil {
// t.Fatal(err)
// }
s.Run(t, name, func(st *testing.T) {
m.Func.Call([]reflect.Value{
reflect.ValueOf(s),
@ -75,26 +105,13 @@ func (s Suite) sub(t *testing.T, m reflect.Method) {
})
}
func (s Suite) Clean() error {
pt, err := s.Parse("/")
if err != nil {
return err
}
_ = pt
if err := s.RemoveAll(pt.Name); err != nil {
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
}
func (s Suite) Test_Create(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
@ -104,22 +121,18 @@ func (s Suite) Test_Create(t *testing.T) {
{in: mould},
{in: ":" + mould},
{in: ip + ":" + mould},
{in: filepath.Dir(mould)},
{in: ":" + filepath.Dir(mould)},
{in: ip + ":" + filepath.Dir(mould)},
}
for _, tt := range table {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
pt, err := pkg.Parse(tt.in)
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
r.NoError(s.MkdirAll(filepath.Dir(pt.Name), 0755))
r.NoError(pkg.MkdirAll(filepath.Dir(pt.Name), 0755))
f, err := s.Create(pt.Name)
f, err := pkg.Create(pt.Name)
r.NoError(err)
r.Equal(pt.Name, f.Name())
@ -129,14 +142,18 @@ func (s Suite) Test_Create(t *testing.T) {
r.Equal(pt.Name, fi.Name())
r.Equal(os.FileMode(0644), fi.Mode())
r.NotZero(fi.ModTime())
r.NoError(s.RemoveAll(pt.String()))
r.NoError(pkg.RemoveAll(pt.String()))
})
}
}
func (s Suite) Test_Create_No_MkdirAll(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
@ -155,12 +172,13 @@ func (s Suite) Test_Create_No_MkdirAll(t *testing.T) {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
pkg, err := s.Make()
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
pt, err := pkg.Parse(tt.in)
r.NoError(err)
_, err = s.Create(pt.Name)
_, err = pkg.Create(pt.Name)
r.Error(err)
})
}
@ -169,7 +187,10 @@ func (s Suite) Test_Create_No_MkdirAll(t *testing.T) {
func (s Suite) Test_Current(t *testing.T) {
r := require.New(t)
info, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
info, err := pkg.Current()
r.NoError(err)
r.NotZero(info)
}
@ -177,10 +198,13 @@ func (s Suite) Test_Current(t *testing.T) {
func (s Suite) Test_Info(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
info, err := s.Info(cur.ImportPath)
cur, err := pkg.Current()
r.NoError(err)
info, err := pkg.Info(cur.ImportPath)
r.NoError(err)
r.NotZero(info)
@ -188,7 +212,11 @@ func (s Suite) Test_Info(t *testing.T) {
func (s Suite) Test_MkdirAll(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
@ -207,21 +235,22 @@ func (s Suite) Test_MkdirAll(t *testing.T) {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
pkg, err := s.Make()
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
pt, err := pkg.Parse(tt.in)
r.NoError(err)
dir := filepath.Dir(pt.Name)
r.NoError(s.MkdirAll(dir, 0755))
r.NoError(pkg.MkdirAll(dir, 0755))
fi, err := s.Stat(dir)
fi, err := pkg.Stat(dir)
r.NoError(err)
r.Equal(dir, fi.Name())
r.Equal(os.FileMode(0755), fi.Mode().Perm())
r.NotZero(fi.ModTime())
r.NoError(s.RemoveAll(pt.String()))
r.NoError(pkg.RemoveAll(pt.String()))
})
}
}
@ -229,7 +258,10 @@ func (s Suite) Test_MkdirAll(t *testing.T) {
func (s Suite) Test_Open_File(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
@ -244,20 +276,22 @@ func (s Suite) Test_Open_File(t *testing.T) {
for _, tt := range table {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
pkg, err := s.Make()
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
r.NoError(s.MkdirAll(filepath.Dir(pt.Name), 0755))
pt, err := pkg.Parse(tt.in)
r.NoError(err)
r.NoError(pkg.RemoveAll(pt.String()))
r.NoError(pkg.MkdirAll(filepath.Dir(pt.Name), 0755))
body := "!" + pt.String()
pkgutil.WriteFile(s, tt.in, []byte(body), 0644)
pkgutil.WriteFile(pkg, tt.in, []byte(body), 0644)
f, err := s.Open(tt.in)
f, err := pkg.Open(tt.in)
r.NoError(err)
r.Equal(pt.Name, f.Path().Name)
@ -265,7 +299,7 @@ func (s Suite) Test_Open_File(t *testing.T) {
r.NoError(err)
r.Equal(body, string(b))
b, err = pkgutil.ReadFile(s, tt.in)
b, err = pkgutil.ReadFile(pkg, tt.in)
r.NoError(err)
r.Equal(body, string(b))
@ -277,7 +311,10 @@ func (s Suite) Test_Open_File(t *testing.T) {
func (s Suite) Test_Parse(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
@ -300,7 +337,7 @@ func (s Suite) Test_Parse(t *testing.T) {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
pt, err := pkg.Parse(tt.in)
r.NoError(err)
r.Equal(tt.exp, pt)
})
@ -310,7 +347,10 @@ func (s Suite) Test_Parse(t *testing.T) {
func (s Suite) Test_Stat_Error(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
@ -330,12 +370,12 @@ func (s Suite) Test_Stat_Error(t *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
pt, err := pkg.Parse(tt.in)
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
r.NoError(pkg.RemoveAll(pt.String()))
_, err = s.Stat(tt.in)
_, err = pkg.Stat(tt.in)
r.Error(err)
})
}
@ -344,7 +384,10 @@ func (s Suite) Test_Stat_Error(t *testing.T) {
func (s Suite) Test_Stat_Dir(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
dir := filepath.Dir(mould)
@ -364,13 +407,13 @@ func (s Suite) Test_Stat_Dir(t *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
pt, err := pkg.Parse(tt.in)
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
r.NoError(pkg.RemoveAll(pt.String()))
r.NoError(s.MkdirAll(pt.Name, 0755))
info, err := s.Stat(tt.in)
r.NoError(pkg.MkdirAll(pt.Name, 0755))
info, err := pkg.Stat(tt.in)
r.NoError(err)
r.Equal(pt.Name, info.Name())
})
@ -380,7 +423,10 @@ func (s Suite) Test_Stat_Dir(t *testing.T) {
func (s Suite) Test_Stat_File(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
@ -398,20 +444,23 @@ func (s Suite) Test_Stat_File(t *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
pkg, err := s.Make()
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
r.NoError(s.MkdirAll(filepath.Dir(pt.Name), 0755))
pt, err := pkg.Parse(tt.in)
r.NoError(err)
f, err := s.Create(tt.in)
r.NoError(pkg.RemoveAll(pt.String()))
r.NoError(pkg.MkdirAll(filepath.Dir(pt.Name), 0755))
f, err := pkg.Create(tt.in)
r.NoError(err)
_, err = io.Copy(f, strings.NewReader("!"+pt.String()))
r.NoError(err)
r.NoError(f.Close())
info, err := s.Stat(tt.in)
info, err := pkg.Stat(tt.in)
r.NoError(err)
r.Equal(pt.Name, info.Name())
})
@ -421,7 +470,12 @@ func (s Suite) Test_Stat_File(t *testing.T) {
// 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_RemoveAll(t *testing.T) {
// panic("not implemented")
// }

View File

@ -12,7 +12,11 @@ import (
func (s Suite) Test_Util_ReadFile(t *testing.T) {
r := require.New(t)
cur, err := s.Current()
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
r.NoError(err)
ip := cur.ImportPath
@ -27,16 +31,18 @@ func (s Suite) Test_Util_ReadFile(t *testing.T) {
for _, tt := range table {
s.Run(t, tt.in, func(st *testing.T) {
r := require.New(st)
pt, err := s.Parse(tt.in)
pkg, err := s.Make()
r.NoError(err)
r.NoError(s.RemoveAll(pt.String()))
r.NoError(s.MkdirAll(filepath.Dir(pt.Name), 0755))
pt, err := pkg.Parse(tt.in)
r.NoError(err)
f, err := s.Create(tt.in)
r.NoError(pkg.RemoveAll(pt.String()))
r.NoError(pkg.MkdirAll(filepath.Dir(pt.Name), 0755))
f, err := pkg.Create(tt.in)
r.NoError(err)
body := "!" + pt.String()
@ -44,7 +50,7 @@ func (s Suite) Test_Util_ReadFile(t *testing.T) {
r.NoError(err)
r.NoError(f.Close())
b, err := pkgutil.ReadFile(s, tt.in)
b, err := pkgutil.ReadFile(pkg, tt.in)
r.NoError(err)
r.Equal(body, string(b))
})

View File

@ -2,7 +2,6 @@ package stdos
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -107,14 +106,6 @@ func (f *Pkger) Parse(p string) (pkging.Path, error) {
return f.paths.Parse(p)
}
func (f *Pkger) ReadFile(s string) ([]byte, error) {
s, err := f.Abs(s)
if err != nil {
return nil, err
}
return ioutil.ReadFile(s)
}
func (f *Pkger) Stat(name string) (os.FileInfo, error) {
pt, err := f.Parse(name)
if err != nil {

View File

@ -1,24 +1,33 @@
package stdos
import (
"path/filepath"
"io/ioutil"
"testing"
"github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/pkgtest"
"github.com/stretchr/testify/require"
)
func Test_Pkger(t *testing.T) {
r := require.New(t)
suite, err := pkgtest.NewSuite("stdos", func() (pkging.Pkger, error) {
mypkging, err := New()
if err != nil {
return nil, err
}
mypkging, err := New()
r.NoError(err)
dir, err := ioutil.TempDir("", "stdos")
if err != nil {
return nil, err
}
mypkging.current.Dir = filepath.Join(mypkging.current.Dir, ".pkgtest")
mypkging.paths.Current = mypkging.current
mypkging.current.Dir = dir
mypkging.paths.Current = mypkging.current
suite, err := pkgtest.NewSuite(mypkging)
r.NoError(err)
return mypkging, nil
})
if err != nil {
t.Fatal(err)
}
suite.Test(t)
}