pkger/here/dir_test.go

68 lines
1.1 KiB
Go
Raw Permalink Normal View History

2019-11-11 01:22:01 +03:00
package here_test
import (
"os"
"path/filepath"
2019-11-11 01:22:01 +03:00
"testing"
"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)
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
}{
{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) {
2019-11-11 02:38:44 +03:00
here.ClearCache()
2019-11-11 01:22:01 +03:00
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)
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
})
}
}