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

Commit

Permalink
Fix and add tests for box paths relative to an imported package (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitspill authored and markbates committed Jan 2, 2019
1 parent 6794593 commit f9dae4b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 15 deletions.
8 changes: 8 additions & 0 deletions v2/_fixtures/import_pkg/import_pkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package import_pkg

import (
"github.com/gobuffalo/packr/v2"
)

var BoxTestNew = packr.New("pkg_test", "./pkg_test")
var BoxTestNewBox = packr.NewBox("./pkg_test")
22 changes: 22 additions & 0 deletions v2/_fixtures/import_pkg/import_pkg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package import_pkg

import (
"testing"

"github.com/gobuffalo/packr/v2"
"github.com/stretchr/testify/require"
)

func Test_NewBox(t *testing.T) {
r := require.New(t)

box := packr.NewBox("./pkg_test")
r.Len(box.List(), 2)
}

func Test_New(t *testing.T) {
r := require.New(t)

box := packr.New("pkg_test", "./pkg_test")
r.Len(box.List(), 2)
}
Empty file.
Empty file.
46 changes: 31 additions & 15 deletions v2/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,53 @@ func NewBox(path string) *Box {
func resolutionDir(og string) string {
ng, _ := filepath.Abs(og)

exists := func(s string) bool {
_, err := os.Stat(s)
if err != nil {
return false
}
plog.Debug("packr", "resolutionDir", "original", og, "resolved", s)
return true
if resolutionDirExists(ng, og) {
return ng
}

if exists(ng) {
// packr.New
_, filename, _, _ := runtime.Caller(3)
ng, ok := resolutionDirTestFilename(filename, og)
if ok {
return ng
}

_, filename, _, _ := runtime.Caller(2)
// packr.NewBox (deprecated)
_, filename, _, _ = runtime.Caller(4)
ng, ok = resolutionDirTestFilename(filename, og)
if ok {
return ng
}

ng = filepath.Join(filepath.Dir(filename), og)
return og
}

func resolutionDirExists(s, og string) bool {
_, err := os.Stat(s)
if err != nil {
return false
}
plog.Debug("packr", "resolutionDir", "original", og, "resolved", s)
return true
}

func resolutionDirTestFilename(filename, og string) (string, bool) {
ng := filepath.Join(filepath.Dir(filename), og)

// // this little hack courtesy of the `-cover` flag!!
cov := filepath.Join("_test", "_obj_test")
ng = strings.Replace(ng, string(filepath.Separator)+cov, "", 1)

if exists(ng) {
return ng
if resolutionDirExists(ng, og) {
return ng, true
}

ng = filepath.Join(envy.GoPath(), "src", ng)
if exists(ng) {
return ng
if resolutionDirExists(ng, og) {
return ng, true
}

return og
return og, false
}

func construct(name string, path string) *Box {
Expand Down
16 changes: 16 additions & 0 deletions v2/box_import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package packr_test

import (
"testing"

"github.com/gobuffalo/packr/v2/_fixtures/import_pkg"
"github.com/stretchr/testify/require"
)

func Test_ImportWithBox(t *testing.T) {
r := require.New(t)

r.Len(import_pkg.BoxTestNew.List(), 2)

r.Len(import_pkg.BoxTestNewBox.List(), 2)
}
2 changes: 2 additions & 0 deletions v2/travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ go install -v ./packr2
packr2 -v clean
packr2 -v
go test -v -timeout=5s -race ./...
packr2 -v clean
go test -v -timeout=5s -race ./...

0 comments on commit f9dae4b

Please sign in to comment.