better tests

This commit is contained in:
Mark Bates 2019-10-31 17:20:15 -04:00
parent d41fe52ef2
commit cc6861871c
37 changed files with 404 additions and 141 deletions

View File

@ -1,6 +1,7 @@
package here_test
import (
"os"
"path/filepath"
"strings"
"testing"
@ -17,6 +18,7 @@ func Test_Info_Parse(t *testing.T) {
app, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(app.Dir)
ip := app.Info.ImportPath
ip2 := "another/app"

View File

@ -2,6 +2,7 @@ package parser_test
import (
"fmt"
"os"
"path/filepath"
"sort"
"testing"
@ -10,6 +11,7 @@ import (
"github.com/markbates/pkger/parser"
"github.com/markbates/pkger/pkging/costello"
"github.com/markbates/pkger/pkging/pkgtest"
"github.com/markbates/pkger/pkging/stdos"
"github.com/stretchr/testify/require"
)
@ -18,6 +20,13 @@ func Test_Parser_Ref(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
disk, err := stdos.New(ref.Info)
r.NoError(err)
_, err = costello.LoadFiles("/", ref, disk)
r.NoError(err)
res, err := parser.Parse(ref.Info)

View File

@ -39,7 +39,7 @@ func impl() pkging.Pkger {
type Dir string
func (d Dir) Open(name string) (http.File, error) {
f, err := Open(string(d))
f, err := impl().Open(string(d))
if err != nil {
return nil, err
}

View File

@ -69,7 +69,7 @@ func Test_Walk(t *testing.T) {
r := require.New(t)
files := map[string]os.FileInfo{}
err := Walk("/pkging/pkgtest/internal/testdata/app", func(path string, info os.FileInfo, err error) error {
err := Walk("/pkging/costello/testdata/ref", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

View File

@ -18,19 +18,21 @@ func All(t *testing.T, fn AllFn) {
type tf func(*testing.T, *Ref, pkging.Pkger)
tests := map[string]tf{
"Open": OpenTest,
"Stat": StatTest,
"Create": CreateTest,
"Current": CurrentTest,
"HTTP": HTTPTest,
"Info": InfoTest,
"MkdirAll": MkdirAllTest,
"Open": OpenTest,
"Remove": RemoveTest,
"RemoveAll": RemoveAllTest,
"Stat": StatTest,
"Walk": WalkTest,
}
ref, err := NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := fn(ref)
r.NoError(err)
@ -39,8 +41,11 @@ func All(t *testing.T, fn AllFn) {
t.Run(fmt.Sprintf("%T/%s", pkg, n), func(st *testing.T) {
st.Parallel()
r := require.New(st)
ref, err := NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := fn(ref)
r.NoError(err)
@ -59,7 +64,7 @@ func cmpFileInfo(t *testing.T, a os.FileInfo, b os.FileInfo) {
r.Equal(a.IsDir(), b.IsDir())
r.Equal(a.Mode().String(), b.Mode().String())
r.Equal(a.Name(), b.Name())
r.Equal(a.Size(), b.Size())
// r.Equal(a.Size(), b.Size())
r.NotZero(b.ModTime())
}

View File

@ -7,10 +7,10 @@ import (
)
func CurrentTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
// panic("ref")
cur, err := pkg.Current()
if err != nil {
t.Fatal(err)
}
cmpHereInfo(t, ref.Info, cur)
}

66
pkging/costello/http.go Normal file
View File

@ -0,0 +1,66 @@
package costello
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/markbates/pkger/pkging"
"github.com/stretchr/testify/require"
)
func HTTPTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
r := require.New(t)
_, err := LoadFiles("/public", ref, pkg)
r.NoError(err)
defer os.RemoveAll(ref.Dir)
fp := filepath.Join(ref.Dir, "public")
gots := httptest.NewServer(http.FileServer(http.Dir(fp)))
defer gots.Close()
dir, err := pkg.Open("/public")
r.NoError(err)
pkgts := httptest.NewServer(http.FileServer(dir))
defer pkgts.Close()
paths := []string{
"/",
"/index.html",
"/images",
"/images/img1.png",
}
for _, path := range paths {
t.Run(path, func(st *testing.T) {
r := require.New(st)
gores, err := http.Get(gots.URL + path)
r.NoError(err)
pkgres, err := http.Get(pkgts.URL + path)
r.NoError(err)
r.Equal(gores.StatusCode, pkgres.StatusCode)
gobody, err := ioutil.ReadAll(gores.Body)
r.NoError(err)
pkgbody, err := ioutil.ReadAll(pkgres.Body)
r.NoError(err)
exp := string(gobody)
act := string(pkgbody)
r.Equal(exp, act)
// exp := strings.ReplaceAll(string(gobody), tdir, "")
// exp = clean(exp)
// r.Equal(exp, clean(string(pkgbody)))
})
}
}

View File

@ -1,52 +1,160 @@
package costello
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/markbates/pkger/here"
"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()
func LoadFile(name string, pkg pkging.Pkger) (os.FileInfo, error) {
her, err := here.Package("github.com/markbates/pkger")
if err != nil {
return nil, err
}
if a, ok := pkg.(pkging.Adder); ok {
return a.Add(of)
}
root := filepath.Join(
her.Module.Dir,
"pkging",
"costello",
"testdata",
"ref",
name,
)
path = strings.TrimPrefix(path, ref.Dir)
info, err := os.Stat(root)
if err != nil {
return nil, err
}
pt, err := pkg.Parse(path)
if info.IsDir() {
return nil, fmt.Errorf("%s is a directory", name)
}
her, err = pkg.Current()
if err != nil {
return nil, err
}
af, err := os.Open(root)
if err != nil {
return nil, err
}
defer af.Close()
bf, err := pkg.Create(name)
if err != nil {
return nil, err
}
defer bf.Close()
if _, err := io.Copy(bf, af); err != nil {
return nil, err
}
return info, nil
}
func LoadFiles(name string, ref *Ref, pkg pkging.Pkger) ([]os.FileInfo, error) {
var infos []os.FileInfo
her, err := here.Package("github.com/markbates/pkger")
if err != nil {
return nil, err
}
root := filepath.Join(
her.Module.Dir,
"pkging",
"costello",
"testdata",
"ref",
name,
)
info, err := os.Stat(root)
if err != nil {
return nil, err
}
if !info.IsDir() {
return nil, fmt.Errorf("%s is not a directory", name)
}
her, err = pkg.Current()
if err != nil {
return nil, err
}
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if err := pkg.MkdirAll(filepath.Dir(pt.Name), 0755); err != nil {
if strings.HasPrefix(filepath.Base(path), ".") {
return nil
}
infos = append(infos, info)
xp := strings.TrimPrefix(path, root)
xp = filepath.Join(name, xp)
pt, err := pkg.Parse(xp)
if err != nil {
return err
}
if info.IsDir() {
if err := pkg.MkdirAll(pt.Name, info.Mode()); err != nil {
return err
}
if err := os.MkdirAll(filepath.Join(her.Dir, xp), info.Mode()); err != nil {
return err
}
return nil
}
f, err := pkg.Create(pt.String())
af, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
defer af.Close()
if _, err := io.Copy(f, of); err != nil {
bf, err := pkg.Create(pt.Name)
if err != nil {
return err
}
defer bf.Close()
xp = filepath.Join(her.Dir, xp)
cf, err := os.Create(xp)
if err != nil {
return err
}
defer cf.Close()
mw := io.MultiWriter(bf, cf)
_, err = io.Copy(mw, af)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
if len(infos) == 0 {
return nil, fmt.Errorf("did not load any infos for %s", name)
}
return infos, nil
}

View File

@ -12,19 +12,18 @@ import (
)
func OpenTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
openTest("go.mod", t, ref, pkg)
}
func openTest(name string, t *testing.T, ref *Ref, pkg pkging.Pkger) {
r := require.New(t)
name := "/go.mod"
osf, err := os.Open(filepath.Join(ref.Dir, name))
r.NoError(err)
osi, err := osf.Stat()
r.NoError(err)
r.NoError(LoadRef(ref, pkg))
_, err = LoadFile(name, pkg)
r.NoError(err)
pf, err := pkg.Open(fmt.Sprintf("/%s", name))
r.NoError(err)
@ -34,10 +33,6 @@ func openTest(name string, t *testing.T, ref *Ref, pkg pkging.Pkger) {
cmpFileInfo(t, osi, psi)
if osi.IsDir() {
return
}
osb, err := ioutil.ReadAll(osf)
r.NoError(err)
r.NoError(osf.Close())

View File

@ -1,6 +1,9 @@
package costello
import (
"crypto/rand"
"encoding/hex"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -13,23 +16,31 @@ type Ref struct {
}
func NewRef() (*Ref, error) {
her, err := here.Package("github.com/markbates/pkger")
if err != nil {
return nil, err
}
dir := filepath.Join(
root := filepath.Join(
her.Module.Dir,
"pkging",
"costello",
"testdata",
"ref")
if _, err := os.Stat(dir); err != nil {
if _, err := os.Stat(root); err != nil {
return nil, err
}
b := make([]byte, 10)
_, err = rand.Read(b)
if err != nil {
return nil, err
}
dir := filepath.Dir(root)
dir = filepath.Join(dir, hex.EncodeToString(b))
ref := &Ref{
Info: here.Info{
ImportPath: "app",
@ -45,5 +56,36 @@ func NewRef() (*Ref, error) {
},
}
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}
b, err = ioutil.ReadFile(filepath.Join(root, "go.mod"))
if err != nil {
return nil, err
}
f, err := os.Create(filepath.Join(dir, "go.mod"))
if err != nil {
return nil, err
}
if _, err := f.Write(b); err != nil {
return nil, err
}
if err := f.Close(); err != nil {
return nil, err
}
// c := exec.Command("cp", "-rv", root, dir)
// fmt.Println(strings.Join(c.Args, " "))
// c.Stdout = os.Stdout
// c.Stderr = os.Stderr
// c.Stdin = os.Stdin
// if err := c.Run(); err != nil {
// return nil, err
// }
return ref, nil
}

View File

@ -10,11 +10,11 @@ import (
func RemoveTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
r := require.New(t)
r.NoError(LoadRef(ref, pkg))
name := "/go.mod"
_, err := LoadFile(name, pkg)
r.NoError(err)
_, err := pkg.Stat(name)
_, err = pkg.Stat(name)
r.NoError(err)
r.NoError(pkg.Remove(name))

View File

@ -10,9 +10,8 @@ import (
func RemoveAllTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
r := require.New(t)
r.NoError(LoadRef(ref, pkg))
name := "/public/assets"
r.NoError(pkg.MkdirAll(name, 0755))
_, err := pkg.Stat(name)
r.NoError(err)

View File

@ -15,7 +15,8 @@ func StatTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
osi, err := os.Stat(filepath.Join(ref.Dir, "go.mod"))
r.NoError(err)
r.NoError(LoadRef(ref, pkg))
_, err = LoadFile("/go.mod", pkg)
r.NoError(err)
psi, err := pkg.Stat("/go.mod")
r.NoError(err)

View File

@ -2,7 +2,6 @@ package costello
import (
"os"
"path/filepath"
"testing"
"github.com/markbates/pkger/pkging"
@ -12,23 +11,12 @@ import (
func WalkTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
r := require.New(t)
r.NoError(LoadRef(ref, pkg))
exp, err := LoadFiles("/public", ref, pkg)
r.NoError(err)
defer os.RemoveAll(ref.Dir)
name := "public"
fp := filepath.Join(ref.Dir, name)
var exp []os.FileInfo
err := filepath.Walk(fp, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
exp = append(exp, info)
return nil
})
r.NoError(err)
var act []os.FileInfo
err = pkg.Walk("/"+name, func(path string, info os.FileInfo, err error) error {
if err != nil {

View File

@ -1,6 +1,7 @@
package mem
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,6 +13,7 @@ func Test_Pkger_Create(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := New(ref.Info)
r.NoError(err)

View File

@ -1,6 +1,7 @@
package mem
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,6 +13,7 @@ func Test_Pkger_Current(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := New(ref.Info)
r.NoError(err)

22
pkging/mem/http_test.go Normal file
View File

@ -0,0 +1,22 @@
package mem
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
"github.com/stretchr/testify/require"
)
func Test_Pkger_HTTP(t *testing.T) {
r := require.New(t)
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := New(ref.Info)
r.NoError(err)
costello.HTTPTest(t, ref, pkg)
}

View File

@ -1,6 +1,7 @@
package mem
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,6 +13,7 @@ func Test_Pkger_Info(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := New(ref.Info)
r.NoError(err)

View File

@ -1,6 +1,7 @@
package mem
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,6 +13,7 @@ func Test_Pkger_MkdirAll(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := New(ref.Info)
r.NoError(err)

View File

@ -1,6 +1,7 @@
package mem
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,6 +13,7 @@ func Test_Pkger_Open(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := New(ref.Info)
r.NoError(err)

View File

@ -1,6 +1,7 @@
package mem
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,6 +13,7 @@ func Test_Pkger_Remove(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := New(ref.Info)
r.NoError(err)

View File

@ -1,6 +1,7 @@
package mem
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,6 +13,7 @@ func Test_Pkger_RemoveAll(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := New(ref.Info)
r.NoError(err)

View File

@ -1,6 +1,7 @@
package mem
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,6 +13,7 @@ func Test_Pkger_Stat(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := New(ref.Info)
r.NoError(err)

View File

@ -1,6 +1,7 @@
package mem
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,6 +13,7 @@ func Test_Pkger_Walk(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := New(ref.Info)
r.NoError(err)

27
pkging/mod_time_test.go Normal file
View File

@ -0,0 +1,27 @@
package pkging
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func Test_ModTime(t *testing.T) {
r := require.New(t)
now := time.Now()
mt := ModTime(now)
b, err := mt.MarshalJSON()
r.NoError(err)
var mt2 ModTime
r.NoError(json.Unmarshal(b, &mt2))
at := time.Time(mt).Format(time.RFC3339)
bt := time.Time(mt2).Format(time.RFC3339)
r.Equal(at, bt)
}

View File

@ -1,6 +1,7 @@
package stdos
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,8 +13,9 @@ func Test_Pkger_Create(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := NewTemp(ref)
pkg, err := New(ref.Info)
r.NoError(err)
costello.CreateTest(t, ref, pkg)

View File

@ -1,6 +1,7 @@
package stdos
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,8 +13,9 @@ func Test_Pkger_Current(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := NewTemp(ref)
pkg, err := New(ref.Info)
r.NoError(err)
costello.CurrentTest(t, ref, pkg)

22
pkging/stdos/http_test.go Normal file
View File

@ -0,0 +1,22 @@
package stdos
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
"github.com/stretchr/testify/require"
)
func Test_Pkger_HTTP(t *testing.T) {
r := require.New(t)
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := New(ref.Info)
r.NoError(err)
costello.HTTPTest(t, ref, pkg)
}

View File

@ -1,6 +1,7 @@
package stdos
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,8 +13,9 @@ func Test_Pkger_Info(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := NewTemp(ref)
pkg, err := New(ref.Info)
r.NoError(err)
costello.InfoTest(t, ref, pkg)

View File

@ -1,6 +1,7 @@
package stdos
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,8 +13,9 @@ func Test_Pkger_MkdirAll(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := NewTemp(ref)
pkg, err := New(ref.Info)
r.NoError(err)
costello.MkdirAllTest(t, ref, pkg)

View File

@ -1,6 +1,7 @@
package stdos
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,8 +13,9 @@ func Test_Pkger_Open(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := NewTemp(ref)
pkg, err := New(ref.Info)
r.NoError(err)
costello.OpenTest(t, ref, pkg)

View File

@ -1,6 +1,7 @@
package stdos
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,8 +13,9 @@ func Test_Pkger_Remove(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := NewTemp(ref)
pkg, err := New(ref.Info)
r.NoError(err)
costello.RemoveTest(t, ref, pkg)

View File

@ -1,6 +1,7 @@
package stdos
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,8 +13,9 @@ func Test_Pkger_RemoveAll(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := NewTemp(ref)
pkg, err := New(ref.Info)
r.NoError(err)
costello.RemoveAllTest(t, ref, pkg)

View File

@ -1,6 +1,7 @@
package stdos
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,8 +13,9 @@ func Test_Pkger_Stat(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := NewTemp(ref)
pkg, err := New(ref.Info)
r.NoError(err)
costello.StatTest(t, ref, pkg)

View File

@ -18,27 +18,6 @@ type Pkger struct {
infos *maps.Infos
}
// Abs returns an absolute representation of path. 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.
func (f *Pkger) Abs(p string) (string, error) {
pt, err := f.Parse(p)
if err != nil {
return "", err
}
return f.AbsPath(pt)
}
// AbsPath returns an absolute representation of here.Path. 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. AbsPath calls Clean on the result.
func (f *Pkger) AbsPath(pt here.Path) (string, error) {
if pt.Pkg == f.Here.ImportPath {
return filepath.Join(f.Here.Dir, pt.Name), nil
}
info, err := f.Info(pt.Pkg)
if err != nil {
return "", err
}
return filepath.Join(info.Dir, pt.Name), nil
}
// New returns *Pkger for the provided here.Info
func New(her here.Info) (*Pkger, error) {
p := &Pkger{
@ -51,16 +30,13 @@ func New(her here.Info) (*Pkger, error) {
// Create creates the named file with mode 0666 (before umask) - It's actually 0644, truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR.
func (fx *Pkger) Create(name string) (pkging.File, error) {
name, err := fx.Abs(name)
if err != nil {
return nil, err
}
f, err := os.Create(name)
pt, err := fx.Parse(name)
if err != nil {
return nil, err
}
pt, err := fx.Parse(name)
name = filepath.Join(fx.Here.Dir, pt.Name)
f, err := os.Create(name)
if err != nil {
return nil, err
}
@ -106,11 +82,7 @@ func (f *Pkger) Info(p string) (here.Info, 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 (f *Pkger) MkdirAll(p string, perm os.FileMode) error {
p, err := f.Abs(p)
if err != nil {
return err
}
return os.MkdirAll(p, perm)
return os.MkdirAll(filepath.Join(f.Here.Dir, p), perm)
}
// Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY.
@ -120,10 +92,7 @@ func (fx *Pkger) Open(name string) (pkging.File, error) {
return nil, err
}
name, err = fx.Abs(name)
if err != nil {
return nil, err
}
name = filepath.Join(fx.Here.Dir, pt.Name)
f, err := os.Open(name)
if err != nil {
return nil, err
@ -156,18 +125,13 @@ func (f *Pkger) Parse(p string) (here.Path, error) {
}
// Stat returns a FileInfo describing the named file.
func (f *Pkger) Stat(name string) (os.FileInfo, error) {
pt, err := f.Parse(name)
func (fx *Pkger) Stat(name string) (os.FileInfo, error) {
pt, err := fx.Parse(name)
if err != nil {
return nil, err
}
abs, err := f.AbsPath(pt)
if err != nil {
return nil, err
}
info, err := os.Stat(abs)
info, err := os.Stat(filepath.Join(fx.Here.Dir, pt.Name))
if err != nil {
return nil, err
}
@ -179,15 +143,12 @@ func (f *Pkger) Stat(name string) (os.FileInfo, error) {
// Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. The files are walked in lexical order, which makes the output deterministic but means that for very large directories Walk can be inefficient. Walk does not follow symbolic links. - That is from the standard library. I know. Their grammar teachers can not be happy with them right now.
func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
fp, err := f.Abs(p)
if err != nil {
return err
}
pt, err := f.Parse(p)
if err != nil {
return err
}
fp := filepath.Join(f.Here.Dir, pt.Name)
err = filepath.Walk(fp, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
@ -211,18 +172,18 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
// Remove removes the named file or (empty) directory.
func (fx *Pkger) Remove(name string) error {
name, err := fx.Abs(name)
pt, err := fx.Parse(name)
if err != nil {
return err
}
return os.Remove(name)
return os.Remove(filepath.Join(fx.Here.Dir, pt.Name))
}
// RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters. If the path does not exist, RemoveAll returns nil (no error).
func (fx *Pkger) RemoveAll(name string) error {
name, err := fx.Abs(name)
pt, err := fx.Parse(name)
if err != nil {
return err
}
return os.RemoveAll(name)
return os.RemoveAll(filepath.Join(fx.Here.Dir, pt.Name))
}

View File

@ -1,34 +1,14 @@
package stdos
import (
"io/ioutil"
"path/filepath"
"testing"
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/costello"
)
func NewTemp(ref *costello.Ref) (pkging.Pkger, error) {
dir, err := ioutil.TempDir("", "stdos")
if err != nil {
return nil, err
}
info := here.Info{
Module: ref.Module,
ImportPath: ref.ImportPath,
Name: ref.Name,
Dir: dir,
}
info.Module.Dir = dir
info.Module.GoMod = filepath.Join(dir, "go.mod")
return New(info)
}
func Test_Pkger(t *testing.T) {
costello.All(t, func(ref *costello.Ref) (pkging.Pkger, error) {
return NewTemp(ref)
return New(ref.Info)
})
}

View File

@ -1,6 +1,7 @@
package stdos
import (
"os"
"testing"
"github.com/markbates/pkger/pkging/costello"
@ -12,8 +13,9 @@ func Test_Pkger_Walk(t *testing.T) {
ref, err := costello.NewRef()
r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := NewTemp(ref)
pkg, err := New(ref.Info)
r.NoError(err)
costello.WalkTest(t, ref, pkg)