forked from italia/publiccode-parser-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv0_test.go
More file actions
809 lines (741 loc) · 36.8 KB
/
v0_test.go
File metadata and controls
809 lines (741 loc) · 36.8 KB
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
package publiccode
import (
"fmt"
"path"
"path/filepath"
"slices"
"testing"
)
func TestValidTestcasesV0_NoNetwork(t *testing.T) {
checkValidFilesNoNetwork("testdata/v0/valid/no-network/*.yml", t)
}
func TestValidWithWarningTestcasesV0_NoNetwork(t *testing.T) {
expected := map[string]error{
"authorsFile.yml": ValidationResults{
ValidationWarning{"legal.authorsFile", "This key is DEPRECATED and will be removed in the future. It's safe to drop it", 71, 3},
},
}
dir := "testdata/v0/valid_with_warnings/no-network/"
testFiles, _ := filepath.Glob(dir + "*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 := parseNoNetwork(file)
checkParseErrors(t, err, testType{file, expected[baseName]})
})
}
for file := range expected {
if !slices.Contains(testFiles, dir+file) {
t.Errorf("No expected file %s", dir+file)
}
}
}
func TestInvalidTestcasesV0_NoNetwork(t *testing.T) {
expected := map[string]error{
// logo
"logo_missing_file.yml": ValidationResults{
ValidationError{"logo", "no such file: " + cwd + "/testdata/v0/invalid/no-network/no_such_file.png", 18, 1},
},
"logo_invalid_png.yml": ValidationResults{
ValidationError{"logo", "image: unknown format", 18, 1},
},
// landingURL
"landingURL_invalid.yml": ValidationResults{
// Just a syntax check here, no check for reachability as network is disabled
ValidationError{"landingURL", "landingURL must be an HTTP URL", 8, 1},
},
// monochromeLogo
"monochromeLogo_invalid_png.yml": ValidationResults{
ValidationWarning{"monochromeLogo", "This key is DEPRECATED and will be removed in the future. Use 'logo' instead", 18, 1},
ValidationError{"monochromeLogo", "image: unknown format", 18, 1},
},
// YAML 1.1 boolean aliases must be rejected
"localisation_localisationReady_yaml11_yes.yml": ValidationResults{
ValidationError{"localisation.localisationReady", "wrong type for this field", 51, 1},
ValidationError{"localisation.localisationReady", "localisationReady is a required field", 51, 3},
ValidationError{"localisation.availableLanguages", "availableLanguages is a required field", 52, 3},
},
"localisation_localisationReady_yaml11_no.yml": ValidationResults{
ValidationError{"localisation.localisationReady", "wrong type for this field", 51, 1},
ValidationError{"localisation.localisationReady", "localisationReady is a required field", 51, 3},
ValidationError{"localisation.availableLanguages", "availableLanguages is a required field", 52, 3},
},
"localisation_localisationReady_yaml11_on.yml": ValidationResults{
ValidationError{"localisation.localisationReady", "wrong type for this field", 51, 1},
ValidationError{"localisation.localisationReady", "localisationReady is a required field", 51, 3},
ValidationError{"localisation.availableLanguages", "availableLanguages is a required field", 52, 3},
},
"localisation_localisationReady_yaml11_off.yml": ValidationResults{
ValidationError{"localisation.localisationReady", "wrong type for this field", 51, 1},
ValidationError{"localisation.localisationReady", "localisationReady is a required field", 51, 3},
ValidationError{"localisation.availableLanguages", "availableLanguages is a required field", 52, 3},
},
}
dir := "testdata/v0/invalid/no-network/"
testFiles, _ := filepath.Glob(dir + "*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 := parseNoNetwork(file)
checkParseErrors(t, err, testType{file, expected[baseName]})
})
}
for file := range expected {
if !slices.Contains(testFiles, dir+file) {
t.Errorf("No expected file %s", dir+file)
}
}
}
func TestInvalidTestcasesV0(t *testing.T) {
expected := map[string]error{
// publiccodeYmlVersion
"publiccodeYmlVersion_missing.yml": ValidationResults{ValidationError{"publiccodeYmlVersion", "publiccodeYmlVersion is a required field", 0, 0}},
"publiccodeYmlVersion_invalid.yml": ValidationResults{
ValidationError{
"publiccodeYmlVersion",
"unsupported version: '1'. Supported versions: 0, 0.2, 0.2.0, 0.2.1, 0.2.2, 0.3, 0.3.0, 0.4, 0.4.0, 0.5.0, 0.5",
0,
0,
},
},
"publiccodeYmlVersion_wrong_type.yml": ValidationResults{
ValidationError{"publiccodeYmlVersion", "wrong type for this field", 2, 1},
},
// name
"name_missing.yml": ValidationResults{ValidationError{"name", "name is a required field", 1, 1}},
"name_nil.yml": ValidationResults{ValidationError{"name", "name is a required field", 4, 1}},
"name_wrong_type.yml": ValidationResults{
ValidationError{"name", "wrong type for this field", 4, 1},
ValidationError{"name", "name is a required field", 4, 1},
},
// applicationSuite
"applicationSuite_wrong_type.yml": ValidationResults{ValidationError{"applicationSuite", "wrong type for this field", 4, 1}},
// url
"url_missing.yml": ValidationResults{ValidationError{"url", "url is a required field", 1, 1}},
"url_wrong_type.yml": ValidationResults{
ValidationError{"url", "wrong type for this field", 6, 1},
ValidationError{"url", "url is a required field", 6, 1},
},
"url_invalid.yml": ValidationResults{
ValidationError{"url", "url must be a valid URL", 6, 1},
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},
},
"landingURL_invalid.yml": ValidationResults{
ValidationError{"landingURL", "landingURL must be an HTTP URL", 8, 1},
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[1]", "isBasedOn[1] must be a valid URL", 11, 5},
},
"isBasedOn_bad_url_string.yml": ValidationResults{
ValidationError{"isBasedOn[0]", "isBasedOn[0] must be a valid URL", 9, 1},
},
// softwareVersion
"softwareVersion_wrong_type.yml": ValidationResults{
ValidationError{"softwareVersion", "wrong type for this field", 8, 1},
},
// releaseDate
"releaseDate_empty.yml": ValidationResults{ValidationError{"releaseDate", "releaseDate must be a date with format 'YYYY-MM-DD'", 8, 1}},
"releaseDate_wrong_type.yml": ValidationResults{
ValidationError{"releaseDate", "wrong type for this field", 8, 1},
},
"releaseDate_invalid.yml": ValidationResults{
ValidationError{"releaseDate", "releaseDate must be a date with format 'YYYY-MM-DD'", 8, 1},
},
"releaseDate_datetime.yml": ValidationResults{
ValidationError{"releaseDate", "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", "invalid file extension for: " + cwd + "/testdata/v0/invalid/logo.mpg", 18, 1},
},
"logo_missing_file.yml": ValidationResults{
ValidationError{"logo", "no such file: " + cwd + "/testdata/v0/invalid/no_such_file.png", 18, 1},
},
"logo_absolute_path.yml": ValidationResults{
ValidationError{"logo", "is an absolute path. Only relative paths or HTTP(s) URLs allowed", 18, 1},
},
"logo_file_scheme.yml": ValidationResults{
ValidationError{"logo", "is a file:// URL. Only relative paths or HTTP(s) URLs allowed", 18, 1},
},
"logo_file_scheme2.yml": ValidationResults{
ValidationError{"logo", "is a file:// URL. Only relative paths or HTTP(s) URLs allowed", 18, 1},
},
"logo_file_scheme3.yml": ValidationResults{
ValidationError{"logo", "is a file:// URL. Only relative paths or HTTP(s) URLs allowed", 18, 1},
},
// Local publiccode.yml and URL in logo: should look for the logo remotely
"logo_missing_url.yml": ValidationResults{
ValidationError{"logo", "HTTP GET failed for https://google.com/no_such_file.png: not found", 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. Use 'logo' instead", 18, 1},
ValidationError{
"monochromeLogo",
"invalid file extension for: " + cwd + "/testdata/v0/invalid/monochromeLogo.mpg",
18,
1,
},
},
"monochromeLogo_missing_file.yml": ValidationResults{
ValidationWarning{"monochromeLogo", "This key is DEPRECATED and will be removed in the future. Use 'logo' instead", 18, 1},
ValidationError{"monochromeLogo", "no such file: " + cwd + "/testdata/v0/invalid/no_such_file.png", 18, 1},
},
// organisation
"organisation_wrong_uri.yml": ValidationResults{
ValidationError{"organisation.uri", "uri is not a valid URI", 19, 3},
},
"organisation_wrong_type.yml": ValidationResults{
ValidationError{"organisation[0]", "wrong type for this field", 18, 1},
},
"organisation_uri_missing.yml": ValidationResults{
ValidationError{"organisation.uri", "uri is a required field", 18, 3},
},
"organisation_uri_wrong_italian_pa.yml": ValidationResults{
ValidationError{"organisation.uri", "uri is not a valid URI", 20, 3},
},
"organisation_uri_wrong_italian_pa2.yml": ValidationResults{
ValidationError{"organisation.uri", "uri must be a valid Italian Public Administration Code (iPA) with format 'urn:x-italian-pa:[codiceIPA]' (see https://www.indicepa.gov.it/public-services/opendata-read-service.php?dstype=FS&filename=amministrazioni.txt)", 19, 3},
},
// inputTypes
"inputTypes_invalid.yml": ValidationResults{
ValidationError{"inputTypes[1]", "inputTypes[1] is not a valid MIME type", 16, 7},
ValidationWarning{"inputTypes", "This key is DEPRECATED and will be removed in the future. It's safe to drop it", 14, 1},
},
"inputTypes_wrong_type.yml": ValidationResults{
ValidationError{"inputTypes.foobar", "wrong type for this field", 15, 1},
},
// outputTypes
"outputTypes_invalid.yml": ValidationResults{
ValidationError{"outputTypes[1]", "outputTypes[1] is not a valid MIME type", 16, 7},
ValidationWarning{"outputTypes", "This key is DEPRECATED and will be removed in the future. It's safe to drop it", 14, 1},
},
"outputTypes_wrong_type.yml": ValidationResults{
ValidationError{"outputTypes.foobar", "wrong type for this field", 15, 1},
},
// platforms
"platforms_missing.yml": ValidationResults{ValidationError{"platforms", "platforms must contain more than 0 items", 1, 1}},
"platforms_wrong_type.yml": ValidationResults{
ValidationError{"platforms", "wrong type for this field", 9, 1},
ValidationError{"platforms", "platforms must contain more than 0 items", 9, 1},
},
// categories
"categories_invalid.yml": ValidationResults{ValidationError{"categories[0]", "categories[0] must be a valid category (see https://github.com/publiccodeyml/publiccode.yml/blob/main/docs/standard/categories-list.rst)", 13, 5}},
// usedBy
"usedBy_wrong_type.yml": ValidationResults{
ValidationError{"usedBy", "wrong type for this field", 14, 1},
},
// fundedBy
"fundedBy_wrong_uri.yml": ValidationResults{
ValidationError{"fundedBy[0].uri", "uri is not a valid URI", 19, 5},
},
"fundedBy_wrong_type.yml": ValidationResults{
ValidationError{"fundedBy.name", "wrong type for this field", 18, 1},
},
"fundedBy_uri_missing.yml": ValidationResults{
ValidationError{"fundedBy[0].uri", "uri is a required field", 18, 5},
},
"fundedBy_uri_wrong_italian_pa.yml": ValidationResults{
ValidationError{"fundedBy[0].uri", "uri is not a valid URI", 20, 5},
},
"fundedBy_uri_wrong_italian_pa2.yml": ValidationResults{
ValidationError{"fundedBy[0].uri", "uri must be a valid Italian Public Administration Code (iPA) with format 'urn:x-italian-pa:[codiceIPA]' (see https://www.indicepa.gov.it/public-services/opendata-read-service.php?dstype=FS&filename=amministrazioni.txt)", 19, 5},
},
// roadmap
"roadmap_invalid.yml": ValidationResults{
ValidationError{"roadmap", "roadmap must be an HTTP URL", 4, 1},
ValidationError{"roadmap", "'foobar' not reachable: missing URL scheme", 4, 1},
},
"roadmap_wrong_type.yml": ValidationResults{
ValidationError{"roadmap", "wrong type for this field", 4, 1},
},
// developmentStatus
"developmentStatus_missing.yml": ValidationResults{
ValidationError{"developmentStatus", "developmentStatus is a required field", 1, 1},
},
"developmentStatus_invalid.yml": ValidationResults{
ValidationError{"developmentStatus", "developmentStatus must be one of the following: \"concept\", \"development\", \"beta\", \"stable\" or \"obsolete\"", 16, 1},
},
"developmentStatus_wrong_type.yml": ValidationResults{
ValidationError{"developmentStatus", "wrong type for this field", 16, 1},
ValidationError{"developmentStatus", "developmentStatus is a required field", 16, 1},
},
// softwareType
"softwareType_missing.yml": ValidationResults{
ValidationError{"softwareType", "softwareType is a required field", 1, 1},
},
"softwareType_invalid.yml": ValidationResults{
ValidationError{"softwareType", "softwareType must be one of the following: \"standalone/mobile\", \"standalone/iot\", \"standalone/desktop\", \"standalone/web\", \"standalone/backend\", \"standalone/other\", \"addon\", \"library\" or \"configurationFiles\"", 17, 1},
},
"softwareType_wrong_type.yml": ValidationResults{
ValidationError{"softwareType", "wrong type for this field", 17, 1},
ValidationError{"softwareType", "softwareType is a required field", 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[2]", "countries[2] must be a valid ISO 3166-1 alpha-2 two-letter country code", 22, 7},
},
"intendedAudience_countries_invalid_iso_3166_1_alpha_2.yml": ValidationResults{
ValidationError{"intendedAudience.countries[2]", "countries[2] must be a valid ISO 3166-1 alpha-2 two-letter country code", 22, 7},
},
"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]", "unsupportedCountries[0] must be a valid ISO 3166-1 alpha-2 two-letter country code", 20, 7},
},
"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]", "scope[0] must be a valid scope (see https://github.com/publiccodeyml/publiccode.yml/blob/main/docs/standard/scope-list.rst)", 20, 9},
},
"intendedAudience_scope_wrong_type.yml": ValidationResults{
ValidationError{"intendedAudience.scope", "wrong type for this field", 19, 1},
},
// description
// description.*
"description_invalid_language.yml": ValidationResults{
ValidationError{"description", "description must be a valid BCP 47 language", 18, 1},
},
"description_en_features_missing.yml": ValidationResults{
ValidationError{"description.en.features", "features must contain more than 0 items", 22, 5},
},
"description_en_features_empty.yml": ValidationResults{
ValidationError{"description.en.features", "features must contain more than 0 items", 39, 5},
},
"description_en_localisedName_wrong_type.yml": ValidationResults{
ValidationError{"description.en.localisedName", "wrong type for this field", 21, 1},
ValidationError{"description", "description must contain more than 0 items", 18, 1},
},
"description_en_genericName_too_long.yml": ValidationResults{
ValidationError{"description.en.genericName", "genericName must be a maximum of 35 characters in length", 22, 5},
ValidationWarning{"description.en.genericName", "This key is DEPRECATED and will be removed in the future. It's safe to drop it", 22, 5},
},
"description_en_shortDescription_missing.yml": ValidationResults{
ValidationError{"description.en.shortDescription", "shortDescription is a required field", 20, 5},
},
"description_en_longDescription_missing.yml": ValidationResults{
ValidationError{"description.en.longDescription", "longDescription is a required field", 20, 5},
},
"description_en_longDescription_too_long.yml": ValidationResults{
ValidationError{"description.en.longDescription", "longDescription must be a maximum of 10000 characters in length", 27, 5},
},
"description_en_longDescription_too_short.yml": ValidationResults{
ValidationError{"description.en.longDescription", "longDescription must be at least 150 characters in length", 27, 5},
},
"description_en_longDescription_too_short_grapheme_clusters.yml": ValidationResults{
ValidationError{"description.en.longDescription", "longDescription must be at least 150 characters in length", 28, 5},
},
"description_en_documentation_invalid.yml": ValidationResults{
ValidationError{"description.en.documentation", "documentation must be an HTTP URL", 25, 5},
ValidationError{"description.en.documentation", "'not_a_url' not reachable: missing URL scheme", 25, 5},
},
"description_en_documentation_wrong_type.yml": ValidationResults{
ValidationError{"description.en.documentation", "wrong type for this field", 25, 1},
ValidationError{"description", "description must contain more than 0 items", 20, 1},
},
"description_en_apiDocumentation_invalid.yml": ValidationResults{
ValidationError{"description.en.apiDocumentation", "apiDocumentation must be an HTTP URL", 41, 5},
ValidationError{"description.en.apiDocumentation", "'abc' not reachable: missing URL scheme", 41, 5},
},
"description_en_apiDocumentation_wrong_type.yml": ValidationResults{
ValidationError{"description.en.apiDocumentation", "wrong type for this field", 43, 1},
ValidationError{"description", "description must contain more than 0 items", 20, 1},
},
"description_en_screenshots_missing_file.yml": ValidationResults{
ValidationError{
"description.en.screenshots[0]",
"'no_such_file.png' is not an image: no such file: " + cwd + "/testdata/v0/invalid/no_such_file.png",
42,
9,
},
},
"description_en_awards_wrong_type.yml": ValidationResults{
ValidationError{"description.en.awards", "wrong type for this field", 40, 1},
ValidationError{"description", "description must contain more than 0 items", 18, 1},
},
"description_en_videos_invalid.yml": ValidationResults{
ValidationError{"description.en.videos[0]", "videos[0] must be an HTTP URL", 41, 9},
ValidationError{"description.en.videos[0]", "'ABC' is not a valid video URL supporting oEmbed: invalid oEmbed link: ABC", 41, 9},
},
"description_en_videos_invalid_oembed.yml": ValidationResults{
ValidationError{"description.en.videos[0]", "'https://google.com' is not a valid video URL supporting oEmbed: invalid oEmbed link: https://google.com", 41, 9},
},
// legal
// legal.*
"legal_missing.yml": ValidationResults{ValidationError{"legal.license", "license is a required field", 0, 0}},
"legal_wrong_type.yml": ValidationResults{
ValidationError{"legal", "wrong type for this field", 46, 1},
ValidationError{"legal.license", "license is a required field", 46, 8},
},
"legal_license_missing.yml": ValidationResults{ValidationError{"legal.license", "license is a required field", 41, 3}},
"legal_license_invalid.yml": ValidationResults{ValidationError{
"legal.license", "license must be a valid license (see https://spdx.org/licenses)", 42, 3,
}},
"legal_authorsFile_missing_file.yml": ValidationResults{
ValidationWarning{
"legal.authorsFile",
"This key is DEPRECATED and will be removed in the future. It's safe to drop it",
42,
3,
},
ValidationError{
"legal.authorsFile",
"'" + cwd + "/testdata/v0/invalid/no_such_authors_file.txt' does not exist: no such file: " + cwd + "/testdata/v0/invalid/no_such_authors_file.txt",
42,
3,
},
},
// maintenance
// maintenance.*
"maintenance_type_missing.yml": ValidationResults{
ValidationError{"maintenance.type", "type is a required field", 47, 3},
},
"maintenance_type_invalid.yml": ValidationResults{
ValidationError{"maintenance.type", "type must be one of the following: \"internal\", \"contract\", \"community\" or \"none\"", 45, 3},
},
"maintenance_contacts_missing_with_type_community.yml": ValidationResults{
ValidationError{"maintenance.contacts", "contacts is a required field when \"type\" is \"community\"", 44, 3},
},
"maintenance_contacts_missing_with_type_internal.yml": ValidationResults{
ValidationError{"maintenance.contacts", "contacts is a required field when \"type\" is \"internal\"", 44, 3},
},
"maintenance_contacts_name_missing.yml": ValidationResults{
ValidationError{"maintenance.contacts[0].name", "name is a required field", 47, 7},
},
"maintenance_contacts_email_invalid.yml": ValidationResults{
ValidationError{"maintenance.contacts[0].email", "email must be a valid email address", 49, 9},
},
"maintenance_contractors_missing_with_type_contract.yml": ValidationResults{
ValidationError{"maintenance.contractors", "contractors is a required field when \"type\" is \"contract\"", 44, 3},
},
"maintenance_contractors_invalid_type.yml": ValidationResults{
ValidationError{"maintenance.contractors", "wrong type for this field", 47, 1},
ValidationError{"maintenance.type", "type is a required field", 44, 3},
},
"maintenance_contractors_name_missing.yml": ValidationResults{
ValidationError{"maintenance.contractors[0].name", "name is a required field", 47, 7},
},
"maintenance_contractors_until_missing.yml": ValidationResults{
ValidationError{"maintenance.contractors[0].until", "until is a required field", 47, 7},
},
"maintenance_contractors_until_invalid.yml": ValidationResults{
ValidationError{"maintenance.contractors[0].until", "until must be a date with format 'YYYY-MM-DD'", 49, 7},
},
"maintenance_contractors_email_invalid.yml": ValidationResults{
ValidationError{"maintenance.contractors[0].email", "email must be a valid email address", 50, 8},
},
"maintenance_contractors_website_invalid.yml": ValidationResults{
ValidationError{"maintenance.contractors[0].website", "website must be an HTTP URL", 52, 7},
},
"maintenance_contractors_when_type_is_community.yml": ValidationResults{
ValidationError{"maintenance.contractors", "contractors must not be present unless \"type\" is \"contract\"", 46, 3},
},
"maintenance_contractors_when_type_is_internal.yml": ValidationResults{
ValidationError{"maintenance.contractors", "contractors must not be present unless \"type\" is \"contract\"", 46, 3},
},
"maintenance_contractors_when_type_is_none.yml": ValidationResults{
ValidationError{"maintenance.contractors", "contractors must not be present unless \"type\" is \"contract\"", 46, 3},
},
// localisation
"localisation_availableLanguages_missing.yml": ValidationResults{
ValidationError{"localisation.availableLanguages", "availableLanguages is a required field", 50, 3},
},
"localisation_availableLanguages_empty.yml": ValidationResults{
ValidationError{"localisation.availableLanguages", "availableLanguages must contain more than 0 items", 52, 3},
},
"localisation_availableLanguages_invalid.yml": ValidationResults{
ValidationError{"localisation.availableLanguages[0]", "availableLanguages[0] must be a valid BCP 47 language", 53, 8},
},
"localisation_localisationReady_missing.yml": ValidationResults{
ValidationError{"localisation.localisationReady", "localisationReady is a required field", 52, 3},
},
// dependsOn
"dependsOn_open_name_wrong_type.yml": ValidationResults{
ValidationError{"dependsOn.open[0]", "wrong type for this field", 56, 1},
},
"dependsOn_open_versionMin_wrong_type.yml": ValidationResults{
ValidationError{"dependsOn.open[0].versionMin", "wrong type for this field", 57, 1},
},
"dependsOn_open_versionMax_wrong_type.yml": ValidationResults{
ValidationError{"dependsOn.open[0].versionMax", "wrong type for this field", 57, 1},
},
"dependsOn_open_version_wrong_type.yml": ValidationResults{
ValidationError{"dependsOn.open[0].version", "wrong type for this field", 57, 1},
},
"dependsOn_open_optional_wrong_type.yml": ValidationResults{
ValidationError{"dependsOn.open[0].optional", "wrong type for this field", 57, 1},
},
// it.*
"it_countryExtensionVersion_invalid.yml": ValidationResults{
ValidationError{"IT.countryExtensionVersion", "countryExtensionVersion must be one of the following: \"0.2\" or \"1.0\"", 12, 3},
},
"it_riuso_codiceIPA_invalid.yml": ValidationResults{
ValidationError{"IT.riuso.codiceIPA", "codiceIPA must be a valid Italian Public Administration Code (iPA) (see https://www.indicepa.gov.it/public-services/opendata-read-service.php?dstype=FS&filename=amministrazioni.txt)", 55, 5},
},
"it_IT_duplicated.yml": ValidationResults{
ValidationWarning{"it", "Lowercase country codes are DEPRECATED and will be removed in the future. Use 'IT' instead", 116, 1},
ValidationError{"it", "'IT' key already present. Remove this key", 116, 1},
},
"it_wrong_case.yml": ValidationResults{
ValidationError{"It", "unknown field \"It\"", 107, 1},
},
"description_en_gb_invalid_bcp47.yml": ValidationResults{
ValidationError{"description", "description must be a valid BCP 47 language", 21, 1},
},
"localisation_availableLanguages_invalid_bcp47.yml": ValidationResults{
ValidationError{"localisation.availableLanguages[0]", "availableLanguages[0] must be a valid BCP 47 language", 54, 8},
},
// misc
"file_encoding.yml": ValidationResults{ValidationError{"", "Invalid UTF-8", 0, 0}},
"invalid_yaml.yml": ValidationResults{ValidationError{"", "value is not allowed in this context. map key-value is pre-defined", 38, 1}},
"mostly_empty.yml": ValidationResults{
ValidationError{"name", "name is a required field", 1, 1},
ValidationError{"url", "url is a required field", 1, 1},
ValidationError{"platforms", "platforms must contain more than 0 items", 1, 1},
ValidationError{"developmentStatus", "developmentStatus is a required field", 1, 1},
ValidationError{"softwareType", "softwareType is a required field", 1, 1},
ValidationError{"description[en-US].shortDescription", "shortDescription is a required field", 3, 10},
ValidationError{"description[en-US].longDescription", "longDescription is a required field", 3, 10},
ValidationError{"description[en-US].features", "features must contain more than 0 items", 3, 10},
ValidationError{"legal.license", "license is a required field", 5, 8},
ValidationError{"maintenance.type", "type is a required field", 6, 14},
ValidationError{"localisation.localisationReady", "localisationReady is a required field", 4, 15},
ValidationError{"localisation.availableLanguages", "availableLanguages is a required field", 4, 15},
},
"unknown_field.yml": ValidationResults{
ValidationError{"foobar", "unknown field \"foobar\"", 10, 1},
},
}
dir := "testdata/v0/invalid/"
testFiles, _ := filepath.Glob(dir + "*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]})
})
}
for file := range expected {
if !slices.Contains(testFiles, dir+file) {
t.Errorf("No expected file %s", dir+file)
}
}
}
// Test v0 valid YAML testcases (testdata/v0/valid/).
func TestValidTestcasesV0(t *testing.T) {
checkValidFiles("testdata/v0/valid/*.yml", t)
}
// Test v0 valid YAML testcases (testdata/v0/valid_with_warnings/).
func TestValidWithWarningsTestcasesV0(t *testing.T) {
expected := map[string]error{
"unicode_grapheme_clusters.yml": ValidationResults{
ValidationWarning{"description.en.genericName", "This key is DEPRECATED and will be removed in the future. It's safe to drop it", 23, 5},
},
"valid.minimal.v0.2.yml": ValidationResults{
ValidationWarning{"publiccodeYmlVersion", "v0.2 is not the latest version, use '0'. Parsing this file as v0.5.", 1, 1},
},
"valid.minimal.v0.3.yml": ValidationResults{
ValidationWarning{"publiccodeYmlVersion", "v0.3 is not the latest version, use '0'. Parsing this file as v0.5.", 1, 1},
},
"valid.minimal.v0.4.yml": ValidationResults{
ValidationWarning{"publiccodeYmlVersion", "v0.4 is not the latest version, use '0'. Parsing this file as v0.5.", 1, 1},
},
"valid.mime_types.yml": ValidationResults{
ValidationWarning{"inputTypes", "This key is DEPRECATED and will be removed in the future. It's safe to drop it", 48, 1},
ValidationWarning{"outputTypes", "This key is DEPRECATED and will be removed in the future. It's safe to drop it", 50, 1},
},
"valid_with_it_conforme.yml": ValidationResults{
ValidationWarning{"IT.conforme", "This key is DEPRECATED and will be removed in the future. It's safe to drop it", 119, 3},
},
"valid_with_country_specific_section_downcase.yml": ValidationResults{
ValidationWarning{"it", "Lowercase country codes are DEPRECATED and will be removed in the future. Use 'IT' instead", 107, 1},
},
"valid_with_lowercase_countries.yml": ValidationResults{
ValidationWarning{"intendedAudience.countries[0]", "Lowercase country codes are DEPRECATED. Use uppercase instead ('IT')", 31, 7},
ValidationWarning{"intendedAudience.countries[1]", "Lowercase country codes are DEPRECATED. Use uppercase instead ('DE')", 32, 7},
ValidationWarning{"intendedAudience.unsupportedCountries[0]", "Lowercase country codes are DEPRECATED. Use uppercase instead ('US')", 34, 7},
},
"valid_with_legal_repoOwner.yml": ValidationResults{
ValidationWarning{"legal.repoOwner", "This key is DEPRECATED and will be removed in the future. Use 'organisation.name' instead", 70, 3},
},
"valid_with_IT_riuso_codiceIPA.yml": ValidationResults{
ValidationWarning{"IT.riuso.codiceIPA", "This key is DEPRECATED and will be removed in the future. Use 'organisation.uri' and set it to 'urn:x-italian-pa:pcm' instead", 119, 5},
},
}
dir := "testdata/v0/valid_with_warnings/"
testFiles, _ := filepath.Glob(dir + "*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]})
})
}
for file := range expected {
if !slices.Contains(testFiles, dir+file) {
t.Errorf("No expected file %s", dir+file)
}
}
}
// Test publiccode.yml remote files for key errors.
func TestDecodeValueErrorsRemote(t *testing.T) {
testRemoteFiles := []testType{
{"https://raw.githubusercontent.com/italia/publiccode-parser-go/refs/heads/main/testdata/v0/valid_with_warnings/valid_with_lowercase_countries.yml", ValidationResults{
ValidationWarning{"intendedAudience.countries[0]", "Lowercase country codes are DEPRECATED. Use uppercase instead ('IT')", 31, 7},
ValidationWarning{"intendedAudience.countries[1]", "Lowercase country codes are DEPRECATED. Use uppercase instead ('DE')", 32, 7},
ValidationWarning{"intendedAudience.unsupportedCountries[0]", "Lowercase country codes are DEPRECATED. Use uppercase instead ('US')", 34, 7},
}},
}
parser, err := NewDefaultParser()
if err != nil {
t.Errorf("Can't create parser: %v", err)
}
for _, test := range testRemoteFiles {
t.Run(fmt.Sprintf("%v", test.err), func(t *testing.T) {
var err error
_, err = parser.Parse(test.file)
checkParseErrors(t, err, test)
})
}
}
// Test that files in fields with relative paths or URLs (ie. logo, screenshots, etc.)
// are checked and expanded correctly
func TestRelativePathsOrURLs(t *testing.T) {
testRemoteFiles := []testType{
// Remote publiccode.yml and relative path in screenshots:
// should look for the screenshot remotely relative to this URL
{"https://raw.githubusercontent.com/italia/publiccode-parser-go/refs/heads/main/testdata/v0/invalid/description_en_screenshots_missing_file.yml", ValidationResults{
ValidationError{"description.en.screenshots[0]", "'no_such_file.png' is not an image: HTTP GET failed for https://raw.githubusercontent.com/italia/publiccode-parser-go/refs/heads/main/testdata/v0/invalid/no_such_file.png: not found", 42, 9},
}},
// Local publiccode.yml and relative path in screenshot:
// should look for the logo relative to this path in the filesystem
{"testdata/v0/invalid/description_en_screenshots_missing_file.yml", ValidationResults{
ValidationError{"description.en.screenshots[0]", "'no_such_file.png' is not an image: no such file: " + cwd + "/testdata/v0/invalid/no_such_file.png", 42, 9},
}},
// Remote publiccode.yml and URL in logo:
// should look for the logo remotely
{"https://raw.githubusercontent.com/italia/publiccode-parser-go/refs/heads/main/testdata/v0/invalid/logo_missing_url.yml", ValidationResults{
ValidationError{"logo", "HTTP GET failed for https://google.com/no_such_file.png: not found", 18, 1},
}},
// Local publiccode.yml and URL in logo:
// should look for the logo remotely
//
// (already tested in TestInvalidTestcasesV0)
// "testdata/v0/invalid/logo_missing_url.yml", ValidationResults{
// ValidationError{"logo", "HTTP GET failed for https://google.com/no_such_file.png: not found", 18, 1},
//}},
}
parser, err := NewDefaultParser()
if err != nil {
t.Errorf("Can't create parser: %v", err)
}
for _, test := range testRemoteFiles {
t.Run(fmt.Sprintf("%v", test.err), func(t *testing.T) {
var err error
_, err = parser.Parse(test.file)
checkParseErrors(t, err, test)
})
}
}
// Test that files in fields with relative paths or URLs (ie. logo, screenshots, etc.)
// are checked and expanded correctly when DisableNetwork is true
func TestRelativePathsOrURLsNoNetworkRemoteChecks(t *testing.T) {
testRemoteFiles := []string{
// Remote publiccode.yml and relative path in screenshots:
// should look for the screenshot remotely relative to this URL,
// but DisableNetwork is true, so no check is performed.
"https://raw.githubusercontent.com/italia/publiccode-parser-go/refs/heads/main/testdata/v0/invalid/description_en_screenshots_missing_file.yml",
// Remote publiccode.yml and URL in logo:
// should look for the logo remotely but DisableNetwork is true, so no check is performed.
"https://raw.githubusercontent.com/italia/publiccode-parser-go/refs/heads/main/testdata/v0/invalid/logo_missing_url.yml",
// Local publiccode.yml and URL in logo:
// should look for the logo remotely but DisableNetwork is true, so no check is performed.
"testdata/v0/invalid/logo_missing_url.yml",
}
for _, file := range testRemoteFiles {
t.Run(file, func(t *testing.T) {
if err := parseNoNetwork(file); err != nil {
t.Errorf("[%s] validation failed for valid file: %T - %s\n", file, err, err)
}
})
}
}
// Test that files in fields with relative paths or URLs (ie. logo, screenshots, etc.)
// are checked and expanded correctly when DisableNetwork is true
func TestRelativePathsOrURLsNoNetwork(t *testing.T) {
testFiles := []testType{
// Local publiccode.yml and relative path in screenshot:
// should look for the logo relative to this path in the filesystem.
// DisableNetwork doesn't affect this so the check *IS* performed.
{"testdata/v0/invalid/description_en_screenshots_missing_file.yml", ValidationResults{
ValidationError{"description.en.screenshots[0]", "'no_such_file.png' is not an image: no such file: " + cwd + "/testdata/v0/invalid/no_such_file.png", 42, 9},
}},
}
parser, err := NewParser(ParserConfig{DisableNetwork: true})
if err != nil {
t.Errorf("Can't create parser: %v", err)
}
for _, test := range testFiles {
t.Run(fmt.Sprintf("%v", test.err), func(t *testing.T) {
var err error
_, err = parser.Parse(test.file)
checkParseErrors(t, err, test)
})
}
}
func TestUrlMissingWithoutPath(t *testing.T) {
expected := map[string]error{
"url_missing.yml": ValidationResults{
ValidationError{"url", "url is a required field", 1, 1},
},
}
parser, err := NewDefaultParser()
if err != nil {
t.Errorf("Can't create parser: %v", err)
}
testFiles, _ := filepath.Glob("testdata/v0/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) {
_, err := parser.Parse(file)
checkParseErrors(t, err, testType{file, expected[baseName]})
})
}
}