From eb8cb64fd4bee65bf00ab6ccf3560c91a6107630 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 30 Jun 2021 17:12:23 +0200 Subject: [PATCH] fix(shell-evaluator): treat cmd whose arg starts with `(` as fn call MONGOSH-858 If the argument to a shell command starts with `(`, we treat it as a function call, since that is probably the intended usage here. --- packages/shell-evaluator/src/shell-evaluator.spec.ts | 7 +++++++ packages/shell-evaluator/src/shell-evaluator.ts | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/shell-evaluator/src/shell-evaluator.spec.ts b/packages/shell-evaluator/src/shell-evaluator.spec.ts index f9cf1ef46b..c11bf837c3 100644 --- a/packages/shell-evaluator/src/shell-evaluator.spec.ts +++ b/packages/shell-evaluator/src/shell-evaluator.spec.ts @@ -44,6 +44,13 @@ describe('ShellEvaluator', () => { expect(useSpy).to.have.been.calledWith('somedb'); }); + it('does not apply special handling for commands when the first argument starts with (', async() => { + const originalEval = sinon.spy(); + await shellEvaluator.customEval(originalEval, 'use (somedb);', {}, ''); + expect(originalEval.firstCall.args[0]).to.include('somedb'); + expect(useSpy).to.have.callCount(0); + }); + it('forwards show commands', async() => { const dontCallEval = () => { throw new Error('unreachable'); }; await shellEvaluator.customEval(dontCallEval, 'show dbs;', {}, ''); diff --git a/packages/shell-evaluator/src/shell-evaluator.ts b/packages/shell-evaluator/src/shell-evaluator.ts index 2c0a5714b1..14aa90632e 100644 --- a/packages/shell-evaluator/src/shell-evaluator.ts +++ b/packages/shell-evaluator/src/shell-evaluator.ts @@ -36,7 +36,7 @@ class ShellEvaluator { const { shellApi } = this.internalState; const argv = input.trim().replace(/;$/, '').split(/\s+/g); const cmd = argv.shift() as keyof typeof shellApi; - if (shellApi[cmd]?.isDirectShellCommand) { + if (shellApi[cmd]?.isDirectShellCommand && !(argv[0] ?? '').startsWith('(')) { return shellApi[cmd](...argv); }