-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathparser_internal_test.go
More file actions
184 lines (165 loc) · 4.78 KB
/
parser_internal_test.go
File metadata and controls
184 lines (165 loc) · 4.78 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
package publiccode
import (
"strings"
"testing"
"github.com/goccy/go-yaml/parser"
)
func TestGetPositionInFileEmptyDocs(t *testing.T) {
// An empty YAML file results in a parsed file with no docs.
file, err := parser.ParseBytes([]byte(""), 0)
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}
line, col := getPositionInFile("someKey", file)
if line != 0 || col != 0 {
t.Errorf("expected 0,0 for empty file, got %d,%d", line, col)
}
}
func TestGetPositionInFileNilBody(t *testing.T) {
// Parse YAML that produces a doc with nil body.
// A YAML file with only comments has a doc with nil Body.
file, err := parser.ParseBytes([]byte("# just a comment\n"), 0)
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}
line, col := getPositionInFile("someKey", file)
if line != 0 || col != 0 {
t.Errorf("expected 0,0 for comment-only file, got %d,%d", line, col)
}
}
func TestDecodeUnknownField(t *testing.T) {
// Parse a YAML with an unknown field, which should be caught by
// the decode function as an UnknownFieldError.
yaml := `publiccodeYmlVersion: "0"
name: test
unknownField: value
`
file, err := parser.ParseBytes([]byte(yaml), 0)
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}
v0 := &PublicCodeV0{}
results := decode([]byte(yaml), v0, file)
if results == nil {
t.Error("expected error for unknown field")
}
}
func TestDecodeWrongType(t *testing.T) {
// Parse YAML where a field has wrong type (e.g. name is a list).
yaml := `publiccodeYmlVersion: "0"
name:
- item1
- item2
`
file, err := parser.ParseBytes([]byte(yaml), 0)
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}
v0 := &PublicCodeV0{}
results := decode([]byte(yaml), v0, file)
if results == nil {
t.Error("expected error for wrong type")
}
}
func TestToURLLocalPath(t *testing.T) {
// toURL with a local file path should return a file:// URL.
u, err := toURL("./testdata/v0/valid/valid.yml")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if u.Scheme != "file" {
t.Errorf("expected file scheme, got %q", u.Scheme)
}
}
func TestToURLHTTPS(t *testing.T) {
u, err := toURL("https://example.com/publiccode.yml")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if u.Host != "example.com" {
t.Errorf("unexpected host: %s", u.Host)
}
}
func TestFindKeyPosNilNode(t *testing.T) {
line, col := findKeyPos(nil, []string{"key"})
if line != 0 || col != 0 {
t.Errorf("expected 0,0 for nil node, got %d,%d", line, col)
}
}
func TestFindKeyAtLineNilNode(t *testing.T) {
result := findKeyAtLine(nil, 1, "")
if result != "" {
t.Errorf("expected empty string for nil node, got %q", result)
}
}
func TestFindKeyPosDocumentNode(t *testing.T) {
file, err := parser.ParseBytes([]byte("key: value\n"), 0)
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}
// file.Docs[0] is a *ast.DocumentNode: exercises the DocumentNode case in findKeyPos
line, col := findKeyPos(file.Docs[0], []string{"key"})
if line == 0 {
t.Errorf("expected non-zero line for 'key', got %d,%d", line, col)
}
}
func TestFindKeyAtLineDocumentNode(t *testing.T) {
file, err := parser.ParseBytes([]byte("key: value\n"), 0)
if err != nil {
t.Fatalf("unexpected parse error: %v", err)
}
// file.Docs[0] is a *ast.DocumentNode: exercises the DocumentNode case in findKeyAtLine
result := findKeyAtLine(file.Docs[0], 1, "")
_ = result
}
func TestParseStreamDeprecatedVersionWarning(t *testing.T) {
// An older but supported version should produce a deprecation warning.
p, _ := NewParser(ParserConfig{DisableNetwork: true})
yaml := `publiccodeYmlVersion: "0.2"
name: test
url: "https://github.com/example/repo.git"
platforms:
- web
developmentStatus: stable
softwareType: "standalone/web"
description:
en:
genericName: Test
shortDescription: "A short description."
longDescription: >
Long description that is longer than one hundred and fifty characters to
satisfy the minimum length requirement for this field in publiccode.yml.
Adding more text here to ensure we definitely hit the 150 character minimum.
features:
- feature1
legal:
license: MIT
maintenance:
type: none
localisation:
localisationReady: true
availableLanguages:
- en
`
_, err := p.ParseStream(strings.NewReader(yaml))
// Should produce a deprecation warning but no validation errors.
if err == nil {
return // no errors at all is also fine
}
vr, ok := err.(ValidationResults)
if !ok {
t.Fatalf("expected ValidationResults, got %T: %v", err, err)
}
hasWarning := false
for _, e := range vr {
if _, ok := e.(ValidationWarning); ok {
hasWarning = true
}
if _, ok := e.(ValidationError); ok {
t.Errorf("unexpected validation error: %v", e)
}
}
if !hasWarning {
t.Error("expected at least one deprecation warning for old version")
}
}