@@ -4,6 +4,7 @@ package json
4
4
5
5
import (
6
6
"bytes"
7
+ jsonenc "encoding/json"
7
8
"fmt"
8
9
"os"
9
10
"strings"
@@ -21,35 +22,6 @@ import (
21
22
22
23
func TestLoad (t * testing.T ) {
23
24
want := example .Copy ()
24
-
25
- want .Relationships = append (want .Relationships , []* spdx.Relationship {
26
- {
27
- RefA : common.DocElementID {ElementRefID : "DOCUMENT" },
28
- RefB : common.DocElementID {ElementRefID : "File" },
29
- Relationship : "DESCRIBES" ,
30
- },
31
- {
32
- RefA : common.DocElementID {ElementRefID : "DOCUMENT" },
33
- RefB : common.DocElementID {ElementRefID : "Package" },
34
- Relationship : "DESCRIBES" ,
35
- },
36
- {
37
- RefA : common.DocElementID {ElementRefID : "Package" },
38
- RefB : common.DocElementID {ElementRefID : "CommonsLangSrc" },
39
- Relationship : "CONTAINS" ,
40
- },
41
- {
42
- RefA : common.DocElementID {ElementRefID : "Package" },
43
- RefB : common.DocElementID {ElementRefID : "JenaLib" },
44
- Relationship : "CONTAINS" ,
45
- },
46
- {
47
- RefA : common.DocElementID {ElementRefID : "Package" },
48
- RefB : common.DocElementID {ElementRefID : "DoapSource" },
49
- Relationship : "CONTAINS" ,
50
- },
51
- }... )
52
-
53
25
file , err := os .Open ("../../../../examples/sample-docs/json/SPDXJSONExample-v2.2.spdx.json" )
54
26
if err != nil {
55
27
panic (fmt .Errorf ("error opening File: %s" , err ))
@@ -62,8 +34,8 @@ func TestLoad(t *testing.T) {
62
34
return
63
35
}
64
36
65
- if ! cmp .Equal (want , got , cmpopts .IgnoreUnexported (spdx.Package {})) {
66
- t .Errorf ("got incorrect struct after parsing YAML example: %s" , cmp . Diff ( want , got , cmpopts . IgnoreUnexported (spdx. Package {})) )
37
+ if diff := cmp .Diff (want , got , cmpopts .IgnoreUnexported (spdx.Package {}), cmpopts . SortSlices ( relationshipLess )); len ( diff ) > 0 {
38
+ t .Errorf ("got incorrect struct after parsing JSON example: %s" , diff )
67
39
return
68
40
}
69
41
}
@@ -91,8 +63,8 @@ func Test_Write(t *testing.T) {
91
63
return
92
64
}
93
65
94
- if ! cmp .Equal (want , got , cmpopts .IgnoreUnexported (spdx.Package {})) {
95
- t .Errorf ("got incorrect struct after writing and re-parsing JSON example: %s" , cmp . Diff ( want , got , cmpopts . IgnoreUnexported (spdx. Package {})) )
66
+ if diff := cmp .Diff (want , got , cmpopts .IgnoreUnexported (spdx.Package {}), cmpopts . SortSlices ( relationshipLess )); len ( diff ) > 0 {
67
+ t .Errorf ("got incorrect struct after writing and re-parsing JSON example: %s" , diff )
96
68
return
97
69
}
98
70
}
@@ -149,7 +121,7 @@ func Test_ShorthandFields(t *testing.T) {
149
121
}
150
122
}
151
123
152
- require . Equal ( t , spdx.Document {
124
+ want := spdx.Document {
153
125
SPDXVersion : spdx .Version ,
154
126
DataLicense : spdx .DataLicense ,
155
127
SPDXIdentifier : "DOCUMENT" ,
@@ -200,7 +172,136 @@ func Test_ShorthandFields(t *testing.T) {
200
172
Relationship : common .TypeRelationshipContains ,
201
173
},
202
174
},
203
- }, doc )
175
+ }
176
+
177
+ if diff := cmp .Diff (want , doc , cmpopts .IgnoreUnexported (spdx.Package {}), cmpopts .SortSlices (relationshipLess )); len (diff ) > 0 {
178
+ t .Errorf ("got incorrect struct after parsing JSON example: %s" , diff )
179
+ return
180
+ }
181
+
182
+ }
183
+
184
+ func Test_ShorthandFieldsNoDuplicates (t * testing.T ) {
185
+ contents := `{
186
+ "spdxVersion": "SPDX-2.2",
187
+ "dataLicense": "CC0-1.0",
188
+ "SPDXID": "SPDXRef-DOCUMENT",
189
+ "name": "SPDX-Tools-v2.0",
190
+ "documentDescribes": [
191
+ "SPDXRef-Container"
192
+ ],
193
+ "packages": [
194
+ {
195
+ "name": "Container",
196
+ "SPDXID": "SPDXRef-Container"
197
+ },
198
+ {
199
+ "name": "Package-1",
200
+ "SPDXID": "SPDXRef-Package-1",
201
+ "versionInfo": "1.1.1",
202
+ "hasFiles": [
203
+ "SPDXRef-File-1",
204
+ "SPDXRef-File-2"
205
+ ]
206
+ },
207
+ {
208
+ "name": "Package-2",
209
+ "SPDXID": "SPDXRef-Package-2",
210
+ "versionInfo": "2.2.2"
211
+ }
212
+ ],
213
+ "files": [
214
+ {
215
+ "fileName": "./f1",
216
+ "SPDXID": "SPDXRef-File-1"
217
+ },
218
+ {
219
+ "fileName": "./f2",
220
+ "SPDXID": "SPDXRef-File-2"
221
+ }
222
+ ],
223
+ "relationships": [
224
+ {
225
+ "spdxElementId": "SPDXRef-Package-1",
226
+ "relationshipType": "CONTAINS",
227
+ "relatedSpdxElement": "SPDXRef-File-1"
228
+ },
229
+ {
230
+ "spdxElementId": "SPDXRef-Package-1",
231
+ "relationshipType": "CONTAINS",
232
+ "relatedSpdxElement": "SPDXRef-File-2"
233
+ }
234
+ ]
235
+ }`
236
+
237
+ doc := spdx.Document {}
238
+ err := json .ReadInto (strings .NewReader (contents ), & doc )
239
+
240
+ require .NoError (t , err )
241
+
242
+ id := func (s string ) common.DocElementID {
243
+ return common.DocElementID {
244
+ ElementRefID : common .ElementID (s ),
245
+ }
246
+ }
247
+
248
+ want := spdx.Document {
249
+ SPDXVersion : spdx .Version ,
250
+ DataLicense : spdx .DataLicense ,
251
+ SPDXIdentifier : "DOCUMENT" ,
252
+ DocumentName : "SPDX-Tools-v2.0" ,
253
+ Packages : []* spdx.Package {
254
+ {
255
+ PackageName : "Container" ,
256
+ PackageSPDXIdentifier : "Container" ,
257
+ FilesAnalyzed : true ,
258
+ },
259
+ {
260
+ PackageName : "Package-1" ,
261
+ PackageSPDXIdentifier : "Package-1" ,
262
+ PackageVersion : "1.1.1" ,
263
+ FilesAnalyzed : true ,
264
+ },
265
+ {
266
+ PackageName : "Package-2" ,
267
+ PackageSPDXIdentifier : "Package-2" ,
268
+ PackageVersion : "2.2.2" ,
269
+ FilesAnalyzed : true ,
270
+ },
271
+ },
272
+ Files : []* spdx.File {
273
+ {
274
+ FileName : "./f1" ,
275
+ FileSPDXIdentifier : "File-1" ,
276
+ },
277
+ {
278
+ FileName : "./f2" ,
279
+ FileSPDXIdentifier : "File-2" ,
280
+ },
281
+ },
282
+ Relationships : []* spdx.Relationship {
283
+ {
284
+ RefA : id ("DOCUMENT" ),
285
+ RefB : id ("Container" ),
286
+ Relationship : common .TypeRelationshipDescribe ,
287
+ },
288
+ {
289
+ RefA : id ("Package-1" ),
290
+ RefB : id ("File-1" ),
291
+ Relationship : common .TypeRelationshipContains ,
292
+ },
293
+ {
294
+ RefA : id ("Package-1" ),
295
+ RefB : id ("File-2" ),
296
+ Relationship : common .TypeRelationshipContains ,
297
+ },
298
+ },
299
+ }
300
+
301
+ if diff := cmp .Diff (want , doc , cmpopts .IgnoreUnexported (spdx.Package {}), cmpopts .SortSlices (relationshipLess )); len (diff ) > 0 {
302
+ t .Errorf ("got incorrect struct after parsing JSON example: %s" , diff )
303
+ return
304
+ }
204
305
}
205
306
206
307
func Test_JsonEnums (t * testing.T ) {
@@ -344,3 +445,9 @@ func Test_JsonEnums(t *testing.T) {
344
445
},
345
446
}, doc )
346
447
}
448
+
449
+ func relationshipLess (a , b * spdx.Relationship ) bool {
450
+ aStr , _ := jsonenc .Marshal (a )
451
+ bStr , _ := jsonenc .Marshal (b )
452
+ return string (aStr ) < string (bStr )
453
+ }
0 commit comments