mirror of https://github.com/markbates/pkger.git
i'm easy to please
This commit is contained in:
parent
38ea627cde
commit
57ff4f849b
Binary file not shown.
|
@ -1,8 +0,0 @@
|
|||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package main
|
||||
|
||||
import _ "app/packrd"
|
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Binary file not shown.
5
go.mod
5
go.mod
|
@ -3,6 +3,9 @@ module github.com/markbates/pkger
|
|||
go 1.13
|
||||
|
||||
require (
|
||||
github.com/gobuffalo/buffalo v0.14.11 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/stretchr/testify v1.4.0
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.4 // indirect
|
||||
)
|
||||
|
|
|
@ -11,10 +11,10 @@ import (
|
|||
func Test_Parser_App(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
info, err := pkgtest.App()
|
||||
app, err := pkgtest.App()
|
||||
r.NoError(err)
|
||||
|
||||
res, err := Parse(info)
|
||||
res, err := Parse(app.Info)
|
||||
|
||||
r.NoError(err)
|
||||
|
||||
|
@ -24,5 +24,5 @@ func Test_Parser_App(t *testing.T) {
|
|||
}
|
||||
|
||||
sort.Strings(act)
|
||||
r.Equal(pkgtest.AppPaths, act)
|
||||
r.Equal(app.Paths.Parser, act)
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package mem_test
|
|||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/pkger/parser"
|
||||
|
@ -16,10 +17,10 @@ import (
|
|||
func Test_Pkger_Embedding(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
info, err := pkgtest.App()
|
||||
app, err := pkgtest.App()
|
||||
r.NoError(err)
|
||||
|
||||
paths, err := parser.Parse(info)
|
||||
paths, err := parser.Parse(app.Info)
|
||||
r.NoError(err)
|
||||
|
||||
ps := make([]string, len(paths))
|
||||
|
@ -27,12 +28,12 @@ func Test_Pkger_Embedding(t *testing.T) {
|
|||
ps[i] = p.String()
|
||||
}
|
||||
|
||||
r.Equal(pkgtest.AppPaths, ps)
|
||||
r.Equal(app.Paths.Parser, ps)
|
||||
|
||||
base, err := mem.New(info)
|
||||
base, err := mem.New(app.Info)
|
||||
r.NoError(err)
|
||||
|
||||
disk, err := stdos.New(info)
|
||||
disk, err := stdos.New(app.Info)
|
||||
r.NoError(err)
|
||||
|
||||
err = disk.Walk("/", func(path string, info os.FileInfo, err error) error {
|
||||
|
@ -63,7 +64,7 @@ func Test_Pkger_Embedding(t *testing.T) {
|
|||
})
|
||||
|
||||
r.NoError(err)
|
||||
r.Equal(rootWalk, res)
|
||||
r.Equal(app.Paths.Root, res)
|
||||
|
||||
res = []string{}
|
||||
err = base.Walk("/public", func(path string, info os.FileInfo, err error) error {
|
||||
|
@ -75,11 +76,11 @@ func Test_Pkger_Embedding(t *testing.T) {
|
|||
})
|
||||
|
||||
r.NoError(err)
|
||||
r.Equal(pubWalk[1:], res)
|
||||
r.Equal(app.Paths.Public, res)
|
||||
|
||||
bb := &bytes.Buffer{}
|
||||
|
||||
err = stuffing.Stuff(bb, info, paths)
|
||||
err = stuffing.Stuff(bb, app.Info, paths)
|
||||
r.NoError(err)
|
||||
|
||||
pkg, err := mem.UnmarshalEmbed(bb.Bytes())
|
||||
|
@ -95,7 +96,10 @@ func Test_Pkger_Embedding(t *testing.T) {
|
|||
})
|
||||
|
||||
r.NoError(err)
|
||||
r.Equal(rootWalk, res)
|
||||
|
||||
exp := append(app.Paths.Public, "app:/")
|
||||
sort.Strings(exp)
|
||||
r.Equal(exp, res)
|
||||
|
||||
res = []string{}
|
||||
err = pkg.Walk("/public", func(path string, info os.FileInfo, err error) error {
|
||||
|
@ -107,29 +111,5 @@ func Test_Pkger_Embedding(t *testing.T) {
|
|||
})
|
||||
|
||||
r.NoError(err)
|
||||
r.Equal(pubWalk, res)
|
||||
}
|
||||
|
||||
var pubWalk = []string{
|
||||
"app:/",
|
||||
"app:/public",
|
||||
"app:/public/images",
|
||||
"app:/public/images/img1.png",
|
||||
"app:/public/images/img2.png",
|
||||
"app:/public/index.html",
|
||||
}
|
||||
|
||||
var rootWalk = []string{
|
||||
"app:/",
|
||||
"app:/go.mod",
|
||||
"app:/main.go",
|
||||
"app:/public",
|
||||
"app:/public/images",
|
||||
"app:/public/images/img1.png",
|
||||
"app:/public/images/img2.png",
|
||||
"app:/public/index.html",
|
||||
"app:/templates",
|
||||
"app:/templates/a.txt",
|
||||
"app:/templates/b",
|
||||
"app:/templates/b/b.txt",
|
||||
r.Equal(app.Paths.Public, res)
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@ func New(info here.Info) (*Pkger, error) {
|
|||
Here: info,
|
||||
}
|
||||
f.infos.Store(info.ImportPath, info)
|
||||
f.MkdirAll("/", 0755)
|
||||
return f, nil
|
||||
}
|
||||
|
||||
|
@ -152,17 +151,19 @@ func (fx *Pkger) RemoveAll(name string) error {
|
|||
|
||||
// Add copies the pkging.File into the *Pkger
|
||||
func (fx *Pkger) Add(f pkging.File) error {
|
||||
fx.MkdirAll("/", 0755)
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if f.Path().Pkg == fx.Here.ImportPath {
|
||||
if err := fx.MkdirAll(filepath.Dir(f.Name()), 0755); err != nil {
|
||||
dir := filepath.Dir(f.Name())
|
||||
if dir != "/" {
|
||||
if err := fx.MkdirAll(dir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mf := &File{
|
||||
her: f.Info(),
|
||||
|
@ -187,7 +188,6 @@ func (fx *Pkger) Add(f pkging.File) 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.
|
||||
func (fx *Pkger) Create(name string) (pkging.File, error) {
|
||||
fx.MkdirAll("/", 0755)
|
||||
pt, err := fx.Parse(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -199,9 +199,11 @@ func (fx *Pkger) Create(name string) (pkging.File, error) {
|
|||
}
|
||||
|
||||
dir := filepath.Dir(pt.Name)
|
||||
if dir != "/" {
|
||||
if _, err := fx.Stat(dir); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
f := &File{
|
||||
path: pt,
|
||||
|
@ -328,22 +330,28 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
|
|||
|
||||
skip := "!"
|
||||
for _, k := range keys {
|
||||
if k.Pkg != pt.Pkg {
|
||||
continue
|
||||
}
|
||||
if !strings.HasPrefix(k.Name, pt.Name) {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(k.Name, skip) {
|
||||
continue
|
||||
}
|
||||
|
||||
fl, ok := f.files.Load(k)
|
||||
if !ok {
|
||||
return fmt.Errorf("could not find %s", k)
|
||||
return os.ErrNotExist
|
||||
}
|
||||
|
||||
fi, err := fl.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fi = pkging.WithName(strings.TrimPrefix(k.Name, pt.Name), fi)
|
||||
|
||||
err = wf(k.String(), fi, nil)
|
||||
if err == filepath.SkipDir {
|
||||
|
||||
|
|
|
@ -9,12 +9,12 @@ import (
|
|||
|
||||
func Test_Pkger(t *testing.T) {
|
||||
suite, err := pkgtest.NewSuite("memos", func() (pkging.Pkger, error) {
|
||||
info, err := pkgtest.App()
|
||||
app, err := pkgtest.App()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pkg, err := New(info)
|
||||
pkg, err := New(app.Info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -6,15 +6,41 @@ import (
|
|||
"github.com/markbates/pkger/here"
|
||||
)
|
||||
|
||||
type AppDetails struct {
|
||||
here.Info
|
||||
Paths struct {
|
||||
Root []string
|
||||
Parser []string
|
||||
Public []string
|
||||
}
|
||||
}
|
||||
|
||||
// App returns here.info that represents the
|
||||
// ./internal/testdata/app. This should be used
|
||||
// by tests.
|
||||
func App() (here.Info, error) {
|
||||
func App() (AppDetails, error) {
|
||||
var app AppDetails
|
||||
|
||||
her, err := here.Package("github.com/markbates/pkger")
|
||||
if err != nil {
|
||||
return her, err
|
||||
return app, err
|
||||
}
|
||||
var info here.Info
|
||||
|
||||
info := here.Info{
|
||||
ImportPath: "app",
|
||||
}
|
||||
|
||||
x := make([]string, len(rootPaths))
|
||||
copy(x, rootPaths)
|
||||
app.Paths.Root = x
|
||||
|
||||
x = make([]string, len(parserPaths))
|
||||
copy(x, parserPaths)
|
||||
app.Paths.Parser = x
|
||||
|
||||
x = make([]string, len(publicPaths))
|
||||
copy(x, publicPaths)
|
||||
app.Paths.Public = x
|
||||
|
||||
ch := filepath.Join(
|
||||
her.Dir,
|
||||
|
@ -25,13 +51,41 @@ func App() (here.Info, error) {
|
|||
"app")
|
||||
|
||||
info.Dir = ch
|
||||
info.ImportPath = "app"
|
||||
return here.Cache(info.ImportPath, func(s string) (here.Info, error) {
|
||||
|
||||
info, err = here.Cache(info.ImportPath, func(s string) (here.Info, error) {
|
||||
return info, nil
|
||||
})
|
||||
if err != nil {
|
||||
return app, err
|
||||
}
|
||||
app.Info = info
|
||||
return app, nil
|
||||
}
|
||||
|
||||
var AppPaths = []string{
|
||||
var rootPaths = []string{
|
||||
"app:/",
|
||||
"app:/go.mod",
|
||||
"app:/main.go",
|
||||
"app:/public",
|
||||
"app:/public/images",
|
||||
"app:/public/images/img1.png",
|
||||
"app:/public/images/img2.png",
|
||||
"app:/public/index.html",
|
||||
"app:/templates",
|
||||
"app:/templates/a.txt",
|
||||
"app:/templates/b",
|
||||
"app:/templates/b/b.txt",
|
||||
}
|
||||
|
||||
var publicPaths = []string{
|
||||
"app:/public",
|
||||
"app:/public/images",
|
||||
"app:/public/images/img1.png",
|
||||
"app:/public/images/img2.png",
|
||||
"app:/public/index.html",
|
||||
}
|
||||
|
||||
var parserPaths = []string{
|
||||
"app:/",
|
||||
"app:/public",
|
||||
"app:/public/images",
|
||||
|
|
|
@ -445,25 +445,49 @@ func (s Suite) Test_Walk(t *testing.T) {
|
|||
|
||||
r.NoError(s.LoadFolder(pkg))
|
||||
|
||||
cur, err := pkg.Current()
|
||||
app, err := App()
|
||||
r.NoError(err)
|
||||
|
||||
ip := cur.ImportPath
|
||||
|
||||
table := []struct {
|
||||
in string
|
||||
exp []string
|
||||
}{
|
||||
{in: ip},
|
||||
{in: "/"},
|
||||
{in: ":/"},
|
||||
{in: ip + ":/"},
|
||||
{in: "/", exp: app.Paths.Root},
|
||||
{in: "/public", exp: app.Paths.Public},
|
||||
}
|
||||
|
||||
for _, tt := range table {
|
||||
s.Run(t, tt.in, func(st *testing.T) {
|
||||
|
||||
tdir, err := ioutil.TempDir("", "")
|
||||
r.NoError(err)
|
||||
defer os.RemoveAll(tdir)
|
||||
r.NoError(s.WriteFolder(tdir))
|
||||
|
||||
var goact []string
|
||||
err = filepath.Walk(filepath.Join(tdir, tt.in), func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
path = strings.TrimPrefix(path, tdir)
|
||||
|
||||
if path == "" || path == "." {
|
||||
path = "/"
|
||||
}
|
||||
|
||||
pt, err := pkg.Parse(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
goact = append(goact, pt.String())
|
||||
return nil
|
||||
})
|
||||
r.NoError(err)
|
||||
r.Equal(tt.exp, goact)
|
||||
|
||||
r := require.New(st)
|
||||
var act []string
|
||||
err := pkg.Walk(tt.in, func(path string, info os.FileInfo, err error) error {
|
||||
err = pkg.Walk(tt.in, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -472,21 +496,7 @@ func (s Suite) Test_Walk(t *testing.T) {
|
|||
})
|
||||
r.NoError(err)
|
||||
|
||||
exp := []string{
|
||||
"app:/",
|
||||
"app:/go.mod",
|
||||
"app:/main.go",
|
||||
"app:/public",
|
||||
"app:/public/images",
|
||||
"app:/public/images/img1.png",
|
||||
"app:/public/images/img2.png",
|
||||
"app:/public/index.html",
|
||||
"app:/templates",
|
||||
"app:/templates/a.txt",
|
||||
"app:/templates/b",
|
||||
"app:/templates/b/b.txt",
|
||||
}
|
||||
r.Equal(exp, act)
|
||||
r.Equal(tt.exp, act)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
|
||||
func Test_Pkger(t *testing.T) {
|
||||
suite, err := pkgtest.NewSuite("stdos", func() (pkging.Pkger, error) {
|
||||
info, err := pkgtest.App()
|
||||
app, err := pkgtest.App()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -20,9 +20,9 @@ func Test_Pkger(t *testing.T) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
info.Dir = dir
|
||||
app.Dir = dir
|
||||
|
||||
mypkging, err := New(info)
|
||||
mypkging, err := New(app.Info)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue