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

Commit 662c20c

Browse files
nlepagemarkbates
authored andcommitted
box.Open: Do not call file.NewFileR when IsDir true, fix #198 (#201)
1 parent d766515 commit 662c20c

2 files changed

Lines changed: 41 additions & 15 deletions

File tree

v2/box.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
"github.com/gobuffalo/packd"
1616
"github.com/gobuffalo/packr/v2/file"
1717
"github.com/gobuffalo/packr/v2/file/resolver"
18-
"github.com/gobuffalo/packr/v2/plog"
1918
"github.com/gobuffalo/packr/v2/internal/takeon/github.com/markbates/oncer"
19+
"github.com/gobuffalo/packr/v2/plog"
2020
)
2121

2222
var _ packd.Box = &Box{}
@@ -28,12 +28,12 @@ var _ packd.Finder = &Box{}
2828
// Box represent a folder on a disk you want to
2929
// have access to in the built Go binary.
3030
type Box struct {
31-
Path string `json:"path"`
32-
Name string `json:"name"`
33-
ResolutionDir string `json:"resolution_dir"`
34-
DefaultResolver resolver.Resolver `json:"default_resolver"`
35-
resolvers resolversMap
36-
dirs dirsMap
31+
Path string `json:"path"`
32+
Name string `json:"name"`
33+
ResolutionDir string `json:"resolution_dir"`
34+
DefaultResolver resolver.Resolver `json:"default_resolver"`
35+
resolvers resolversMap
36+
dirs dirsMap
3737
}
3838

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

126126
// HasDir returns true if the directory exists in the box
127127
func (b *Box) HasDir(name string) bool {
128+
if name == "/" {
129+
return b.Has("index.html")
130+
}
128131
oncer.Do("packr2/box/HasDir"+b.Name, func() {
129132
for _, f := range b.List() {
130133
for d := filepath.Dir(f); d != "."; d = filepath.Dir(d) {
131134
b.dirs.Store(d, true)
132135
}
133136
}
134137
})
135-
if name == "/" {
136-
return b.Has("index.html")
137-
}
138138
_, ok := b.dirs.Load(name)
139139
return ok
140140
}
@@ -149,7 +149,13 @@ func (b *Box) Open(name string) (http.File, error) {
149149
}
150150
return f, err
151151
}
152-
f, err = file.NewFileR(name, f)
152+
info, err := f.FileInfo()
153+
if err != nil {
154+
return f, err
155+
}
156+
if !info.IsDir() {
157+
f, err = file.NewFileR(name, f)
158+
}
153159
plog.Debug(b, "Open", "name", f.Name(), "file", f.Name())
154160
return f, err
155161
}

v2/box_test.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,30 @@ func Test_Box_Open(t *testing.T) {
190190

191191
box.DefaultResolver = d
192192

193-
for _, x := range []string{"foo.txt", "/foo.txt", "bar", "/bar", "baz", "/baz"} {
194-
f, err := box.Open(x)
195-
r.NoError(err)
196-
r.NotZero(f)
193+
type pathTest struct {
194+
path string
195+
isDir bool
196+
}
197+
198+
pathTests := []pathTest{
199+
{"foo.txt", false},
200+
{"/foo.txt", false},
201+
{"bar", false},
202+
{"/bar", false},
203+
{"baz", true},
204+
{"/baz", true},
205+
{"baz/index.html", false},
206+
{"/baz/index.html", false},
207+
}
208+
209+
for _, x := range pathTests {
210+
f, err := box.Open(x.path)
211+
r.NoError(err, "for path %#v", x.path)
212+
r.NotZero(f, "for path %#v", x.path)
213+
214+
stat, err := f.Stat()
215+
r.NoError(err, "for path %#v", x.path)
216+
r.Equal(x.isDir, stat.IsDir(), "stat.IsDir() != %t for path %#v", true, x.path)
197217
}
198218

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

0 commit comments

Comments
 (0)