Fix path separator matching in filecount input (#6077)

This commit is contained in:
Daniel Nelson 2019-07-08 14:46:00 -07:00 committed by GitHub
parent 5dea2175d2
commit c5d8e63a0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -21,7 +21,7 @@ func Compile(path string) (*GlobPath, error) {
out := GlobPath{ out := GlobPath{
hasMeta: hasMeta(path), hasMeta: hasMeta(path),
HasSuperMeta: hasSuperMeta(path), HasSuperMeta: hasSuperMeta(path),
path: path, path: filepath.FromSlash(path),
} }
// if there are no glob meta characters in the path, don't bother compiling // 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 return &out, nil
} }
// Match returns all files matching the expression // Match returns all files matching the expression.
// If it's a static path, returns path // If it's a static path, returns path.
// All returned path will have the host platform separator.
func (g *GlobPath) Match() []string { func (g *GlobPath) Match() []string {
if !g.hasMeta { if !g.hasMeta {
return []string{g.path} return []string{g.path}
@ -82,7 +83,8 @@ func (g *GlobPath) Match() []string {
return out 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 { func (g *GlobPath) MatchString(path string) bool {
if !g.HasSuperMeta { if !g.HasSuperMeta {
res, _ := filepath.Match(g.path, path) 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 // - any directory under these roots may contain a matching file
// - no file outside of these roots can match the pattern // - no file outside of these roots can match the pattern
// Note that it returns both files and directories. // Note that it returns both files and directories.
// All returned path will have the host platform separator.
func (g *GlobPath) GetRoots() []string { func (g *GlobPath) GetRoots() []string {
if !g.hasMeta { if !g.hasMeta {
return []string{g.path} return []string{g.path}

View File

@ -87,3 +87,14 @@ func TestMatch_ErrPermission(t *testing.T) {
require.Equal(t, test.expected, actual) 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)
}