2022-05-07 18:55:15 +00:00
|
|
|
//go:build go1.16
|
|
|
|
// +build go1.16
|
|
|
|
|
|
|
|
// Before Go 1.16 filepath.Match returned early on a failed match,
|
|
|
|
// and thus did not report any later syntax error in the pattern.
|
|
|
|
// https://go.dev/doc/go1.16#path/filepath
|
|
|
|
|
|
|
|
package filter_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/filter"
|
|
|
|
rtest "github.com/restic/restic/internal/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidPatterns(t *testing.T) {
|
|
|
|
// Test invalid patterns are detected and returned
|
|
|
|
t.Run("detect-invalid-patterns", func(t *testing.T) {
|
2022-09-09 20:29:05 +00:00
|
|
|
err := filter.ValidatePatterns([]string{"*.foo", "*[._]log[.-][0-9]", "!*[._]log[.-][0-9]"})
|
2022-05-07 18:55:15 +00:00
|
|
|
|
2022-09-09 20:29:05 +00:00
|
|
|
rtest.Assert(t, err != nil, "Expected invalid patterns to be detected")
|
2022-05-07 18:55:15 +00:00
|
|
|
|
2022-09-09 20:29:05 +00:00
|
|
|
if ip, ok := err.(*filter.InvalidPatternError); ok {
|
|
|
|
rtest.Equals(t, ip.InvalidPatterns, []string{"*[._]log[.-][0-9]", "!*[._]log[.-][0-9]"})
|
|
|
|
} else {
|
|
|
|
t.Errorf("wrong error type %v", err)
|
|
|
|
}
|
2022-05-07 18:55:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Test all patterns defined in matchTests are valid
|
|
|
|
patterns := make([]string, 0)
|
|
|
|
|
|
|
|
for _, data := range matchTests {
|
|
|
|
patterns = append(patterns, data.pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("validate-patterns", func(t *testing.T) {
|
2022-09-09 20:29:05 +00:00
|
|
|
err := filter.ValidatePatterns(patterns)
|
2022-05-07 18:55:15 +00:00
|
|
|
|
2022-09-09 20:29:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2022-05-07 18:55:15 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Test all patterns defined in childMatchTests are valid
|
|
|
|
childPatterns := make([]string, 0)
|
|
|
|
|
|
|
|
for _, data := range childMatchTests {
|
|
|
|
childPatterns = append(childPatterns, data.pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("validate-child-patterns", func(t *testing.T) {
|
2022-09-09 20:29:05 +00:00
|
|
|
err := filter.ValidatePatterns(childPatterns)
|
2022-05-07 18:55:15 +00:00
|
|
|
|
2022-09-09 20:29:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2022-05-07 18:55:15 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|