pkger/pkger_test.go

132 lines
2.6 KiB
Go
Raw Normal View History

2019-10-21 23:23:18 +03:00
package pkger_test
2019-09-22 01:00:53 +03:00
import (
2019-10-21 23:23:18 +03:00
"fmt"
2019-09-22 01:00:53 +03:00
"os"
"path/filepath"
"testing"
2019-10-21 23:23:18 +03:00
"github.com/markbates/pkger"
2019-10-09 20:21:54 +03:00
"github.com/markbates/pkger/here"
2019-10-21 23:23:18 +03:00
"github.com/markbates/pkger/pkging/pkgtest"
2019-09-22 01:00:53 +03:00
"github.com/stretchr/testify/require"
)
func Test_Parse(t *testing.T) {
r := require.New(t)
2019-10-21 23:23:18 +03:00
app, err := pkgtest.App()
2019-09-22 01:00:53 +03:00
r.NoError(err)
2019-10-21 23:23:18 +03:00
pt, err := pkger.Parse(fmt.Sprintf("%s:/public/index.html", app.Info.ImportPath))
r.NoError(err)
r.Equal(app.Info.ImportPath, pt.Pkg)
r.Equal("/public/index.html", pt.Name)
2019-09-22 01:00:53 +03:00
}
func Test_Abs(t *testing.T) {
r := require.New(t)
2019-10-21 23:23:18 +03:00
s, err := pkger.Abs("/rocket.ship")
2019-09-22 01:00:53 +03:00
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-21 23:23:18 +03:00
s, err := pkger.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)
2019-10-21 23:23:18 +03:00
info, err := pkger.Current()
2019-09-22 19:29:35 +03:00
r.NoError(err)
r.Equal("github.com/markbates/pkger", info.ImportPath)
}
func Test_Info(t *testing.T) {
r := require.New(t)
2019-10-21 23:23:18 +03:00
info, err := pkger.Info("github.com/markbates/pkger")
2019-09-22 19:29:35 +03:00
r.NoError(err)
r.Equal("github.com/markbates/pkger", info.ImportPath)
}
func Test_Create(t *testing.T) {
r := require.New(t)
2019-10-21 23:23:18 +03:00
pkger.MkdirAll("/tmp", 0755)
defer pkger.RemoveAll("/tmp")
f, err := pkger.Create("/tmp/test.create")
2019-09-22 19:29:35 +03:00
r.NoError(err)
r.Equal("/tmp/test.create", f.Name())
r.NoError(f.Close())
}
func Test_MkdirAll(t *testing.T) {
r := require.New(t)
2019-10-21 23:23:18 +03:00
_, err := pkger.Open("/tmp")
2019-09-22 19:29:35 +03:00
r.Error(err)
2019-10-21 23:23:18 +03:00
r.NoError(pkger.MkdirAll("/tmp", 0755))
defer pkger.RemoveAll("/tmp")
2019-09-22 19:29:35 +03:00
2019-10-21 23:23:18 +03:00
f, err := pkger.Open("/tmp")
2019-09-22 19:29:35 +03:00
r.NoError(err)
r.Equal("/tmp", f.Name())
r.NoError(f.Close())
}
func Test_Stat(t *testing.T) {
r := require.New(t)
2019-10-21 23:23:18 +03:00
info, err := pkger.Stat("/go.mod")
2019-09-22 19:29:35 +03:00
r.NoError(err)
r.Equal("/go.mod", info.Name())
}
func Test_Walk(t *testing.T) {
r := require.New(t)
files := map[string]os.FileInfo{}
2019-10-21 23:23:18 +03:00
err := pkger.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)
2019-10-21 23:23:18 +03:00
pkger.MkdirAll("/tmp", 0755)
defer pkger.RemoveAll("/tmp")
f, err := pkger.Create("/tmp/test.create")
2019-09-22 19:29:35 +03:00
r.NoError(err)
r.Equal("/tmp/test.create", f.Name())
r.NoError(f.Close())
2019-10-21 23:23:18 +03:00
r.NoError(pkger.Remove("/tmp/test.create"))
2019-09-22 19:29:35 +03:00
2019-10-21 23:23:18 +03:00
_, err = pkger.Stat("/tmp/test.create")
2019-09-22 19:29:35 +03:00
r.Error(err)
}