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"
|
2019-10-21 17:27:04 +03:00
|
|
|
"github.com/markbates/pkger/pkging/pkgutil"
|
2019-10-18 19:01:48 +03:00
|
|
|
"github.com/markbates/pkger/pkging/stdos"
|
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 22:16:53 +03:00
|
|
|
app, err := pkgtest.App()
|
2019-09-21 19:51:29 +03:00
|
|
|
r.NoError(err)
|
|
|
|
|
2019-10-21 17:27:04 +03:00
|
|
|
res, err := parser.Parse(app.Info)
|
2019-09-21 19:51:29 +03:00
|
|
|
r.NoError(err)
|
|
|
|
|
2019-10-21 17:27:04 +03:00
|
|
|
files, err := res.Files()
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
ps := make([]string, len(files))
|
|
|
|
for i, f := range files {
|
|
|
|
ps[i] = f.Path.String()
|
2019-10-15 23:40:45 +03:00
|
|
|
}
|
|
|
|
|
2019-10-18 22:16:53 +03:00
|
|
|
r.Equal(app.Paths.Parser, ps)
|
2019-10-18 19:01:48 +03:00
|
|
|
|
2019-10-18 22:16:53 +03:00
|
|
|
base, err := mem.New(app.Info)
|
2019-10-18 19:01:48 +03:00
|
|
|
r.NoError(err)
|
|
|
|
|
2019-10-18 22:16:53 +03:00
|
|
|
disk, err := stdos.New(app.Info)
|
2019-10-18 19:01:48 +03:00
|
|
|
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)
|
|
|
|
|
2019-10-21 17:27:04 +03:00
|
|
|
var act []string
|
2019-10-18 19:01:48 +03:00
|
|
|
err = base.Walk("/", func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-21 17:27:04 +03:00
|
|
|
act = append(act, path)
|
2019-10-18 19:01:48 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
r.NoError(err)
|
2019-10-21 17:27:04 +03:00
|
|
|
r.Equal(app.Paths.Root, act)
|
2019-10-18 19:01:48 +03:00
|
|
|
|
2019-10-21 17:27:04 +03:00
|
|
|
act = []string{}
|
2019-10-18 19:01:48 +03:00
|
|
|
err = base.Walk("/public", func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-21 17:27:04 +03:00
|
|
|
act = append(act, path)
|
2019-10-18 19:01:48 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
r.NoError(err)
|
2019-10-21 17:27:04 +03:00
|
|
|
r.Equal(app.Paths.Public, act)
|
2019-10-15 23:40:45 +03:00
|
|
|
|
|
|
|
bb := &bytes.Buffer{}
|
2019-09-21 19:51:29 +03:00
|
|
|
|
2019-10-21 17:27:04 +03:00
|
|
|
err = pkgutil.Stuff(bb, app.Info, res)
|
2019-09-21 19:51:29 +03:00
|
|
|
r.NoError(err)
|
2019-10-09 20:21:54 +03:00
|
|
|
|
2019-10-21 00:52:16 +03:00
|
|
|
pkg := &mem.Pkger{}
|
|
|
|
err = pkg.UnmarshalEmbed(bb.Bytes())
|
2019-10-15 23:40:45 +03:00
|
|
|
r.NoError(err)
|
2019-09-21 19:51:29 +03:00
|
|
|
|
2019-10-21 17:27:04 +03:00
|
|
|
act = []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-21 17:27:04 +03:00
|
|
|
act = append(act, 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 22:16:53 +03:00
|
|
|
|
2019-10-21 17:27:04 +03:00
|
|
|
r.Equal(app.Paths.Public, act)
|
2019-10-18 19:01:48 +03:00
|
|
|
|
2019-10-21 17:27:04 +03:00
|
|
|
act = []string{}
|
2019-10-18 19:01:48 +03:00
|
|
|
err = pkg.Walk("/public", func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-21 17:27:04 +03:00
|
|
|
act = append(act, path)
|
2019-10-18 19:01:48 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
r.NoError(err)
|
2019-10-21 17:27:04 +03:00
|
|
|
r.Equal(app.Paths.Public, act)
|
2019-09-21 19:51:29 +03:00
|
|
|
}
|