@@ -3,6 +3,7 @@ package scip
33import (
44 "bytes"
55 "compress/gzip"
6+ "io"
67 "os"
78 "regexp"
89 "testing"
@@ -14,42 +15,23 @@ import (
1415 "google.golang.org/protobuf/proto"
1516)
1617
18+ func TestEmpty (t * testing.T ) {
19+ index := Index {}
20+ checkRoundtrip (t , & index )
21+ }
22+
1723func TestFuzz (t * testing.T ) {
1824 pat := regexp .MustCompile ("^(state|sizeCache|unknownFields|SignatureDocumentation)$" )
1925 f := fuzz .New ().NumElements (0 , 2 ).SkipFieldsWithPattern (pat )
2026 for i := 0 ; i < 100 ; i ++ {
2127 index := Index {}
2228 f .Fuzz (& index )
2329
24- indexBytes , err := proto .Marshal (& index )
25- require .NoError (t , err )
26- bytesReader := bytes .NewReader (indexBytes )
27- parsedIndex := Index {}
28-
29- indexVisitor := IndexVisitor {func (metadata * Metadata ) {
30- parsedIndex .Metadata = metadata
31- }, func (document * Document ) {
32- parsedIndex .Documents = append (parsedIndex .Documents , document )
33- }, func (extSym * SymbolInformation ) {
34- parsedIndex .ExternalSymbols = append (parsedIndex .ExternalSymbols , extSym )
35- }}
36-
37- if err := indexVisitor .ParseStreaming (bytesReader ); err != nil {
38- t .Fatalf ("failed to parse index: %s\n got error: %v" ,
39- protojson.MarshalOptions {Multiline : true }.Format (& index ), err )
40- }
41-
42- if ! proto .Equal (& index , & parsedIndex ) {
43- want := protojson.MarshalOptions {Multiline : true }.Format (& index )
44- got := protojson.MarshalOptions {Multiline : true }.Format (& parsedIndex )
45- diff := cmp .Diff (want , got )
46- require .NotEqual (t , diff , "" )
47- t .Fatalf ("index (-want, +got): %s" , diff )
48- }
30+ checkRoundtrip (t , & index )
4931 }
5032}
5133
52- func TestLargeDocuments (t * testing.T ) {
34+ func getTestIndex (t * testing.T ) * gzip. Reader {
5335 // Copied from the Sourcegraph monorepo, which triggered a bug
5436 // where Reader.read() didn't actually fill a buffer completely,
5537 // due to the presence of large documents.
@@ -61,7 +43,63 @@ func TestLargeDocuments(t *testing.T) {
6143 if err != nil {
6244 t .Fatalf ("unexpected error unzipping test file: %s" , err )
6345 }
46+ return reader
47+ }
48+
49+ func TestLargeDocuments (t * testing.T ) {
50+ reader := getTestIndex (t )
51+ _ = parseStreaming (t , reader )
52+ }
53+
54+ func TestDocumentsOnly (t * testing.T ) {
55+ pat := regexp .MustCompile ("^(state|sizeCache|unknownFields|SignatureDocumentation)$" )
56+ f := fuzz .New ().NumElements (0 , 2 ).SkipFieldsWithPattern (pat )
57+ for i := 0 ; i < 100 ; i ++ {
58+ index := Index {}
59+ f .Fuzz (& index )
60+
61+ parsedIndex := Index {}
62+
63+ indexVisitor := IndexVisitor {VisitDocument : func (document * Document ) {
64+ parsedIndex .Documents = append (parsedIndex .Documents , document )
65+ }}
66+
67+ indexBytes , err := proto .Marshal (& index )
68+ require .NoError (t , err )
69+ bytesReader := bytes .NewReader (indexBytes )
70+
71+ if err := indexVisitor .ParseStreaming (bytesReader ); err != nil {
72+ t .Fatalf ("got error parsing index %v" , err )
73+ }
74+
75+ onlyDocumentsIndex := Index {}
76+ onlyDocumentsIndex .Documents = index .Documents
77+
78+ checkIndexEqual (t , & onlyDocumentsIndex , & parsedIndex )
79+ }
80+ }
81+
82+ func checkIndexEqual (t * testing.T , expected * Index , got * Index ) {
83+ if ! proto .Equal (expected , got ) {
84+ want := protojson.MarshalOptions {Multiline : true }.Format (expected )
85+ got := protojson.MarshalOptions {Multiline : true }.Format (got )
86+ diff := cmp .Diff (want , got )
87+ require .NotEqual (t , diff , "" )
88+ t .Fatalf ("index (-want, +got): %s" , diff )
89+ }
90+ }
91+
92+ func checkRoundtrip (t * testing.T , index * Index ) {
93+ indexBytes , err := proto .Marshal (index )
94+ require .NoError (t , err )
95+ bytesReader := bytes .NewReader (indexBytes )
96+
97+ parsedIndex := parseStreaming (t , bytesReader )
98+
99+ checkIndexEqual (t , index , parsedIndex )
100+ }
64101
102+ func parseStreaming (t * testing.T , reader io.Reader ) * Index {
65103 parsedIndex := Index {}
66104
67105 indexVisitor := IndexVisitor {func (metadata * Metadata ) {
@@ -75,4 +113,5 @@ func TestLargeDocuments(t *testing.T) {
75113 if err := indexVisitor .ParseStreaming (reader ); err != nil {
76114 t .Fatalf ("got error parsing index %v" , err )
77115 }
116+ return & parsedIndex
78117}
0 commit comments