2019-10-16 18:13:11 +03:00
|
|
|
package mem_test
|
2019-09-21 19:51:29 +03:00
|
|
|
|
|
|
|
import (
|
2019-10-15 23:40:45 +03:00
|
|
|
"bytes"
|
2019-09-21 19:51:29 +03:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2019-10-15 23:40:45 +03:00
|
|
|
"github.com/markbates/pkger/parser"
|
2019-10-16 18:13:11 +03:00
|
|
|
"github.com/markbates/pkger/pkging/mem"
|
2019-10-18 19:01:48 +03:00
|
|
|
"github.com/markbates/pkger/pkging/pkgtest"
|
|
|
|
"github.com/markbates/pkger/pkging/stdos"
|
2019-10-16 18:13:11 +03:00
|
|
|
"github.com/markbates/pkger/pkging/stuffing"
|
2019-09-21 19:51:29 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2019-10-15 23:40:45 +03:00
|
|
|
func Test_Pkger_Embedding(t *testing.T) {
|
2019-09-21 19:51:29 +03:00
|
|
|
r := require.New(t)
|
|
|
|
|
2019-10-18 19:01:48 +03:00
|
|
|
info, err := pkgtest.App()
|
2019-09-21 19:51:29 +03:00
|
|
|
r.NoError(err)
|
|
|
|
|
2019-10-15 23:40:45 +03:00
|
|
|
paths, err := parser.Parse(info)
|
2019-09-21 19:51:29 +03:00
|
|
|
r.NoError(err)
|
|
|
|
|
2019-10-15 23:40:45 +03:00
|
|
|
ps := make([]string, len(paths))
|
|
|
|
for i, p := range paths {
|
|
|
|
ps[i] = p.String()
|
|
|
|
}
|
|
|
|
|
2019-10-18 19:01:48 +03:00
|
|
|
r.Equal(pkgtest.AppPaths, ps)
|
|
|
|
|
|
|
|
base, err := mem.New(info)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
disk, err := stdos.New(info)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
err = disk.Walk("/", func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := disk.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return base.Add(f)
|
|
|
|
|
|
|
|
})
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
var res []string
|
|
|
|
err = base.Walk("/", func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res = append(res, path)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(rootWalk, res)
|
|
|
|
|
|
|
|
res = []string{}
|
|
|
|
err = base.Walk("/public", func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res = append(res, path)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(pubWalk[1:], res)
|
2019-10-15 23:40:45 +03:00
|
|
|
|
|
|
|
bb := &bytes.Buffer{}
|
2019-09-21 19:51:29 +03:00
|
|
|
|
2019-10-16 18:13:11 +03:00
|
|
|
err = stuffing.Stuff(bb, info, paths)
|
2019-09-21 19:51:29 +03:00
|
|
|
r.NoError(err)
|
2019-10-09 20:21:54 +03:00
|
|
|
|
2019-10-16 18:13:11 +03:00
|
|
|
pkg, err := mem.UnmarshalEmbed(bb.Bytes())
|
2019-10-15 23:40:45 +03:00
|
|
|
r.NoError(err)
|
2019-09-21 19:51:29 +03:00
|
|
|
|
2019-10-18 19:01:48 +03:00
|
|
|
res = []string{}
|
2019-10-15 23:40:45 +03:00
|
|
|
err = pkg.Walk("/", func(path string, info os.FileInfo, err error) error {
|
2019-09-21 19:51:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-15 23:40:45 +03:00
|
|
|
res = append(res, path)
|
2019-09-21 19:51:29 +03:00
|
|
|
return nil
|
|
|
|
})
|
2019-10-15 23:40:45 +03:00
|
|
|
|
2019-09-21 19:51:29 +03:00
|
|
|
r.NoError(err)
|
2019-10-18 19:01:48 +03:00
|
|
|
r.Equal(rootWalk, res)
|
|
|
|
|
|
|
|
res = []string{}
|
|
|
|
err = pkg.Walk("/public", func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res = append(res, path)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(pubWalk, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
var pubWalk = []string{
|
|
|
|
"app:/",
|
|
|
|
"app:/public",
|
|
|
|
"app:/public/images",
|
2019-10-18 19:27:58 +03:00
|
|
|
"app:/public/images/img1.png",
|
|
|
|
"app:/public/images/img2.png",
|
2019-10-18 19:01:48 +03:00
|
|
|
"app:/public/index.html",
|
2019-10-15 23:40:45 +03:00
|
|
|
}
|
|
|
|
|
2019-10-18 19:01:48 +03:00
|
|
|
var rootWalk = []string{
|
|
|
|
"app:/",
|
|
|
|
"app:/go.mod",
|
|
|
|
"app:/main.go",
|
|
|
|
"app:/public",
|
|
|
|
"app:/public/images",
|
2019-10-18 19:27:58 +03:00
|
|
|
"app:/public/images/img1.png",
|
|
|
|
"app:/public/images/img2.png",
|
2019-10-18 19:01:48 +03:00
|
|
|
"app:/public/index.html",
|
|
|
|
"app:/templates",
|
|
|
|
"app:/templates/a.txt",
|
|
|
|
"app:/templates/b",
|
|
|
|
"app:/templates/b/b.txt",
|
2019-09-21 19:51:29 +03:00
|
|
|
}
|