From 830ed679a868e19ffd08aa09abfdc593f081f323 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Fri, 20 Sep 2019 10:56:26 -0400 Subject: [PATCH] when you wake up feeling old --- pkging/mem/file.go | 151 +++++++++++++++++++------------------- pkging/mem/file_test.go | 52 +++++++++++++ pkging/mem/mem.go | 6 +- pkging/mem/mem_test.go | 5 +- pkging/pkgtest/http.go | 9 --- pkging/pkgtest/suite.go | 143 +++++++++++++++++++++++++++++++++--- pkging/stdos/file.go | 15 ++-- pkging/stdos/file_test.go | 29 ++++++++ pkging/stdos/stdos.go | 6 +- 9 files changed, 303 insertions(+), 113 deletions(-) create mode 100644 pkging/mem/file_test.go create mode 100644 pkging/stdos/file_test.go diff --git a/pkging/mem/file.go b/pkging/mem/file.go index c0c1548..a70d076 100644 --- a/pkging/mem/file.go +++ b/pkging/mem/file.go @@ -2,7 +2,6 @@ package mem import ( "bytes" - "encoding/json" "fmt" "io" "net/http" @@ -111,25 +110,25 @@ func (f File) String() string { return f.Path().String() } -func (f File) Format(st fmt.State, verb rune) { - switch verb { - case 'v': - if st.Flag('+') { - b, err := json.MarshalIndent(f, "", " ") - if err != nil { - fmt.Fprint(os.Stderr, err) - return - } - fmt.Fprint(st, string(b)) - return - } - fmt.Fprint(st, f.String()) - case 'q': - fmt.Fprintf(st, "%q", f.String()) - default: - fmt.Fprint(st, f.String()) - } -} +// func (f File) Format(st fmt.State, verb rune) { +// switch verb { +// case 'v': +// if st.Flag('+') { +// b, err := json.MarshalIndent(f, "", " ") +// if err != nil { +// fmt.Fprint(os.Stderr, err) +// return +// } +// fmt.Fprint(st, string(b)) +// return +// } +// fmt.Fprint(st, f.String()) +// case 'q': +// fmt.Fprintf(st, "%q", f.String()) +// default: +// fmt.Fprint(st, f.String()) +// } +// } func (f *File) Readdir(count int) ([]os.FileInfo, error) { var infos []os.FileInfo @@ -206,60 +205,60 @@ func (f *File) Open(name string) (http.File, error) { return di, nil } -func (f File) MarshalJSON() ([]byte, error) { - m := map[string]interface{}{ - "info": f.info, - "her": f.her, - "path": f.path, - "data": f.data, - "parent": f.parent, - } - return json.Marshal(m) -} +// func (f File) MarshalJSON() ([]byte, error) { +// m := map[string]interface{}{ +// "info": f.info, +// "her": f.her, +// "path": f.path, +// "data": f.data, +// "parent": f.parent, +// } +// return json.Marshal(m) +// } -func (f *File) UnmarshalJSON(b []byte) error { - m := map[string]json.RawMessage{} - if err := json.Unmarshal(b, &m); err != nil { - return err - } - - info, ok := m["info"] - if !ok { - return fmt.Errorf("missing info") - } - - f.info = &pkging.FileInfo{} - if err := json.Unmarshal(info, f.info); err != nil { - return err - } - - her, ok := m["her"] - if !ok { - return fmt.Errorf("missing her") - } - if err := json.Unmarshal(her, &f.her); err != nil { - return err - } - - path, ok := m["path"] - if !ok { - return fmt.Errorf("missing path") - } - if err := json.Unmarshal(path, &f.path); err != nil { - return err - } - - parent, ok := m["parent"] - if !ok { - return fmt.Errorf("missing parent") - } - if err := json.Unmarshal(parent, &f.parent); err != nil { - return err - } - - if err := json.Unmarshal(m["data"], &f.data); err != nil { - return err - } - - return nil -} +// func (f *File) UnmarshalJSON(b []byte) error { +// m := map[string]json.RawMessage{} +// if err := json.Unmarshal(b, &m); err != nil { +// return err +// } +// +// info, ok := m["info"] +// if !ok { +// return fmt.Errorf("missing info") +// } +// +// f.info = &pkging.FileInfo{} +// if err := json.Unmarshal(info, f.info); err != nil { +// return err +// } +// +// her, ok := m["her"] +// if !ok { +// return fmt.Errorf("missing her") +// } +// if err := json.Unmarshal(her, &f.her); err != nil { +// return err +// } +// +// path, ok := m["path"] +// if !ok { +// return fmt.Errorf("missing path") +// } +// if err := json.Unmarshal(path, &f.path); err != nil { +// return err +// } +// +// parent, ok := m["parent"] +// if !ok { +// return fmt.Errorf("missing parent") +// } +// if err := json.Unmarshal(parent, &f.parent); err != nil { +// return err +// } +// +// if err := json.Unmarshal(m["data"], &f.data); err != nil { +// return err +// } +// +// return nil +// } diff --git a/pkging/mem/file_test.go b/pkging/mem/file_test.go new file mode 100644 index 0000000..93c4e78 --- /dev/null +++ b/pkging/mem/file_test.go @@ -0,0 +1,52 @@ +package mem + +import ( + "io/ioutil" + "testing" + + "github.com/markbates/pkger/here" + "github.com/stretchr/testify/require" +) + +func Test_File_Seek(t *testing.T) { + r := require.New(t) + + info, err := here.Current() + r.NoError(err) + + pkg, err := New(info) + r.NoError(err) + + f, err := pkg.Create(":/wilco.band") + r.NoError(err) + + data := []byte("a shot in the arm") + f.Write(data) + r.NoError(f.Close()) + + f, err = pkg.Open(":/wilco.band") + r.NoError(err) + + b, err := ioutil.ReadAll(f) + r.NoError(err) + r.Equal(data, b) + + _, err = f.Seek(0, 0) + r.NoError(err) + + b, err = ioutil.ReadAll(f) + r.NoError(err) + r.Equal(data, b) + + b, err = ioutil.ReadAll(f) + r.NoError(err) + r.NotEqual(data, b) + + _, err = f.Seek(10, 0) + r.NoError(err) + + b, err = ioutil.ReadAll(f) + r.NoError(err) + r.NotEqual(data, b) + r.Equal([]byte("the arm"), b) +} diff --git a/pkging/mem/mem.go b/pkging/mem/mem.go index c1293f1..8f4ff97 100644 --- a/pkging/mem/mem.go +++ b/pkging/mem/mem.go @@ -29,6 +29,8 @@ func New(info here.Info) (*Pkger, error) { files: &maps.Files{}, current: info, } + f.infos.Store(info.ImportPath, info) + f.MkdirAll("/", 0755) return f, nil } @@ -105,6 +107,7 @@ func (fx *Pkger) RemoveAll(name string) error { } func (fx *Pkger) Create(name string) (pkging.File, error) { + fx.MkdirAll("/", 0755) pt, err := fx.Parse(name) if err != nil { return nil, err @@ -115,7 +118,8 @@ func (fx *Pkger) Create(name string) (pkging.File, error) { return nil, err } - if _, err := fx.Stat(filepath.Dir(pt.Name)); err != nil { + dir := filepath.Dir(pt.Name) + if _, err := fx.Stat(dir); err != nil { return nil, err } diff --git a/pkging/mem/mem_test.go b/pkging/mem/mem_test.go index 79effb2..724e767 100644 --- a/pkging/mem/mem_test.go +++ b/pkging/mem/mem_test.go @@ -15,13 +15,12 @@ func Test_Pkger(t *testing.T) { return nil, err } - wh, err := New(info) + pkg, err := New(info) if err != nil { return nil, err } - WithInfo(wh, info) - return wh, nil + return pkg, nil }) if err != nil { t.Fatal(err) diff --git a/pkging/pkgtest/http.go b/pkging/pkgtest/http.go index 2365bdf..d8c2747 100644 --- a/pkging/pkgtest/http.go +++ b/pkging/pkgtest/http.go @@ -12,24 +12,15 @@ import ( "github.com/stretchr/testify/require" ) -// examples/app -// ├── Dockerfile -// ├── Makefile -// ├── go.mod -// ├── go.sum // ├── main.go // ├── public // │   ├── images -// │   │   ├── mark-small.png // │   │   ├── mark.png -// │   │   ├── mark_250px.png -// │   │   └── mark_400px.png // │   └── index.html // └── templates // ├── a.txt // └── b // └── b.txt - func (s Suite) LoadFolder(pkg pkging.Pkger) error { files := []string{ "/main.go", diff --git a/pkging/pkgtest/suite.go b/pkging/pkgtest/suite.go index 5863da5..26b4577 100644 --- a/pkging/pkgtest/suite.go +++ b/pkging/pkgtest/suite.go @@ -67,7 +67,7 @@ func (s Suite) Test(t *testing.T) { // } // } // if ns, ok := s.Pkger.(WithRootable); ok { -// dir, err := ioutil.TempDir("", "") +// dir, err := ioutil.TempDir("") // if err != nil { // return s, err // } @@ -467,15 +467,134 @@ func (s Suite) Test_Stat_File(t *testing.T) { } } -// func (s Suite) Test_Walk(t *testing.T) { -// panic("not implemented") -// } +func (s Suite) Test_Walk(t *testing.T) { + r := require.New(t) -// -// func (s Suite) Test_Remove(t *testing.T) { -// panic("not implemented") -// } -// -// func (s Suite) Test_RemoveAll(t *testing.T) { -// panic("not implemented") -// } + pkg, err := s.Make() + r.NoError(err) + r.NoError(s.LoadFolder(pkg)) + + cur, err := pkg.Current() + r.NoError(err) + + ip := cur.ImportPath + + table := []struct { + in string + }{ + {in: ip}, + {in: "/"}, + {in: ":/"}, + {in: ip + ":/"}, + } + + for _, tt := range table { + s.Run(t, tt.in, func(st *testing.T) { + r := require.New(st) + var act []string + err := pkg.Walk(tt.in, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + act = append(act, path) + return nil + }) + r.NoError(err) + + exp := []string{ + "github.com/markbates/pkger:/", + "github.com/markbates/pkger:/main.go", + "github.com/markbates/pkger:/public", + "github.com/markbates/pkger:/public/images", + "github.com/markbates/pkger:/public/images/mark.png", + "github.com/markbates/pkger:/public/index.html", + "github.com/markbates/pkger:/templates", + "github.com/markbates/pkger:/templates/a.txt", + "github.com/markbates/pkger:/templates/b", + "github.com/markbates/pkger:/templates/b/b.txt", + } + r.Equal(exp, act) + }) + } + +} + +func (s Suite) Test_Remove(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: "/public/images/mark.png"}, + {in: ":/public/images/mark.png"}, + {in: ip + ":/public/images/mark.png"}, + } + + for _, tt := range table { + s.Run(t, tt.in, func(st *testing.T) { + r := require.New(st) + + pkg, err := s.Make() + r.NoError(err) + r.NoError(s.LoadFolder(pkg)) + + _, err = pkg.Stat(tt.in) + r.NoError(err) + + r.NoError(pkg.Remove(tt.in)) + + _, err = pkg.Stat(tt.in) + r.Error(err) + + r.Error(pkg.Remove("unknown")) + }) + } + +} + +func (s Suite) Test_RemoveAll(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: "/public"}, + {in: ":/public"}, + {in: ip + ":/public"}, + } + + for _, tt := range table { + s.Run(t, tt.in, func(st *testing.T) { + r := require.New(st) + + pkg, err := s.Make() + r.NoError(err) + r.NoError(s.LoadFolder(pkg)) + + _, err = pkg.Stat(tt.in) + r.NoError(err) + + r.NoError(pkg.RemoveAll(tt.in)) + + _, err = pkg.Stat(tt.in) + r.Error(err) + }) + } + +} diff --git a/pkging/stdos/file.go b/pkging/stdos/file.go index 303e697..249c277 100644 --- a/pkging/stdos/file.go +++ b/pkging/stdos/file.go @@ -20,11 +20,11 @@ type File struct { } func NewFile(fx pkging.Pkger, osf *os.File) (*File, error) { - pt, err := fx.Parse(osf.Name()) + name := osf.Name() + pt, err := fx.Parse(name) if err != nil { return nil, err } - info, err := osf.Stat() if err != nil { return nil, err @@ -97,15 +97,10 @@ func (f *File) Stat() (os.FileInfo, error) { return f.info, nil } - abs, err := f.Abs() + nf, err := NewFile(f.pkging, f.File) if err != nil { return nil, err } - - info, err := os.Stat(abs) - if err != nil { - return nil, err - } - f.info = pkging.NewFileInfo(info) - return info, nil + (*f) = *nf + return f.info, nil } diff --git a/pkging/stdos/file_test.go b/pkging/stdos/file_test.go new file mode 100644 index 0000000..90e0e5c --- /dev/null +++ b/pkging/stdos/file_test.go @@ -0,0 +1,29 @@ +package stdos + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func Test_File_Stat_No_Info(t *testing.T) { + r := require.New(t) + + pkg, err := New() + r.NoError(err) + + f, err := pkg.Open(":/pkging/stdos/file_test.go") + r.NoError(err) + defer f.Close() + + sf, ok := f.(*File) + r.True(ok) + + oi := sf.info + sf.info = nil + + info, err := sf.Stat() + r.NoError(err) + r.Equal(oi.Name(), info.Name()) + // r.Equal("", f.Name()) +} diff --git a/pkging/stdos/stdos.go b/pkging/stdos/stdos.go index 2b65997..f5cde28 100644 --- a/pkging/stdos/stdos.go +++ b/pkging/stdos/stdos.go @@ -43,13 +43,15 @@ func New() (*Pkger, error) { if err != nil { return nil, err } - return &Pkger{ + p := &Pkger{ infos: &maps.Infos{}, paths: &maps.Paths{ Current: info, }, current: info, - }, nil + } + p.infos.Store(info.ImportPath, info) + return p, nil } func (fx *Pkger) Create(name string) (pkging.File, error) {