From a00a0471a8714fddfffef47b7079880c7efc725b Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 8 Aug 2019 13:58:48 +0100 Subject: [PATCH] filter: Add UsesDirectoryFilters method --- fs/filter/filter.go | 16 +++++++++ fs/filter/filter_test.go | 73 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/fs/filter/filter.go b/fs/filter/filter.go index aa9d45ce1..3944c2df7 100644 --- a/fs/filter/filter.go +++ b/fs/filter/filter.go @@ -570,3 +570,19 @@ func (f *Filter) MakeListR(ctx context.Context, NewObject func(ctx context.Conte return g.Wait() } } + +// UsesDirectoryFilters returns true if the filter uses directory +// filters and false if it doesn't. +// +// This is used in deciding whether to walk directories or use ListR +func (f *Filter) UsesDirectoryFilters() bool { + if len(f.dirRules.rules) == 0 { + return false + } + rule := f.dirRules.rules[0] + re := rule.Regexp.String() + if rule.Include == true && re == "^.*$" { + return false + } + return true +} diff --git a/fs/filter/filter_test.go b/fs/filter/filter_test.go index e5c360ab2..276ed83e6 100644 --- a/fs/filter/filter_test.go +++ b/fs/filter/filter_test.go @@ -589,3 +589,76 @@ func TestFilterMatchesFromDocs(t *testing.T) { } } } + +func TestNewFilterUsesDirectoryFilters(t *testing.T) { + for i, test := range []struct { + rules []string + want bool + }{ + { + rules: []string{}, + want: false, + }, + { + rules: []string{ + "+ *", + }, + want: false, + }, + { + rules: []string{ + "+ *.jpg", + "- *", + }, + want: false, + }, + { + rules: []string{ + "- *.jpg", + }, + want: false, + }, + { + rules: []string{ + "- *.jpg", + "+ *", + }, + want: false, + }, + { + rules: []string{ + "+ dir/*.jpg", + "- *", + }, + want: true, + }, + { + rules: []string{ + "+ dir/**", + }, + want: true, + }, + { + rules: []string{ + "- dir/**", + }, + want: true, + }, + { + rules: []string{ + "- /dir/**", + }, + want: true, + }, + } { + what := fmt.Sprintf("#%d", i) + f, err := NewFilter(nil) + require.NoError(t, err) + for _, rule := range test.rules { + err := f.AddRule(rule) + require.NoError(t, err, what) + } + got := f.UsesDirectoryFilters() + assert.Equal(t, test.want, got, fmt.Sprintf("%s: %s", what, f.DumpFilters())) + } +}