Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
box.Open: Do not call file.NewFileR when IsDir true, fix #198 (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlepage authored and markbates committed Jul 8, 2019
1 parent d766515 commit 662c20c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
28 changes: 17 additions & 11 deletions v2/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/gobuffalo/packd"
"github.com/gobuffalo/packr/v2/file"
"github.com/gobuffalo/packr/v2/file/resolver"
"github.com/gobuffalo/packr/v2/plog"
"github.com/gobuffalo/packr/v2/internal/takeon/github.com/markbates/oncer"
"github.com/gobuffalo/packr/v2/plog"
)

var _ packd.Box = &Box{}
Expand All @@ -28,12 +28,12 @@ var _ packd.Finder = &Box{}
// Box represent a folder on a disk you want to
// have access to in the built Go binary.
type Box struct {
Path string `json:"path"`
Name string `json:"name"`
ResolutionDir string `json:"resolution_dir"`
DefaultResolver resolver.Resolver `json:"default_resolver"`
resolvers resolversMap
dirs dirsMap
Path string `json:"path"`
Name string `json:"name"`
ResolutionDir string `json:"resolution_dir"`
DefaultResolver resolver.Resolver `json:"default_resolver"`
resolvers resolversMap
dirs dirsMap
}

// NewBox returns a Box that can be used to
Expand Down Expand Up @@ -125,16 +125,16 @@ func (b *Box) Has(name string) bool {

// HasDir returns true if the directory exists in the box
func (b *Box) HasDir(name string) bool {
if name == "/" {
return b.Has("index.html")
}
oncer.Do("packr2/box/HasDir"+b.Name, func() {
for _, f := range b.List() {
for d := filepath.Dir(f); d != "."; d = filepath.Dir(d) {
b.dirs.Store(d, true)
}
}
})
if name == "/" {
return b.Has("index.html")
}
_, ok := b.dirs.Load(name)
return ok
}
Expand All @@ -149,7 +149,13 @@ func (b *Box) Open(name string) (http.File, error) {
}
return f, err
}
f, err = file.NewFileR(name, f)
info, err := f.FileInfo()
if err != nil {
return f, err
}
if !info.IsDir() {
f, err = file.NewFileR(name, f)
}
plog.Debug(b, "Open", "name", f.Name(), "file", f.Name())
return f, err
}
Expand Down
28 changes: 24 additions & 4 deletions v2/box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,30 @@ func Test_Box_Open(t *testing.T) {

box.DefaultResolver = d

for _, x := range []string{"foo.txt", "/foo.txt", "bar", "/bar", "baz", "/baz"} {
f, err := box.Open(x)
r.NoError(err)
r.NotZero(f)
type pathTest struct {
path string
isDir bool
}

pathTests := []pathTest{
{"foo.txt", false},
{"/foo.txt", false},
{"bar", false},
{"/bar", false},
{"baz", true},
{"/baz", true},
{"baz/index.html", false},
{"/baz/index.html", false},
}

for _, x := range pathTests {
f, err := box.Open(x.path)
r.NoError(err, "for path %#v", x.path)
r.NotZero(f, "for path %#v", x.path)

stat, err := f.Stat()
r.NoError(err, "for path %#v", x.path)
r.Equal(x.isDir, stat.IsDir(), "stat.IsDir() != %t for path %#v", true, x.path)
}

f, err := box.Open("idontexist.txt")
Expand Down

0 comments on commit 662c20c

Please sign in to comment.