mirror of https://github.com/markbates/pkger.git
to bring you my love
This commit is contained in:
parent
545b970a33
commit
1154513c57
26
README.md
26
README.md
|
@ -121,13 +121,12 @@ func run() error {
|
|||
```bash
|
||||
# compile the go binary as usual and run the app:
|
||||
$ go build -v; ./app
|
||||
/public | 128 | drwxr-xr-x | 2019-10-17T15:02:57-04:00 |
|
||||
/public/images | 192 | drwxr-xr-x | 2019-10-17T15:02:57-04:00 |
|
||||
/public/images/mark-small.png | 649549 | -rw-r--r-- | 2019-10-17T15:02:56-04:00 |
|
||||
/public/images/img1.png | 50401191 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 |
|
||||
/public/images/mark_250px.png | 27718 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 |
|
||||
/public/images/mark_400px.png | 63543 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 |
|
||||
/public/index.html | 257 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 |
|
||||
|
||||
public | 128 | drwxr-xr-x | 2019-10-18T16:24:55-04:00 |
|
||||
images | 128 | drwxr-xr-x | 2019-10-18T16:24:55-04:00 |
|
||||
img1.png | 27718 | -rw-r--r-- | 2019-10-18T16:24:55-04:00 |
|
||||
img2.png | 27718 | -rw-r--r-- | 2019-10-18T16:24:55-04:00 |
|
||||
index.html | 257 | -rw-r--r-- | 2019-10-18T16:24:55-04:00 |
|
||||
```
|
||||
|
||||
### Output With Packing
|
||||
|
@ -138,11 +137,10 @@ $ pkger
|
|||
|
||||
# compile the go binary as usual and run the app:
|
||||
$ go build -v; ./app
|
||||
/ | 128 | drwxr-xr-x | 2019-10-17T15:02:57-04:00 |
|
||||
/images | 192 | drwxr-xr-x | 2019-10-17T15:02:57-04:00 |
|
||||
/images/mark-small.png | 649549 | -rw-r--r-- | 2019-10-17T15:02:56-04:00 |
|
||||
/images/img1.png | 50401191 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 |
|
||||
/images/mark_250px.png | 27718 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 |
|
||||
/images/mark_400px.png | 63543 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 |
|
||||
/index.html | 257 | -rw-r--r-- | 2019-10-17T15:02:57-04:00 |
|
||||
|
||||
public | 128 | drwxr-xr-x | 2019-10-18T16:24:55-04:00 |
|
||||
images | 128 | drwxr-xr-x | 2019-10-18T16:24:55-04:00 |
|
||||
img1.png | 27718 | -rw-r--r-- | 2019-10-18T16:24:55-04:00 |
|
||||
img2.png | 27718 | -rw-r--r-- | 2019-10-18T16:24:55-04:00 |
|
||||
index.html | 257 | -rw-r--r-- | 2019-10-18T16:24:55-04:00 |
|
||||
```
|
||||
|
|
|
@ -37,6 +37,7 @@ func Test_Info_Parse(t *testing.T) {
|
|||
{in: ip2 + ":" + name, exp: here.Path{Pkg: ip2, Name: name}},
|
||||
{in: ip2, exp: here.Path{Pkg: ip2, Name: "/"}},
|
||||
{in: ip2 + ":", exp: here.Path{Pkg: ip2, Name: "/"}},
|
||||
{in: filepath.Join(app.Info.Dir, "public"), exp: here.Path{Pkg: ip, Name: "/public"}},
|
||||
}
|
||||
|
||||
for _, tt := range table {
|
||||
|
|
|
@ -66,7 +66,7 @@ func Test_Create(t *testing.T) {
|
|||
defer RemoveAll("/tmp")
|
||||
f, err := Create("/tmp/test.create")
|
||||
r.NoError(err)
|
||||
r.Equal("/tmp/test.create", f.Name())
|
||||
r.Equal("github.com/markbates/pkger:/tmp/test.create", f.Name())
|
||||
r.NoError(f.Close())
|
||||
}
|
||||
|
||||
|
@ -80,7 +80,7 @@ func Test_MkdirAll(t *testing.T) {
|
|||
|
||||
f, err := Open("/tmp")
|
||||
r.NoError(err)
|
||||
r.Equal("/tmp", f.Name())
|
||||
r.Equal("github.com/markbates/pkger:/tmp", f.Name())
|
||||
r.NoError(f.Close())
|
||||
}
|
||||
|
||||
|
@ -89,7 +89,7 @@ func Test_Stat(t *testing.T) {
|
|||
|
||||
info, err := Stat("/go.mod")
|
||||
r.NoError(err)
|
||||
r.Equal("/go.mod", info.Name())
|
||||
r.Equal("go.mod", info.Name())
|
||||
}
|
||||
|
||||
func Test_Walk(t *testing.T) {
|
||||
|
@ -115,7 +115,6 @@ func Test_Remove(t *testing.T) {
|
|||
defer RemoveAll("/tmp")
|
||||
f, err := Create("/tmp/test.create")
|
||||
r.NoError(err)
|
||||
r.Equal("/tmp/test.create", f.Name())
|
||||
r.NoError(f.Close())
|
||||
r.NoError(Remove("/tmp/test.create"))
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package pkging
|
||||
|
||||
import "os"
|
||||
|
||||
type Adder interface {
|
||||
Add(files ...File) error
|
||||
Add(files ...*os.File) error
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package pkging
|
|||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -15,6 +14,7 @@ type Details struct {
|
|||
IsDir bool `json:"is_dir"`
|
||||
Sys interface{} `json:"sys"`
|
||||
}
|
||||
|
||||
type FileInfo struct {
|
||||
Details `json:"details"`
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ var _ os.FileInfo = &FileInfo{}
|
|||
func NewFileInfo(info os.FileInfo) *FileInfo {
|
||||
fi := &FileInfo{
|
||||
Details: Details{
|
||||
Name: cleanName(info.Name()),
|
||||
Name: info.Name(),
|
||||
Size: info.Size(),
|
||||
Mode: info.Mode(),
|
||||
ModTime: ModTime(info.ModTime()),
|
||||
|
@ -63,29 +63,3 @@ func NewFileInfo(info os.FileInfo) *FileInfo {
|
|||
}
|
||||
return fi
|
||||
}
|
||||
|
||||
func WithName(name string, info os.FileInfo) *FileInfo {
|
||||
fo := NewFileInfo(info)
|
||||
fo.Details.Name = cleanName(name)
|
||||
return fo
|
||||
}
|
||||
|
||||
func WithRelName(name string, info os.FileInfo) *FileInfo {
|
||||
fo := NewFileInfo(info)
|
||||
|
||||
s := cleanName(name)
|
||||
s = strings.TrimPrefix(s, "/")
|
||||
|
||||
fo.Details.Name = s
|
||||
return fo
|
||||
}
|
||||
|
||||
func cleanName(s string) string {
|
||||
if strings.Contains(s, "\\") {
|
||||
s = strings.Replace(s, "\\", "/", -1)
|
||||
}
|
||||
if !strings.HasPrefix(s, "/") {
|
||||
s = "/" + s
|
||||
}
|
||||
return s
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package pkging_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/markbates/pkger/pkging"
|
||||
"github.com/markbates/pkger/pkging/pkgtest"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_NewFileInfo(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
app, err := pkgtest.App()
|
||||
r.NoError(err)
|
||||
|
||||
exp, err := os.Stat(filepath.Join(app.Info.Dir, "go.mod"))
|
||||
r.NoError(err)
|
||||
|
||||
act := pkging.NewFileInfo(exp)
|
||||
|
||||
r.Equal(exp.Name(), act.Name())
|
||||
r.Equal(exp.Size(), act.Size())
|
||||
r.Equal(exp.Mode(), act.Mode())
|
||||
r.Equal(exp.IsDir(), act.IsDir())
|
||||
r.Equal(exp.ModTime().Format(time.RFC3339), act.ModTime().Format(time.RFC3339))
|
||||
|
||||
}
|
|
@ -1,82 +0,0 @@
|
|||
package pkging
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_NewFileInfo(t *testing.T) {
|
||||
|
||||
in := []string{
|
||||
"/public/images/img1.png",
|
||||
"public/images/img1.png",
|
||||
"/public\\images/img1.png",
|
||||
"public/images\\img1.png",
|
||||
"\\public\\images\\img1.png",
|
||||
"public\\images\\img1.png",
|
||||
"\\public/images\\img1.png",
|
||||
"public\\images/img1.png",
|
||||
"\\public\\images\\img1.png",
|
||||
}
|
||||
|
||||
const exp = "/public/images/img1.png"
|
||||
for _, n := range in {
|
||||
t.Run(n, func(st *testing.T) {
|
||||
r := require.New(st)
|
||||
|
||||
f1 := &FileInfo{
|
||||
Details: Details{
|
||||
Name: n,
|
||||
Size: 42,
|
||||
Mode: os.FileMode(0644),
|
||||
IsDir: true,
|
||||
},
|
||||
}
|
||||
|
||||
f2 := NewFileInfo(f1)
|
||||
|
||||
r.Equal(exp, f2.Name())
|
||||
r.Equal(f1.Size(), f2.Size())
|
||||
r.Equal(f1.Mode(), f2.Mode())
|
||||
r.Equal(f1.IsDir(), f2.IsDir())
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_WithName(t *testing.T) {
|
||||
|
||||
f1 := &FileInfo{
|
||||
Details: Details{
|
||||
Name: "/walls/crumbling",
|
||||
Size: 42,
|
||||
Mode: os.FileMode(0644),
|
||||
IsDir: true,
|
||||
},
|
||||
}
|
||||
|
||||
const exp = "/public/images/img1.png"
|
||||
in := []string{
|
||||
"/public/images/img1.png",
|
||||
"public/images/img1.png",
|
||||
"/public\\images/img1.png",
|
||||
"public/images\\img1.png",
|
||||
"\\public\\images\\img1.png",
|
||||
"public\\images\\img1.png",
|
||||
"\\public/images\\img1.png",
|
||||
"public\\images/img1.png",
|
||||
"\\public\\images\\img1.png",
|
||||
}
|
||||
|
||||
for _, n := range in {
|
||||
t.Run(n, func(st *testing.T) {
|
||||
r := require.New(st)
|
||||
|
||||
f2 := WithName(n, f1)
|
||||
|
||||
r.Equal(exp, f2.Name())
|
||||
})
|
||||
}
|
||||
}
|
|
@ -3,32 +3,84 @@ package mem
|
|||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/pkging"
|
||||
)
|
||||
|
||||
var _ pkging.Adder = &Pkger{}
|
||||
|
||||
// Add copies the pkging.File into the *Pkger
|
||||
func (fx *Pkger) Add(files ...pkging.File) error {
|
||||
func (fx *Pkger) Add(files ...*os.File) error {
|
||||
for _, f := range files {
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if f.Path().Pkg == fx.Here.ImportPath {
|
||||
dir := filepath.Dir(f.Name())
|
||||
if dir != "/" {
|
||||
if err := fx.MkdirAll(dir, 0755); err != nil {
|
||||
pt, err := fx.Parse(f.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
her, err := here.Package(pt.Pkg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
err = filepath.Walk(f.Name(), func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
pt, err := fx.Parse(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
her, err := here.Package(pt.Pkg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mf := &File{
|
||||
Here: her,
|
||||
info: pkging.NewFileInfo(info),
|
||||
path: pt,
|
||||
pkging: fx,
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
bb := &bytes.Buffer{}
|
||||
_, err = io.Copy(bb, f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mf.data = bb.Bytes()
|
||||
}
|
||||
|
||||
fx.files.Store(mf.Path(), mf)
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
mf := &File{
|
||||
Here: f.Info(),
|
||||
Here: her,
|
||||
info: pkging.NewFileInfo(info),
|
||||
path: f.Path(),
|
||||
path: pt,
|
||||
pkging: fx,
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
package mem_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/pkger/pkging/mem"
|
||||
"github.com/markbates/pkger/pkging/pkgtest"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_Pkger_Add(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
app, err := pkgtest.App()
|
||||
r.NoError(err)
|
||||
|
||||
pkg, err := mem.New(app.Info)
|
||||
r.NoError(err)
|
||||
|
||||
root := app.Info.Dir
|
||||
|
||||
var exp []os.FileInfo
|
||||
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
exp = append(exp, info)
|
||||
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
return pkg.Add(f)
|
||||
|
||||
})
|
||||
|
||||
r.NoError(err)
|
||||
|
||||
sort.Slice(exp, func(i, j int) bool {
|
||||
return exp[i].Name() < exp[j].Name()
|
||||
})
|
||||
|
||||
var act []os.FileInfo
|
||||
err = pkg.Walk("/", func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
act = append(act, info)
|
||||
return nil
|
||||
})
|
||||
r.NoError(err)
|
||||
|
||||
sort.Slice(act, func(i, j int) bool {
|
||||
return act[i].Name() < act[j].Name()
|
||||
})
|
||||
|
||||
r.Len(act, len(exp))
|
||||
|
||||
for i, e := range exp {
|
||||
a := act[i]
|
||||
|
||||
r.Equal(e.Name(), a.Name())
|
||||
r.Equal(e.Size(), a.Size())
|
||||
r.Equal(e.Mode(), a.Mode())
|
||||
r.Equal(e.IsDir(), a.IsDir())
|
||||
}
|
||||
|
||||
}
|
|
@ -3,13 +3,13 @@ package mem_test
|
|||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/pkger/parser"
|
||||
"github.com/markbates/pkger/pkging/mem"
|
||||
"github.com/markbates/pkger/pkging/pkgtest"
|
||||
"github.com/markbates/pkger/pkging/pkgutil"
|
||||
"github.com/markbates/pkger/pkging/stdos"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
|
@ -35,22 +35,16 @@ func Test_Pkger_Embedding(t *testing.T) {
|
|||
base, err := mem.New(app.Info)
|
||||
r.NoError(err)
|
||||
|
||||
disk, err := stdos.New(app.Info)
|
||||
r.NoError(err)
|
||||
|
||||
err = disk.Walk("/", func(path string, info os.FileInfo, err error) error {
|
||||
err = filepath.Walk(app.Info.Dir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
f, err := disk.Open(path)
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
return base.Add(f)
|
||||
|
||||
})
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/markbates/pkger/here"
|
||||
|
@ -100,9 +99,9 @@ func (f File) Stat() (os.FileInfo, error) {
|
|||
return f.info, nil
|
||||
}
|
||||
|
||||
// Name retuns the name of the file
|
||||
// Name retuns the name of the file in pkger format
|
||||
func (f File) Name() string {
|
||||
return f.info.Name()
|
||||
return f.path.String()
|
||||
}
|
||||
|
||||
// Abs returns an absolute representation of the file. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.
|
||||
|
@ -148,7 +147,6 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
info = pkging.WithRelName(strings.TrimPrefix(info.Name(), f.parent.Name), info)
|
||||
infos = append(infos, info)
|
||||
if info.IsDir() && path != root {
|
||||
return filepath.SkipDir
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -193,7 +194,7 @@ func (fx *Pkger) Create(name string) (pkging.File, error) {
|
|||
path: pt,
|
||||
info: &pkging.FileInfo{
|
||||
Details: pkging.Details{
|
||||
Name: pt.Name,
|
||||
Name: filepath.Base(name),
|
||||
Mode: 0644,
|
||||
ModTime: pkging.ModTime(time.Now()),
|
||||
},
|
||||
|
@ -208,53 +209,39 @@ func (fx *Pkger) Create(name string) (pkging.File, error) {
|
|||
|
||||
// MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm (before umask) are used for all directories that MkdirAll creates. If path is already a directory, MkdirAll does nothing and returns nil.
|
||||
func (fx *Pkger) MkdirAll(p string, perm os.FileMode) error {
|
||||
path, err := fx.Parse(p)
|
||||
pt, err := fx.Parse(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
root := path.Name
|
||||
dir, name := path.Split(pt.Name)
|
||||
|
||||
cur, err := fx.Current()
|
||||
if err != nil {
|
||||
return err
|
||||
if dir != "/" {
|
||||
if err := fx.MkdirAll(dir, perm); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for root != "" {
|
||||
pt := here.Path{
|
||||
Pkg: path.Pkg,
|
||||
Name: root,
|
||||
}
|
||||
if _, ok := fx.files.Load(pt); ok {
|
||||
root = filepath.Dir(root)
|
||||
if root == "/" || root == "\\" {
|
||||
break
|
||||
}
|
||||
continue
|
||||
}
|
||||
f := &File{
|
||||
Here: cur,
|
||||
pkging: fx,
|
||||
path: pt,
|
||||
info: &pkging.FileInfo{
|
||||
Details: pkging.Details{
|
||||
Name: pt.Name,
|
||||
Mode: perm,
|
||||
ModTime: pkging.ModTime(time.Now()),
|
||||
},
|
||||
|
||||
if dir == "/" && name == "" {
|
||||
dir = filepath.Base(fx.Here.Dir)
|
||||
}
|
||||
|
||||
f := &File{
|
||||
Here: fx.Here,
|
||||
pkging: fx,
|
||||
path: pt,
|
||||
info: &pkging.FileInfo{
|
||||
Details: pkging.Details{
|
||||
IsDir: true,
|
||||
Name: name,
|
||||
Mode: perm,
|
||||
ModTime: pkging.ModTime(time.Now()),
|
||||
},
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.info.Details.IsDir = true
|
||||
f.info.Details.Mode = perm
|
||||
if err := f.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
fx.files.Store(pt, f)
|
||||
root = filepath.Dir(root)
|
||||
},
|
||||
}
|
||||
|
||||
if err := f.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
fx.files.Store(pt, f)
|
||||
return nil
|
||||
|
||||
}
|
||||
|
@ -280,7 +267,7 @@ func (fx *Pkger) Open(name string) (pkging.File, error) {
|
|||
}
|
||||
nf := &File{
|
||||
pkging: fx,
|
||||
info: pkging.WithName(f.info.Name(), f.info),
|
||||
info: pkging.NewFileInfo(f.info),
|
||||
path: f.path,
|
||||
data: f.data,
|
||||
Here: f.Here,
|
||||
|
@ -333,7 +320,7 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
|
|||
return err
|
||||
}
|
||||
|
||||
fi = pkging.WithName(strings.TrimPrefix(k.Name, pt.Name), fi)
|
||||
fi = pkging.NewFileInfo(fi)
|
||||
|
||||
err = wf(k.String(), fi, nil)
|
||||
if err == filepath.SkipDir {
|
||||
|
|
|
@ -1,54 +1,12 @@
|
|||
package pkgtest
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/pkger/pkging/pkgutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func (s Suite) Test_File_Info(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
pkg, err := s.Make()
|
||||
r.NoError(err)
|
||||
|
||||
cur, err := pkg.Current()
|
||||
r.NoError(err)
|
||||
|
||||
ip := cur.ImportPath
|
||||
table := []struct {
|
||||
in string
|
||||
}{
|
||||
{in: mould},
|
||||
{in: ":" + mould},
|
||||
{in: ip + ":" + mould},
|
||||
}
|
||||
|
||||
for _, tt := range table {
|
||||
s.Run(t, tt.in, func(st *testing.T) {
|
||||
r := require.New(st)
|
||||
|
||||
r.NoError(pkg.RemoveAll(mould))
|
||||
r.NoError(pkg.MkdirAll(filepath.Dir(tt.in), 0755))
|
||||
err := pkgutil.WriteFile(pkg, tt.in, []byte(mould), 0644)
|
||||
r.NoError(err)
|
||||
|
||||
f, err := pkg.Open(tt.in)
|
||||
r.NoError(err)
|
||||
r.Equal(mould, f.Name())
|
||||
r.Equal(cur.ImportPath, f.Info().ImportPath)
|
||||
r.NoError(f.Close())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// func (s Suite) Test_File_Read(t *testing.T) {
|
||||
// panic("not implemented")
|
||||
// }
|
||||
//
|
||||
func (s Suite) Test_File_Readdir(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
|
@ -104,16 +62,3 @@ func (s Suite) Test_File_Readdir(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// func (s Suite) Test_File_Seek(t *testing.T) {
|
||||
// panic("not implemented")
|
||||
// }
|
||||
//
|
||||
// func (s Suite) Test_File_Stat(t *testing.T) {
|
||||
// panic("not implemented")
|
||||
// }
|
||||
//
|
||||
// func (s Suite) Test_File_Write(t *testing.T) {
|
||||
// panic("not implemented")
|
||||
// }
|
||||
|
|
|
@ -64,16 +64,16 @@ func (s Suite) LoadFolder(pkg pkging.Pkger) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
of, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer of.Close()
|
||||
|
||||
if a, ok := pkg.(pkging.Adder); ok {
|
||||
return a.Add(of)
|
||||
}
|
||||
|
||||
path = strings.TrimPrefix(path, app.Dir)
|
||||
|
||||
pt, err := pkg.Parse(path)
|
||||
|
@ -85,6 +85,9 @@ func (s Suite) LoadFolder(pkg pkging.Pkger) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
f, err := pkg.Create(pt.String())
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -2,7 +2,6 @@ package pkgtest
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -102,13 +101,13 @@ func (s Suite) Test_Create(t *testing.T) {
|
|||
|
||||
f, err := pkg.Create(pt.Name)
|
||||
r.NoError(err)
|
||||
r.Equal(pt.Name, f.Name())
|
||||
r.Equal(pt.String(), f.Name())
|
||||
|
||||
fi, err := f.Stat()
|
||||
r.NoError(err)
|
||||
r.NoError(f.Close())
|
||||
|
||||
r.Equal(pt.Name, fi.Name())
|
||||
r.Equal(filepath.Base(pt.Name), fi.Name())
|
||||
r.NotZero(fi.ModTime())
|
||||
r.NoError(pkg.RemoveAll(pt.String()))
|
||||
})
|
||||
|
@ -218,7 +217,7 @@ func (s Suite) Test_MkdirAll(t *testing.T) {
|
|||
if runtime.GOOS == "windows" {
|
||||
dir = strings.Replace(dir, "\\", "/", -1)
|
||||
}
|
||||
r.Equal(dir, fi.Name())
|
||||
r.Equal(filepath.Base(dir), fi.Name())
|
||||
r.NotZero(fi.ModTime())
|
||||
r.NoError(pkg.RemoveAll(pt.String()))
|
||||
})
|
||||
|
@ -354,87 +353,51 @@ func (s Suite) Test_Stat_Error(t *testing.T) {
|
|||
func (s Suite) Test_Stat_Dir(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
app, err := App()
|
||||
r.NoError(err)
|
||||
|
||||
exp, err := os.Stat(filepath.Join(app.Info.Dir, "public"))
|
||||
r.NoError(err)
|
||||
|
||||
pkg, err := s.Make()
|
||||
r.NoError(err)
|
||||
|
||||
cur, err := pkg.Current()
|
||||
r.NoError(s.LoadFolder(pkg))
|
||||
|
||||
act, err := pkg.Stat("/public")
|
||||
r.NoError(err)
|
||||
|
||||
dir := filepath.Dir(mould)
|
||||
ip := cur.ImportPath
|
||||
r.Equal(exp.Name(), act.Name())
|
||||
r.Equal(exp.Size(), act.Size())
|
||||
r.Equal(exp.Mode(), act.Mode())
|
||||
r.Equal(exp.IsDir(), act.IsDir())
|
||||
r.NotZero(act.ModTime())
|
||||
|
||||
table := []struct {
|
||||
in string
|
||||
}{
|
||||
{in: ip},
|
||||
{in: dir},
|
||||
{in: ":" + dir},
|
||||
{in: ip + ":" + dir},
|
||||
}
|
||||
|
||||
for _, tt := range table {
|
||||
s.Run(t, tt.in, func(st *testing.T) {
|
||||
|
||||
r := require.New(st)
|
||||
|
||||
pt, err := pkg.Parse(tt.in)
|
||||
r.NoError(err)
|
||||
|
||||
r.NoError(pkg.RemoveAll(pt.String()))
|
||||
|
||||
r.NoError(pkg.MkdirAll(pt.Name, 0755))
|
||||
info, err := pkg.Stat(tt.in)
|
||||
r.NoError(err)
|
||||
r.Equal(pt.Name, info.Name())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s Suite) Test_Stat_File(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
app, err := App()
|
||||
r.NoError(err)
|
||||
|
||||
exp, err := os.Stat(filepath.Join(app.Info.Dir, "go.mod"))
|
||||
r.NoError(err)
|
||||
|
||||
pkg, err := s.Make()
|
||||
r.NoError(err)
|
||||
|
||||
cur, err := pkg.Current()
|
||||
r.NoError(s.LoadFolder(pkg))
|
||||
|
||||
act, err := pkg.Stat("/go.mod")
|
||||
r.NoError(err)
|
||||
|
||||
ip := cur.ImportPath
|
||||
table := []struct {
|
||||
in string
|
||||
}{
|
||||
{in: mould},
|
||||
{in: ":" + mould},
|
||||
{in: ip + ":" + mould},
|
||||
{in: hart},
|
||||
}
|
||||
r.Equal(exp.Name(), act.Name())
|
||||
r.Equal(exp.Size(), act.Size())
|
||||
r.Equal(exp.Mode(), act.Mode())
|
||||
r.Equal(exp.IsDir(), act.IsDir())
|
||||
r.NotZero(act.ModTime())
|
||||
|
||||
for _, tt := range table {
|
||||
s.Run(t, tt.in, func(st *testing.T) {
|
||||
|
||||
r := require.New(st)
|
||||
|
||||
pkg, err := s.Make()
|
||||
r.NoError(err)
|
||||
|
||||
pt, err := pkg.Parse(tt.in)
|
||||
r.NoError(err)
|
||||
|
||||
r.NoError(pkg.RemoveAll(pt.String()))
|
||||
r.NoError(pkg.MkdirAll(filepath.Dir(pt.Name), 0755))
|
||||
|
||||
f, err := pkg.Create(tt.in)
|
||||
r.NoError(err)
|
||||
|
||||
_, err = io.Copy(f, strings.NewReader("!"+pt.String()))
|
||||
r.NoError(err)
|
||||
r.NoError(f.Close())
|
||||
|
||||
info, err := pkg.Stat(tt.in)
|
||||
r.NoError(err)
|
||||
r.Equal(pt.Name, info.Name())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s Suite) Test_Walk(t *testing.T) {
|
||||
|
|
|
@ -7,15 +7,9 @@ import (
|
|||
"github.com/markbates/pkger/here"
|
||||
"github.com/markbates/pkger/parser"
|
||||
"github.com/markbates/pkger/pkging/mem"
|
||||
"github.com/markbates/pkger/pkging/stdos"
|
||||
)
|
||||
|
||||
func Stuff(w io.Writer, c here.Info, decls parser.Decls) error {
|
||||
disk, err := stdos.New(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pkg, err := mem.New(c)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -28,47 +22,16 @@ func Stuff(w io.Writer, c here.Info, decls parser.Decls) error {
|
|||
|
||||
for _, pf := range files {
|
||||
err = func() error {
|
||||
df, err := disk.Open(pf.Path.String())
|
||||
df, err := os.Open(pf.Abs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer df.Close()
|
||||
|
||||
info, err := df.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := pkg.Add(df); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
err = disk.Walk(df.Path().String(), func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
f, err := disk.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if err := pkg.Add(f); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return err
|
||||
}()
|
||||
|
||||
|
|
|
@ -34,9 +34,9 @@ func (f *File) Info() here.Info {
|
|||
return f.her
|
||||
}
|
||||
|
||||
// Name retuns the name of the file
|
||||
func (f *File) Name() string {
|
||||
return f.info.Name()
|
||||
// Name retuns the name of the file in pkger format
|
||||
func (f File) Name() string {
|
||||
return f.path.String()
|
||||
}
|
||||
|
||||
// Readdir reads the contents of the directory associated with file and returns a slice of up to n FileInfo values, as would be returned by Lstat, in directory order. Subsequent calls on the same file will yield further FileInfos.
|
||||
|
@ -45,16 +45,7 @@ func (f *File) Name() string {
|
|||
//
|
||||
// If n <= 0, Readdir returns all the FileInfo from the directory in a single slice. In this case, if Readdir succeeds (reads all the way to the end of the directory), it returns the slice and a nil error. If it encounters an error before the end of the directory, Readdir returns the FileInfo read until that point and a non-nil error.
|
||||
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
|
||||
osinfos, err := f.File.Readdir(count)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
infos := make([]os.FileInfo, len(osinfos))
|
||||
for i, info := range osinfos {
|
||||
infos[i] = pkging.WithRelName(info.Name(), info)
|
||||
}
|
||||
return infos, err
|
||||
return f.File.Readdir(count)
|
||||
}
|
||||
|
||||
// Open implements the http.FileSystem interface. A FileSystem implements access to a collection of named files. The elements in a file path are separated by slash ('/', U+002F) characters, regardless of host operating system convention.
|
||||
|
@ -82,6 +73,6 @@ func (f *File) Stat() (os.FileInfo, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.info = pkging.WithName(f.path.Name, info)
|
||||
f.info = pkging.NewFileInfo(info)
|
||||
return f.info, nil
|
||||
}
|
||||
|
|
|
@ -80,7 +80,7 @@ func (fx *Pkger) Create(name string) (pkging.File, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nf.info = pkging.WithName(pt.Name, info)
|
||||
nf.info = pkging.NewFileInfo(info)
|
||||
return nf, nil
|
||||
}
|
||||
|
||||
|
@ -145,7 +145,7 @@ func (fx *Pkger) Open(name string) (pkging.File, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nf.info = pkging.WithName(pt.Name, info)
|
||||
nf.info = pkging.NewFileInfo(info)
|
||||
|
||||
return nf, nil
|
||||
}
|
||||
|
@ -172,7 +172,7 @@ func (f *Pkger) Stat(name string) (os.FileInfo, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
info = pkging.WithName(pt.Name, pkging.NewFileInfo(info))
|
||||
info = pkging.NewFileInfo(info)
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
@ -203,7 +203,7 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
|
|||
path = strings.TrimPrefix(path, info.Dir)
|
||||
path = strings.ReplaceAll(path, "\\", "/")
|
||||
pt.Name = path
|
||||
return wf(pt.String(), pkging.WithName(path, pkging.NewFileInfo(fi)), nil)
|
||||
return wf(pt.String(), pkging.NewFileInfo(fi), nil)
|
||||
})
|
||||
|
||||
return err
|
||||
|
|
Loading…
Reference in New Issue