-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathcli_test.go
More file actions
635 lines (563 loc) · 17 KB
/
cli_test.go
File metadata and controls
635 lines (563 loc) · 17 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
package cli
import (
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/Boeing/config-file-validator/v2/internal/testhelper"
"github.com/Boeing/config-file-validator/v2/pkg/finder"
"github.com/Boeing/config-file-validator/v2/pkg/reporter"
"github.com/Boeing/config-file-validator/v2/pkg/schemastore"
)
func Test_CLI(t *testing.T) {
dir := testhelper.CreateFixtureDir(t, "json", "yaml", "toml")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir),
)
cli := Init(
WithFinder(fsFinder),
WithReporters(reporter.NewStdoutReporter("")),
WithGroupOutput([]string{""}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLIWithMultipleReporters(t *testing.T) {
dir := testhelper.CreateFixtureDir(t, "json", "yaml")
tmpOut := t.TempDir()
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir),
)
cli := Init(
WithFinder(fsFinder),
WithReporters(
reporter.NewJSONReporter(tmpOut+"/result.json"),
reporter.JunitReporter{},
),
WithGroupOutput([]string{""}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLIWithFailedValidation(t *testing.T) {
dir := t.TempDir()
testhelper.WriteFile(t, dir, "bad.json", testhelper.InvalidContent["json"])
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir),
)
cli := Init(WithFinder(fsFinder))
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 1, exitStatus)
}
func Test_CLIBadPath(t *testing.T) {
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots("/bad/path"),
)
cli := Init(WithFinder(fsFinder))
exitStatus, err := cli.Run()
require.Error(t, err)
require.Equal(t, 1, exitStatus)
}
func Test_CLIWithGroup(t *testing.T) {
dir := testhelper.CreateFixtureDir(t, "json", "yaml")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir),
)
cli := Init(
WithFinder(fsFinder),
WithReporters(reporter.NewStdoutReporter("")),
WithGroupOutput([]string{"pass-fail", "directory"}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLIReportErr(t *testing.T) {
dir := testhelper.CreateFixtureDir(t, "json")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir),
)
cli := Init(
WithFinder(fsFinder),
WithReporters(reporter.NewJSONReporter("./wrong/path")),
WithGroupOutput([]string{""}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 1, exitStatus)
}
func Test_CLISchemaAutoValidation(t *testing.T) {
// SARIF files have built-in schema validation — should auto-validate
file := testhelper.CreateFixtureFile(t, "sarif")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(file),
)
cli := Init(WithFinder(fsFinder))
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLISchemaAutoSkipsNoSchema(t *testing.T) {
// JSON without $schema — should pass (syntax only)
file := testhelper.CreateFixtureFile(t, "json")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(file),
)
cli := Init(WithFinder(fsFinder))
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLIRequireSchemaFailsNoSchema(t *testing.T) {
// JSON without $schema + --require-schema should fail
file := testhelper.CreateFixtureFile(t, "json")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(file),
)
cli := Init(
WithFinder(fsFinder),
WithRequireSchema(true),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 1, exitStatus)
}
func Test_CLIRequireSchemaPassesWithSchema(t *testing.T) {
// SARIF has built-in schema — should pass even with --require-schema
file := testhelper.CreateFixtureFile(t, "sarif")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(file),
)
cli := Init(
WithFinder(fsFinder),
WithRequireSchema(true),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLIRequireSchemaIgnoresNonSchemaTypes(t *testing.T) {
// INI doesn't implement SchemaValidator — should pass even with --require-schema
file := testhelper.CreateFixtureFile(t, "ini")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(file),
)
cli := Init(
WithFinder(fsFinder),
WithRequireSchema(true),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLIWithQuiet(t *testing.T) {
file := testhelper.CreateFixtureFile(t, "json")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(file),
)
cli := Init(
WithFinder(fsFinder),
WithQuiet(true),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLIWithUnreadableFile(t *testing.T) {
file := testhelper.CreateFixtureFile(t, "json")
err := os.Chmod(file, 0000)
require.NoError(t, err)
defer func() { _ = os.Chmod(file, 0600) }()
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(file),
)
cli := Init(WithFinder(fsFinder))
exitStatus, err := cli.Run()
require.Error(t, err)
require.Equal(t, 1, exitStatus)
}
func Test_CLISingleGroupJSON(t *testing.T) {
file := testhelper.CreateFixtureFile(t, "json")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(file),
)
cli := Init(
WithFinder(fsFinder),
WithReporters(reporter.NewJSONReporter("")),
WithGroupOutput([]string{"filetype"}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLIDoubleGroupJSON(t *testing.T) {
file := testhelper.CreateFixtureFile(t, "json")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(file),
)
cli := Init(
WithFinder(fsFinder),
WithReporters(reporter.NewJSONReporter("")),
WithGroupOutput([]string{"filetype", "directory"}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLITripleGroupJSON(t *testing.T) {
file := testhelper.CreateFixtureFile(t, "json")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(file),
)
cli := Init(
WithFinder(fsFinder),
WithReporters(reporter.NewJSONReporter("")),
WithGroupOutput([]string{"filetype", "directory", "pass-fail"}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLISchemaMapValid(t *testing.T) {
dir := t.TempDir()
testhelper.WriteFile(t, dir, "config.json", `{"host": "db", "port": 5432}`)
schema := testhelper.WriteFile(t, dir, "schema.json", `{
"type": "object",
"properties": {
"host": {"type": "string"},
"port": {"type": "integer"}
},
"additionalProperties": false
}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/config.json"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaMap(map[string]string{"config.json": schema}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLISchemaMapInvalid(t *testing.T) {
dir := t.TempDir()
testhelper.WriteFile(t, dir, "config.json", `{"host": "db", "port": "bad"}`)
schema := testhelper.WriteFile(t, dir, "schema.json", `{
"type": "object",
"properties": {
"host": {"type": "string"},
"port": {"type": "integer"}
}
}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/config.json"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaMap(map[string]string{"config.json": schema}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 1, exitStatus)
}
func Test_CLISchemaMapGlob(t *testing.T) {
dir := t.TempDir()
sub := testhelper.CreateSubdir(t, dir, "configs")
testhelper.WriteFile(t, sub, "db.json", `{"host": "db", "port": 5432}`)
schema := testhelper.WriteFile(t, dir, "schema.json", `{
"type": "object",
"properties": {
"host": {"type": "string"},
"port": {"type": "integer"}
}
}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(sub + "/db.json"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaMap(map[string]string{"**/configs/*.json": schema}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLISchemaMapYAML(t *testing.T) {
dir := t.TempDir()
testhelper.WriteFile(t, dir, "config.yaml", "host: db\nport: 5432\n")
schema := testhelper.WriteFile(t, dir, "schema.json", `{
"type": "object",
"properties": {
"host": {"type": "string"},
"port": {"type": "integer"}
},
"additionalProperties": false
}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/config.yaml"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaMap(map[string]string{"config.yaml": schema}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLISchemaMapTOML(t *testing.T) {
dir := t.TempDir()
testhelper.WriteFile(t, dir, "config.toml", "host = \"db\"\nport = 5432\n")
schema := testhelper.WriteFile(t, dir, "schema.json", `{
"type": "object",
"properties": {
"host": {"type": "string"},
"port": {"type": "integer"}
},
"additionalProperties": false
}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/config.toml"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaMap(map[string]string{"config.toml": schema}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLISchemaMapUnmatched(t *testing.T) {
// File doesn't match schema-map pattern — passes syntax-only
file := testhelper.CreateFixtureFile(t, "json")
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(file),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaMap(map[string]string{"other.json": "/nonexistent"}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLISchemaStoreValid(t *testing.T) {
dir := t.TempDir()
bundle := setupMiniSchemaStore(t)
testhelper.WriteFile(t, dir, "package.json", `{"name": "app", "version": "1.0.0"}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/package.json"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaStore(bundle),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLISchemaStoreInvalid(t *testing.T) {
dir := t.TempDir()
bundle := setupMiniSchemaStore(t)
testhelper.WriteFile(t, dir, "package.json", `{"name": "app", "version": 123}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/package.json"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaStore(bundle),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 1, exitStatus)
}
func Test_CLISchemaStoreUnmatched(t *testing.T) {
// File not in catalog — passes syntax-only
dir := t.TempDir()
bundle := setupMiniSchemaStore(t)
testhelper.WriteFile(t, dir, "random.json", `{"key": "value"}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/random.json"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaStore(bundle),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLISchemaMapPriorityOverStore(t *testing.T) {
// schema-map should win over schemastore
dir := t.TempDir()
bundle := setupMiniSchemaStore(t)
testhelper.WriteFile(t, dir, "package.json", `{"name": "app", "version": "1.0.0"}`)
strict := testhelper.WriteFile(t, dir, "strict.json", `{
"type": "object",
"required": ["id"],
"properties": {"id": {"type": "integer"}},
"additionalProperties": false
}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/package.json"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaMap(map[string]string{"package.json": strict}),
WithSchemaStore(bundle),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 1, exitStatus) // fails against strict schema
}
func Test_CLIDocumentSchemaPriorityOverAll(t *testing.T) {
// Document $schema should win over schema-map and schemastore
dir := t.TempDir()
bundle := setupMiniSchemaStore(t)
ownSchema := testhelper.WriteFile(t, dir, "own.json", `{
"type": "object",
"properties": {"title": {"type": "string"}}
}`)
testhelper.WriteFile(t, dir, "package.json", `{"$schema": "`+ownSchema+`", "title": "hello"}`)
strict := testhelper.WriteFile(t, dir, "strict.json", `{
"type": "object",
"required": ["id"],
"additionalProperties": false
}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/package.json"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaMap(map[string]string{"package.json": strict}),
WithSchemaStore(bundle),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus) // passes against own schema
}
func Test_CLIRequireSchemaWithSchemaStore(t *testing.T) {
// Unmatched file + --require-schema + --schemastore should fail
dir := t.TempDir()
bundle := setupMiniSchemaStore(t)
testhelper.WriteFile(t, dir, "random.json", `{"key": "value"}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/random.json"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaStore(bundle),
WithRequireSchema(true),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 1, exitStatus)
}
func Test_CLINoSchema(t *testing.T) {
// File with $schema should pass syntax-only when --no-schema is set
dir := t.TempDir()
schema := testhelper.WriteFile(t, dir, "schema.json", `{
"type": "object",
"required": ["id"],
"additionalProperties": false
}`)
// This file would FAIL schema validation (missing "id", extra "name")
testhelper.WriteFile(t, dir, "config.json", `{"$schema": "`+schema+`", "name": "test"}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/config.json"),
)
cli := Init(
WithFinder(fsFinder),
WithNoSchema(true),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus) // passes because schema is skipped
}
func Test_CLINoSchemaWithSchemaStore(t *testing.T) {
// --no-schema skips schemastore matching too
dir := t.TempDir()
bundle := setupMiniSchemaStore(t)
// version:123 would fail against package.json schema
testhelper.WriteFile(t, dir, "package.json", `{"name": "app", "version": 123}`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/package.json"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaStore(bundle),
WithNoSchema(true),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus) // passes because schema is skipped
}
func setupMiniSchemaStore(t *testing.T) *schemastore.Store {
t.Helper()
dir := t.TempDir()
catalogDir := dir + "/src/api/json"
require.NoError(t, os.MkdirAll(catalogDir, 0755))
schemaDir := dir + "/src/schemas/json"
require.NoError(t, os.MkdirAll(schemaDir, 0755))
catalog := `{"schemas":[{"name":"package.json","fileMatch":["package.json"],"url":"https://www.schemastore.org/package.json"}]}`
require.NoError(t, os.WriteFile(catalogDir+"/catalog.json", []byte(catalog), 0600))
pkgSchema := `{"type":"object","properties":{"name":{"type":"string"},"version":{"type":"string"}}}`
require.NoError(t, os.WriteFile(schemaDir+"/package.json", []byte(pkgSchema), 0600))
store, err := schemastore.Open(dir)
require.NoError(t, err)
return store
}
func Test_CLISchemaMapXMLValid(t *testing.T) {
dir := t.TempDir()
xsdContent := `<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="config">
<xs:complexType>
<xs:sequence>
<xs:element name="host" type="xs:string"/>
<xs:element name="port" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>`
schemaPath := testhelper.WriteFile(t, dir, "schema.xsd", xsdContent)
testhelper.WriteFile(t, dir, "config.xml", `<?xml version="1.0"?><config><host>db</host><port>5432</port></config>`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/config.xml"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaMap(map[string]string{"config.xml": schemaPath}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 0, exitStatus)
}
func Test_CLISchemaMapXMLInvalid(t *testing.T) {
dir := t.TempDir()
xsdContent := `<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="config">
<xs:complexType>
<xs:sequence>
<xs:element name="host" type="xs:string"/>
<xs:element name="port" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>`
schemaPath := testhelper.WriteFile(t, dir, "schema.xsd", xsdContent)
testhelper.WriteFile(t, dir, "config.xml", `<?xml version="1.0"?><config><host>db</host><port>bad</port></config>`)
fsFinder := finder.FileSystemFinderInit(
finder.WithPathRoots(dir + "/config.xml"),
)
cli := Init(
WithFinder(fsFinder),
WithSchemaMap(map[string]string{"config.xml": schemaPath}),
)
exitStatus, err := cli.Run()
require.NoError(t, err)
require.Equal(t, 1, exitStatus)
}