From 4b2c1af5602186696e16c67db25f6f4a2a4502b3 Mon Sep 17 00:00:00 2001 From: aramissennyeydd Date: Tue, 4 Feb 2025 10:27:57 -0500 Subject: [PATCH] add test case for running a command and positional arg handling Signed-off-by: aramissennyeydd --- .../cli/src/wiring/CliInitializer.test.ts | 99 +++++++++++++++++++ packages/cli/src/wiring/CliInitializer.ts | 4 +- 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 packages/cli/src/wiring/CliInitializer.test.ts diff --git a/packages/cli/src/wiring/CliInitializer.test.ts b/packages/cli/src/wiring/CliInitializer.test.ts new file mode 100644 index 0000000000..9ee252cfc5 --- /dev/null +++ b/packages/cli/src/wiring/CliInitializer.test.ts @@ -0,0 +1,99 @@ +/* + * Copyright 2025 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { CliInitializer } from './CliInitializer'; +import { createCliPlugin } from './factory'; + +process.exit = jest.fn() as any; + +describe('CliInitializer', () => { + beforeEach(() => { + jest.resetAllMocks(); + }); + it('should run commands', async () => { + expect.assertions(2); + process.argv = ['node', 'cli', 'test']; + const initializer = new CliInitializer(); + initializer.add( + createCliPlugin({ + pluginId: 'test', + init: async reg => + reg.addCommand({ + path: ['test'], + description: 'test', + execute: ({ args }) => { + expect(args).toEqual([]); + return Promise.resolve(); + }, + }), + }), + ); + await initializer.run(); + expect(process.exit).toHaveBeenCalledWith(0); + }); + + it('should pass positional args to the subcommand', async () => { + expect.assertions(2); + process.argv = ['node', 'cli', 'test', '[positional]', '']; + const initializer = new CliInitializer(); + initializer.add( + createCliPlugin({ + pluginId: 'test', + init: async reg => + reg.addCommand({ + path: ['test'], + description: 'test', + execute: ({ args }) => { + expect(args).toEqual(['[positional]', '']); + return Promise.resolve(); + }, + }), + }), + ); + await initializer.run(); + expect(process.exit).toHaveBeenCalledWith(0); + }); + + it('should pass positional args to the subcommand if nested', async () => { + expect.assertions(2); + process.argv = [ + 'node', + 'cli', + 'test', + 'nested', + 'command', + '[positional]', + '', + ]; + const initializer = new CliInitializer(); + initializer.add( + createCliPlugin({ + pluginId: 'test', + init: async reg => + reg.addCommand({ + path: ['test', 'nested', 'command'], + description: 'test', + execute: ({ args }) => { + expect(args).toEqual(['[positional]', '']); + return Promise.resolve(); + }, + }), + }), + ); + await initializer.run(); + expect(process.exit).toHaveBeenCalledWith(0); + }); +}); diff --git a/packages/cli/src/wiring/CliInitializer.ts b/packages/cli/src/wiring/CliInitializer.ts index 7655e93777..d7fdfdccab 100644 --- a/packages/cli/src/wiring/CliInitializer.ts +++ b/packages/cli/src/wiring/CliInitializer.ts @@ -17,7 +17,7 @@ import { CommandGraph } from './CommandGraph'; import { CliFeature, InternalCliFeature, InternalCliPlugin } from './types'; import { CommandRegistry } from './CommandRegistry'; -import { program } from 'commander'; +import { Command } from 'commander'; import { version } from '../lib/version'; import chalk from 'chalk'; import { exitWithError } from '../lib/errors'; @@ -61,6 +61,8 @@ export class CliInitializer { */ async run() { await this.#doInit(); + + const program = new Command(); program .name('backstage-cli') .version(version)