From 90fba21e05f20ca70a0916c5fa7b202bd3d531e6 Mon Sep 17 00:00:00 2001 From: Patrick Pirringer Date: Thu, 28 May 2020 09:32:21 +0200 Subject: [PATCH] Always accept initial path Always accept the path that packr2 should work on, even if it contains ignored path segments. Ignore prefixes continue to work as before, but only for sub paths. This enables the use of packr2 in CI builds where the project is placed in a temporary location for building, which contains '.' or '_' prefix. --- v2/jam/parser/prospect.go | 28 +++++++++++++++++++------ v2/jam/parser/prospect_test.go | 38 ++++++++++++++++++++-------------- v2/jam/parser/roots.go | 30 +++++++++++++-------------- 3 files changed, 60 insertions(+), 36 deletions(-) diff --git a/v2/jam/parser/prospect.go b/v2/jam/parser/prospect.go index 652db5b..2b368d0 100644 --- a/v2/jam/parser/prospect.go +++ b/v2/jam/parser/prospect.go @@ -11,11 +11,11 @@ import ( var DefaultIgnoredFolders = []string{".", "_", "vendor", "node_modules", "_fixtures", "testdata"} -func IsProspect(path string, ignore ...string) (status bool) { +func IsProspect(root string, path string, ignore ...string) (status bool) { // plog.Debug("parser", "IsProspect", "path", path, "ignore", ignore) defer func() { if status { - plog.Debug("parser", "IsProspect (TRUE)", "path", path, "status", status) + plog.Debug("parser", "IsProspect (TRUE)", "root", root, "path", path, "status", status) } }() if path == "." { @@ -52,10 +52,7 @@ func IsProspect(path string, ignore ...string) (status bool) { ignore[i] = strings.TrimSpace(strings.ToLower(x)) } - parts := strings.Split(resolver.OsPath(path), string(filepath.Separator)) - if len(parts) == 0 { - return false - } + parts := partsUnderRoot(root, path) for _, i := range ignore { for _, p := range parts { @@ -75,3 +72,22 @@ func IsProspect(path string, ignore ...string) (status bool) { return ext == ".go" } + +func partsUnderRoot(root string, path string) []string { + root = filepath.Clean(resolver.OsPath(root)) + path = filepath.Clean(resolver.OsPath(path)) + rootParts := strings.Split(root, string(filepath.Separator)) + pathParts := strings.Split(path, string(filepath.Separator)) + + if len(rootParts) > len(pathParts) { + rootParts = rootParts[:len(pathParts)] + } + + for i, r := range rootParts { + if pathParts[i] != r { + return pathParts[i:] + } + } + + return pathParts[len(rootParts):] +} diff --git a/v2/jam/parser/prospect_test.go b/v2/jam/parser/prospect_test.go index f023f81..f3bda44 100644 --- a/v2/jam/parser/prospect_test.go +++ b/v2/jam/parser/prospect_test.go @@ -1,6 +1,7 @@ package parser import ( + "fmt" "testing" "github.com/stretchr/testify/require" @@ -8,30 +9,37 @@ import ( func Test_IsProspect(t *testing.T) { table := []struct { + root string path string pass bool }{ - {"foo/.git/config", false}, - {"foo/.git/baz.go", false}, - {"a.go", true}, - {".", true}, - {"a/b.go", true}, - {"a/b_test.go", false}, - {"a/b-packr.go", false}, - {"a/vendor/b.go", false}, - {"a/_c/c.go", false}, - {"a/_c/e/fe/f/c.go", false}, - {"a/d/_d.go", false}, - {"a/d/", false}, + {"", "foo/.git/config", false}, + {"", "foo/.git/baz.go", false}, + {"", "a.go", true}, + {"", ".", true}, + {"", "a/b.go", true}, + {"", "a/b_test.go", false}, + {"", "a/b-packr.go", false}, + {"", "a/vendor/b.go", false}, + {"", "a/_c/c.go", false}, + {"", "a/_c/e/fe/f/c.go", false}, + {"", "a/d/_d.go", false}, + {"", "a/d/", false}, + {".", ".", true}, + {"a", "a/b.go", true}, + {"a/vendor", "a/vendor/b.go", true}, + {"a", "a/vendor/b.go", false}, + {".ci", ".ci/a/b.go", true}, + {"a", "a/.ci/b.go", false}, } for _, tt := range table { - t.Run(tt.path, func(st *testing.T) { + t.Run(fmt.Sprintf("%s:%s", tt.root, tt.path), func(st *testing.T) { r := require.New(st) if tt.pass { - r.True(IsProspect(tt.path, ".", "_")) + r.True(IsProspect(tt.root, tt.path, ".", "_")) } else { - r.False(IsProspect(tt.path, ".", "_")) + r.False(IsProspect(tt.root, tt.path, ".", "_")) } }) } diff --git a/v2/jam/parser/roots.go b/v2/jam/parser/roots.go index a550182..6318f83 100644 --- a/v2/jam/parser/roots.go +++ b/v2/jam/parser/roots.go @@ -35,25 +35,25 @@ func NewFromRoots(roots []string, opts *RootsOptions) (*Parser, error) { } p := New() plog.Debug(p, "NewFromRoots", "roots", roots, "options", opts) - callback := func(path string, de *godirwalk.Dirent) error { - if IsProspect(path, opts.Ignores...) { - if de.IsDir() { + for _, root := range roots { + plog.Debug(p, "NewFromRoots", "walking", root) + callback := func(path string, de *godirwalk.Dirent) error { + if IsProspect(root, path, opts.Ignores...) { + if de.IsDir() { + return nil + } + roots = append(roots, path) return nil } - roots = append(roots, path) + if de.IsDir() { + return filepath.SkipDir + } return nil } - if de.IsDir() { - return filepath.SkipDir + wopts := &godirwalk.Options{ + FollowSymbolicLinks: true, + Callback: callback, } - return nil - } - wopts := &godirwalk.Options{ - FollowSymbolicLinks: true, - Callback: callback, - } - for _, root := range roots { - plog.Debug(p, "NewFromRoots", "walking", root) err := godirwalk.Walk(root, wopts) if err != nil { return p, err @@ -70,7 +70,7 @@ func NewFromRoots(roots []string, opts *RootsOptions) (*Parser, error) { names, _ = fd.findAllGoFilesImports(r) } for _, n := range names { - if IsProspect(n) { + if IsProspect(n, n) { plog.Debug(p, "NewFromRoots", "mapping", n) dd[n] = n }