pkger/pkging/mem/embed_test.go

116 lines
2.0 KiB
Go
Raw Normal View History

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"
2019-10-18 22:16:53 +03:00
"sort"
2019-09-21 19:51:29 +03:00
"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 22:16:53 +03:00
app, err := pkgtest.App()
2019-09-21 19:51:29 +03:00
r.NoError(err)
2019-10-18 22:16:53 +03:00
paths, err := parser.Parse(app.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 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)
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)
2019-10-18 22:16:53 +03:00
r.Equal(app.Paths.Root, res)
2019-10-18 19:01:48 +03:00
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)
2019-10-18 22:16:53 +03:00
r.Equal(app.Paths.Public, res)
2019-10-15 23:40:45 +03:00
bb := &bytes.Buffer{}
2019-09-21 19:51:29 +03:00
2019-10-18 22:16:53 +03:00
err = stuffing.Stuff(bb, app.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 22:16:53 +03:00
exp := append(app.Paths.Public, "app:/")
sort.Strings(exp)
r.Equal(exp, res)
2019-10-18 19:01:48 +03:00
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)
2019-10-18 22:16:53 +03:00
r.Equal(app.Paths.Public, res)
2019-09-21 19:51:29 +03:00
}