Skip to content

Commit f46593b

Browse files
committed
wip
1 parent 57264ce commit f46593b

6 files changed

+120
-31
lines changed

parser.go

+16-14
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,13 @@ func NewDefaultParser() (*Parser, error) {
9494

9595
// ParseStream reads the data and tries to parse it. Returns an error if fails.
9696
func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
97-
var ve ValidationResults
98-
9997
b, err := io.ReadAll(in)
10098
if err != nil {
101-
ve = append(ve, newValidationError("", fmt.Sprintf("Can't read the stream: %v", err)))
102-
103-
return nil, ve
99+
return nil, ValidationResults{newValidationError("", fmt.Sprintf("Can't read the stream: %v", err))}
104100
}
105101

106102
if !utf8.Valid(b) {
107-
ve = append(ve, newValidationError("", "Invalid UTF-8"))
108-
109-
return nil, ve
103+
return nil, ValidationResults{newValidationError("", "Invalid UTF-8")}
110104
}
111105

112106
// First, decode the YAML into yaml.Node so we can access line and column
@@ -121,19 +115,27 @@ func (p *Parser) ParseStream(in io.Reader) (PublicCode, error) {
121115
node = *node.Content[0]
122116
} else {
123117
// YAML is malformed
124-
ve = append(ve, toValidationError(err.Error(), nil))
125-
126-
return nil, ve;
118+
return nil, ValidationResults{toValidationError(err.Error(), nil)}
127119
}
128120

129121
_, version := getNodes("publiccodeYmlVersion", &node)
130122
if version == nil {
131-
ve = append(ve, newValidationError("publiccodeYmlVersion", "required"))
123+
return nil, ValidationResults{newValidationError("publiccodeYmlVersion", "required")}
124+
}
125+
if version.ShortTag() != "!!str" {
126+
line, column := getPositionInFile("publiccodeYmlVersion", node)
132127

133-
return nil, ve
128+
return nil, ValidationResults{ValidationError{
129+
Key: "publiccodeYmlVersion",
130+
Description: "wrong type for this field",
131+
Line: line,
132+
Column: column,
133+
}}
134134
}
135135

136-
if slices.Contains(SupportedVersions, version.Value) && strings.HasPrefix(version.Value, "0.2") {
136+
var ve ValidationResults
137+
138+
if slices.Contains(SupportedVersions, version.Value) && !strings.HasPrefix(version.Value, "0.3") {
137139
latestVersion := SupportedVersions[len(SupportedVersions)-1]
138140
line, column := getPositionInFile("publiccodeYmlVersion", node)
139141

parser_test.go

+49-16
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ func parse(file string) error {
3333
return err
3434
}
3535

36+
func parseNoNetwork(file string) error {
37+
var p *Parser
38+
var err error
39+
40+
if p, err = NewParser(ParserConfig{DisableNetwork: true}); err != nil {
41+
return err
42+
}
43+
44+
_, err = p.Parse(file)
45+
46+
return err
47+
}
48+
3649
// Check all the YAML files matching the glob pattern and fail for each file
3750
// with parsing or validation errors.
3851
func checkValidFiles(pattern string, t *testing.T) {
@@ -69,12 +82,39 @@ func TestValidPreviousStandardVersion(t *testing.T) {
6982
})
7083
})
7184
}
72-
func TestInvalidTestcasesV0_3(t *testing.T) {
73-
cwd, err := os.Getwd()
74-
if err != nil {
75-
t.Errorf("Can't get current working directory")
85+
86+
func TestInvalidTestcasesV0_3_NoNetwork(t *testing.T) {
87+
expected := map[string]error{
88+
// logo
89+
"logo_missing_file.yml": ValidationResults{
90+
ValidationError{"logo", "no such file: no_such_file.png", 18, 1},
91+
},
92+
"logo_invalid_png.yml": ValidationResults{
93+
ValidationError{"logo", "image: unknown format", 18, 1},
94+
},
95+
96+
// monochromeLogo
97+
"monochromeLogo_invalid_png.yml": ValidationResults{
98+
ValidationWarning{"monochromeLogo", "This key is DEPRECATED and will be removed in the future", 18, 1},
99+
ValidationError{"monochromeLogo", "image: unknown format", 18, 1},
100+
},
101+
76102
}
77103

104+
testFiles, _ := filepath.Glob("testdata/v0.3/invalid/no-network/*yml")
105+
for _, file := range testFiles {
106+
baseName := path.Base(file)
107+
if expected[baseName] == nil {
108+
t.Errorf("No expected data for file %s", baseName)
109+
}
110+
t.Run(file, func(t *testing.T) {
111+
err := parseNoNetwork(file)
112+
checkParseErrors(t, err, testType{file, expected[baseName]})
113+
})
114+
}
115+
}
116+
117+
func TestInvalidTestcasesV0_3(t *testing.T) {
78118
expected := map[string]error{
79119
// publiccodeYmlVersion
80120
"publiccodeYmlVersion_missing.yml": ValidationResults{ValidationError{"publiccodeYmlVersion", "required", 0, 0}},
@@ -83,7 +123,7 @@ func TestInvalidTestcasesV0_3(t *testing.T) {
83123
}},
84124
"publiccodeYmlVersion_wrong_type.yml": ValidationResults{
85125
ValidationError{"publiccodeYmlVersion", "wrong type for this field", 2, 1},
86-
ValidationError{"publiccodeYmlVersion", "required", 2, 1}},
126+
},
87127

88128
// name
89129
"name_missing.yml": ValidationResults{ValidationError{"name", "required", 1, 1}},
@@ -152,13 +192,10 @@ func TestInvalidTestcasesV0_3(t *testing.T) {
152192
ValidationError{"logo", "wrong type for this field", 18, 1},
153193
},
154194
"logo_unsupported_extension.yml": ValidationResults{
155-
ValidationError{"logo", fmt.Sprintf("invalid file extension for: %s/logo.mpg", cwd), 18, 1},
195+
ValidationError{"logo", "invalid file extension for: https://raw.githubusercontent.com/italia/developers.italia.it/main/logo.mpg", 18, 1},
156196
},
157197
"logo_missing_file.yml": ValidationResults{
158-
ValidationError{"logo", fmt.Sprintf("no such file: %s/no_such_file.png", cwd), 18, 1},
159-
},
160-
"logo_invalid_png.yml": ValidationResults{
161-
ValidationError{"logo", "image: unknown format", 18, 1},
198+
ValidationError{"logo", "no such file: https://raw.githubusercontent.com/italia/developers.italia.it/main/no_such_file.png", 18, 1},
162199
},
163200

164201
// monochromeLogo
@@ -169,18 +206,14 @@ func TestInvalidTestcasesV0_3(t *testing.T) {
169206
ValidationWarning{"monochromeLogo", "This key is DEPRECATED and will be removed in the future", 18, 1},
170207
ValidationError{
171208
"monochromeLogo",
172-
fmt.Sprintf("invalid file extension for: %s/monochromeLogo.mpg", cwd),
209+
"invalid file extension for: https://raw.githubusercontent.com/italia/developers.italia.it/main/monochromeLogo.mpg",
173210
18,
174211
1,
175212
},
176213
},
177214
"monochromeLogo_missing_file.yml": ValidationResults{
178215
ValidationWarning{"monochromeLogo", "This key is DEPRECATED and will be removed in the future", 18, 1},
179-
ValidationError{"monochromeLogo", fmt.Sprintf("no such file: %s/no_such_file.png", cwd), 18, 1},
180-
},
181-
"monochromeLogo_invalid_png.yml": ValidationResults{
182-
ValidationWarning{"monochromeLogo", "This key is DEPRECATED and will be removed in the future", 18, 1},
183-
ValidationError{"monochromeLogo", "image: unknown format", 18, 1},
216+
ValidationError{"monochromeLogo", "no such file: https://raw.githubusercontent.com/italia/developers.italia.it/main/no_such_file.png", 18, 1},
184217
},
185218

186219
// inputTypes

testdata/v0.3/invalid/legal_license_missing.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ description:
3838
- Just one feature
3939

4040
legal:
41-
authorsFile: testdata/AUTHORS
41+
repoOwner: foobar
4242
# Should NOT validate: license is missing
4343

4444
maintenance:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
publiccodeYmlVersion: "0.3"
2+
3+
name: Medusa
4+
url: "https://github.com/italia/developers.italia.it.git"
5+
releaseDate: "2017-04-15"
6+
7+
platforms:
8+
- web
9+
10+
categories:
11+
- cloud-management
12+
13+
developmentStatus: development
14+
15+
softwareType: "standalone/other"
16+
17+
# Should NOT validate: logo must exist as a file
18+
logo: "no_such_file.png"
19+
20+
description:
21+
eng:
22+
localisedName: Medusa
23+
shortDescription: >
24+
A rather short description which
25+
is probably useless
26+
longDescription: >
27+
Very long description of this software, also split
28+
on multiple rows. You should note what the software
29+
is and why one should need it. This is 158 characters.
30+
Very long description of this software, also split
31+
on multiple rows. You should note what the software
32+
is and why one should need it. This is 316 characters.
33+
Very long description of this software, also split
34+
on multiple rows. You should note what the software
35+
is and why one should need it. This is 474 characters.
36+
Very long description of this software, also split
37+
on multiple rows. You should note what the software
38+
is and why one should need it. This is 632 characters.
39+
features:
40+
- Just one feature
41+
42+
legal:
43+
license: AGPL-3.0-or-later
44+
45+
maintenance:
46+
type: "community"
47+
48+
contacts:
49+
- name: Francesco Rossi
50+
51+
localisation:
52+
localisationReady: true
53+
availableLanguages:
54+
- eng

0 commit comments

Comments
 (0)