filter: Add UsesDirectoryFilters method

This commit is contained in:
Nick Craig-Wood 2019-08-08 13:58:48 +01:00
parent 9e81fc343e
commit a00a0471a8
2 changed files with 89 additions and 0 deletions

View File

@ -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
}

View File

@ -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()))
}
}