add test case for running a command and positional arg handling
Signed-off-by: aramissennyeydd <aramis.sennyey@doordash.com>
This commit is contained in:
@@ -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]', '<arg>'];
|
||||
const initializer = new CliInitializer();
|
||||
initializer.add(
|
||||
createCliPlugin({
|
||||
pluginId: 'test',
|
||||
init: async reg =>
|
||||
reg.addCommand({
|
||||
path: ['test'],
|
||||
description: 'test',
|
||||
execute: ({ args }) => {
|
||||
expect(args).toEqual(['[positional]', '<arg>']);
|
||||
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]',
|
||||
'<arg>',
|
||||
];
|
||||
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]', '<arg>']);
|
||||
return Promise.resolve();
|
||||
},
|
||||
}),
|
||||
}),
|
||||
);
|
||||
await initializer.run();
|
||||
expect(process.exit).toHaveBeenCalledWith(0);
|
||||
});
|
||||
});
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user