mirror of
https://github.com/rclone/rclone.git
synced 2024-11-22 15:30:06 +08:00
filter: Add UsesDirectoryFilters method
This commit is contained in:
parent
9e81fc343e
commit
a00a0471a8
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user