pkger/pkging/pkgtest/util.go

59 lines
1005 B
Go
Raw Permalink Normal View History

2019-09-03 19:02:55 +03:00
package pkgtest
import (
"io"
"path/filepath"
"strings"
"testing"
"github.com/markbates/pkger/pkging/pkgutil"
"github.com/stretchr/testify/require"
)
func (s Suite) Test_Util_ReadFile(t *testing.T) {
r := require.New(t)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
cur, err := pkg.Current()
2019-09-03 19:02:55 +03:00
r.NoError(err)
ip := cur.ImportPath
table := []struct {
in string
}{
{in: mould},
{in: ":" + mould},
{in: ip + ":" + mould},
{in: hart},
}
for _, tt := range table {
2019-09-03 23:45:32 +03:00
s.Run(t, tt.in, func(st *testing.T) {
2019-09-03 19:02:55 +03:00
r := require.New(st)
2019-09-12 04:29:39 +03:00
pkg, err := s.Make()
r.NoError(err)
pt, err := pkg.Parse(tt.in)
2019-09-03 19:02:55 +03:00
r.NoError(err)
2019-09-12 04:29:39 +03:00
r.NoError(pkg.RemoveAll(pt.String()))
r.NoError(pkg.MkdirAll(filepath.Dir(pt.Name), 0755))
2019-09-03 19:02:55 +03:00
2019-09-12 04:29:39 +03:00
f, err := pkg.Create(tt.in)
2019-09-03 19:02:55 +03:00
r.NoError(err)
body := "!" + pt.String()
_, err = io.Copy(f, strings.NewReader(body))
r.NoError(err)
r.NoError(f.Close())
2019-09-12 04:29:39 +03:00
b, err := pkgutil.ReadFile(pkg, tt.in)
2019-09-03 19:02:55 +03:00
r.NoError(err)
r.Equal(body, string(b))
})
}
}