pkger/pkger_test.go

124 lines
2.3 KiB
Go
Raw Normal View History

2019-09-22 01:00:53 +03:00
package pkger
import (
"os"
"path/filepath"
"testing"
2019-10-09 20:21:54 +03:00
"github.com/markbates/pkger/here"
2019-09-22 01:00:53 +03:00
"github.com/stretchr/testify/require"
)
func Test_Parse(t *testing.T) {
r := require.New(t)
pt, err := Parse("github.com/rocket/ship:/little")
r.NoError(err)
r.Equal("github.com/rocket/ship", pt.Pkg)
r.Equal("/little", pt.Name)
}
func Test_Abs(t *testing.T) {
r := require.New(t)
s, err := Abs(":/rocket.ship")
r.NoError(err)
pwd, err := os.Getwd()
r.NoError(err)
r.Equal(filepath.Join(pwd, "rocket.ship"), s)
}
func Test_AbsPath(t *testing.T) {
r := require.New(t)
2019-10-09 20:21:54 +03:00
s, err := AbsPath(here.Path{
2019-09-22 01:00:53 +03:00
Pkg: "github.com/markbates/pkger",
Name: "/rocket.ship",
})
r.NoError(err)
pwd, err := os.Getwd()
r.NoError(err)
r.Equal(filepath.Join(pwd, "rocket.ship"), s)
}
2019-09-22 19:29:35 +03:00
func Test_Current(t *testing.T) {
r := require.New(t)
info, err := Current()
r.NoError(err)
r.Equal("github.com/markbates/pkger", info.ImportPath)
}
func Test_Info(t *testing.T) {
r := require.New(t)
info, err := Info("github.com/markbates/pkger")
r.NoError(err)
r.Equal("github.com/markbates/pkger", info.ImportPath)
}
func Test_Create(t *testing.T) {
r := require.New(t)
MkdirAll("/tmp", 0755)
defer RemoveAll("/tmp")
f, err := Create("/tmp/test.create")
r.NoError(err)
2019-10-24 23:22:15 +03:00
r.Equal("github.com/markbates/pkger:/tmp/test.create", f.Name())
2019-09-22 19:29:35 +03:00
r.NoError(f.Close())
}
func Test_MkdirAll(t *testing.T) {
r := require.New(t)
_, err := Open("/tmp")
r.Error(err)
r.NoError(MkdirAll("/tmp", 0755))
defer RemoveAll("/tmp")
f, err := Open("/tmp")
r.NoError(err)
2019-10-24 23:22:15 +03:00
r.Equal("github.com/markbates/pkger:/tmp", f.Name())
2019-09-22 19:29:35 +03:00
r.NoError(f.Close())
}
func Test_Stat(t *testing.T) {
r := require.New(t)
info, err := Stat("/go.mod")
r.NoError(err)
2019-10-24 23:22:15 +03:00
r.Equal("go.mod", info.Name())
2019-09-22 19:29:35 +03:00
}
func Test_Walk(t *testing.T) {
r := require.New(t)
files := map[string]os.FileInfo{}
2019-10-18 19:01:48 +03:00
err := Walk("/pkging/pkgtest/internal/testdata/app", func(path string, info os.FileInfo, err error) error {
2019-09-22 19:29:35 +03:00
if err != nil {
return err
}
files[path] = info
return nil
})
r.NoError(err)
2019-09-22 19:56:22 +03:00
r.True(len(files) > 10)
2019-09-22 19:29:35 +03:00
}
func Test_Remove(t *testing.T) {
r := require.New(t)
MkdirAll("/tmp", 0755)
defer RemoveAll("/tmp")
f, err := Create("/tmp/test.create")
r.NoError(err)
r.NoError(f.Close())
r.NoError(Remove("/tmp/test.create"))
_, err = Stat("/tmp/test.create")
r.Error(err)
}