please don't stick me on the late shift

This commit is contained in:
Mark Bates 2019-10-30 13:05:35 -04:00
parent 4481d6120d
commit 0edd9cf5cc
4 changed files with 124 additions and 1 deletions

View File

@ -1 +1,52 @@
package costello
import (
"io"
"os"
"path/filepath"
"strings"
"github.com/markbates/pkger/pkging"
)
func LoadRef(ref *Ref, pkg pkging.Pkger) error {
return filepath.Walk(ref.Dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
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, ref.Dir)
pt, err := pkg.Parse(path)
if err != nil {
return err
}
if err := pkg.MkdirAll(filepath.Dir(pt.Name), 0755); err != nil {
return err
}
if info.IsDir() {
return nil
}
f, err := pkg.Create(pt.String())
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, of); err != nil {
return err
}
return nil
})
}

View File

@ -1 +1,45 @@
package costello
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/markbates/pkger/pkging"
"github.com/stretchr/testify/require"
)
func OpenTest(t *testing.T, pkg pkging.Pkger) {
r := require.New(t)
app, err := NewRef()
r.NoError(err)
osf, err := os.Open(filepath.Join(app.Dir, "go.mod"))
r.NoError(err)
osi, err := osf.Stat()
r.NoError(err)
osb, err := ioutil.ReadAll(osf)
r.NoError(err)
r.NoError(osf.Close())
pf, err := pkg.Open("/go.mod")
r.NoError(err)
psi, err := pf.Stat()
r.NoError(err)
psb, err := ioutil.ReadAll(pf)
r.NoError(err)
r.NoError(pf.Close())
r.Equal(osi.Name(), psi.Name())
r.Equal(osi.Mode(), psi.Mode())
r.Equal(osi.Size(), psi.Size())
r.Equal(osi.ModTime().Format(time.RFC3339), psi.ModTime().Format(time.RFC3339))
r.Equal(osb, psb)
}

View File

@ -25,7 +25,12 @@ func (fx *Pkger) Add(files ...*os.File) error {
return err
}
her, err := here.Package(pt.Pkg)
dir := f.Name()
if !info.IsDir() {
dir = filepath.Dir(dir)
}
her, err := here.Dir(dir)
if err != nil {
return err
}

23
pkging/mem/create_test.go Normal file
View File

@ -0,0 +1,23 @@
package mem
import (
"testing"
"github.com/markbates/pkger/pkging/costello"
"github.com/stretchr/testify/require"
)
func Test_Pkger_Open(t *testing.T) {
r := require.New(t)
app, err := costello.NewRef()
r.NoError(err)
pkg, err := New(app.Info)
r.NoError(err)
err = costello.LoadRef(app, pkg)
r.NoError(err)
costello.OpenTest(t, pkg)
}