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

Commit

Permalink
Merge pull request #34 from jasonish/directory-404
Browse files Browse the repository at this point in the history
rfc: error on virtual directory that does not exist
  • Loading branch information
markbates authored Jan 4, 2018
2 parents 4b0f48d + 48a3dc0 commit 7458c91
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
29 changes: 25 additions & 4 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -45,9 +46,10 @@ func NewBox(path string) Box {
// Box represent a folder on a disk you want to
// have access to in the built Go binary.
type Box struct {
Path string
callingDir string
data map[string][]byte
Path string
callingDir string
data map[string][]byte
directories map[string]bool
}

// String of the file asked for or an empty string.
Expand Down Expand Up @@ -102,6 +104,10 @@ func (b Box) decompress(bb []byte) []byte {
}

func (b Box) find(name string) (File, error) {
if b.directories == nil {
b.indexDirectories()
}

cleanName := filepath.ToSlash(filepath.Clean(name))
// Ensure name is not outside the box
if strings.HasPrefix(cleanName, "../") {
Expand All @@ -121,7 +127,10 @@ func (b Box) find(name string) (File, error) {
// returns http.StatusNotFound instead of http.StatusInternalServerError.
return nil, os.ErrNotExist
}
return newVirtualDir(cleanName), nil
if _, ok := b.directories[cleanName]; ok {
return newVirtualDir(cleanName), nil
}
return nil, os.ErrNotExist
}

// Not found in the box virtual fs, try to get it from the file system
Expand Down Expand Up @@ -187,3 +196,15 @@ func (b Box) List() []string {
}
return keys
}

func (b *Box) indexDirectories() {
b.directories = map[string]bool{}
if _, ok := data[b.Path]; ok {
for name, _ := range data[b.Path] {
prefix, _ := path.Split(name)
// Even on Windows the suffix appears to be a /
prefix = strings.TrimSuffix(prefix, "/")
b.directories[prefix] = true
}
}
}
12 changes: 10 additions & 2 deletions box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func Test_Box_Walk_Virtual(t *testing.T) {
return nil
})
r.NoError(err)
r.Equal(3, count)
r.Equal(4, count)
}

func Test_List_Virtual(t *testing.T) {
r := require.New(t)
mustHave := []string{"a", "b", "c"}
mustHave := []string{"a", "b", "c", "d/a"}
actual := virtualBox.List()
sort.Strings(actual)
r.Equal(mustHave, actual)
Expand Down Expand Up @@ -115,3 +115,11 @@ func Test_Box_find(t *testing.T) {
})
}
}

func Test_Virtual_Directory_Not_Found(t *testing.T) {
r := require.New(t)
_, err := virtualBox.find("d")
r.NoError(err)
_, err = virtualBox.find("does-not-exist")
r.Error(err)
}
1 change: 1 addition & 0 deletions packr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func init() {
PackBytes(virtualBox.Path, "a", []byte("a"))
PackBytes(virtualBox.Path, "b", []byte("b"))
PackBytes(virtualBox.Path, "c", []byte("c"))
PackBytes(virtualBox.Path, "d/a", []byte("d/a"))
}

func Test_PackBytes(t *testing.T) {
Expand Down

0 comments on commit 7458c91

Please sign in to comment.