2019-11-01 23:08:10 +03:00
|
|
|
package pkgtest
|
2019-10-30 20:05:35 +03:00
|
|
|
|
|
|
|
import (
|
2019-10-31 00:15:49 +03:00
|
|
|
"fmt"
|
2019-10-31 00:32:47 +03:00
|
|
|
"os"
|
2019-10-30 23:39:05 +03:00
|
|
|
"testing"
|
2019-10-30 20:05:35 +03:00
|
|
|
|
2020-05-15 22:32:44 +03:00
|
|
|
"github.com/markbates/pkger/here"
|
2019-10-30 20:05:35 +03:00
|
|
|
"github.com/markbates/pkger/pkging"
|
2019-10-30 23:39:05 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-10-30 20:05:35 +03:00
|
|
|
)
|
|
|
|
|
2019-10-30 23:39:05 +03:00
|
|
|
type AllFn func(ref *Ref) (pkging.Pkger, error)
|
|
|
|
|
2019-10-31 18:06:20 +03:00
|
|
|
func All(t *testing.T, fn AllFn) {
|
2019-10-30 23:39:05 +03:00
|
|
|
r := require.New(t)
|
2019-10-30 23:56:22 +03:00
|
|
|
|
|
|
|
type tf func(*testing.T, *Ref, pkging.Pkger)
|
2019-10-30 23:39:05 +03:00
|
|
|
|
|
|
|
tests := map[string]tf{
|
2019-10-31 00:32:47 +03:00
|
|
|
"Create": CreateTest,
|
|
|
|
"Current": CurrentTest,
|
2019-11-01 00:20:15 +03:00
|
|
|
"HTTP": HTTPTest,
|
2019-10-31 00:32:47 +03:00
|
|
|
"Info": InfoTest,
|
|
|
|
"MkdirAll": MkdirAllTest,
|
2019-11-01 00:20:15 +03:00
|
|
|
"Open": OpenTest,
|
2019-10-31 00:32:47 +03:00
|
|
|
"Remove": RemoveTest,
|
|
|
|
"RemoveAll": RemoveAllTest,
|
2019-11-01 00:20:15 +03:00
|
|
|
"Stat": StatTest,
|
2019-10-31 00:32:47 +03:00
|
|
|
"Walk": WalkTest,
|
2019-10-30 23:39:05 +03:00
|
|
|
}
|
|
|
|
|
2019-10-31 18:06:20 +03:00
|
|
|
ref, err := NewRef()
|
|
|
|
r.NoError(err)
|
2019-11-01 00:20:15 +03:00
|
|
|
defer os.RemoveAll(ref.Dir)
|
2019-10-31 18:06:20 +03:00
|
|
|
|
2019-10-31 00:15:49 +03:00
|
|
|
pkg, err := fn(ref)
|
|
|
|
r.NoError(err)
|
|
|
|
|
2019-10-30 23:39:05 +03:00
|
|
|
for n, tt := range tests {
|
2019-10-31 00:15:49 +03:00
|
|
|
t.Run(fmt.Sprintf("%T/%s", pkg, n), func(st *testing.T) {
|
|
|
|
st.Parallel()
|
2019-10-31 18:06:20 +03:00
|
|
|
|
2019-11-01 00:20:15 +03:00
|
|
|
r := require.New(st)
|
|
|
|
|
2019-10-31 18:06:20 +03:00
|
|
|
ref, err := NewRef()
|
|
|
|
r.NoError(err)
|
2019-11-01 00:20:15 +03:00
|
|
|
defer os.RemoveAll(ref.Dir)
|
2019-10-31 18:06:20 +03:00
|
|
|
|
2019-10-30 23:39:05 +03:00
|
|
|
pkg, err := fn(ref)
|
|
|
|
r.NoError(err)
|
|
|
|
|
2019-10-30 23:56:22 +03:00
|
|
|
tt(st, ref, pkg)
|
2019-10-30 23:39:05 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-30 20:05:35 +03:00
|
|
|
}
|
2019-10-31 00:32:47 +03:00
|
|
|
|
2019-11-01 22:53:54 +03:00
|
|
|
func CmpFileInfo(t *testing.T, a os.FileInfo, b os.FileInfo) {
|
2019-10-31 00:32:47 +03:00
|
|
|
t.Helper()
|
2019-10-31 18:06:20 +03:00
|
|
|
|
2019-10-31 00:32:47 +03:00
|
|
|
r := require.New(t)
|
|
|
|
r.Equal(a.IsDir(), b.IsDir())
|
|
|
|
r.Equal(a.Name(), b.Name())
|
2019-10-31 18:06:20 +03:00
|
|
|
r.NotZero(b.ModTime())
|
2019-11-01 23:04:37 +03:00
|
|
|
|
|
|
|
if a.IsDir() {
|
|
|
|
r.True(b.Mode().IsDir(), b.Mode().String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.True(b.Mode().IsRegular(), b.Mode().String())
|
2019-10-31 18:06:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmpHereInfo(t *testing.T, a here.Info, b here.Info) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
r.NotZero(a)
|
|
|
|
r.NotZero(b)
|
|
|
|
|
|
|
|
r.Equal(a.ImportPath, b.ImportPath)
|
|
|
|
r.Equal(a.Name, b.Name)
|
|
|
|
|
|
|
|
am := a.Module
|
|
|
|
bm := b.Module
|
|
|
|
|
|
|
|
r.Equal(am.Path, bm.Path)
|
|
|
|
r.Equal(am.Main, bm.Main)
|
|
|
|
r.Equal(am.GoVersion, bm.GoVersion)
|
2019-10-31 00:32:47 +03:00
|
|
|
}
|