Repository navigation
Expand file tree
/
Copy pathdocument_test.go
More file actions
76 lines (67 loc) · 2.37 KB
/
Copy pathdocument_test.go
File metadata and controls
76 lines (67 loc) · 2.37 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
package publiccode
import (
"errors"
"reflect"
"strings"
"testing"
)
func TestParseStreamNotMapping(t *testing.T) {
const (
html = "the file is not a YAML mapping of publiccode.yml keys but looks like an HTML page: use the URL of the raw file, not of the page that shows it"
notMapping = "the file is not a YAML mapping of publiccode.yml keys"
)
tests := []struct {
name string
yaml string
description string
line, column int
}{
{"list", "- a\n- b\n", notMapping, 1, 1},
{"string", "hello\n", notMapping, 1, 1},
{"block string", "|\n hello\n", notMapping, 1, 1},
{"number", "42\n", notMapping, 1, 1},
{"boolean", "true\n", notMapping, 1, 1},
{"null", "null\n", notMapping, 1, 1},
{"anchored list", "&x\n- a\n", notMapping, 1, 1},
{"tagged list", "--- !!seq\n- a\n", notMapping, 1, 5},
{"list after comments", "# publiccode.yml\n\n- a\n", notMapping, 3, 1},
{"HTML as a string", "<html><body>Not found</body></html>\n", html, 1, 1},
{"indented HTML", "\n\n <!DOCTYPE html>\n<html lang=\"en\">\n</html>\n", html, 3, 3},
// A colon in the text makes the page invalid YAML.
{"HTML with a syntax error", "<!DOCTYPE html>\n<p>Note: read this</p>\n", html, 1, 1},
}
p, err := NewParser(ParserConfig{DisableNetwork: true, DisableExternalChecks: true})
if err != nil {
t.Fatal(err)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := p.ParseStream(strings.NewReader(tt.yaml))
want := ValidationResults{ValidationError{Description: tt.description, Line: tt.line, Column: tt.column}}
var got ValidationResults
if !errors.As(err, &got) || !reflect.DeepEqual(got, want) {
t.Errorf("got %#v, want %#v", err, want)
}
})
}
}
func TestParseStreamMappingIsNotReportedAsNotMapping(t *testing.T) {
p, err := NewParser(ParserConfig{DisableNetwork: true, DisableExternalChecks: true})
if err != nil {
t.Fatal(err)
}
for _, yaml := range []string{
"publiccodeYmlVersion: \"0\"\n",
"--- !!map\npubliccodeYmlVersion: \"0\"\n",
"&doc\npubliccodeYmlVersion: \"0\"\n",
"{publiccodeYmlVersion: \"0\"}\n",
// A merge key starts with "<" too.
"<<: {publiccodeYmlVersion: \"0\"}\n",
"<<: {publiccodeYmlVersion: \"0\"}\nname: [\n",
} {
_, err := p.ParseStream(strings.NewReader(yaml))
if err != nil && strings.Contains(err.Error(), "not a YAML mapping") {
t.Errorf("%q: %v", yaml, err)
}
}
}