diff --git a/packages/core/server/src/__tests__/command.test.ts b/packages/core/server/src/__tests__/command.test.ts new file mode 100644 index 0000000000..613f67a8b0 --- /dev/null +++ b/packages/core/server/src/__tests__/command.test.ts @@ -0,0 +1,70 @@ +import { mockDatabase } from '@nocobase/database'; +import Application, { ApplicationOptions } from '../application'; + +const mockServer = (options?: ApplicationOptions) => { + return new Application({ + database: mockDatabase(), + acl: false, + ...options, + }); +}; + +describe('command', () => { + let app: Application; + + afterEach(async () => { + if (app) { + await app.destroy(); + } + }); + + test('case1', async () => { + app = mockServer({ + plugins: [], + }); + + let val; + + app + .command('c1') + // .option('-p, --pepper') + .option('-r1, --retry1 [retry1]') + .option('-r2, --retry2 [retry2]') + .option('-r3, --retry3 [retry3]') + .action((opts, cli) => { + val = opts; + }); + + await app.runCommand('c1', '-r1', '2'); + expect(val).toEqual({ retry1: '2' }); + await app.runCommand('c1', '-r2', '3'); + expect(val).toEqual({ retry2: '3' }); + await app.runCommand('c1', '-r3', '4'); + expect(val).toEqual({ retry3: '4' }); + }); + + test('case2', async () => { + app = mockServer({ + plugins: [], + }); + + let val; + + app + .command('c1') + // .option('-p, --pepper') + .option('-r1, --retry1 [retry1]', '', '1') + .option('-r2, --retry2 [retry2]', '', '2') + .option('-r3, --retry3 [retry3]', '', '3') + .action((opts, cli) => { + val = opts; + }); + + await app.runCommand('c1', '-r1', '2'); + expect(val).toEqual({ retry1: '2', retry2: '2', retry3: '3' }); + await app.runCommand('c1', '-r2', '3'); + expect(val).toEqual({ retry1: '1', retry2: '3', retry3: '3' }); + await app.runCommand('c1', '-r3', '4'); + expect(val).toEqual({ retry1: '1', retry2: '2', retry3: '4' }); + }); +}); diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index a38f085e8e..c02a3c77f6 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -433,8 +433,13 @@ export class Application exten } finally { const _actionCommand = this._actionCommand; if (_actionCommand) { + const options = _actionCommand['options']; _actionCommand['_optionValues'] = {}; _actionCommand['_optionValueSources'] = {}; + _actionCommand['options'] = []; + for (const option of options) { + _actionCommand.addOption(option); + } } this.activatedCommand = null; this._actionCommand = null;