diff --git a/here/parse_test.go b/here/parse_test.go index cddaef6..484e843 100644 --- a/here/parse_test.go +++ b/here/parse_test.go @@ -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" diff --git a/parser/parser_test.go b/parser/parser_test.go index ac77c30..39c7a12 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -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) diff --git a/pkger.go b/pkger.go index 67cefb5..2cf1dc6 100644 --- a/pkger.go +++ b/pkger.go @@ -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 } diff --git a/pkger_test.go b/pkger_test.go index bf1addf..0507cd1 100644 --- a/pkger_test.go +++ b/pkger_test.go @@ -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 } diff --git a/pkging/costello/costello.go b/pkging/costello/costello.go index 0342a2c..1d94ca9 100644 --- a/pkging/costello/costello.go +++ b/pkging/costello/costello.go @@ -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()) } diff --git a/pkging/costello/current.go b/pkging/costello/current.go index f16be92..27265b4 100644 --- a/pkging/costello/current.go +++ b/pkging/costello/current.go @@ -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) } diff --git a/pkging/costello/http.go b/pkging/costello/http.go new file mode 100644 index 0000000..e0f15c4 --- /dev/null +++ b/pkging/costello/http.go @@ -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))) + }) + } +} diff --git a/pkging/costello/load_ref.go b/pkging/costello/load_ref.go index eda283e..5fa7fa5 100644 --- a/pkging/costello/load_ref.go +++ b/pkging/costello/load_ref.go @@ -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 } diff --git a/pkging/costello/open.go b/pkging/costello/open.go index 6c33b62..8f12dc5 100644 --- a/pkging/costello/open.go +++ b/pkging/costello/open.go @@ -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()) diff --git a/pkging/costello/ref.go b/pkging/costello/ref.go index 95ae879..7264105 100644 --- a/pkging/costello/ref.go +++ b/pkging/costello/ref.go @@ -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 } diff --git a/pkging/costello/remove.go b/pkging/costello/remove.go index 45cd0fc..fb27ccd 100644 --- a/pkging/costello/remove.go +++ b/pkging/costello/remove.go @@ -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)) diff --git a/pkging/costello/remove_all.go b/pkging/costello/remove_all.go index 9df1705..4e170ab 100644 --- a/pkging/costello/remove_all.go +++ b/pkging/costello/remove_all.go @@ -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) diff --git a/pkging/costello/stat.go b/pkging/costello/stat.go index 6e2243f..8e2e43b 100644 --- a/pkging/costello/stat.go +++ b/pkging/costello/stat.go @@ -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) diff --git a/pkging/costello/walk.go b/pkging/costello/walk.go index 8ca9fb2..6879dce 100644 --- a/pkging/costello/walk.go +++ b/pkging/costello/walk.go @@ -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 { diff --git a/pkging/mem/create_test.go b/pkging/mem/create_test.go index de56d6e..2f35e60 100644 --- a/pkging/mem/create_test.go +++ b/pkging/mem/create_test.go @@ -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) diff --git a/pkging/mem/current_test.go b/pkging/mem/current_test.go index f1afc1d..624a77e 100644 --- a/pkging/mem/current_test.go +++ b/pkging/mem/current_test.go @@ -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) diff --git a/pkging/mem/http_test.go b/pkging/mem/http_test.go new file mode 100644 index 0000000..0cd467f --- /dev/null +++ b/pkging/mem/http_test.go @@ -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) +} diff --git a/pkging/mem/info_test.go b/pkging/mem/info_test.go index 3752db8..719ac75 100644 --- a/pkging/mem/info_test.go +++ b/pkging/mem/info_test.go @@ -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) diff --git a/pkging/mem/mkdirall_test.go b/pkging/mem/mkdirall_test.go index f98634e..7f5f8db 100644 --- a/pkging/mem/mkdirall_test.go +++ b/pkging/mem/mkdirall_test.go @@ -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) diff --git a/pkging/mem/open_test.go b/pkging/mem/open_test.go index a9ca653..b7d34fd 100644 --- a/pkging/mem/open_test.go +++ b/pkging/mem/open_test.go @@ -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) diff --git a/pkging/mem/remove_test.go b/pkging/mem/remove_test.go index c62a1bc..c9d32a6 100644 --- a/pkging/mem/remove_test.go +++ b/pkging/mem/remove_test.go @@ -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) diff --git a/pkging/mem/removeall_test.go b/pkging/mem/removeall_test.go index da06e5e..3d48d78 100644 --- a/pkging/mem/removeall_test.go +++ b/pkging/mem/removeall_test.go @@ -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) diff --git a/pkging/mem/stat_test.go b/pkging/mem/stat_test.go index 88cb02e..c2ba470 100644 --- a/pkging/mem/stat_test.go +++ b/pkging/mem/stat_test.go @@ -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) diff --git a/pkging/mem/walk_test.go b/pkging/mem/walk_test.go index c4e76c7..0a8dc2c 100644 --- a/pkging/mem/walk_test.go +++ b/pkging/mem/walk_test.go @@ -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) diff --git a/pkging/mod_time_test.go b/pkging/mod_time_test.go new file mode 100644 index 0000000..508a9e6 --- /dev/null +++ b/pkging/mod_time_test.go @@ -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) +} diff --git a/pkging/stdos/create_test.go b/pkging/stdos/create_test.go index 9a49049..b06a8be 100644 --- a/pkging/stdos/create_test.go +++ b/pkging/stdos/create_test.go @@ -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) diff --git a/pkging/stdos/current_test.go b/pkging/stdos/current_test.go index 41af520..bd6c2ef 100644 --- a/pkging/stdos/current_test.go +++ b/pkging/stdos/current_test.go @@ -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) diff --git a/pkging/stdos/http_test.go b/pkging/stdos/http_test.go new file mode 100644 index 0000000..52f35b1 --- /dev/null +++ b/pkging/stdos/http_test.go @@ -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) +} diff --git a/pkging/stdos/info_test.go b/pkging/stdos/info_test.go index 119fee4..d83ba35 100644 --- a/pkging/stdos/info_test.go +++ b/pkging/stdos/info_test.go @@ -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) diff --git a/pkging/stdos/mkdirall_test.go b/pkging/stdos/mkdirall_test.go index 315b6a4..eb701b9 100644 --- a/pkging/stdos/mkdirall_test.go +++ b/pkging/stdos/mkdirall_test.go @@ -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) diff --git a/pkging/stdos/open_test.go b/pkging/stdos/open_test.go index fc92f35..c7e7cde 100644 --- a/pkging/stdos/open_test.go +++ b/pkging/stdos/open_test.go @@ -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) diff --git a/pkging/stdos/remove_test.go b/pkging/stdos/remove_test.go index d0f16d4..f42b572 100644 --- a/pkging/stdos/remove_test.go +++ b/pkging/stdos/remove_test.go @@ -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) diff --git a/pkging/stdos/removeall_test.go b/pkging/stdos/removeall_test.go index 86370ea..9ced5bb 100644 --- a/pkging/stdos/removeall_test.go +++ b/pkging/stdos/removeall_test.go @@ -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) diff --git a/pkging/stdos/stat_test.go b/pkging/stdos/stat_test.go index 9a97c0a..325516f 100644 --- a/pkging/stdos/stat_test.go +++ b/pkging/stdos/stat_test.go @@ -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) diff --git a/pkging/stdos/stdos.go b/pkging/stdos/stdos.go index d264b77..75777c3 100644 --- a/pkging/stdos/stdos.go +++ b/pkging/stdos/stdos.go @@ -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)) } diff --git a/pkging/stdos/stdos_test.go b/pkging/stdos/stdos_test.go index d951890..43a1baa 100644 --- a/pkging/stdos/stdos_test.go +++ b/pkging/stdos/stdos_test.go @@ -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) }) } diff --git a/pkging/stdos/walk_test.go b/pkging/stdos/walk_test.go index 79a0765..ad47915 100644 --- a/pkging/stdos/walk_test.go +++ b/pkging/stdos/walk_test.go @@ -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)