pkger/parser/parser_test.go

74 lines
1.4 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-11-01 00:20:15 +03:00
"os"
2019-10-23 20:19:39 +03:00
"path/filepath"
2019-11-01 22:53:54 +03:00
"strings"
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-11-01 23:08:10 +03:00
"github.com/markbates/pkger/pkging/pkgtest"
2019-11-01 00:20:15 +03:00
"github.com/markbates/pkger/pkging/stdos"
2019-08-01 19:03:12 +03:00
"github.com/stretchr/testify/require"
)
func Test_Parser_Ref(t *testing.T) {
r := require.New(t)
2019-11-01 23:08:10 +03:00
ref, err := pkgtest.NewRef()
r.NoError(err)
2019-11-01 00:20:15 +03:00
defer os.RemoveAll(ref.Dir)
disk, err := stdos.New(ref.Info)
r.NoError(err)
2019-11-01 23:08:10 +03:00
_, err = pkgtest.LoadFiles("/", ref, disk)
2019-11-01 00:20:15 +03:00
r.NoError(err)
res, err := parser.Parse(ref.Info)
r.NoError(err)
files, err := res.Files()
r.NoError(err)
2019-11-05 19:44:15 +03:00
r.Len(files, 23)
2019-11-01 22:53:54 +03:00
for _, f := range files {
2019-11-05 19:44:15 +03:00
if f.Path.Pkg == ref.Module.Path {
r.True(strings.HasPrefix(f.Abs, ref.Dir), "%q %q", f.Abs, ref.Dir)
} else {
r.False(strings.HasPrefix(f.Abs, ref.Dir), "%q %q", f.Abs, ref.Dir)
}
2019-11-01 22:53:54 +03:00
}
}
2019-11-01 22:53:54 +03:00
func Test_Parser_Example_HTTP(t *testing.T) {
2019-10-15 23:40:45 +03:00
r := require.New(t)
2019-11-01 22:53:54 +03:00
cur, err := here.Current()
2019-10-15 23:40:45 +03:00
r.NoError(err)
2019-11-01 22:53:54 +03:00
pwd, err := os.Getwd()
2019-10-21 17:27:04 +03:00
r.NoError(err)
2019-11-01 22:53:54 +03:00
defer os.Chdir(pwd)
2019-10-21 17:27:04 +03:00
2019-11-01 22:53:54 +03:00
root := filepath.Join(cur.Dir, "examples", "http", "pkger")
r.NoError(os.Chdir(root))
2019-10-23 20:19:39 +03:00
2019-11-01 22:53:54 +03:00
her, err := here.Dir(".")
2019-10-23 20:19:39 +03:00
r.NoError(err)
2019-11-01 22:53:54 +03:00
res, err := parser.Parse(her)
2019-10-23 20:19:39 +03:00
r.NoError(err)
files, err := res.Files()
r.NoError(err)
2019-11-01 22:53:54 +03:00
r.Len(files, 5)
2019-10-23 20:19:39 +03:00
2019-11-01 22:53:54 +03:00
for _, f := range files {
r.True(strings.HasPrefix(f.Abs, her.Dir), "%q %q", f.Abs, her.Dir)
r.True(strings.HasPrefix(f.Path.Name, "/public"), "%q %q", f.Path.Name, "/public")
2019-10-23 20:19:39 +03:00
}
}