mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 08:55:33 +00:00
fix(cli): reset command options (#2645)
This commit is contained in:
parent
282645ed8b
commit
0c132d7940
70
packages/core/server/src/__tests__/command.test.ts
Normal file
70
packages/core/server/src/__tests__/command.test.ts
Normal file
@ -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' });
|
||||
});
|
||||
});
|
@ -433,8 +433,13 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> 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;
|
||||
|
Loading…
Reference in New Issue
Block a user