forked from github/gh-aw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyaml_error_test.go
More file actions
337 lines (310 loc) · 10.6 KB
/
Copy pathyaml_error_test.go
File metadata and controls
337 lines (310 loc) · 10.6 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
//go:build !integration
package parser
import (
"fmt"
"strings"
"testing"
"github.com/goccy/go-yaml"
"github.com/stretchr/testify/assert"
)
// TestFormatYAMLError tests the new FormatYAMLError function that uses yaml.FormatError()
func TestFormatYAMLError(t *testing.T) {
tests := []struct {
name string
yamlContent string
frontmatterLineOffset int
expectedLineCol string // Expected [line:col] format in output
expectSourceContext bool // Should contain source code lines with | markers
expectVisualPointer bool // Should contain visual ^ pointer
}{
{
name: "invalid mapping with offset 1",
yamlContent: "invalid: yaml: syntax",
frontmatterLineOffset: 1,
expectedLineCol: "[1:10]",
expectSourceContext: true,
expectVisualPointer: true,
},
{
name: "invalid mapping with offset 5",
yamlContent: "invalid: yaml: syntax",
frontmatterLineOffset: 5,
expectedLineCol: "[5:10]",
expectSourceContext: true,
expectVisualPointer: true,
},
{
name: "indentation error",
yamlContent: "name: test\n invalid_indentation: here",
frontmatterLineOffset: 3,
expectedLineCol: "[3:",
expectSourceContext: true,
expectVisualPointer: true,
},
{
name: "duplicate key",
yamlContent: "name: test\nname: duplicate",
frontmatterLineOffset: 2,
expectedLineCol: "[3:1]",
expectSourceContext: true,
expectVisualPointer: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Generate an actual goccy/go-yaml error
var result map[string]any
err := yaml.Unmarshal([]byte(tt.yamlContent), &result)
if err == nil {
t.Errorf("Expected YAML parsing to fail for content: %q", tt.yamlContent)
return
}
// Format the error with the new function
formatted := FormatYAMLError(err, tt.frontmatterLineOffset, tt.yamlContent)
// Check for expected [line:col] format
if !strings.Contains(formatted, tt.expectedLineCol) {
t.Errorf("Expected output to contain '%s', got:\n%s", tt.expectedLineCol, formatted)
}
// Check for source context (lines with | markers)
if tt.expectSourceContext && !strings.Contains(formatted, "|") {
t.Errorf("Expected output to contain source context with '|' markers, got:\n%s", formatted)
}
// Check for visual pointer
if tt.expectVisualPointer && !strings.Contains(formatted, "^") {
t.Errorf("Expected output to contain visual pointer '^', got:\n%s", formatted)
}
// Verify "already defined at" references also have adjusted line numbers
if strings.Contains(formatted, "already defined at") {
if tt.frontmatterLineOffset > 1 && strings.Contains(formatted, "already defined at [1:") {
t.Errorf("Expected 'already defined at' line numbers to be adjusted, got:\n%s", formatted)
}
}
t.Logf("Formatted error:\n%s", formatted)
})
}
}
// TestFormatYAMLErrorAdjustment specifically tests line number adjustment
func TestFormatYAMLErrorAdjustment(t *testing.T) {
yamlContent := "name: test\nname: duplicate"
tests := []struct {
offset int
expectedFirstLine string
expectedSecondLine string
expectedDefinedAt string
}{
{
offset: 1,
expectedFirstLine: " 1 |",
expectedSecondLine: "> 2 |",
expectedDefinedAt: "already defined at [1:1]",
},
{
offset: 5,
expectedFirstLine: " 5 |",
expectedSecondLine: "> 6 |",
expectedDefinedAt: "already defined at [5:1]",
},
{
offset: 10,
expectedFirstLine: " 10 |",
expectedSecondLine: "> 11 |",
expectedDefinedAt: "already defined at [10:1]",
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("offset_%d", tt.offset), func(t *testing.T) {
var result map[string]any
err := yaml.Unmarshal([]byte(yamlContent), &result)
if err == nil {
t.Errorf("Expected YAML parsing to fail")
return
}
formatted := FormatYAMLError(err, tt.offset, yamlContent)
// Check first line number
if !strings.Contains(formatted, tt.expectedFirstLine) {
t.Errorf("Expected first line number format '%s', got:\n%s", tt.expectedFirstLine, formatted)
}
// Check second line number
if !strings.Contains(formatted, tt.expectedSecondLine) {
t.Errorf("Expected second line number format '%s', got:\n%s", tt.expectedSecondLine, formatted)
}
// Check "already defined at" reference
if !strings.Contains(formatted, tt.expectedDefinedAt) {
t.Errorf("Expected 'already defined at' reference '%s', got:\n%s", tt.expectedDefinedAt, formatted)
}
t.Logf("Formatted error (offset %d):\n%s", tt.offset, formatted)
})
}
}
// TestTranslateYAMLError tests that cryptic goccy/go-yaml parser messages are translated
// to user-friendly descriptions, and that source context lines are left untouched.
func TestTranslateYAMLError(t *testing.T) {
tests := []struct {
name string
input string
contains string // expected substring in translated output
excludes string // must NOT appear in the first (header) line
sourceContextCheck string // if non-empty, must appear in the source context (after first line)
}{
{
name: "unexpected key name translated",
input: "[1:1] unexpected key name\n> 1 | engine claude\n ^",
contains: "missing ':' after key",
excludes: "unexpected key name",
},
{
name: "mapping value not allowed translated",
input: "[1:6] mapping value is not allowed in this context\n> 1 | key: value: extra\n ^",
contains: "unexpected ':'",
excludes: "mapping value is not allowed in this context",
},
{
name: "string used where mapping expected translated",
input: "[1:1] string was used where mapping is expected\n> 1 | name value\n ^",
contains: "expected a YAML mapping",
excludes: "string was used where mapping is expected",
},
{
name: "tab character error translated",
input: "[1:7] tab character cannot use as a map key directly\n> 1 | \tengine: claude\n ^",
contains: "tab character in key",
excludes: "tab character cannot use as a map key directly",
},
{
name: "unrecognized messages are returned unchanged",
input: "[2:1] mapping key \"name\" already defined at [1:1]\n> 2 | name: dup\n ^",
contains: "already defined",
},
{
name: "empty string is returned unchanged",
input: "",
contains: "",
},
{
name: "pattern in source context line is not replaced",
input: "[1:1] unexpected key name\n> 1 | unexpected key name: here\n ^",
// The header should be translated, but source context must remain untouched.
contains: "missing ':' after key",
sourceContextCheck: "unexpected key name: here",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := translateYAMLError(tt.input)
if tt.contains != "" {
assert.Contains(t, result, tt.contains,
"translateYAMLError should contain %q\nResult: %s", tt.contains, result)
}
// Verify source context lines are preserved untouched when applicable.
if tt.sourceContextCheck != "" {
_, rest, _ := strings.Cut(result, "\n")
assert.Contains(t, rest, tt.sourceContextCheck,
"source context should be preserved unchanged\nContext: %s", rest)
}
// For cases with excludes, the pattern should not appear in the header line.
if tt.excludes != "" {
firstLine, _, _ := strings.Cut(result, "\n")
assert.NotContains(t, firstLine, tt.excludes,
"translateYAMLError header should not contain %q\nHeader: %s", tt.excludes, firstLine)
}
})
}
}
// TestTranslateYAMLMessage tests the exported TranslateYAMLMessage function used
// by both the parser and workflow packages.
func TestTranslateYAMLMessage(t *testing.T) {
tests := []struct {
name string
input string
contains string
excludes string
}{
{
name: "unexpected key name",
input: "unexpected key name",
contains: "missing ':' after key",
excludes: "unexpected key name",
},
{
name: "non-map value is specified",
input: "non-map value is specified",
contains: "expected a YAML mapping",
excludes: "non-map value is specified",
},
{
name: "found character that cannot start any token",
input: "found character that cannot start any token",
contains: "invalid character",
excludes: "found character that cannot start any token",
},
{
name: "unrecognized message is unchanged",
input: "some other error",
contains: "some other error",
},
{
name: "empty string is unchanged",
input: "",
contains: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := TranslateYAMLMessage(tt.input)
if tt.contains != "" {
assert.Contains(t, result, tt.contains,
"TranslateYAMLMessage should contain %q\nResult: %s", tt.contains, result)
}
if tt.excludes != "" {
assert.NotContains(t, result, tt.excludes,
"TranslateYAMLMessage should not contain %q\nResult: %s", tt.excludes, result)
}
})
}
}
// TestFormatYAMLErrorTranslation verifies that FormatYAMLError applies translations
// to the underlying goccy/go-yaml error messages.
func TestFormatYAMLErrorTranslation(t *testing.T) {
tests := []struct {
name string
yamlContent string
shouldContain string
shouldExclude string
}{
{
name: "missing colon translated",
yamlContent: "engine claude\nmodel: gpt-4",
shouldContain: "missing ':' after key",
shouldExclude: "unexpected key name",
},
{
name: "extra colon translated",
yamlContent: "key: value: extra",
shouldContain: "unexpected ':'",
shouldExclude: "mapping value is not allowed in this context",
},
{
name: "plain string translated",
yamlContent: "not a mapping",
shouldContain: "expected a YAML mapping",
shouldExclude: "string was used where mapping is expected",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result map[string]any
err := yaml.Unmarshal([]byte(tt.yamlContent), &result)
if err == nil {
t.Skipf("Expected YAML parsing to fail for content: %q", tt.yamlContent)
return
}
formatted := FormatYAMLError(err, 1, tt.yamlContent)
assert.Contains(t, formatted, tt.shouldContain,
"FormatYAMLError should contain %q\nResult: %s", tt.shouldContain, formatted)
if tt.shouldExclude != "" {
assert.NotContains(t, formatted, tt.shouldExclude,
"FormatYAMLError should not contain %q\nResult: %s", tt.shouldExclude, formatted)
}
})
}
}