forked from italia/publiccode-parser-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
601 lines (544 loc) · 22.1 KB
/
parser_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package publiccode
import (
"bytes"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
"testing"
)
type testType struct {
file string
err error
}
// Parse the YAML file passed as argument, using the current directory
// as base path.
//
// Return nil if the parsing succeded or an error if it failed.
func parse(file string) error {
var p *Parser
var err error
if p, err = NewParserWithPath(file, "."); err != nil {
return err
}
return p.Parse()
}
// Check all the YAML files matching the glob pattern and fail for each file
// with parsing or validation errors.
func checkValidFiles(pattern string, t *testing.T) {
testFiles, _ := filepath.Glob(pattern)
for _, file := range testFiles {
t.Run(file, func(t *testing.T) {
err := parse(file)
if err != nil {
t.Errorf("[%s] validation failed for valid file: %T - %s\n", file, err, err)
}
})
}
}
func checkParseErrors(t *testing.T, err error, test testType) {
if test.err == nil && err != nil {
t.Errorf("[%s] unexpected error: %v\n", test.file, err)
} else if test.err != nil && err == nil {
t.Errorf("[%s] no error generated\n", test.file)
} else if test.err != nil && err != nil {
if !reflect.DeepEqual(test.err, err) {
t.Errorf("[%s] wrong error generated:\n%T - %s\n- instead of:\n%T - %s", test.file, err, err, test.err, test.err)
}
}
}
func TestValidPreviousStandardVersion(t *testing.T) {
file := "testdata/v0.2/valid/valid.minimal.yml"
t.Run(file, func(t *testing.T) {
err := parse(file)
checkParseErrors(t, err, testType{
file, ValidationResults{
ValidationWarning{"publiccodeYmlVersion", "v0.2 is not the latest version, use '0.3'. Parsing this file as v0.3.", 1, 1},
},
})
})
}
func TestInvalidTestcasesV0_3(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Errorf("Can't get current working directory")
}
expected := map[string]error{
// publiccodeYmlVersion
"publiccodeYmlVersion_missing.yml": ValidationResults{ValidationError{"publiccodeYmlVersion", "required", 0, 0}},
"publiccodeYmlVersion_invalid.yml": ValidationResults{ValidationError{
"publiccodeYmlVersion", "must be one of the following: 0.2 0.2.0 0.2.1 0.2.2 0.3 0.3.0", 2, 1,
}},
"publiccodeYmlVersion_wrong_type.yml": ValidationResults{
ValidationError{"publiccodeYmlVersion", "wrong type for this field", 2, 1},
ValidationError{"publiccodeYmlVersion", "required", 2, 1}},
// name
"name_missing.yml": ValidationResults{ValidationError{"name", "required", 1, 1}},
"name_nil.yml": ValidationResults{ValidationError{"name", "required", 4, 1}},
"name_wrong_type.yml": ValidationResults{
ValidationError{"name", "wrong type for this field", 4, 1},
ValidationError{"name", "required", 4, 1},
},
// applicationSuite
"applicationSuite_wrong_type.yml": ValidationResults{ValidationError{"applicationSuite", "wrong type for this field", 4, 1}},
// url
"url_missing.yml": ValidationResults{ValidationError{"url", "required", 1, 1}},
"url_wrong_type.yml": ValidationResults{
ValidationError{"url", "wrong type for this field", 6, 1},
ValidationError{"url", "'' not reachable: missing URL scheme", 6, 1},
ValidationError{"url", "is not a valid code repository", 6, 1},
},
"url_invalid.yml": ValidationResults{
ValidationError{"url", "'foobar' not reachable: missing URL scheme", 6, 1},
ValidationError{"url", "is not a valid code repository", 6, 1},
},
// landingURL
"landingURL_wrong_type.yml": ValidationResults{
ValidationError{"landingURL", "wrong type for this field", 8, 1},
ValidationError{"landingURL", "'' not reachable: missing URL scheme", 8, 1},
},
"landingURL_invalid.yml": ValidationResults{
ValidationError{"landingURL", "'???' not reachable: missing URL scheme", 8, 1},
},
// isBasedOn
"isBasedOn_wrong_type.yml": ValidationResults{
ValidationError{"isBasedOn.foobar", "wrong type for this field", 10, 1},
},
"isBasedOn_bad_url_array.yml": ValidationResults{
ValidationError{"isBasedOn", "wrong type for this field", 8, 1},
},
"isBasedOn_bad_url_string.yml": ValidationResults{
ValidationError{"isBasedOn", "'???' not reachable: missing URL scheme", 8, 1},
},
// softwareVersion
"softwareVersion_wrong_type.yml": ValidationResults{
ValidationError{"softwareVersion", "wrong type for this field", 8, 1},
},
// releaseDate
"releaseDate_missing.yml": ValidationResults{ValidationError{"releaseDate", "required", 1, 1}},
"releaseDate_wrong_type.yml": ValidationResults{
ValidationError{"releaseDate", "wrong type for this field", 8, 1},
ValidationError{"releaseDate", "required", 8, 1},
},
"releaseDate_invalid.yml": ValidationResults{
ValidationError{"releaseDate", "must be a date with format 'YYYY-MM-DD'", 8, 1},
},
// logo
"logo_wrong_type.yml": ValidationResults{
ValidationError{"logo", "wrong type for this field", 18, 1},
},
"logo_unsupported_extension.yml": ValidationResults{
ValidationError{"logo", fmt.Sprintf("invalid file extension for: %s/logo.mpg", cwd), 18, 1},
},
"logo_missing_file.yml": ValidationResults{
ValidationError{"logo", fmt.Sprintf("no such file: %s/no_such_file.png", cwd), 18, 1},
},
"logo_invalid_png.yml": ValidationResults{
ValidationError{"logo", "image: unknown format", 18, 1},
},
// monochromeLogo
"monochromeLogo_wrong_type.yml": ValidationResults{
ValidationError{"monochromeLogo", "wrong type for this field", 18, 1},
},
"monochromeLogo_unsupported_extension.yml": ValidationResults{
ValidationWarning{"monochromeLogo", "This key is DEPRECATED and will be removed in the future", 18, 1},
ValidationError{
"monochromeLogo",
fmt.Sprintf("invalid file extension for: %s/monochromeLogo.mpg", cwd),
18,
1,
},
},
"monochromeLogo_missing_file.yml": ValidationResults{
ValidationWarning{"monochromeLogo", "This key is DEPRECATED and will be removed in the future", 18, 1},
ValidationError{"monochromeLogo", fmt.Sprintf("no such file: %s/no_such_file.png", cwd), 18, 1},
},
"monochromeLogo_invalid_png.yml": ValidationResults{
ValidationWarning{"monochromeLogo", "This key is DEPRECATED and will be removed in the future", 18, 1},
ValidationError{"monochromeLogo", "image: unknown format", 18, 1},
},
// inputTypes
"inputTypes_invalid.yml": ValidationResults{
ValidationWarning{"inputTypes", "This key is DEPRECATED and will be removed in the future", 14, 1},
ValidationError{"inputTypes[1]", "'foobar' is not a MIME type", 1, 1},
},
"inputTypes_wrong_type.yml": ValidationResults{
ValidationError{"inputTypes.foobar", "wrong type for this field", 15, 1},
},
// outputTypes
"outputTypes_invalid.yml": ValidationResults{
ValidationWarning{"outputTypes", "This key is DEPRECATED and will be removed in the future", 14, 1},
ValidationError{"outputTypes[1]", "'foobar' is not a MIME type", 1, 1},
},
"outputTypes_wrong_type.yml": ValidationResults{
ValidationError{"outputTypes.foobar", "wrong type for this field", 15, 1},
},
// platforms
"platforms_missing.yml": ValidationResults{ValidationError{"platforms", "must be more than 0", 1, 1}},
"platforms_wrong_type.yml": ValidationResults{
ValidationError{"platforms", "wrong type for this field", 9, 1},
ValidationError{"platforms", "must be more than 0", 9, 1},
},
// categories
"categories_missing.yml": ValidationResults{
ValidationError{"categories", "required", 1, 1},
},
"categories_nil.yml": ValidationResults{
ValidationError{"categories", "required", 12, 1},
},
"categories_empty.yml": ValidationResults{
ValidationError{"categories", "must be more than 0", 12, 1},
},
"categories_invalid.yml": ValidationResults{ValidationError{"categories[0]", "must be a valid category", 1, 1}},
// usedBy
"usedBy_wrong_type.yml": ValidationResults{
ValidationError{"usedBy", "wrong type for this field", 14, 1},
},
// roadmap
"roadmap_invalid.yml": ValidationResults{
ValidationError{"roadmap", "'foobar' not reachable: missing URL scheme", 4, 1},
},
"roadmap_wrong_type.yml": ValidationResults{
ValidationError{"roadmap", "wrong type for this field", 4, 1},
ValidationError{"roadmap", "'' not reachable: missing URL scheme", 4, 1},
},
// developmentStatus
"developmentStatus_missing.yml": ValidationResults{
ValidationError{"developmentStatus", "required", 1, 1},
},
"developmentStatus_invalid.yml": ValidationResults{
ValidationError{"developmentStatus", "must be one of the following: concept development beta stable obsolete", 16, 1},
},
"developmentStatus_wrong_type.yml": ValidationResults{
ValidationError{"developmentStatus", "wrong type for this field", 16, 1},
ValidationError{"developmentStatus", "required", 16, 1},
},
// softwareType
"softwareType_missing.yml": ValidationResults{
ValidationError{"softwareType", "required", 1, 1},
},
"softwareType_invalid.yml": ValidationResults{
ValidationError{"softwareType", "must be one of the following: standalone/mobile standalone/iot standalone/desktop standalone/web standalone/backend standalone/other addon library configurationFiles", 17, 1},
},
"softwareType_wrong_type.yml": ValidationResults{
ValidationError{"softwareType", "wrong type for this field", 17, 1},
ValidationError{"softwareType", "required", 17, 1},
},
// intendedAudience
// intendedAudience.*
"intendedAudience_wrong_type.yml": ValidationResults{
ValidationError{"intendedAudience", "wrong type for this field", 18, 1},
},
"intendedAudience_countries_invalid_country.yml": ValidationResults{
ValidationError{"intendedAudience.countries[1]", "must be a valid lowercase ISO 3166-1 alpha-2 two-letter country code", 18, 5},
},
"intendedAudience_countries_wrong_type.yml": ValidationResults{
ValidationError{"intendedAudience.countries", "wrong type for this field", 19, 1},
},
"intendedAudience_unsupportedCountries_invalid_country.yml": ValidationResults{
ValidationError{"intendedAudience.unsupportedCountries[0]", "must be a valid lowercase ISO 3166-1 alpha-2 two-letter country code", 18, 5},
},
"intendedAudience_unsupportedCountries_wrong_type.yml": ValidationResults{
ValidationError{"intendedAudience.unsupportedCountries", "wrong type for this field", 19, 1},
},
"intendedAudience_scope_invalid_scope.yml": ValidationResults{
ValidationError{"intendedAudience.scope[0]", "must be a valid scope", 18, 5},
},
"intendedAudience_scope_wrong_type.yml": ValidationResults{
ValidationError{"intendedAudience.scope", "wrong type for this field", 19, 1},
},
// description
// description.*
"description_eng_features_missing.yml": ValidationResults{
ValidationError{"description.eng.features", "must be more than 0", 22, 5},
},
"description_eng_features_empty.yml": ValidationResults{
ValidationError{"description.eng.features", "must be more than 0", 39, 5},
},
"description_eng_localisedName_wrong_type.yml": ValidationResults{
ValidationError{"description.eng.localisedName", "wrong type for this field", 21, 1},
},
"description_eng_genericName_too_long.yml": ValidationResults{
ValidationError{"description.eng.genericName", "must be less or equal than 35", 22, 5},
ValidationWarning{"description.eng.genericName", "This key is DEPRECATED and will be removed in the future", 22, 5},
},
"description_eng_shortDescription_missing.yml": ValidationResults{
ValidationError{"description.eng.shortDescription", "required", 20, 5},
},
"description_eng_shortDescription_too_short.yml": ValidationResults{
ValidationError{"description.eng.shortDescription", "required", 20, 5},
},
"description_eng_longDescription_missing.yml": ValidationResults{
ValidationError{"description.eng.longDescription", "required", 20, 5},
},
"description_eng_longDescription_too_long.yml": ValidationResults{
ValidationError{"description.eng.longDescription", "must be less or equal than 10000", 27, 5},
},
"description_eng_longDescription_too_short.yml": ValidationResults{
ValidationError{"description.eng.longDescription", "must be more or equal than 150", 27, 5},
},
"description_eng_longDescription_too_short_grapheme_clusters.yml": ValidationResults{
ValidationError{"description.eng.longDescription", "must be more or equal than 150", 28, 5},
},
"description_eng_screenshots_missing_file.yml": ValidationResults{
ValidationError{
"description.eng.screenshots[0]",
fmt.Sprintf("'no_such_file.png' is not an image: no such file : %s/no_such_file.png", cwd),
20,
5,
},
},
"description_eng_awards_wrong_type.yml": ValidationResults{
ValidationError{"description.eng.awards", "wrong type for this field", 40, 1},
},
"description_eng_videos_invalid.yml": ValidationResults{
ValidationError{"description.eng.videos[0]", "'https://google.com' is not a valid video URL supporting oEmbed: invalid oembed link: https://google.com", 20, 5},
},
// legal
// legal.*
"legal_missing.yml": ValidationResults{ValidationError{"legal.license", "required", 0, 0}},
"legal_wrong_type.yml": ValidationResults{
ValidationError{"legal", "wrong type for this field", 46, 1},
ValidationError{"legal.license", "required", 46, 8},
},
"legal_license_missing.yml": ValidationResults{ValidationError{"legal.license", "required", 41, 3}},
"legal_license_invalid.yml": ValidationResults{ValidationError{
"legal.license", "invalid license 'Invalid License'", 42, 3,
}},
"legal_authorsFile_missing_file.yml": ValidationResults{
ValidationError{
"legal.authorsFile",
fmt.Sprintf("'%s/no_such_authors_file.txt' does not exist", cwd),
42,
3,
},
},
// maintenance
// maintenance.*
"maintenance_type_missing.yml": ValidationResults{
ValidationError{"maintenance.type", "required", 47, 3},
},
"maintenance_type_invalid.yml": ValidationResults{
ValidationError{"maintenance.type", "must be one of the following: internal contract community none", 45, 3},
},
"maintenance_contacts_missing_with_type_community.yml": ValidationResults{
ValidationError{"maintenance.contacts", "required_if Type community", 44, 3},
},
"maintenance_contacts_missing_with_type_internal.yml": ValidationResults{
ValidationError{"maintenance.contacts", "required_if Type internal", 44, 3},
},
"maintenance_contacts_name_missing.yml": ValidationResults{
ValidationError{"maintenance.contacts[0].name", "required", 0, 0},
},
"maintenance_contacts_email_invalid.yml": ValidationResults{
ValidationError{"maintenance.contacts[0].email", "must be a valid email", 0, 0},
},
"maintenance_contractors_missing_with_type_contract.yml": ValidationResults{
ValidationError{"maintenance.contractors", "required_if Type contract", 44, 3},
},
"maintenance_contractors_invalid_type.yml": ValidationResults{
ValidationError{"maintenance.contractors", "wrong type for this field", 47, 1},
ValidationError{"maintenance.contractors", "required_if Type contract", 47, 3},
},
"maintenance_contractors_name_missing.yml": ValidationResults{
ValidationError{"maintenance.contractors[0].name", "required", 0, 0},
},
"maintenance_contractors_until_missing.yml": ValidationResults{
ValidationError{"maintenance.contractors[0].until", "required", 0, 0},
},
"maintenance_contractors_until_invalid.yml": ValidationResults{
ValidationError{"maintenance.contractors[0].until", "must be a date with format 'YYYY-MM-DD'", 0, 0},
},
"maintenance_contractors_email_invalid.yml": ValidationResults{
ValidationError{"maintenance.contractors[0].email", "must be a valid email", 0, 0},
},
// localisation
"localisation_availableLanguages_missing.yml": ValidationResults{
ValidationError{"localisation.availableLanguages", "required", 50, 3},
},
"localisation_availableLanguages_empty.yml": ValidationResults{
ValidationError{"localisation.availableLanguages", "must be more than 0", 52, 3},
},
"localisation_availableLanguages_invalid.yml": ValidationResults{
ValidationError{"localisation.availableLanguages[0]", "must be a valid BCP 47 language", 50, 3},
},
// TODO: Enable this test when https://github.com/italia/publiccode-parser-go/issues/47
// is fixed
//
// "localisation_availableLanguages_invalid_bcp47.yml": ValidationResults{
// ValidationError{"localisation.availableLanguages[0]", "must be a valid BCP 47 language", 56, 3},
// },
"localisation_localisationReady_missing.yml": ValidationResults{
ValidationError{"localisation.localisationReady", "required", 52, 3},
},
// dependsOn
"dependsOn_open_name_wrong_type.yml": ValidationResults{
ValidationError{"dependsOn.open.name", "wrong type for this field", 56, 1},
ValidationError{"dependsOn.open[0].name", "required", 0, 0},
},
"dependsOn_open_versionMin_wrong_type.yml": ValidationResults{
ValidationError{"dependsOn.open.versionMin", "wrong type for this field", 57, 1},
},
"dependsOn_open_versionMax_wrong_type.yml": ValidationResults{
ValidationError{"dependsOn.open.versionMax", "wrong type for this field", 57, 1},
},
"dependsOn_open_version_wrong_type.yml": ValidationResults{
ValidationError{"dependsOn.open.version", "wrong type for this field", 57, 1},
},
"dependsOn_open_optional_wrong_type.yml": ValidationResults{
ValidationError{"dependsOn.open.optional", "wrong type for this field", 57, 1},
},
// it.*
"it_riuso_codiceIPA_invalid.yml": ValidationResults{
ValidationError{"it.riuso.codiceIPA", "must be a valid Italian Public Administration Code (iPA)", 55, 5},
},
// misc
"file_encoding.yml": ValidationResults{ValidationError{"", "Invalid UTF-8", 0, 0}},
"invalid_yaml.yml": ValidationResults{ValidationError{"", "yaml: did not find expected key", 18, 1}},
}
testFiles, _ := filepath.Glob("testdata/v0.3/invalid/*yml")
for _, file := range testFiles {
baseName := path.Base(file)
if expected[baseName] == nil {
t.Errorf("No expected data for file %s", baseName)
}
t.Run(file, func(t *testing.T) {
err := parse(file)
checkParseErrors(t, err, testType{file, expected[baseName]})
})
}
}
// Test v0.3 valid YAML testcases (testdata/v0.3/valid/).
func TestValidTestcasesV0_3(t *testing.T) {
checkValidFiles("testdata/v0.3/valid/*.yml", t)
}
// Test v0.3 valid YAML testcases (testdata/v0.3/valid_with_warnings/).
func TestValidWithWarningsTestcasesV0_3(t *testing.T) {
expected := map[string]error{
"unicode_grapheme_clusters.yml": ValidationResults{
ValidationWarning{"description.eng.genericName", "This key is DEPRECATED and will be removed in the future", 23, 5},
},
}
testFiles, _ := filepath.Glob("testdata/v0.3/valid_with_warnings/*yml")
for _, file := range testFiles {
baseName := path.Base(file)
if expected[baseName] == nil {
t.Errorf("No expected data for file %s", baseName)
}
t.Run(file, func(t *testing.T) {
err := parse(file)
checkParseErrors(t, err, testType{file, expected[baseName]})
})
}
}
// Test publiccode.yml remote files for key errors.
func TestDecodeValueErrorsRemote(t *testing.T) {
testRemoteFiles := []testType{
{"https://raw.githubusercontent.com/italia/publiccode-editor/master/publiccode.yml", ValidationResults{
ValidationWarning{
"publiccodeYmlVersion", "v0.2 is not the latest version, use '0.3'. Parsing this file as v0.3.", 1, 1,
},
ValidationWarning{"description.it.genericName", "This key is DEPRECATED and will be removed in the future", 12, 5},
}},
}
for _, test := range testRemoteFiles {
t.Run(fmt.Sprintf("%v", test.err), func(t *testing.T) {
var p *Parser
var err error
if p, err = NewParser(test.file); err != nil {
t.Errorf("Can't create parser for %s", test.file)
}
err = p.Parse()
checkParseErrors(t, err, test)
})
}
}
func TestUrlMissingWithoutPath(t *testing.T) {
expected := map[string]error{
"url_missing.yml": ValidationResults{
ValidationError{"url", "required", 1, 1},
},
}
testFiles, _ := filepath.Glob("testdata/v0.3/invalid/url_missing.yml")
for _, file := range testFiles {
baseName := path.Base(file)
if expected[baseName] == nil {
t.Errorf("No expected data for file %s", baseName)
}
t.Run(file, func(t *testing.T) {
parser, err := NewParser(file)
if err != nil {
t.Errorf("Can't create parser for %s", file)
}
err = parser.Parse()
checkParseErrors(t, err, testType{file, expected[baseName]})
})
}
}
func TestIsReachable(t *testing.T) {
var p Parser
p.DisableNetwork = true
u, _ := url.Parse("https://google.com/404")
if reachable, _ := p.isReachable(*u); !reachable {
t.Errorf("isReachable() returned false with DisableNetwork enabled")
}
}
// Test that the exported YAML passes validation again, and that re-exporting it
// matches the first export (lossless roundtrip).
func TestExport(t *testing.T) {
var p *Parser
var err error
if p, err = NewParser("testdata/v0.3/valid/valid.yml"); err != nil {
t.Errorf("Can't create Parser: %v", err)
}
p.DisableNetwork = true
err = p.Parse()
if err != nil {
t.Errorf("Failed to parse valid file: %v", err)
}
yaml1, err := p.ToYAML()
if err != nil {
t.Errorf("Failed to export YAML: %v", err)
}
var p2 *Parser
if p2, err = NewParser("/dev/null"); err != nil {
t.Errorf("Can't create Parser: %v", err)
}
p2.DisableNetwork = true
err = p2.ParseBytes(yaml1)
if err != nil {
t.Errorf("Failed to parse exported file: %v", err)
}
yaml2, err := p2.ToYAML()
if err != nil {
t.Errorf("Failed to export YAML again: %v", err)
}
if !bytes.Equal(yaml1, yaml2) {
t.Errorf("Exported YAML files do not match; roundtrip is not lossless")
}
}
// Test the toURL function
func TestToURL(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Errorf("Can't get current working directory")
}
expected := map[string]*url.URL{
"file.txt": &url.URL{Scheme: "file", Path: fmt.Sprintf("%s/file.txt", cwd)},
"/path/file.txt": &url.URL{Scheme: "file", Path: "/path/file.txt"},
"https://developers.italia.it/": &url.URL{Scheme: "https", Host: "developers.italia.it", Path: "/"},
"https://developers.italia.it/file.txt": &url.URL{Scheme: "https", Host: "developers.italia.it", Path: "/file.txt"},
"http://developers.italia.it/": &url.URL{Scheme: "http", Host: "developers.italia.it", Path: "/"},
}
for in, out := range expected {
var u *url.URL
if u, err = toURL(in); err != nil {
t.Errorf("%s: got error %v", in, err)
}
if *u != *out {
t.Errorf("%s: expected %v got %v", in, out, u)
}
}
}