2019-11-11 01:22:01 +03:00
|
|
|
package here_test
|
|
|
|
|
|
|
|
import (
|
2019-11-11 02:22:06 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-11-11 01:22:01 +03:00
|
|
|
"testing"
|
|
|
|
|
2019-11-11 02:22:06 +03:00
|
|
|
"github.com/markbates/pkger/here"
|
2019-11-11 01:22:01 +03:00
|
|
|
"github.com/markbates/pkger/pkging/pkgtest"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Dir(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
ref, err := pkgtest.NewRef()
|
|
|
|
r.NoError(err)
|
|
|
|
|
2019-11-11 02:22:06 +03:00
|
|
|
root := ref.Dir
|
|
|
|
|
|
|
|
r.NoError(err)
|
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
|
|
|
public := filepath.Join(root, "public")
|
|
|
|
r.NoError(os.MkdirAll(public, 0755))
|
|
|
|
|
|
|
|
gf := filepath.Join(root, "cmd", "main.go")
|
|
|
|
r.NoError(os.MkdirAll(filepath.Dir(gf), 0755))
|
|
|
|
|
|
|
|
f, err := os.Create(gf)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
_, err = f.Write([]byte("package main"))
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
r.NoError(f.Close())
|
|
|
|
|
2019-11-11 01:22:01 +03:00
|
|
|
table := []struct {
|
|
|
|
in string
|
|
|
|
err bool
|
|
|
|
}{
|
2019-11-11 02:22:06 +03:00
|
|
|
{in: root, err: false},
|
|
|
|
{in: public, err: false},
|
|
|
|
{in: gf, err: false},
|
|
|
|
{in: filepath.Join(root, "."), err: false},
|
|
|
|
{in: "/unknown", err: true},
|
2019-11-11 01:22:01 +03:00
|
|
|
}
|
|
|
|
for _, tt := range table {
|
|
|
|
t.Run(tt.in, func(st *testing.T) {
|
|
|
|
r := require.New(st)
|
|
|
|
|
2019-11-11 02:27:57 +03:00
|
|
|
info, err := here.Dir(tt.in)
|
2019-11-11 01:22:01 +03:00
|
|
|
if tt.err {
|
|
|
|
r.Error(err)
|
2019-11-11 02:22:06 +03:00
|
|
|
return
|
2019-11-11 01:22:01 +03:00
|
|
|
}
|
|
|
|
r.NoError(err)
|
|
|
|
|
2019-11-11 02:27:57 +03:00
|
|
|
r.NotZero(info)
|
|
|
|
r.NotZero(info.Dir)
|
|
|
|
r.NotZero(info.Name)
|
|
|
|
r.NotZero(info.Module)
|
|
|
|
|
2019-11-11 01:22:01 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|