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

View File

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

View File

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

View File

@ -69,7 +69,7 @@ func Test_Walk(t *testing.T) {
r := require.New(t) r := require.New(t)
files := map[string]os.FileInfo{} 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 { if err != nil {
return err return err
} }

View File

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

View File

@ -7,10 +7,10 @@ import (
) )
func CurrentTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { func CurrentTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
// panic("ref")
cur, err := pkg.Current() cur, err := pkg.Current()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
cmpHereInfo(t, ref.Info, cur) 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 package costello
import ( import (
"fmt"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging"
) )
func LoadRef(ref *Ref, pkg pkging.Pkger) error { func LoadFile(name string, pkg pkging.Pkger) (os.FileInfo, error) {
return filepath.Walk(ref.Dir, func(path string, info os.FileInfo, err error) error { her, err := here.Package("github.com/markbates/pkger")
if err != nil { if err != nil {
return err return nil, err
} }
of, err := os.Open(path)
root := filepath.Join(
her.Module.Dir,
"pkging",
"costello",
"testdata",
"ref",
name,
)
info, err := os.Stat(root)
if err != nil { if err != nil {
return err return nil, err
}
defer of.Close()
if a, ok := pkg.(pkging.Adder); ok {
return a.Add(of)
} }
path = strings.TrimPrefix(path, ref.Dir) if info.IsDir() {
return nil, fmt.Errorf("%s is a directory", name)
}
pt, err := pkg.Parse(path) 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 { if err != nil {
return err 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 return err
} }
if info.IsDir() { 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 return nil
} }
f, err := pkg.Create(pt.String())
af, err := os.Open(path)
if err != nil { if err != nil {
return err 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 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 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) { 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) r := require.New(t)
name := "/go.mod"
osf, err := os.Open(filepath.Join(ref.Dir, name)) osf, err := os.Open(filepath.Join(ref.Dir, name))
r.NoError(err) r.NoError(err)
osi, err := osf.Stat() osi, err := osf.Stat()
r.NoError(err) r.NoError(err)
r.NoError(LoadRef(ref, pkg)) _, err = LoadFile(name, pkg)
r.NoError(err)
pf, err := pkg.Open(fmt.Sprintf("/%s", name)) pf, err := pkg.Open(fmt.Sprintf("/%s", name))
r.NoError(err) r.NoError(err)
@ -34,10 +33,6 @@ func openTest(name string, t *testing.T, ref *Ref, pkg pkging.Pkger) {
cmpFileInfo(t, osi, psi) cmpFileInfo(t, osi, psi)
if osi.IsDir() {
return
}
osb, err := ioutil.ReadAll(osf) osb, err := ioutil.ReadAll(osf)
r.NoError(err) r.NoError(err)
r.NoError(osf.Close()) r.NoError(osf.Close())

View File

@ -1,6 +1,9 @@
package costello package costello
import ( import (
"crypto/rand"
"encoding/hex"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -13,23 +16,31 @@ type Ref struct {
} }
func NewRef() (*Ref, error) { func NewRef() (*Ref, error) {
her, err := here.Package("github.com/markbates/pkger") her, err := here.Package("github.com/markbates/pkger")
if err != nil { if err != nil {
return nil, err return nil, err
} }
dir := filepath.Join( root := filepath.Join(
her.Module.Dir, her.Module.Dir,
"pkging", "pkging",
"costello", "costello",
"testdata", "testdata",
"ref") "ref")
if _, err := os.Stat(dir); err != nil { if _, err := os.Stat(root); err != nil {
return nil, err 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{ ref := &Ref{
Info: here.Info{ Info: here.Info{
ImportPath: "app", 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 return ref, nil
} }

View File

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

View File

@ -10,9 +10,8 @@ import (
func RemoveAllTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { func RemoveAllTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
r := require.New(t) r := require.New(t)
r.NoError(LoadRef(ref, pkg))
name := "/public/assets" name := "/public/assets"
r.NoError(pkg.MkdirAll(name, 0755))
_, err := pkg.Stat(name) _, err := pkg.Stat(name)
r.NoError(err) 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")) osi, err := os.Stat(filepath.Join(ref.Dir, "go.mod"))
r.NoError(err) r.NoError(err)
r.NoError(LoadRef(ref, pkg)) _, err = LoadFile("/go.mod", pkg)
r.NoError(err)
psi, err := pkg.Stat("/go.mod") psi, err := pkg.Stat("/go.mod")
r.NoError(err) r.NoError(err)

View File

@ -2,7 +2,6 @@ package costello
import ( import (
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging"
@ -12,23 +11,12 @@ import (
func WalkTest(t *testing.T, ref *Ref, pkg pkging.Pkger) { func WalkTest(t *testing.T, ref *Ref, pkg pkging.Pkger) {
r := require.New(t) 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" 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 var act []os.FileInfo
err = pkg.Walk("/"+name, func(path string, info os.FileInfo, err error) error { err = pkg.Walk("/"+name, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -18,27 +18,6 @@ type Pkger struct {
infos *maps.Infos 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 // New returns *Pkger for the provided here.Info
func New(her here.Info) (*Pkger, error) { func New(her here.Info) (*Pkger, error) {
p := &Pkger{ 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. // 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) { func (fx *Pkger) Create(name string) (pkging.File, error) {
name, err := fx.Abs(name) pt, err := fx.Parse(name)
if err != nil {
return nil, err
}
f, err := os.Create(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
pt, err := fx.Parse(name) name = filepath.Join(fx.Here.Dir, pt.Name)
f, err := os.Create(name)
if err != nil { if err != nil {
return nil, err 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. // 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 { func (f *Pkger) MkdirAll(p string, perm os.FileMode) error {
p, err := f.Abs(p) return os.MkdirAll(filepath.Join(f.Here.Dir, p), perm)
if err != nil {
return err
}
return os.MkdirAll(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. // 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 return nil, err
} }
name, err = fx.Abs(name) name = filepath.Join(fx.Here.Dir, pt.Name)
if err != nil {
return nil, err
}
f, err := os.Open(name) f, err := os.Open(name)
if err != nil { if err != nil {
return nil, err return nil, err
@ -156,18 +125,13 @@ func (f *Pkger) Parse(p string) (here.Path, error) {
} }
// Stat returns a FileInfo describing the named file. // Stat returns a FileInfo describing the named file.
func (f *Pkger) Stat(name string) (os.FileInfo, error) { func (fx *Pkger) Stat(name string) (os.FileInfo, error) {
pt, err := f.Parse(name) pt, err := fx.Parse(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
abs, err := f.AbsPath(pt) info, err := os.Stat(filepath.Join(fx.Here.Dir, pt.Name))
if err != nil {
return nil, err
}
info, err := os.Stat(abs)
if err != nil { if err != nil {
return nil, err 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. // 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 { 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) pt, err := f.Parse(p)
if err != nil { if err != nil {
return err return err
} }
fp := filepath.Join(f.Here.Dir, pt.Name)
err = filepath.Walk(fp, func(path string, fi os.FileInfo, err error) error { err = filepath.Walk(fp, func(path string, fi os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@ -211,18 +172,18 @@ func (f *Pkger) Walk(p string, wf filepath.WalkFunc) error {
// Remove removes the named file or (empty) directory. // Remove removes the named file or (empty) directory.
func (fx *Pkger) Remove(name string) error { func (fx *Pkger) Remove(name string) error {
name, err := fx.Abs(name) pt, err := fx.Parse(name)
if err != nil { if err != nil {
return err 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). // 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 { func (fx *Pkger) RemoveAll(name string) error {
name, err := fx.Abs(name) pt, err := fx.Parse(name)
if err != nil { if err != nil {
return err return err
} }
return os.RemoveAll(name) return os.RemoveAll(filepath.Join(fx.Here.Dir, pt.Name))
} }

View File

@ -1,34 +1,14 @@
package stdos package stdos
import ( import (
"io/ioutil"
"path/filepath"
"testing" "testing"
"github.com/markbates/pkger/here"
"github.com/markbates/pkger/pkging" "github.com/markbates/pkger/pkging"
"github.com/markbates/pkger/pkging/costello" "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) { func Test_Pkger(t *testing.T) {
costello.All(t, func(ref *costello.Ref) (pkging.Pkger, error) { 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 package stdos
import ( import (
"os"
"testing" "testing"
"github.com/markbates/pkger/pkging/costello" "github.com/markbates/pkger/pkging/costello"
@ -12,8 +13,9 @@ func Test_Pkger_Walk(t *testing.T) {
ref, err := costello.NewRef() ref, err := costello.NewRef()
r.NoError(err) r.NoError(err)
defer os.RemoveAll(ref.Dir)
pkg, err := NewTemp(ref) pkg, err := New(ref.Info)
r.NoError(err) r.NoError(err)
costello.WalkTest(t, ref, pkg) costello.WalkTest(t, ref, pkg)