forked from TrueCloudLab/restic
test: add tests for reading patterns from file
This commit is contained in:
parent
14d2799b44
commit
4e449ffaff
2 changed files with 59 additions and 4 deletions
|
@ -73,12 +73,12 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
|
|||
hasExcludes := len(opts.Excludes) > 0 || len(opts.InsensitiveExcludes) > 0
|
||||
hasIncludes := len(opts.Includes) > 0 || len(opts.InsensitiveIncludes) > 0
|
||||
|
||||
excludePatterns, err := opts.excludePatternOptions.CollectPatterns()
|
||||
excludePatternFns, err := opts.excludePatternOptions.CollectPatterns()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
includePatterns, err := opts.includePatternOptions.CollectPatterns()
|
||||
includePatternFns, err := opts.includePatternOptions.CollectPatterns()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -150,7 +150,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
|
|||
}
|
||||
|
||||
selectExcludeFilter := func(item string, _ string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
|
||||
for _, rejectFn := range excludePatterns {
|
||||
for _, rejectFn := range excludePatternFns {
|
||||
matched := rejectFn(item)
|
||||
|
||||
// An exclude filter is basically a 'wildcard but foo',
|
||||
|
@ -166,7 +166,7 @@ func runRestore(ctx context.Context, opts RestoreOptions, gopts GlobalOptions,
|
|||
}
|
||||
|
||||
selectIncludeFilter := func(item string, _ string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
|
||||
for _, includeFn := range includePatterns {
|
||||
for _, includeFn := range includePatternFns {
|
||||
selectedForRestore, childMayBeSelected = includeFn(item)
|
||||
}
|
||||
|
||||
|
|
|
@ -97,3 +97,58 @@ func TestRestoreFailsWhenUsingInvalidPatterns(t *testing.T) {
|
|||
*[._]log[.-][0-9]
|
||||
!*[._]log[.-][0-9]`, err.Error())
|
||||
}
|
||||
|
||||
func TestRestoreFailsWhenUsingInvalidPatternsFromFile(t *testing.T) {
|
||||
env, cleanup := withTestEnvironment(t)
|
||||
defer cleanup()
|
||||
|
||||
testRunInit(t, env.gopts)
|
||||
|
||||
// Create an include file with some invalid patterns
|
||||
includeFile := env.base + "/includefile"
|
||||
fileErr := os.WriteFile(includeFile, []byte("*.go\n*[._]log[.-][0-9]\n!*[._]log[.-][0-9]"), 0644)
|
||||
if fileErr != nil {
|
||||
t.Fatalf("Could not write include file: %v", fileErr)
|
||||
}
|
||||
|
||||
err := testRunRestoreAssumeFailure("latest", RestoreOptions{includePatternOptions: includePatternOptions{IncludeFiles: []string{includeFile}}}, env.gopts)
|
||||
rtest.Equals(t, `Fatal: --include-file: invalid pattern(s) provided:
|
||||
*[._]log[.-][0-9]
|
||||
!*[._]log[.-][0-9]`, err.Error())
|
||||
|
||||
// Create an exclude file with some invalid patterns
|
||||
excludeFile := env.base + "/excludefile"
|
||||
fileErr = os.WriteFile(excludeFile, []byte("*.go\n*[._]log[.-][0-9]\n!*[._]log[.-][0-9]"), 0644)
|
||||
if fileErr != nil {
|
||||
t.Fatalf("Could not write exclude file: %v", fileErr)
|
||||
}
|
||||
|
||||
err = testRunRestoreAssumeFailure("latest", RestoreOptions{excludePatternOptions: excludePatternOptions{ExcludeFiles: []string{excludeFile}}}, env.gopts)
|
||||
rtest.Equals(t, `Fatal: --exclude-file: invalid pattern(s) provided:
|
||||
*[._]log[.-][0-9]
|
||||
!*[._]log[.-][0-9]`, err.Error())
|
||||
|
||||
// Create an insentive include file with some invalid patterns
|
||||
insensitiveIncludeFile := env.base + "/insensitiveincludefile"
|
||||
fileErr = os.WriteFile(insensitiveIncludeFile, []byte("*.go\n*[._]log[.-][0-9]\n!*[._]log[.-][0-9]"), 0644)
|
||||
if fileErr != nil {
|
||||
t.Fatalf("Could not write insensitive include file: %v", fileErr)
|
||||
}
|
||||
|
||||
err = testRunRestoreAssumeFailure("latest", RestoreOptions{includePatternOptions: includePatternOptions{InsensitiveIncludeFiles: []string{insensitiveIncludeFile}}}, env.gopts)
|
||||
rtest.Equals(t, `Fatal: --iinclude-file: invalid pattern(s) provided:
|
||||
*[._]log[.-][0-9]
|
||||
!*[._]log[.-][0-9]`, err.Error())
|
||||
|
||||
// Create an insensitive exclude file with some invalid patterns
|
||||
insensitiveExcludeFile := env.base + "/insensitiveexcludefile"
|
||||
fileErr = os.WriteFile(insensitiveExcludeFile, []byte("*.go\n*[._]log[.-][0-9]\n!*[._]log[.-][0-9]"), 0644)
|
||||
if fileErr != nil {
|
||||
t.Fatalf("Could not write insensitive exclude file: %v", fileErr)
|
||||
}
|
||||
|
||||
err = testRunRestoreAssumeFailure("latest", RestoreOptions{excludePatternOptions: excludePatternOptions{InsensitiveExcludeFiles: []string{insensitiveExcludeFile}}}, env.gopts)
|
||||
rtest.Equals(t, `Fatal: --iexclude-file: invalid pattern(s) provided:
|
||||
*[._]log[.-][0-9]
|
||||
!*[._]log[.-][0-9]`, err.Error())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue