-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.test.js
More file actions
658 lines (607 loc) · 21 KB
/
Copy pathcli.test.js
File metadata and controls
658 lines (607 loc) · 21 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
import { doesNotMatch, match, ok, strictEqual, throws } from "node:assert";
import { execFile } from "node:child_process";
import { copyFile, mkdtemp, readdir, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import test from "node:test";
import { promisify } from "node:util";
import { program, reportError } from "./cli.js";
const execFileAsync = promisify(execFile);
const cli = resolve(import.meta.dirname, "cli.js");
const fixture = (name) => resolve(import.meta.dirname, "__test__", name);
// Mirrors the shebang's own flag so stderr carries only what the CLI wrote,
// which lets a test assert stderr is exactly empty.
const run = (...args) =>
execFileAsync("node", ["--disable-warning=DEP0040", cli, ...args], {
cwd: import.meta.dirname,
});
test("cli validate (default) with --valid and valid schema", async () => {
const { stdout } = await run(fixture("simple.schema.json"), "--valid");
match(stdout, /is valid/);
});
test("cli validate coerces --strict false to a boolean (does not enforce)", async () => {
// A schema with an unknown keyword fails to compile under strict mode; with
// `--strict false` coerced to a real boolean, it compiles and is valid.
const { stdout } = await run(
fixture("unknown-keyword.schema.json"),
"--strict",
"false",
);
match(stdout, /is valid/);
});
test("cli validate coerces --strict true to a boolean (enforces)", async () => {
// Coerced to boolean true, strict mode rejects the unknown keyword at compile
// time, so the schema fails to compile and the command exits non-zero. (As a
// raw string "true" it would merely warn — proving the coercion took effect.)
try {
await run(fixture("unknown-keyword.schema.json"), "--strict", "true");
throw new Error("Expected process to exit with non-zero code");
} catch (e) {
strictEqual(e.code, 1);
match(e.stderr, /schema failed to compile|unknown keyword/);
}
});
test("cli validate of an uncompilable schema exits 1 even with no flag", async () => {
try {
await run(fixture("uncompilable.schema.json"));
throw new Error("Expected process to exit with non-zero code");
} catch (e) {
strictEqual(e.code, 1);
match(e.stderr, /schema failed to compile/);
}
});
test("cli validate accepts numeric --loop-enum", async () => {
const { stdout } = await run(
fixture("simple.schema.json"),
"--valid",
"--loop-enum",
"5",
);
match(stdout, /is valid/);
});
test("cli validate rejects non-numeric --loop-enum", async () => {
try {
await run(fixture("simple.schema.json"), "--loop-enum", "abc");
throw new Error("Expected process to exit with non-zero code");
} catch (e) {
ok(e.code !== 0);
}
});
test("cli validate passes through string option modes unchanged (--strict log)", async () => {
// parseBoolish coerces "true"/"false" to booleans but must pass recognized
// string modes ("log", "array", "empty") straight through to AJV.
const { stdout } = await run(
fixture("unknown-keyword.schema.json"),
"--strict",
"log",
);
// strict:"log" warns rather than throwing, so the schema still compiles.
match(stdout, /is valid/);
});
test("cli validate accepts --no-messages", async () => {
const { stdout } = await run(
fixture("simple.schema.json"),
"--valid",
"--no-messages",
);
match(stdout, /is valid/);
});
test("cli validate with test data and --valid", async () => {
const { stdout } = await run(
fixture("simple.schema.json"),
"-d",
fixture("simple.data.json"),
"--valid",
);
match(stdout, /is valid/);
});
test("cli validate with invalid data and --invalid", async () => {
const { stdout } = await run(
fixture("simple.schema.json"),
"-d",
fixture("invalid.data.json"),
"--invalid",
);
match(stdout, /is invalid/);
});
test("cli validate reports every error by default (allErrors)", async () => {
// invalid.data.json violates both `name` (string) and `age` (integer).
const { stderr } = await run(
fixture("simple.schema.json"),
"-d",
fixture("invalid.data.json"),
);
match(stderr, /must be string/);
match(stderr, /must be integer/);
});
test("cli validate --all-errors false stops at the first error", async () => {
const { stderr } = await run(
fixture("simple.schema.json"),
"-d",
fixture("invalid.data.json"),
"--all-errors",
"false",
);
match(stderr, /must be string/);
doesNotMatch(stderr, /must be integer/);
});
test("cli validate accepts the documented README invocation", async () => {
// The README's bundle example passes exactly these flags; they must all be
// recognized options.
const { stdout } = await run(
fixture("simple.schema.json"),
"--valid",
"--strict",
"true",
"--coerce-types",
"array",
"--all-errors",
"true",
"--use-defaults",
"empty",
);
match(stdout, /is valid/);
});
test("cli transpile command emits an ESM validator", async () => {
const { stdout } = await run("transpile", fixture("simple.schema.json"));
match(stdout, /export/);
});
test("cli transpile accepts --all-errors false", async () => {
const { stdout } = await run(
"transpile",
fixture("simple.schema.json"),
"--all-errors",
"false",
);
match(stdout, /export/);
});
test("cli deref command emits dereferenced JSON", async () => {
const { stdout } = await run("deref", fixture("simple.schema.json"));
const parsed = JSON.parse(stdout);
ok(parsed.properties.name);
});
test("cli sast command with secure schema reports no issues", async () => {
const { stdout } = await run("sast", fixture("secure.schema.json"));
match(stdout, /has no issues/);
});
test("cli sast command coerces numeric --override-max-items", async () => {
// With the string "2000" (uncoerced) the override comparison fails and the
// large enum is still reported; coerced to a number it clears the finding.
const { stdout } = await run(
"sast",
fixture("large-enum.schema.json"),
"--override-max-items",
"2000",
);
match(stdout, /has no issues/);
});
test("cli sast command with insecure schema and --fail should exit 1", async () => {
try {
await run("sast", fixture("insecure.schema.json"), "--fail");
throw new Error("Expected process to exit with code 1");
} catch (e) {
strictEqual(e.code, 1);
}
});
test("cli ftl command emits an ESM module", async () => {
const { stdout } = await run("ftl", fixture("hello.ftl"), "--locale", "en");
match(stdout, /export/);
});
test("cli missing input file errors cleanly without a stack trace", async () => {
try {
await run(fixture("nonexistent.json"), "--valid");
throw new Error("Expected process to exit with non-zero code");
} catch (e) {
strictEqual(e.code, 1);
// Clean message, not a raw unhandled-rejection stack trace.
doesNotMatch(e.stderr, /\n\s+at /);
doesNotMatch(e.stderr, /node:internal/);
}
});
test("cli --help should print usage", async () => {
const { stdout } = await run("--help");
match(stdout, /Usage/);
});
// ---------------------------------------------------------------------------
// In-process tests of the commander wiring. The subprocess tests above can't be
// attributed to cli.js by per-test coverage analysis (a child process has its
// own instrumentation), so these import the program directly and assert the
// flags, descriptions, presets and arg parsers that define the CLI surface.
// ---------------------------------------------------------------------------
const REF_DESC =
"The schema in <input> can reference any of these schemas with $ref keyword.";
const OUTPUT_JS_DESC =
"Path to store the resulting JavaScript file. Will be in ESM.";
const EXPECTED = {
name: "ajv",
description:
"Validate, transpile, dereference, and audit JSON-Schema files using AJV",
commands: {
validate: {
isDefault: true,
arg: {
name: "input",
desc: "Paths or glob patterns of JSON-Schema files to validate",
},
options: [
{
flags: "--valid",
desc: "When not valid throw exit(1)",
preset: true,
},
{
flags: "--invalid",
desc: "When not invalid throw exit(1)",
preset: true,
},
{ flags: "-r, --ref-schema-files <refSchemaFiles...>", desc: REF_DESC },
{ flags: "--strict [strict]", desc: "true/false/log", preset: true },
{
flags: "--use-defaults [useDefaults]",
desc: "replace missing properties/items with the values from default keyword",
preset: true,
},
{
flags: "--coerce-types [coerceTypes]",
desc: "change type of data to match type keyword",
preset: true,
},
{
flags: "--all-errors [allErrors]",
desc: "report all errors instead of stopping at the first (true/false, default true)",
preset: true,
},
{
flags: "--no-messages",
desc: "exclude human-readable text messages from errors",
},
{
flags: "--loop-enum <loopEnum>",
desc: "max size of enum to compile to expression (rather than to loop)",
},
{
flags: "-d, --test-data-files <testDataFiles...>",
desc: "The data files to validate against.",
},
],
},
transpile: {
arg: {
name: "input",
desc: "Paths or glob patterns of JSON-Schema files to transpile",
},
options: [
{ flags: "-r, --ref-schema-files <refSchemaFiles...>", desc: REF_DESC },
{ flags: "--strict [strict]", desc: "true/false/log", preset: true },
{
flags: "--use-defaults [useDefaults]",
desc: "replace missing properties/items with the values from default keyword",
preset: true,
},
{
flags: "--coerce-types [coerceTypes]",
desc: "change type of data to match type keyword",
preset: true,
},
{
flags: "--all-errors [allErrors]",
desc: "report all errors instead of stopping at the first (true/false, default true)",
preset: true,
},
{
flags: "--no-messages",
desc: "exclude human-readable text messages from errors",
},
{
flags: "--loop-enum <loopEnum>",
desc: "max size of enum to compile to expression (rather than to loop)",
},
{ flags: "-o, --output <output>", desc: OUTPUT_JS_DESC },
],
},
deref: {
arg: {
name: "input",
desc: "Paths or glob patterns of JSON-Schema files to deref relative $ref",
},
options: [
{ flags: "-r, --ref-schema-files <refSchemaFiles...>", desc: REF_DESC },
{
flags: "--offline",
desc: "Do not fetch remote $ref URLs over the network (resolve local/-r schemas only).",
preset: true,
},
{
flags: "-o, --output <output>",
desc: "Path to store the resulting JSON-Schema file.",
},
],
},
sast: {
arg: {
name: "input",
desc: "Paths or glob patterns of JSON-Schema files to audit for security",
},
options: [
{ flags: "-r, --ref-schema-files <refSchemaFiles...>", desc: REF_DESC },
{
flags: "-f, --fail",
desc: "When issues found throw exit(1)",
preset: true,
},
{
flags: "--override-max-items <overrideMaxItems>",
desc: "Override the max items limit (default 1024). Removes maxItems errors when the array size is within this limit. Values <= 1024 are a no-op.",
},
{
flags: "--override-max-depth <overrideMaxDepth>",
desc: "Override the max schema depth limit (default 32).",
},
{
flags: "--override-max-properties <overrideMaxProperties>",
desc: "Override the max properties limit (default 1024). Removes maxProperties errors when the property count is within this limit. Values <= 1024 are a no-op.",
},
{
flags: "--redos-timeout-ms <redosTimeoutMs>",
desc: "Per-pattern time budget for ReDoS analysis in ms (default 1000). A pattern that exceeds it is fail-closed as unsafe. Raise only for trusted first-party schemas.",
},
{
flags: "--redos-heap-budget-bytes <redosHeapBudgetBytes>",
desc: "Retained-heap budget for ReDoS analysis in bytes (default 134217728). Once exceeded the remaining patterns are not analyzed. Keep --max-old-space-size well above it.",
},
{
flags: "--ignore <ignore...>",
desc: "Suppress errors by `instancePath` or `instancePath:keyword` (exact match).",
},
{
flags: "--offline",
desc: "Skip DNS lookups for remote $ref URLs (disables SSRF resolution).",
preset: true,
},
{
flags: "--dns-timeout-ms <dnsTimeoutMs>",
desc: "Per-hostname DNS lookup timeout in ms for SSRF checks (default 5000).",
},
{
flags: "--dns-concurrency <dnsConcurrency>",
desc: "Max concurrent DNS lookups for SSRF checks (default 10).",
},
{
flags: "--lang <lang>",
desc: 'Target language for deserialization-vector checks. One of: js, py, rb, rs, java, kotlin, clojure, cs, vb, fsharp, php, objc, swift, ex, lua, default. (default: "default" — union of all languages)',
defaultValue: "default",
},
{
flags: "-o, --output <output>",
desc: "Path to store the resulting JSON issues file.",
},
],
},
ftl: {
arg: {
name: "input",
desc: "Paths or glob patterns of Fluent files to transpile",
},
options: [
{
flags: "--locale <locale...>",
desc: "What locale(s) to be used. Multiple can be set to allow for fallback. i.e. en-CA",
mandatory: true,
},
{ flags: "-o, --output <output>", desc: OUTPUT_JS_DESC },
],
},
},
};
test("cli program name and description are wired", () => {
strictEqual(program.name(), EXPECTED.name);
strictEqual(program.description(), EXPECTED.description);
});
test("cli registers exactly the expected commands", () => {
strictEqual(
program.commands.map((c) => c.name()).join(","),
Object.keys(EXPECTED.commands).join(","),
);
});
test("cli validate is the default command", () => {
strictEqual(program._defaultCommandName, "validate");
});
for (const [name, spec] of Object.entries(EXPECTED.commands)) {
test(`cli ${name} command argument and options are wired`, () => {
const cmd = program.commands.find((c) => c.name() === name);
ok(cmd, `command ${name} should exist`);
// <input> argument name + description
const arg = cmd.registeredArguments[0];
strictEqual(arg.name(), spec.arg.name);
strictEqual(arg.description, spec.arg.desc);
// every option's flags + description + preset/default/mandatory
strictEqual(
cmd.options.map((o) => o.flags).join("\n"),
spec.options.map((o) => o.flags).join("\n"),
);
for (const expected of spec.options) {
const opt = cmd.options.find((o) => o.flags === expected.flags);
ok(opt, `${name} should have option ${expected.flags}`);
strictEqual(opt.description, expected.desc);
strictEqual(opt.presetArg, expected.preset ?? undefined);
strictEqual(opt.defaultValue, expected.defaultValue ?? undefined);
strictEqual(opt.mandatory, expected.mandatory ?? false);
}
});
}
// parseBoolish (wired on --strict) coerces boolean-ish strings but passes other
// recognized string modes straight through.
const boolishParser = () =>
program.commands
.find((c) => c.name() === "validate")
.options.find((o) => o.flags === "--strict [strict]").parseArg;
test("cli --strict parser coerces 'true'/'false' and passes modes through", () => {
const parse = boolishParser();
strictEqual(parse("true"), true);
strictEqual(parse("false"), false);
strictEqual(parse("log"), "log");
});
// parseNumeric (wired on --loop-enum) coerces to a number or throws.
const numericParser = () =>
program.commands
.find((c) => c.name() === "validate")
.options.find((o) => o.flags === "--loop-enum <loopEnum>").parseArg;
test("cli --loop-enum parser coerces numbers and rejects non-numbers", () => {
const parse = numericParser();
strictEqual(parse("5"), 5);
strictEqual(parse("0"), 0);
throws(() => parse("abc"), /Expected a number, received "abc"/);
});
test("cli reportError prints the message and exits 1", (t) => {
const mockError = t.mock.method(console, "error", () => {});
const mockExit = t.mock.method(process, "exit", () => {});
reportError(new Error("boom"));
strictEqual(mockError.mock.calls.length, 1);
strictEqual(mockError.mock.calls[0].arguments[0], "boom");
strictEqual(mockExit.mock.calls.length, 1);
strictEqual(mockExit.mock.calls[0].arguments[0], 1);
});
test("cli accepts multiple inputs and reports each", async () => {
const { stdout } = await run(
fixture("simple.schema.json"),
fixture("secure.schema.json"),
);
match(stdout, /simple\.schema\.json is valid/);
match(stdout, /secure\.schema\.json is valid/);
});
test("cli input argument is variadic for every command", () => {
for (const cmd of program.commands) {
ok(
cmd.registeredArguments[0].variadic,
`${cmd.name()} should take multiple inputs`,
);
}
});
test("cli expands a glob pattern the shell never saw", async () => {
// Quoted so the shell cannot expand it — execFile does no expansion either,
// so the pattern reaches fs.glob verbatim.
const { stdout } = await run("validate", "__test__/s*.schema.json");
match(stdout, /secure\.schema\.json is valid/);
match(stdout, /simple\.schema\.json is valid/);
});
test("cli errors when a pattern matches nothing", async () => {
try {
await run("validate", "__test__/no-such-*.json");
throw new Error("Expected process to exit with non-zero code");
} catch (e) {
strictEqual(e.code, 1);
match(e.stderr, /No files matched/);
}
});
test("cli runs every input before exiting 1 on failure", async () => {
// uncompilable comes first: fail-fast would never reach the two good files.
try {
await run(
fixture("uncompilable.schema.json"),
fixture("simple.schema.json"),
fixture("secure.schema.json"),
);
throw new Error("Expected process to exit with non-zero code");
} catch (e) {
strictEqual(e.code, 1);
match(e.stderr, /schema failed to compile/);
match(e.stdout, /simple\.schema\.json is valid/);
match(e.stdout, /secure\.schema\.json is valid/);
}
});
test("cli rejects --output with more than one input", async () => {
try {
await run(
"transpile",
fixture("simple.schema.json"),
fixture("secure.schema.json"),
"-o",
"/dev/null",
);
throw new Error("Expected process to exit with non-zero code");
} catch (e) {
strictEqual(e.code, 1);
match(e.stderr, /--output takes a single input, received 2/);
}
});
test("cli batch writes each output beside its input", async () => {
const dir = await mkdtemp(join(tmpdir(), "ajv-batch-"));
try {
const a = join(dir, "simple.schema.json");
const b = join(dir, "secure.schema.json");
await copyFile(fixture("simple.schema.json"), a);
await copyFile(fixture("secure.schema.json"), b);
await run("transpile", a, b);
const js = await readdir(dir);
ok(js.includes("simple.schema.js"), "expected simple.schema.js");
ok(js.includes("secure.schema.js"), "expected secure.schema.js");
// deref also emits JSON, so it must not mirror back onto the input.
await run("deref", a, b);
const after = await readdir(dir);
ok(after.includes("simple.schema.deref.json"), "expected .deref.json");
strictEqual(
JSON.parse(await readFile(a, "utf8")).$id,
JSON.parse(await readFile(fixture("simple.schema.json"), "utf8")).$id,
"input schema must be left untouched",
);
} finally {
await rm(dir, { recursive: true, force: true });
}
});
test("cli reports a non-CommandFailure error and still runs the rest", async () => {
// hello.ftl is a real file but not JSON, so readJson throws inside the loop —
// a different path from a CommandFailure, and its message must still surface.
try {
await run(fixture("hello.ftl"), fixture("simple.schema.json"));
throw new Error("Expected process to exit with non-zero code");
} catch (e) {
strictEqual(e.code, 1);
match(e.stderr, /Failed to parse JSON in .*hello\.ftl/);
match(e.stdout, /simple\.schema\.json is valid/);
}
});
test("cli batch sast writes issues beside each input", async () => {
const dir = await mkdtemp(join(tmpdir(), "ajv-sast-"));
try {
const a = join(dir, "insecure.schema.json");
const b = join(dir, "large-enum-insecure.schema.json");
await copyFile(fixture("insecure.schema.json"), a);
await copyFile(fixture("large-enum-insecure.schema.json"), b);
await run("sast", a, b);
const written = await readdir(dir);
ok(written.includes("insecure.schema.sast.json"), "expected .sast.json");
ok(
written.includes("large-enum-insecure.schema.sast.json"),
"expected .sast.json for the second input",
);
} finally {
await rm(dir, { recursive: true, force: true });
}
});
test("cli batch ftl writes a bundle beside each input", async () => {
const dir = await mkdtemp(join(tmpdir(), "ajv-ftl-"));
try {
const a = join(dir, "one.ftl");
const b = join(dir, "two.ftl");
await copyFile(fixture("hello.ftl"), a);
await copyFile(fixture("hello.ftl"), b);
await run("ftl", a, b, "--locale", "en");
const written = await readdir(dir);
ok(written.includes("one.js"), "expected one.js");
ok(written.includes("two.js"), "expected two.js");
} finally {
await rm(dir, { recursive: true, force: true });
}
});
test("cli keeps stderr clean when a command reported its own failure", async () => {
// A CommandFailure has already printed to stdout, so the loop must not also
// echo its (empty) message — that would put a blank line on stderr.
try {
await run(fixture("simple.schema.json"), "--invalid");
throw new Error("Expected process to exit with non-zero code");
} catch (e) {
strictEqual(e.code, 1);
strictEqual(e.stderr, "");
match(e.stdout, /is valid, expected invalid/);
}
});