diff --git a/Makefile b/Makefile index 94018ac..b728e38 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/examples/app/go.mod b/examples/app/go.mod index 184f927..fc5a5dd 100644 --- a/examples/app/go.mod +++ b/examples/app/go.mod @@ -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 diff --git a/examples/app/go.sum b/examples/app/go.sum index d3cbc9f..1945221 100644 --- a/examples/app/go.sum +++ b/examples/app/go.sum @@ -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= diff --git a/here/dir.go b/here/dir.go index 1b37dde..d0d1034 100644 --- a/here/dir.go +++ b/here/dir.go @@ -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 + }) } diff --git a/here/here.go b/here/here.go index dc4ee62..a5a282f 100644 --- a/here/here.go +++ b/here/here.go @@ -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 +} diff --git a/here/info_map.go b/here/info_map.go new file mode 100644 index 0000000..b9471ef --- /dev/null +++ b/here/info_map.go @@ -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 +} diff --git a/here/pkg.go b/here/pkg.go index aeb2db2..3a2bc3c 100644 --- a/here/pkg.go +++ b/here/pkg.go @@ -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 + }) } diff --git a/internal/maps/files.go b/internal/maps/files.go index ddd3ccd..bf5ab3a 100644 --- a/internal/maps/files.go +++ b/internal/maps/files.go @@ -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 { diff --git a/internal/maps/infos.go b/internal/maps/infos.go index a0d4871..0eed02f 100644 --- a/internal/maps/infos.go +++ b/internal/maps/infos.go @@ -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 { diff --git a/internal/maps/paths.go b/internal/maps/paths.go index e114c5e..969df6b 100644 --- a/internal/maps/paths.go +++ b/internal/maps/paths.go @@ -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 { diff --git a/pkging/mem/mem.go b/pkging/mem/mem.go index c1f3465..c1293f1 100644 --- a/pkging/mem/mem.go +++ b/pkging/mem/mem.go @@ -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 } diff --git a/pkging/mem/mem_test.go b/pkging/mem/mem_test.go index 925b26b..79effb2 100644 --- a/pkging/mem/mem_test.go +++ b/pkging/mem/mem_test.go @@ -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) } diff --git a/pkging/pkgtest/file.go b/pkging/pkgtest/file.go index 11025f1..72d7fa6 100644 --- a/pkging/pkgtest/file.go +++ b/pkging/pkgtest/file.go @@ -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) diff --git a/pkging/pkgtest/http.go b/pkging/pkgtest/http.go index f0a88b4..2365bdf 100644 --- a/pkging/pkgtest/http.go +++ b/pkging/pkgtest/http.go @@ -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() diff --git a/pkging/pkgtest/suite.go b/pkging/pkgtest/suite.go index 7ddc8f5..5863da5 100644 --- a/pkging/pkgtest/suite.go +++ b/pkging/pkgtest/suite.go @@ -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") +// } diff --git a/pkging/pkgtest/util.go b/pkging/pkgtest/util.go index 5a71098..6386183 100644 --- a/pkging/pkgtest/util.go +++ b/pkging/pkgtest/util.go @@ -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)) }) diff --git a/pkging/stdos/stdos.go b/pkging/stdos/stdos.go index 44ffa0e..2b65997 100644 --- a/pkging/stdos/stdos.go +++ b/pkging/stdos/stdos.go @@ -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 { diff --git a/pkging/stdos/stdos_test.go b/pkging/stdos/stdos_test.go index e61edc0..09e31c1 100644 --- a/pkging/stdos/stdos_test.go +++ b/pkging/stdos/stdos_test.go @@ -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) }