From 78c1f2839e1ad2f5d9173b2ae6d61720cb8f55ee Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 7 Dec 2016 11:17:10 +0000 Subject: [PATCH] Fix filters to add ** rules to directory rules This fixes `--exclude ".*{,/**}"` to exclude all . files and . directories. --- fs/filter.go | 2 +- fs/filter_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/fs/filter.go b/fs/filter.go index 20e16e2de..ee14d2d22 100644 --- a/fs/filter.go +++ b/fs/filter.go @@ -273,7 +273,7 @@ func (f *Filter) addDirGlobs(Include bool, glob string) error { func (f *Filter) Add(Include bool, glob string) error { isDirRule := strings.HasSuffix(glob, "/") isFileRule := !isDirRule - if strings.HasSuffix(glob, "**") { + if strings.Contains(glob, "**") { isDirRule, isFileRule = true, true } re, err := globToRegexp(glob) diff --git a/fs/filter_test.go b/fs/filter_test.go index b0fe17bc8..3e24b6b84 100644 --- a/fs/filter_test.go +++ b/fs/filter_test.go @@ -1,6 +1,7 @@ package fs import ( + "fmt" "io/ioutil" "os" "strings" @@ -338,6 +339,71 @@ func TestNewFilterMatches(t *testing.T) { assert.False(t, f.InActive()) } +func TestFilterAddDirRuleOrFileRule(t *testing.T) { + for _, test := range []struct { + included bool + glob string + want string + }{ + { + false, + "potato", + `--- File filter rules --- +- (^|/)potato$ +--- Directory filter rules ---`, + }, + { + true, + "potato", + `--- File filter rules --- ++ (^|/)potato$ +--- Directory filter rules --- ++ ^.*$`, + }, + { + false, + "*", + `--- File filter rules --- +- (^|/)[^/]*$ +--- Directory filter rules --- +- ^.*$`, + }, + { + true, + "*", + `--- File filter rules --- ++ (^|/)[^/]*$ +--- Directory filter rules --- ++ ^.*$`, + }, + { + false, + ".*{,/**}", + `--- File filter rules --- +- (^|/)\.[^/]*(|/.*)$ +--- Directory filter rules --- +- (^|/)\.[^/]*(|/.*)$`, + }, + { + true, + "a/b/c/d", + `--- File filter rules --- ++ (^|/)a/b/c/d$ +--- Directory filter rules --- ++ (^|/)a/b/c/$ ++ (^|/)a/b/$ ++ (^|/)a/$`, + }, + } { + f, err := NewFilter() + require.NoError(t, err) + err = f.Add(test.included, test.glob) + require.NoError(t, err) + got := f.DumpFilters() + assert.Equal(t, test.want, got, fmt.Sprintf("Add(%v, %q)", test.included, test.glob)) + } +} + func TestFilterForEachLine(t *testing.T) { file := testFile(t, `; comment one