pkger/parser/parser_test.go

94 lines
1.5 KiB
Go
Raw Normal View History

2019-10-21 17:27:04 +03:00
package parser_test
2019-08-01 19:03:12 +03:00
import (
2019-10-21 17:27:04 +03:00
"fmt"
2019-10-23 20:19:39 +03:00
"path/filepath"
2019-08-02 00:35:42 +03:00
"sort"
2019-08-01 19:03:12 +03:00
"testing"
2019-10-23 20:19:39 +03:00
"github.com/markbates/pkger/here"
2019-10-21 17:27:04 +03:00
"github.com/markbates/pkger/parser"
2019-10-18 19:01:48 +03:00
"github.com/markbates/pkger/pkging/pkgtest"
2019-08-01 19:03:12 +03:00
"github.com/stretchr/testify/require"
)
2019-10-15 23:40:45 +03:00
func Test_Parser_App(t *testing.T) {
r := require.New(t)
2019-10-18 22:16:53 +03:00
app, err := pkgtest.App()
2019-10-15 23:40:45 +03:00
r.NoError(err)
2019-10-21 17:27:04 +03:00
res, err := parser.Parse(app.Info)
2019-10-15 23:40:45 +03:00
r.NoError(err)
2019-10-21 17:27:04 +03:00
files, err := res.Files()
r.NoError(err)
act := make([]string, len(files))
for i := 0; i < len(files); i++ {
act[i] = files[i].Path.String()
2019-10-15 23:40:45 +03:00
}
sort.Strings(act)
2019-10-21 17:27:04 +03:00
for _, a := range act {
fmt.Println(a)
}
2019-10-18 22:16:53 +03:00
r.Equal(app.Paths.Parser, act)
2019-10-15 23:40:45 +03:00
}
2019-10-23 20:19:39 +03:00
func Test_Parse_Dynamic_Files(t *testing.T) {
r := require.New(t)
app, err := dynamic()
r.NoError(err)
res, err := parser.Parse(app.Info)
r.NoError(err)
files, err := res.Files()
r.NoError(err)
r.Len(files, 1)
f := files[0]
r.Equal("/go.mod", f.Path.Name)
}
// dynamic returns here.info that represents the
// ./internal/testdata/app. This should be used
// by tests.
func dynamic() (pkgtest.AppDetails, error) {
var app pkgtest.AppDetails
her, err := here.Package("github.com/markbates/pkger")
if err != nil {
return app, err
}
info := here.Info{
ImportPath: "dynamic",
}
ch := filepath.Join(
her.Dir,
"pkging",
"pkgtest",
"internal",
"testdata",
"dynamic")
info.Dir = ch
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
}