test: should correctly parse the command options (#2688)

This commit is contained in:
ChengLei Shao 2023-09-21 10:03:39 +08:00 committed by GitHub
parent 196bec0ede
commit 10e46b21f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -40,4 +40,21 @@ describe('app command', () => {
expect(app.cli.parseHandleByIPCServer(['node', 'cli', 'nocobase', 'subparent', 'testaa'])).toBeTruthy(); expect(app.cli.parseHandleByIPCServer(['node', 'cli', 'nocobase', 'subparent', 'testaa'])).toBeTruthy();
expect(app.cli.parseHandleByIPCServer(['node', 'cli', 'nocobase', 'subparent', 'testbb'])).toBeFalsy(); expect(app.cli.parseHandleByIPCServer(['node', 'cli', 'nocobase', 'subparent', 'testbb'])).toBeFalsy();
}); });
it('should correctly parse the command multiple times with varying parameters', async () => {
const fn = jest.fn();
app
.command('test1')
.option('-a, --aaa <aaa>', 'aaa option')
.action((options) => {
fn(options);
});
await app.runCommand('test1', '-a', 'aaa');
expect(fn).toBeCalledWith({ aaa: 'aaa' });
await app.runCommand('test1');
expect(fn).toBeCalledWith({});
});
}); });

View File

@ -368,7 +368,6 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
.usage('[command] [options]') .usage('[command] [options]')
.hook('preAction', async (_, actionCommand) => { .hook('preAction', async (_, actionCommand) => {
this._actionCommand = actionCommand; this._actionCommand = actionCommand;
this.activatedCommand = { this.activatedCommand = {
name: getCommandFullName(actionCommand), name: getCommandFullName(actionCommand),
}; };
@ -416,6 +415,7 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
return command; return command;
} catch (error) { } catch (error) {
console.log({ error });
if (!this.activatedCommand) { if (!this.activatedCommand) {
this.activatedCommand = { this.activatedCommand = {
name: 'unknown', name: 'unknown',
@ -442,8 +442,8 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
_actionCommand.addOption(option); _actionCommand.addOption(option);
} }
} }
this.activatedCommand = null;
this._actionCommand = null; this._actionCommand = null;
this.activatedCommand = null;
} }
} }