An open source design system that's fully customizable and agent ready
TypeScript
test(cli): runCli builds only the commands its arguments need (#10010)
Christopher Chedeau2f035fef4c28
diff --git a/.changeset/cli-test-run-cli-bin-commands.md b/.changeset/cli-test-run-cli-bin-commands.mdnew file mode 100644index 0000000000..b1a01c67be--- /dev/null+++ b/.changeset/cli-test-run-cli-bin-commands.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[chore] Internal: the CLI's in-process test helper builds only the commands its arguments need, as a run of the bin does.++@vjeuxdiff --git a/packages/cli/clients/cli/commands-for-args.test.mjs b/packages/cli/clients/cli/commands-for-args.test.mjsindex 63afcb034d..851c789494 100644--- a/packages/cli/clients/cli/commands-for-args.test.mjs+++ b/packages/cli/clients/cli/commands-for-args.test.mjs@@ -98,8 +98,8 @@ describe('commandsForArgs', () => {describe('a run that loads only its command prints what the full program prints', () => {/** @param {string[]} argv */const expectSameAnswer = async argv => {- const asBin = await runCli(argv, {cwd: dir, asBin: true});- const everything = await runCli(argv, {cwd: dir});+ const asBin = await runCli(argv, {cwd: dir});+ const everything = await runCli(argv, {cwd: dir, allCommands: true});expect(asBin, argv.join(' ')).toEqual(everything);};diff --git a/packages/cli/test-utils/run-cli.mjs b/packages/cli/test-utils/run-cli.mjsindex 42b7fd86c9..d43ff99ce8 100644--- a/packages/cli/test-utils/run-cli.mjs+++ b/packages/cli/test-utils/run-cli.mjs@@ -39,16 +39,18 @@ class ExitSignal extends Error {* Run the Astryx CLI in-process and capture its observable surface.** @param {string[]} args argv after `astryx` (e.g. `['component', '--list']`)- * @param {string | {cwd?: string, asBin?: boolean}} [cwdOrOpts] cwd- * (positional), or `{cwd}`. `asBin` builds the program as a run of the bin- * does: with only the commands these arguments need.+ * @param {string | {cwd?: string, allCommands?: boolean}} [cwdOrOpts] cwd+ * (positional), or `{cwd}`. The program has only the commands these+ * arguments need, as a run of the bin builds it. `allCommands` builds it+ * with every command, which no run of the bin does.* @returns {Promise<{status: number, code: number, stdout: string, stderr: string}>}*/export async function runCli(args, cwdOrOpts) {const cwd =typeof cwdOrOpts === 'string' ? cwdOrOpts : cwdOrOpts && cwdOrOpts.cwd;- const asBin = typeof cwdOrOpts === 'object' && cwdOrOpts?.asBin === true;- const program = await createProgram(asBin ? {argv: args} : undefined);+ const allCommands =+ typeof cwdOrOpts === 'object' && cwdOrOpts?.allCommands === true;+ const program = await createProgram(allCommands ? undefined : {argv: args});// Commander's own exits (parse errors, --help, --version, unknown option)// throw a CommanderError instead of calling process.exit.program.exitOverride();diff --git a/packages/cli/test-utils/run-cli.test.mjs b/packages/cli/test-utils/run-cli.test.mjsnew file mode 100644index 0000000000..947a38bc81--- /dev/null+++ b/packages/cli/test-utils/run-cli.test.mjs@@ -0,0 +1,50 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file runCli builds the program a run of the bin builds (run.mjs passes its+ * arguments to createProgram), so a test drives the commands a user's run+ * loads, and no more.+ */++import {describe, expect, it, vi} from 'vitest';++const built = vi.hoisted(() => ({names: /** @type {string[][]} */ ([])}));++vi.mock('../clients/cli/index.mjs', async importOriginal => {+ /** @type {typeof import('../clients/cli/index.mjs')} */+ const original = await importOriginal();+ return {+ ...original,+ /** @param {Parameters<typeof original.createProgram>[0]} options */+ createProgram: async options => {+ const program = await original.createProgram(options);+ built.names.push(program.commands.map(command => command.name()));+ return program;+ },+ };+});++const {createProgram} = await import('../clients/cli/index.mjs');+const {runCli} = await import('./run-cli.mjs');++describe('runCli', () => {+ it('builds the commands a run of the bin builds for the same arguments', async () => {+ for (const argv of [+ ['search', 'zzqq'],+ ['--json', 'docs'],+ ['--detail', 'brief', 'component', 'Button'],+ ['--version'],+ ['--help'],+ ['help', 'docs'],+ ['no-such-command'],+ ]) {+ built.names.length = 0;+ await runCli(argv);+ const [helper, bin] = [+ built.names[0],+ (await createProgram({argv})).commands.map(command => command.name()),+ ];+ expect(helper, argv.join(' ')).toEqual(bin);+ }+ });+});