From c5d8e63a0f420d3587bebaeb36df091844909298 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Mon, 8 Jul 2019 14:46:00 -0700 Subject: [PATCH] Fix path separator matching in filecount input (#6077) --- internal/globpath/globpath.go | 11 +++++++---- internal/globpath/globpath_test.go | 11 +++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/internal/globpath/globpath.go b/internal/globpath/globpath.go index b21d93520..d4e7ffd87 100644 --- a/internal/globpath/globpath.go +++ b/internal/globpath/globpath.go @@ -21,7 +21,7 @@ func Compile(path string) (*GlobPath, error) { out := GlobPath{ hasMeta: hasMeta(path), HasSuperMeta: hasSuperMeta(path), - path: path, + path: filepath.FromSlash(path), } // if there are no glob meta characters in the path, don't bother compiling @@ -41,8 +41,9 @@ func Compile(path string) (*GlobPath, error) { return &out, nil } -// Match returns all files matching the expression -// If it's a static path, returns path +// Match returns all files matching the expression. +// If it's a static path, returns path. +// All returned path will have the host platform separator. func (g *GlobPath) Match() []string { if !g.hasMeta { return []string{g.path} @@ -82,7 +83,8 @@ func (g *GlobPath) Match() []string { return out } -// MatchString test a string against the glob +// MatchString tests the path string against the glob. The path should contain +// the host platform separator. func (g *GlobPath) MatchString(path string) bool { if !g.HasSuperMeta { res, _ := filepath.Match(g.path, path) @@ -96,6 +98,7 @@ func (g *GlobPath) MatchString(path string) bool { // - any directory under these roots may contain a matching file // - no file outside of these roots can match the pattern // Note that it returns both files and directories. +// All returned path will have the host platform separator. func (g *GlobPath) GetRoots() []string { if !g.hasMeta { return []string{g.path} diff --git a/internal/globpath/globpath_test.go b/internal/globpath/globpath_test.go index 476ba9243..60562d8f8 100644 --- a/internal/globpath/globpath_test.go +++ b/internal/globpath/globpath_test.go @@ -87,3 +87,14 @@ func TestMatch_ErrPermission(t *testing.T) { require.Equal(t, test.expected, actual) } } + +func TestWindowsSeparator(t *testing.T) { + if runtime.GOOS != "windows" { + t.Skip("Skipping Windows only test") + } + + glob, err := Compile("testdata/nested1") + require.NoError(t, err) + ok := glob.MatchString("testdata\\nested1") + require.True(t, ok) +}