@@ -3,6 +3,7 @@ package scip
3
3
import (
4
4
"bytes"
5
5
"compress/gzip"
6
+ "io"
6
7
"os"
7
8
"regexp"
8
9
"testing"
@@ -14,42 +15,23 @@ import (
14
15
"google.golang.org/protobuf/proto"
15
16
)
16
17
18
+ func TestEmpty (t * testing.T ) {
19
+ index := Index {}
20
+ checkRoundtrip (t , & index )
21
+ }
22
+
17
23
func TestFuzz (t * testing.T ) {
18
24
pat := regexp .MustCompile ("^(state|sizeCache|unknownFields|SignatureDocumentation)$" )
19
25
f := fuzz .New ().NumElements (0 , 2 ).SkipFieldsWithPattern (pat )
20
26
for i := 0 ; i < 100 ; i ++ {
21
27
index := Index {}
22
28
f .Fuzz (& index )
23
29
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 )
49
31
}
50
32
}
51
33
52
- func TestLargeDocuments (t * testing.T ) {
34
+ func getTestIndex (t * testing.T ) * gzip. Reader {
53
35
// Copied from the Sourcegraph monorepo, which triggered a bug
54
36
// where Reader.read() didn't actually fill a buffer completely,
55
37
// due to the presence of large documents.
@@ -61,7 +43,63 @@ func TestLargeDocuments(t *testing.T) {
61
43
if err != nil {
62
44
t .Fatalf ("unexpected error unzipping test file: %s" , err )
63
45
}
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
+ }
64
101
102
+ func parseStreaming (t * testing.T , reader io.Reader ) * Index {
65
103
parsedIndex := Index {}
66
104
67
105
indexVisitor := IndexVisitor {func (metadata * Metadata ) {
@@ -75,4 +113,5 @@ func TestLargeDocuments(t *testing.T) {
75
113
if err := indexVisitor .ParseStreaming (reader ); err != nil {
76
114
t .Fatalf ("got error parsing index %v" , err )
77
115
}
116
+ return & parsedIndex
78
117
}
0 commit comments