fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

1package shards 2 3import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/sourcegraph/zoekt" 9 "github.com/sourcegraph/zoekt/query" 10) 11 12func TestSearchTypeRepo(t *testing.T) { 13 ss := newShardedSearcher(2) 14 nextShardNum := 1 15 addShard := func(docs ...zoekt.Document) { 16 b := testIndexBuilder(t, &zoekt.Repository{ID: 1, Name: "reponame"}, docs...) 17 shard := searcherForTest(t, b) 18 ss.replace(map[string]zoekt.Searcher{fmt.Sprintf("key-%d", nextShardNum): shard}) 19 nextShardNum++ 20 } 21 addShard( 22 zoekt.Document{Name: "f1", Content: []byte("bla the needle")}, 23 zoekt.Document{Name: "f2", Content: []byte("another file another needle")}) 24 addShard( 25 zoekt.Document{Name: "f3", Content: []byte("another shard")}) 26 27 searcher := &typeRepoSearcher{ss} 28 search := func(q query.Q, o ...zoekt.SearchOptions) *zoekt.SearchResult { 29 t.Helper() 30 var opts zoekt.SearchOptions 31 if len(o) > 0 { 32 opts = o[0] 33 } 34 res, err := searcher.Search(context.Background(), q, &opts) 35 if err != nil { 36 t.Fatalf("Search(%s): %v", q, err) 37 } 38 return res 39 } 40 wantSingleMatch := func(res *zoekt.SearchResult, want string) { 41 t.Helper() 42 fmatches := res.Files 43 if len(fmatches) != 1 || len(fmatches[0].LineMatches) != 1 { 44 t.Fatalf("got %v, want 1 matches", fmatches) 45 } 46 got := fmt.Sprintf("%s:%d", fmatches[0].FileName, fmatches[0].LineMatches[0].LineFragments[0].Offset) 47 if got != want { 48 t.Errorf("1: got %s, want %s", got, want) 49 } 50 } 51 52 // type filter matches in different file 53 res := search(query.NewAnd( 54 &query.Type{ 55 Type: query.TypeRepo, 56 Child: &query.Substring{Pattern: "bla"}, 57 }, 58 &query.Substring{Pattern: "file"})) 59 wantSingleMatch(res, "f2:8") 60 61 // type filter matches in same file. Do not include that result 62 res = search(query.NewAnd( 63 &query.Type{ 64 Type: query.TypeRepo, 65 Child: &query.Substring{Pattern: "needle"}, 66 }, 67 &query.Substring{Pattern: "file"})) 68 wantSingleMatch(res, "f2:8") 69 70 // type filter matches path in different file 71 res = search(query.NewAnd( 72 &query.Type{ 73 Type: query.TypeRepo, 74 Child: &query.Substring{Pattern: "f1", FileName: true}, 75 }, 76 &query.Substring{Pattern: "file"})) 77 wantSingleMatch(res, "f2:8") 78 79 // type filter matches path in same file 80 res = search(query.NewAnd( 81 &query.Type{ 82 Type: query.TypeRepo, 83 Child: &query.Substring{Pattern: "f2", FileName: true}, 84 }, 85 &query.Substring{Pattern: "file"})) 86 wantSingleMatch(res, "f2:8") 87 88 // no match by content 89 res = search(query.NewAnd( 90 &query.Type{ 91 Type: query.TypeRepo, 92 Child: &query.Substring{Pattern: "nope"}, 93 }, 94 &query.Substring{Pattern: "file"})) 95 if len(res.Files) != 0 { 96 t.Fatalf("got %v, want 0 matches", len(res.Files)) 97 } 98 99 // no match by path 100 res = search(query.NewAnd( 101 &query.Type{ 102 Type: query.TypeRepo, 103 Child: &query.Substring{Pattern: "nope", FileName: true}, 104 }, 105 &query.Substring{Pattern: "file"})) 106 if len(res.Files) != 0 { 107 t.Fatalf("got %v, want 0 matches", len(res.Files)) 108 } 109 110 // type filter matches in a different shard 111 res = search(query.NewAnd( 112 &query.Type{ 113 Type: query.TypeRepo, 114 Child: &query.Substring{Pattern: "another shard"}, 115 }, 116 &query.Substring{Pattern: "file"})) 117 wantSingleMatch(res, "f2:8") 118}