mirror of
https://github.com/morluto/rea.git
synced 2026-11-08 07:46:28 +00:00
The help renderer appends "(default: X)" whenever the option field declares a
schema default, so descriptions that also spell the default out print it twice:
--block-size <value> Power-of-two block size from 256 to 65536 bytes
(default 4096) (default: 4096)
Remove the hand-written default from the eleven options that own a schema
default across inspect-byte-entropy, inspect-devicetree,
inspect-packet-capture, inspect-packet-fields, inspect-signal-decoders and
decode-signal-recording, matching the rule in cliDecimalField's contract that
the domain field owns bounds, defaults and help.
Options without a schema default keep their prose: --match_limit, --line_limit,
--limit and --packet_limit are nullable or optional, so the renderer emits no
annotation and the description is the only place the effective value appears.
Add a boundary test asserting each such option states its default exactly once,
so a restated default cannot come back unnoticed.
89 lines
2.4 KiB
TypeScript
89 lines
2.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { createCli } from "../../../src/cli.js";
|
|
|
|
type CliInstance = ReturnType<typeof createCli>;
|
|
|
|
const serve = async (
|
|
cli: CliInstance,
|
|
argv: readonly string[],
|
|
): Promise<{ readonly stdout: string; readonly exitCode: number }> => {
|
|
let stdout = "";
|
|
let exitCode = 0;
|
|
await cli.serve([...argv], {
|
|
env: {},
|
|
exit: (code) => {
|
|
exitCode = code;
|
|
},
|
|
stdout: (text) => {
|
|
stdout += text;
|
|
},
|
|
});
|
|
return { stdout, exitCode };
|
|
};
|
|
|
|
/**
|
|
* Options whose owning field already declares a schema default. The help
|
|
* renderer appends that default, so the description must not restate it.
|
|
*/
|
|
const singleDefaultOptions: readonly {
|
|
readonly command: string;
|
|
readonly options: readonly string[];
|
|
}[] = [
|
|
{ command: "inspect-byte-entropy", options: ["--block-size", "--offset"] },
|
|
{
|
|
command: "inspect-devicetree",
|
|
options: [
|
|
"--max-file-bytes",
|
|
"--max-nodes",
|
|
"--max-properties",
|
|
"--max-depth",
|
|
],
|
|
},
|
|
{
|
|
command: "decode-signal-recording",
|
|
options: ["--line_offset", "--line_limit"],
|
|
},
|
|
{
|
|
command: "inspect-packet-capture",
|
|
options: ["--match_offset", "--match_limit"],
|
|
},
|
|
{
|
|
command: "inspect-packet-fields",
|
|
options: ["--kind", "--offset"],
|
|
},
|
|
{ command: "inspect-signal-decoders", options: ["--offset"] },
|
|
];
|
|
|
|
const optionLine = (
|
|
help: string,
|
|
option: string,
|
|
): string | undefined => {
|
|
const line = help
|
|
.split(/\r?\n/u)
|
|
.find((candidate) => candidate.trimStart().startsWith(`${option} `));
|
|
return line?.trim();
|
|
};
|
|
|
|
describe("CLI help default annotations", () => {
|
|
it("states each schema-owned option default exactly once", async () => {
|
|
const cli = createCli({});
|
|
const repeated: string[] = [];
|
|
for (const { command, options } of singleDefaultOptions) {
|
|
const { stdout, exitCode } = await serve(cli, [command, "--help"]);
|
|
expect(exitCode, command).toBe(0);
|
|
for (const option of options) {
|
|
const line = optionLine(stdout, option);
|
|
if (line === undefined) {
|
|
repeated.push(`${command} ${option}: option line missing`);
|
|
continue;
|
|
}
|
|
const statements = line.match(/default/giu)?.length ?? 0;
|
|
if (statements !== 1)
|
|
repeated.push(`${command} ${option}: ${statements} default statements`);
|
|
}
|
|
}
|
|
expect(repeated).toEqual([]);
|
|
});
|
|
});
|