From 9666017c2c0c9da081840aeb25f0bcb99a7dc14e Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sun, 1 Sep 2019 13:21:34 -0400 Subject: [PATCH] champagne illinois --- fs/fstest/fstest.go | 14 +++++------ fs/fstest/suite.go | 53 +++++++++++++++++++++++++++++++++++++++--- fs/memfs/stat.go | 2 +- internal/maps/paths.go | 6 +++-- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/fs/fstest/fstest.go b/fs/fstest/fstest.go index d2d5856..342d371 100644 --- a/fs/fstest/fstest.go +++ b/fs/fstest/fstest.go @@ -2,7 +2,6 @@ package fstest import ( "path" - "strings" "github.com/markbates/pkger/fs" ) @@ -23,14 +22,13 @@ func Files(fx fs.FileSystem) (TestFiles, error) { return tf, nil } -func Path(fx fs.FileSystem, ps ...string) (fs.Path, error) { - name := path.Join(ps...) - name = path.Join(".fstest", name) - if !strings.HasPrefix(name, "/") { - name = "/" + name +func Path(fx fs.FileSystem, p string) (fs.Path, error) { + pt, err := fx.Parse(p) + if err != nil { + return pt, err } - - return fx.Parse(name) + pt.Name = path.Join("/.fstest", pt.Name) + return pt, nil } var fileList = []string{ diff --git a/fs/fstest/suite.go b/fs/fstest/suite.go index 3680248..d243048 100644 --- a/fs/fstest/suite.go +++ b/fs/fstest/suite.go @@ -63,7 +63,7 @@ func (s *FileSystem) sub(t *testing.T, m reflect.Method) { } func (s *FileSystem) Clean() error { - pt, err := Path(s) + pt, err := Path(s, "/") if err != nil { return err } @@ -81,7 +81,7 @@ func (s *FileSystem) Clean() error { func (s *FileSystem) Test_Create(t *testing.T) { r := require.New(t) - pt, err := Path(s, "i", "want", "candy.song") + pt, err := Path(s, "i/want/candy.song") r.NoError(err) f, err := s.Create(pt.Name) @@ -139,6 +139,10 @@ func (s *FileSystem) Test_Parse(t *testing.T) { {in: ":/foo.go", exp: fs.Path{Pkg: ip, Name: "/foo.go"}}, {in: ip + ":/foo.go", exp: fs.Path{Pkg: ip, Name: "/foo.go"}}, {in: ip, exp: fs.Path{Pkg: ip, Name: "/"}}, + {in: ":", exp: fs.Path{Pkg: ip, Name: "/"}}, + {in: "github.com/old/97s:/foo.go", exp: fs.Path{Pkg: "github.com/old/97s", Name: "/foo.go"}}, + {in: "github.com/old/97s", exp: fs.Path{Pkg: "github.com/old/97s", Name: "/"}}, + {in: "github.com/old/97s:", exp: fs.Path{Pkg: "github.com/old/97s", Name: "/"}}, } for _, tt := range table { @@ -157,7 +161,50 @@ func (s *FileSystem) Test_ReadFile(t *testing.T) { } func (s *FileSystem) Test_Stat(t *testing.T) { - panic("not implemented") + r := require.New(t) + + cur, err := s.Current() + r.NoError(err) + + ip := cur.ImportPath + table := []struct { + in string + err bool + }{ + {in: "/foo.go", err: false}, + {in: ":/foo.go", err: false}, + {in: ip + ":/foo.go", err: false}, + {in: ip, err: false}, + {in: "/no.go", err: true}, + } + + for _, tt := range table { + t.Run(tt.in, func(st *testing.T) { + r := require.New(st) + + if tt.err { + _, err := s.Stat(tt.in) + r.Error(err) + return + } + + pt, err := Path(s, tt.in) + fmt.Println(">>>TODO fs/fstest/suite.go:189: tt.in ", tt.in) + fmt.Println(">>>TODO fs/fstest/suite.go:189: pt ", pt) + r.NoError(err) + + // r.Fail(pt.String()) + // f, err := s.Create(tt.in) + // r.NoError(err) + // _, err = io.Copy(f, strings.NewReader("!"+pt.String())) + // r.NoError(err) + // r.NoError(f.Close()) + + info, err := s.Stat(tt.in) + r.NoError(err) + r.Equal(pt.Name, info.Name()) + }) + } } func (s *FileSystem) Test_Walk(t *testing.T) { diff --git a/fs/memfs/stat.go b/fs/memfs/stat.go index 67f702f..0e9fc6f 100644 --- a/fs/memfs/stat.go +++ b/fs/memfs/stat.go @@ -14,5 +14,5 @@ func (fx *FS) Stat(name string) (os.FileInfo, error) { if ok { return f.Stat() } - return nil, fmt.Errorf("could not stat %s", name) + return nil, fmt.Errorf("could not stat %s", pt) } diff --git a/internal/maps/paths.go b/internal/maps/paths.go index 356bec9..9f64b50 100644 --- a/internal/maps/paths.go +++ b/internal/maps/paths.go @@ -130,12 +130,14 @@ func (m *Paths) Keys() []string { func (m *Paths) Parse(p string) (fs.Path, error) { p = strings.Replace(p, "\\", "/", -1) + p = strings.TrimSpace(p) + pt, ok := m.Load(p) if ok { return pt, nil } - if len(p) == 0 { - return m.build(p, "", "") + if len(p) == 0 || p == ":" { + return m.build("", "", "") } res := pathrx.FindAllStringSubmatch(p, -1)