Repository navigation
Expand file tree
/
Copy pathconformance_test.go
More file actions
433 lines (398 loc) · 12.1 KB
/
Copy pathconformance_test.go
File metadata and controls
433 lines (398 loc) · 12.1 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
package vers
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"sort"
"testing"
)
const tupleSize = 2
type versTestFile struct {
Tests []versTestCase `json:"tests"`
}
type versTestCase struct {
Description string `json:"description"`
TestGroup string `json:"test_group"`
TestType string `json:"test_type"`
Input json.RawMessage `json:"input"`
ExpectedOutput json.RawMessage `json:"expected_output"`
ExpectedFailure bool `json:"expected_failure"`
ExpectedMessage string `json:"expected_message"`
}
type conformanceSkipFile struct {
Skips []conformanceSkip `json:"skips"`
}
type conformanceSkip struct {
File string `json:"file"`
TestType string `json:"test_type"`
Input json.RawMessage `json:"input"`
ExpectedOutput json.RawMessage `json:"expected_output,omitempty"`
Reason string `json:"reason"`
used bool
}
func TestConformance(t *testing.T) {
files, err := filepath.Glob(filepath.Join("testdata", "*", "tests", "*.json"))
if err != nil {
t.Fatalf("find conformance files: %v", err)
}
if len(files) == 0 {
t.Fatal("no conformance files found")
}
skips := loadConformanceSkips(t)
for _, filename := range files {
relative, err := filepath.Rel("testdata", filename)
if err != nil {
t.Fatalf("make %s relative to testdata: %v", filename, err)
}
relative = filepath.ToSlash(relative)
t.Run(relative, func(t *testing.T) {
tf := loadTestFile(t, filename)
for i, tc := range tf.Tests {
name := fmt.Sprintf("%04d_%s", i, tc.Description)
t.Run(name, func(t *testing.T) {
if reason, ok := skips.match(relative, tc); ok {
t.Skip(reason)
}
runConformanceCase(t, tc)
})
}
})
}
skips.assertAllUsed(t)
}
func loadTestFile(t *testing.T, filename string) *versTestFile {
t.Helper()
data, err := os.ReadFile(filename)
if err != nil {
t.Fatalf("read test file %s: %v", filename, err)
}
var tf versTestFile
if err := json.Unmarshal(data, &tf); err != nil {
t.Fatalf("parse test file %s: %v", filename, err)
}
return &tf
}
func loadConformanceSkips(t *testing.T) *conformanceSkipFile {
t.Helper()
filename := filepath.Join("testdata", "local", "skip.json")
data, err := os.ReadFile(filename)
if err != nil {
t.Fatalf("read conformance skips: %v", err)
}
var skips conformanceSkipFile
if err := json.Unmarshal(data, &skips); err != nil {
t.Fatalf("parse conformance skips: %v", err)
}
seen := make(map[string]bool, len(skips.Skips))
for i := range skips.Skips {
skip := &skips.Skips[i]
if skip.File == "" || skip.TestType == "" || len(skip.Input) == 0 || skip.Reason == "" {
t.Fatalf("conformance skip %d must have file, test_type, input, and reason", i)
}
key := skip.identity(t)
if seen[key] {
t.Fatalf("duplicate conformance skip %d for %s", i, skip.File)
}
seen[key] = true
}
return &skips
}
func (skips *conformanceSkipFile) match(filename string, tc versTestCase) (string, bool) {
for i := range skips.Skips {
skip := &skips.Skips[i]
if skip.File != filename || skip.TestType != tc.TestType || !equalJSON(skip.Input, tc.Input) {
continue
}
if len(skip.ExpectedOutput) > 0 && !equalJSON(skip.ExpectedOutput, tc.ExpectedOutput) {
continue
}
skip.used = true
return skip.Reason, true
}
return "", false
}
func (skips *conformanceSkipFile) assertAllUsed(t *testing.T) {
t.Helper()
for _, skip := range skips.Skips {
if !skip.used {
t.Errorf("unused conformance skip for %s %s: %s", skip.File, skip.TestType, skip.Reason)
}
}
}
func (skip conformanceSkip) identity(t *testing.T) string {
t.Helper()
return skip.File + "\x00" + skip.TestType + "\x00" + normalizedJSON(t, skip.Input) +
"\x00" + normalizedJSON(t, skip.ExpectedOutput)
}
func normalizedJSON(t *testing.T, data json.RawMessage) string {
t.Helper()
if len(data) == 0 {
return ""
}
var value any
if err := json.Unmarshal(data, &value); err != nil {
t.Fatalf("parse JSON value: %v", err)
}
normalized, err := json.Marshal(value)
if err != nil {
t.Fatalf("normalize JSON value: %v", err)
}
return string(normalized)
}
func equalJSON(a, b json.RawMessage) bool {
var av, bv any
if json.Unmarshal(a, &av) != nil || json.Unmarshal(b, &bv) != nil {
return false
}
return reflect.DeepEqual(av, bv)
}
func runConformanceCase(t *testing.T, tc versTestCase) {
t.Helper()
switch tc.TestType {
case "from_native":
runFromNativeCase(t, tc)
case "containment":
runContainmentCase(t, tc)
case "validate", "roundtrip":
runValidateCase(t, tc)
case "comparison":
runComparisonCase(t, tc)
case "equality":
runEqualityCase(t, tc)
case "parse":
runParseCase(t, tc)
default:
t.Fatalf("unsupported conformance test type %q", tc.TestType)
}
}
func runFromNativeCase(t *testing.T, tc versTestCase) {
t.Helper()
var input struct {
NativeRange string `json:"native_range"`
Type string `json:"type"`
Scheme string `json:"scheme"`
}
decodeConformanceValue(t, tc.Input, &input)
scheme := chooseAlias(t, "from-native type", input.Type, input.Scheme)
r, err := ParseNative(input.NativeRange, scheme)
if !checkConformanceError(t, tc, err) {
return
}
var expected string
decodeConformanceValue(t, tc.ExpectedOutput, &expected)
if got := ToVersString(r, scheme); got != expected {
t.Errorf("ParseNative(%q, %q) = %q, want %q", input.NativeRange, scheme, got, expected)
}
}
func runContainmentCase(t *testing.T, tc versTestCase) {
t.Helper()
var input struct {
Vers string `json:"vers"`
Version string `json:"version"`
}
decodeConformanceValue(t, tc.Input, &input)
r, err := Parse(input.Vers)
if !checkConformanceError(t, tc, err) {
return
}
var expected bool
decodeConformanceValue(t, tc.ExpectedOutput, &expected)
if got := r.Contains(input.Version); got != expected {
t.Errorf("Parse(%q).Contains(%q) = %v, want %v", input.Vers, input.Version, got, expected)
}
}
func runValidateCase(t *testing.T, tc versTestCase) {
t.Helper()
input := decodeVersInput(t, tc.Input)
r, err := defaultParser.parseVersURI(input, false)
if !checkConformanceError(t, tc, err) {
return
}
var expected string
decodeConformanceValue(t, tc.ExpectedOutput, &expected)
if got := ToVersString(r, r.Scheme); got != expected {
t.Errorf("validate %q = %q, want %q", input, got, expected)
}
}
func runComparisonCase(t *testing.T, tc versTestCase) {
t.Helper()
input, scheme := decodeComparisonInput(t, tc.Input)
var expected []string
decodeConformanceValue(t, tc.ExpectedOutput, &expected)
if len(input) != len(expected) || len(input) < tupleSize {
t.Fatalf("comparison has %d inputs and %d outputs, want equal lengths of at least %d", len(input), len(expected), tupleSize)
}
got := append([]string(nil), input...)
sort.SliceStable(got, func(i, j int) bool {
return CompareWithScheme(got[i], got[j], scheme) < 0
})
for i := range got {
if CompareWithScheme(got[i], expected[i], scheme) != 0 {
t.Errorf("sorted versions = %v, want %v", got, expected)
return
}
}
}
func runEqualityCase(t *testing.T, tc versTestCase) {
t.Helper()
versions, scheme := decodeComparisonInput(t, tc.Input)
if len(versions) != tupleSize {
t.Fatalf("equality has %d inputs, want %d", len(versions), tupleSize)
}
var expected bool
decodeConformanceValue(t, tc.ExpectedOutput, &expected)
got := CompareWithScheme(versions[0], versions[1], scheme) == 0
if got != expected {
t.Errorf("CompareWithScheme(%q, %q, %q) == 0 is %v, want %v", versions[0], versions[1], scheme, got, expected)
}
}
func runParseCase(t *testing.T, tc versTestCase) {
t.Helper()
var input string
decodeConformanceValue(t, tc.Input, &input)
r, err := defaultParser.parseVersURI(input, true)
if !checkConformanceError(t, tc, err) {
return
}
var output struct {
Type string `json:"type"`
Scheme string `json:"scheme"`
Constraints [][]string `json:"constraints"`
VersionConstraints [][]string `json:"version_constraints"`
}
decodeConformanceValue(t, tc.ExpectedOutput, &output)
scheme := chooseAlias(t, "parse output type", output.Type, output.Scheme)
constraints := chooseConstraintsAlias(t, output.Constraints, output.VersionConstraints)
if r.Scheme != scheme {
t.Errorf("Parse(%q) scheme = %q, want %q", input, r.Scheme, scheme)
}
if got := rangeConstraints(r); !reflect.DeepEqual(got, constraints) {
t.Errorf("Parse(%q) constraints = %v, want %v", input, got, constraints)
}
}
func decodeComparisonInput(t *testing.T, data json.RawMessage) ([]string, string) {
t.Helper()
var input struct {
InputType string `json:"input_type"`
InputScheme string `json:"input_scheme"`
Versions []string `json:"versions"`
}
decodeConformanceValue(t, data, &input)
return input.Versions, chooseAlias(t, "comparison input type", input.InputType, input.InputScheme)
}
func decodeVersInput(t *testing.T, data json.RawMessage) string {
t.Helper()
var input string
if err := json.Unmarshal(data, &input); err == nil {
return input
}
var legacy struct {
Vers string `json:"vers"`
}
decodeConformanceValue(t, data, &legacy)
if legacy.Vers == "" {
t.Fatal("VERS input is empty")
}
return legacy.Vers
}
func decodeConformanceValue(t *testing.T, data json.RawMessage, value any) {
t.Helper()
if len(data) == 0 {
t.Fatal("missing conformance value")
}
if err := json.Unmarshal(data, value); err != nil {
t.Fatalf("decode conformance value: %v", err)
}
}
func chooseAlias(t *testing.T, name, current, legacy string) string {
t.Helper()
if current != "" && legacy != "" && current != legacy {
t.Fatalf("conflicting %s values %q and %q", name, current, legacy)
}
if current != "" {
return current
}
if legacy == "" {
t.Fatalf("missing %s", name)
}
return legacy
}
func chooseConstraintsAlias(t *testing.T, current, legacy [][]string) [][]string {
t.Helper()
if current != nil && legacy != nil && !reflect.DeepEqual(current, legacy) {
t.Fatalf("conflicting parse output constraints %v and %v", current, legacy)
}
if current != nil {
return current
}
if legacy == nil {
t.Fatal("missing parse output constraints")
}
return legacy
}
func checkConformanceError(t *testing.T, tc versTestCase, err error) bool {
t.Helper()
if tc.ExpectedFailure {
if err == nil {
t.Errorf("expected failure: %s", tc.ExpectedMessage)
} else if tc.ExpectedMessage != "" && err.Error() != tc.ExpectedMessage {
t.Errorf("error = %q, want %q", err, tc.ExpectedMessage)
}
return false
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
return true
}
func rangeConstraints(r *Range) [][]string {
intervals := r.Intervals
if len(r.RawConstraints) > 0 {
intervals = r.RawConstraints
}
constraints := make([][]string, 0, len(intervals)+len(r.Exclusions))
for _, interval := range intervals {
if interval.Min != "" && interval.Min == interval.Max && interval.MinInclusive && interval.MaxInclusive {
constraints = append(constraints, []string{"=", interval.Min})
continue
}
if interval.Min != "" {
operator := ">"
if interval.MinInclusive {
operator = ">="
}
constraints = append(constraints, []string{operator, interval.Min})
}
if interval.Max != "" {
operator := "<"
if interval.MaxInclusive {
operator = "<="
}
constraints = append(constraints, []string{operator, interval.Max})
}
}
for _, version := range r.Exclusions {
constraints = append(constraints, []string{"!=", version})
}
return constraints
}
func TestConformanceSkipMatch(t *testing.T) {
skips := conformanceSkipFile{Skips: []conformanceSkip{{
File: "vers-spec/tests/example.json",
TestType: "containment",
Input: json.RawMessage(`{"vers":"vers:npm/*","version":"1.0.0"}`),
ExpectedOutput: json.RawMessage(`true`),
Reason: "documented divergence",
}}}
tc := versTestCase{
TestType: "containment",
Input: json.RawMessage(`{"version":"1.0.0","vers":"vers:npm/*"}`),
ExpectedOutput: json.RawMessage(`true`),
}
reason, ok := skips.match("vers-spec/tests/example.json", tc)
if !ok || reason != "documented divergence" || !skips.Skips[0].used {
t.Fatalf("match() = %q, %v; used = %v", reason, ok, skips.Skips[0].used)
}
}