func BenchmarkFilterLines(b *testing.B) { pattern := "sdk/*/cpp/*/*vars.html" lines := extractTestLines(b) var c uint b.ResetTimer() for i := 0; i < b.N; i++ { c = 0 for _, line := range lines { match, err := filter.Match(pattern, line) if err != nil { b.Fatal(err) } if match { c++ } } if c != 3 { b.Fatalf("wrong number of matches: expected 3, got %d", c) } } }
func TestFilterPatternsFile(t *testing.T) { lines := extractTestLines(t) var testPatterns = []struct { pattern string hits uint }{ {"*.html", 18249}, {"sdk", 22186}, {"sdk/*/cpp/*/*vars.html", 3}, } for _, test := range testPatterns { var c uint for _, line := range lines { match, err := filter.Match(test.pattern, line) if err != nil { t.Error(err) continue } if match { c++ // fmt.Printf("pattern %q, line %q\n", test.pattern, line) } } if c != test.hits { t.Errorf("wrong number of hits for pattern %q: want %d, got %d", test.pattern, test.hits, c) } } }
func testpattern(t *testing.T, pattern, path string, shouldMatch bool) { match, err := filter.Match(pattern, path) if err != nil { t.Errorf("test pattern %q failed: expected no error for path %q, but error returned: %v", pattern, path, err) } if match != shouldMatch { t.Errorf("test: filter.Match(%q, %q): expected %v, got %v", pattern, path, shouldMatch, match) } }
func TestMatch(t *testing.T) { for i, test := range matchTests { match, err := filter.Match(test.pattern, test.path) if err != nil { t.Errorf("test %d failed: expected no error for pattern %q, but error returned: %v", i, test.pattern, err) continue } if match != test.match { t.Errorf("test %d: filter.Match(%q, %q): expected %v, got %v", i, test.pattern, test.path, test.match, match) } } }
func TestRestoreFilter(t *testing.T) { testfiles := []struct { name string size uint }{ {"testfile1.c", 100}, {"testfile2.exe", 101}, {"subdir1/subdir2/testfile3.docx", 102}, {"subdir1/subdir2/testfile4.c", 102}, } withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { cmdInit(t, global) for _, test := range testfiles { p := filepath.Join(env.testdata, test.name) OK(t, os.MkdirAll(filepath.Dir(p), 0755)) OK(t, appendRandomData(p, test.size)) } cmdBackup(t, global, []string{env.testdata}, nil) cmdCheck(t, global) snapshotID := cmdList(t, global, "snapshots")[0] // no restore filter should restore all files cmdRestore(t, global, filepath.Join(env.base, "restore0"), snapshotID) for _, test := range testfiles { OK(t, testFileSize(filepath.Join(env.base, "restore0", "testdata", test.name), int64(test.size))) } for i, pat := range []string{"*.c", "*.exe", "*", "*file3*"} { base := filepath.Join(env.base, fmt.Sprintf("restore%d", i+1)) cmdRestoreExcludes(t, global, base, snapshotID, []string{pat}) for _, test := range testfiles { err := testFileSize(filepath.Join(base, "testdata", test.name), int64(test.size)) if ok, _ := filter.Match(pat, filepath.Base(test.name)); !ok { OK(t, err) } else { Assert(t, os.IsNotExist(err), "expected %v to not exist in restore step %v, but it exists, err %v", test.name, i+1, err) } } } }) }
func ExampleMatch_wildcards() { match, _ := filter.Match("/home/[uU]ser/?.go", "/home/user/F.go") fmt.Printf("match: %v\n", match) // Output: // match: true }
func ExampleMatch() { match, _ := filter.Match("*.go", "/home/user/file.go") fmt.Printf("match: %v\n", match) // Output: // match: true }