2019-07-31 23:29:49 +03:00
|
|
|
package pkger
|
|
|
|
|
|
|
|
import (
|
2019-08-03 00:37:27 +03:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-07-31 23:29:49 +03:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_index_Create(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
i := newIndex()
|
|
|
|
|
2019-08-02 05:34:32 +03:00
|
|
|
f, err := i.Create(Path{
|
2019-07-31 23:29:49 +03:00
|
|
|
Name: "/hello.txt",
|
|
|
|
})
|
|
|
|
r.NoError(err)
|
|
|
|
r.NotNil(f)
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
r.NoError(err)
|
|
|
|
|
2019-08-02 00:35:42 +03:00
|
|
|
r.Equal("hello.txt", fi.Name())
|
2019-07-31 23:29:49 +03:00
|
|
|
r.Equal(os.FileMode(0666), fi.Mode())
|
|
|
|
r.NotZero(fi.ModTime())
|
|
|
|
|
|
|
|
her := f.her
|
|
|
|
r.NotZero(her)
|
|
|
|
r.Equal("github.com/markbates/pkger", her.ImportPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_index_Create_Write(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
i := newIndex()
|
|
|
|
|
2019-08-02 05:34:32 +03:00
|
|
|
f, err := i.Create(Path{
|
2019-07-31 23:29:49 +03:00
|
|
|
Name: "/hello.txt",
|
|
|
|
})
|
|
|
|
r.NoError(err)
|
|
|
|
r.NotNil(f)
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
r.NoError(err)
|
|
|
|
r.Zero(fi.Size())
|
|
|
|
|
2019-08-02 00:35:42 +03:00
|
|
|
r.Equal("hello.txt", fi.Name())
|
2019-07-31 23:29:49 +03:00
|
|
|
|
|
|
|
mt := fi.ModTime()
|
|
|
|
r.NotZero(mt)
|
|
|
|
|
|
|
|
sz, err := io.Copy(f, strings.NewReader(radio))
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(int64(1381), sz)
|
|
|
|
|
|
|
|
r.NoError(f.Close())
|
|
|
|
r.Equal(int64(1381), fi.Size())
|
|
|
|
r.NotZero(fi.ModTime())
|
|
|
|
r.NotEqual(mt, fi.ModTime())
|
|
|
|
}
|
2019-08-03 00:37:27 +03:00
|
|
|
|
|
|
|
func Test_index_JSON(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
i := newIndex()
|
|
|
|
|
|
|
|
f, err := i.Create(Path{
|
|
|
|
Name: "/radio.radio",
|
|
|
|
})
|
|
|
|
r.NoError(err)
|
|
|
|
r.NotNil(f)
|
|
|
|
fmt.Fprint(f, radio)
|
|
|
|
r.NoError(f.Close())
|
|
|
|
|
2019-08-03 23:36:56 +03:00
|
|
|
c, err := i.Stat()
|
2019-08-03 00:37:27 +03:00
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(curPkg, c.ImportPath)
|
|
|
|
|
|
|
|
_, err = i.Info("github.com/markbates/hepa")
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
r.Equal(1, len(i.Files.Keys()))
|
|
|
|
r.Equal(1, len(i.Infos.Keys()))
|
2019-08-03 23:36:56 +03:00
|
|
|
r.NotZero(i.Current)
|
2019-08-03 00:37:27 +03:00
|
|
|
|
|
|
|
jason, err := json.Marshal(i)
|
|
|
|
r.NoError(err)
|
|
|
|
r.NotZero(jason)
|
|
|
|
|
|
|
|
i2 := &index{}
|
|
|
|
|
|
|
|
r.NoError(json.Unmarshal(jason, i2))
|
|
|
|
|
|
|
|
r.NotNil(i2.Infos)
|
|
|
|
r.NotNil(i2.Files)
|
2019-08-03 23:36:56 +03:00
|
|
|
r.NotZero(i2.Current)
|
2019-08-03 00:37:27 +03:00
|
|
|
r.Equal(1, len(i2.Files.Keys()))
|
|
|
|
r.Equal(1, len(i2.Infos.Keys()))
|
2019-08-03 01:14:48 +03:00
|
|
|
|
|
|
|
f2, err := i2.Open(Path{Name: "/radio.radio"})
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(f.data, f2.data)
|
2019-08-03 00:37:27 +03:00
|
|
|
}
|
2019-08-05 00:13:27 +03:00
|
|
|
|
|
|
|
func Test_index_Parse(t *testing.T) {
|
|
|
|
table := []struct {
|
|
|
|
in string
|
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{in: "", out: curPkg + ":/"},
|
|
|
|
{in: curPkg, out: curPkg + ":/"},
|
|
|
|
// {in: curPkg + "/foo.go", out: curPkg + ":/foo.go"},
|
|
|
|
// {in: "/foo.go", out: curPkg + ":/foo.go"},
|
|
|
|
{in: "github.com/markbates/pkger/internal/examples/app", out: "github.com/markbates/pkger/internal/examples/app:/"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range table {
|
|
|
|
t.Run(tt.in, func(st *testing.T) {
|
|
|
|
r := require.New(st)
|
|
|
|
pt, err := Parse(tt.in)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(tt.out, pt.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|