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

Configure Feed

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

Add small test for v15 backwards compatibility (#23)

* add test for backwards compatibility for v15

* update version

* WIP use smaller index

Change-Id: Id28f9477a400b7d5649bbc0e8a4d567813792fae

* reduce test index + use golden file

* cleanup

* update test

author
Kevin Zheng
committer
GitHub
date (Aug 29, 2019, 12:40 AM -0700) commit a9f49976 parent 5baf23ee
+423
+115
read_test.go
··· 16 16 17 17 import ( 18 18 "bytes" 19 + "context" 20 + "encoding/json" 21 + "flag" 22 + "fmt" 23 + "io/ioutil" 24 + "os" 19 25 "reflect" 20 26 "testing" 27 + 28 + "github.com/google/zoekt/query" 21 29 ) 30 + 31 + var update = flag.Bool("update", false, "update golden files") 22 32 23 33 func TestReadWrite(t *testing.T) { 24 34 b, err := NewIndexBuilder(nil) ··· 98 108 t.Errorf("got trigram bcd at bits %v, want sz 2", data.fileNameNgrams) 99 109 } 100 110 } 111 + 112 + func loadShard(fn string) (Searcher, error) { 113 + f, err := os.Open(fn) 114 + if err != nil { 115 + return nil, err 116 + } 117 + 118 + iFile, err := NewIndexFile(f) 119 + if err != nil { 120 + return nil, err 121 + } 122 + s, err := NewSearcher(iFile) 123 + if err != nil { 124 + iFile.Close() 125 + return nil, fmt.Errorf("NewSearcher(%s): %v", fn, err) 126 + } 127 + 128 + return s, nil 129 + } 130 + 131 + func TestReadSearch(t *testing.T) { 132 + type out struct { 133 + FormatVersion int 134 + FeatureVersion int 135 + FileMatches [][]FileMatch 136 + } 137 + 138 + qs := []query.Q{ 139 + &query.Substring{Pattern: "func main", Content: true}, 140 + &query.Regexp{Regexp: mustParseRE("^package"), Content: true}, 141 + &query.Symbol{&query.Substring{Pattern: "num"}}, 142 + &query.Symbol{&query.Regexp{Regexp: mustParseRE("sage$")}}, 143 + } 144 + 145 + shards := []string{"ctagsrepo_v16.00000", "repo_v15.00000", "repo_v16.00000"} 146 + for _, name := range shards { 147 + shard, err := loadShard("testdata/shards/" + name + ".zoekt") 148 + if err != nil { 149 + t.Fatalf("error loading shard %s %v", name, err) 150 + } 151 + 152 + index, ok := shard.(*indexData) 153 + if !ok { 154 + t.Fatalf("expected *indexData for %s", name) 155 + } 156 + 157 + golden := "testdata/golden/TestReadSearch/" + name + ".golden" 158 + 159 + if *update { 160 + got := out{ 161 + FormatVersion: index.metaData.IndexFormatVersion, 162 + FeatureVersion: index.metaData.IndexFeatureVersion, 163 + } 164 + for _, q := range qs { 165 + res, err := shard.Search(context.Background(), q, &SearchOptions{}) 166 + if err != nil { 167 + t.Fatalf("failed search %s on %s during updating: %v", q, name, err) 168 + } 169 + got.FileMatches = append(got.FileMatches, res.Files) 170 + } 171 + 172 + if raw, err := json.MarshalIndent(got, "", " "); err != nil { 173 + t.Errorf("failed marshalling search results for %s during updating: %v", name, err) 174 + continue 175 + } else if err := ioutil.WriteFile(golden, raw, 0644); err != nil { 176 + t.Errorf("failed writing search results for %s during updating: %v", name, err) 177 + continue 178 + } 179 + } 180 + 181 + var want out 182 + if buf, err := ioutil.ReadFile(golden); err != nil { 183 + t.Fatalf("failed reading search results for %s: %v", name, err) 184 + } else if err := json.Unmarshal(buf, &want); err != nil { 185 + t.Fatalf("failed unmarshalling search results for %s: %v", name, err) 186 + } 187 + 188 + if index.metaData.IndexFormatVersion != want.FormatVersion { 189 + t.Errorf("got %d index format version, want %d for %s", index.metaData.IndexFormatVersion, want.FormatVersion, name) 190 + } 191 + 192 + if index.metaData.IndexFeatureVersion != want.FeatureVersion { 193 + t.Errorf("got %d index feature version, want %d for %s", index.metaData.IndexFeatureVersion, want.FeatureVersion, name) 194 + } 195 + 196 + for j, q := range qs { 197 + res, err := shard.Search(context.Background(), q, &SearchOptions{}) 198 + if err != nil { 199 + t.Fatalf("failed search %s on %s: %v", q, name, err) 200 + } 201 + 202 + if len(res.Files) != len(want.FileMatches[j]) { 203 + t.Fatalf("got %d file matches for %s on %s, want %d", len(res.Files), q, name, len(want.FileMatches[j])) 204 + } 205 + 206 + if len(want.FileMatches[j]) == 0 { 207 + continue 208 + } 209 + 210 + if !reflect.DeepEqual(res.Files, want.FileMatches[j]) { 211 + t.Errorf("matches for %s on %s\ngot:\n%v\nwant:\n%v", q, name, res.Files[0], want.FileMatches[j]) 212 + } 213 + } 214 + } 215 + }
+148
testdata/golden/TestReadSearch/ctagsrepo_v16.00000.golden
··· 1 + { 2 + "FormatVersion": 16, 3 + "FeatureVersion": 9, 4 + "FileMatches": [ 5 + [ 6 + { 7 + "Score": 910, 8 + "Debug": "", 9 + "FileName": "main.go", 10 + "Repository": "repo", 11 + "Branches": null, 12 + "LineMatches": [ 13 + { 14 + "Line": "ZnVuYyBtYWluKCkgew==", 15 + "LineStart": 69, 16 + "LineEnd": 82, 17 + "LineNumber": 10, 18 + "FileName": false, 19 + "Score": 501, 20 + "LineFragments": [ 21 + { 22 + "LineOffset": 0, 23 + "Offset": 69, 24 + "MatchLength": 9, 25 + "SymbolInfo": null 26 + } 27 + ] 28 + } 29 + ], 30 + "Content": null, 31 + "Checksum": "n9fUYqacPXg=", 32 + "Language": "go", 33 + "SubRepositoryName": "", 34 + "SubRepositoryPath": "", 35 + "Version": "" 36 + } 37 + ], 38 + [ 39 + { 40 + "Score": 710, 41 + "Debug": "", 42 + "FileName": "main.go", 43 + "Repository": "repo", 44 + "Branches": null, 45 + "LineMatches": [ 46 + { 47 + "Line": "cGFja2FnZSBtYWlu", 48 + "LineStart": 0, 49 + "LineEnd": 12, 50 + "LineNumber": 1, 51 + "FileName": false, 52 + "Score": 501, 53 + "LineFragments": [ 54 + { 55 + "LineOffset": 0, 56 + "Offset": 0, 57 + "MatchLength": 7, 58 + "SymbolInfo": null 59 + } 60 + ] 61 + } 62 + ], 63 + "Content": null, 64 + "Checksum": "n9fUYqacPXg=", 65 + "Language": "go", 66 + "SubRepositoryName": "", 67 + "SubRepositoryPath": "", 68 + "Version": "" 69 + } 70 + ], 71 + [ 72 + { 73 + "Score": 7910, 74 + "Debug": "", 75 + "FileName": "main.go", 76 + "Repository": "repo", 77 + "Branches": null, 78 + "LineMatches": [ 79 + { 80 + "Line": "CW51bSAgICAgPSA1", 81 + "LineStart": 34, 82 + "LineEnd": 46, 83 + "LineNumber": 6, 84 + "FileName": false, 85 + "Score": 7501, 86 + "LineFragments": [ 87 + { 88 + "LineOffset": 1, 89 + "Offset": 35, 90 + "MatchLength": 3, 91 + "SymbolInfo": { 92 + "Sym": "num", 93 + "Kind": "var", 94 + "Parent": "main", 95 + "ParentKind": "package" 96 + } 97 + } 98 + ] 99 + } 100 + ], 101 + "Content": null, 102 + "Checksum": "n9fUYqacPXg=", 103 + "Language": "go", 104 + "SubRepositoryName": "", 105 + "SubRepositoryPath": "", 106 + "Version": "" 107 + } 108 + ], 109 + [ 110 + { 111 + "Score": 5760, 112 + "Debug": "", 113 + "FileName": "main.go", 114 + "Repository": "repo", 115 + "Branches": null, 116 + "LineMatches": [ 117 + { 118 + "Line": "CW1lc3NhZ2UgPSAiaGVsbG8i", 119 + "LineStart": 47, 120 + "LineEnd": 65, 121 + "LineNumber": 7, 122 + "FileName": false, 123 + "Score": 5551, 124 + "LineFragments": [ 125 + { 126 + "LineOffset": 4, 127 + "Offset": 51, 128 + "MatchLength": 4, 129 + "SymbolInfo": { 130 + "Sym": "message", 131 + "Kind": "var", 132 + "Parent": "main", 133 + "ParentKind": "package" 134 + } 135 + } 136 + ] 137 + } 138 + ], 139 + "Content": null, 140 + "Checksum": "n9fUYqacPXg=", 141 + "Language": "go", 142 + "SubRepositoryName": "", 143 + "SubRepositoryPath": "", 144 + "Version": "" 145 + } 146 + ] 147 + ] 148 + }
+74
testdata/golden/TestReadSearch/repo_v15.00000.golden
··· 1 + { 2 + "FormatVersion": 15, 3 + "FeatureVersion": 8, 4 + "FileMatches": [ 5 + [ 6 + { 7 + "Score": 910, 8 + "Debug": "", 9 + "FileName": "main.go", 10 + "Repository": "repo", 11 + "Branches": null, 12 + "LineMatches": [ 13 + { 14 + "Line": "ZnVuYyBtYWluKCkgew==", 15 + "LineStart": 69, 16 + "LineEnd": 82, 17 + "LineNumber": 10, 18 + "FileName": false, 19 + "Score": 501, 20 + "LineFragments": [ 21 + { 22 + "LineOffset": 0, 23 + "Offset": 69, 24 + "MatchLength": 9, 25 + "SymbolInfo": null 26 + } 27 + ] 28 + } 29 + ], 30 + "Content": null, 31 + "Checksum": "n9fUYqacPXg=", 32 + "Language": "", 33 + "SubRepositoryName": "", 34 + "SubRepositoryPath": "", 35 + "Version": "" 36 + } 37 + ], 38 + [ 39 + { 40 + "Score": 710, 41 + "Debug": "", 42 + "FileName": "main.go", 43 + "Repository": "repo", 44 + "Branches": null, 45 + "LineMatches": [ 46 + { 47 + "Line": "cGFja2FnZSBtYWlu", 48 + "LineStart": 0, 49 + "LineEnd": 12, 50 + "LineNumber": 1, 51 + "FileName": false, 52 + "Score": 501, 53 + "LineFragments": [ 54 + { 55 + "LineOffset": 0, 56 + "Offset": 0, 57 + "MatchLength": 7, 58 + "SymbolInfo": null 59 + } 60 + ] 61 + } 62 + ], 63 + "Content": null, 64 + "Checksum": "n9fUYqacPXg=", 65 + "Language": "", 66 + "SubRepositoryName": "", 67 + "SubRepositoryPath": "", 68 + "Version": "" 69 + } 70 + ], 71 + null, 72 + null 73 + ] 74 + }
+74
testdata/golden/TestReadSearch/repo_v16.00000.golden
··· 1 + { 2 + "FormatVersion": 16, 3 + "FeatureVersion": 9, 4 + "FileMatches": [ 5 + [ 6 + { 7 + "Score": 910, 8 + "Debug": "", 9 + "FileName": "main.go", 10 + "Repository": "repo", 11 + "Branches": null, 12 + "LineMatches": [ 13 + { 14 + "Line": "ZnVuYyBtYWluKCkgew==", 15 + "LineStart": 69, 16 + "LineEnd": 82, 17 + "LineNumber": 10, 18 + "FileName": false, 19 + "Score": 501, 20 + "LineFragments": [ 21 + { 22 + "LineOffset": 0, 23 + "Offset": 69, 24 + "MatchLength": 9, 25 + "SymbolInfo": null 26 + } 27 + ] 28 + } 29 + ], 30 + "Content": null, 31 + "Checksum": "n9fUYqacPXg=", 32 + "Language": "", 33 + "SubRepositoryName": "", 34 + "SubRepositoryPath": "", 35 + "Version": "" 36 + } 37 + ], 38 + [ 39 + { 40 + "Score": 710, 41 + "Debug": "", 42 + "FileName": "main.go", 43 + "Repository": "repo", 44 + "Branches": null, 45 + "LineMatches": [ 46 + { 47 + "Line": "cGFja2FnZSBtYWlu", 48 + "LineStart": 0, 49 + "LineEnd": 12, 50 + "LineNumber": 1, 51 + "FileName": false, 52 + "Score": 501, 53 + "LineFragments": [ 54 + { 55 + "LineOffset": 0, 56 + "Offset": 0, 57 + "MatchLength": 7, 58 + "SymbolInfo": null 59 + } 60 + ] 61 + } 62 + ], 63 + "Content": null, 64 + "Checksum": "n9fUYqacPXg=", 65 + "Language": "", 66 + "SubRepositoryName": "", 67 + "SubRepositoryPath": "", 68 + "Version": "" 69 + } 70 + ], 71 + null, 72 + null 73 + ] 74 + }
+12
testdata/repo/main.go
··· 1 + package main 2 + 3 + import "fmt" 4 + 5 + var ( 6 + num = 5 7 + message = "hello" 8 + ) 9 + 10 + func main() { 11 + fmt.Println(message, num) 12 + }
testdata/shards/ctagsrepo_v16.00000.zoekt

This is a binary file and will not be displayed.

testdata/shards/repo_v15.00000.zoekt

This is a binary file and will not be displayed.

testdata/shards/repo_v16.00000.zoekt

This is a binary file and will not be displayed.