From 3ceacdb5f2b07d2dd68dcf1568cb2e5b6d53539d Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 9 Apr 2021 15:54:48 +0200 Subject: [PATCH] fix(cli-repl): print result of --eval MONGOSH-683 `--eval` always prints its result in the old shell (with or without `--shell`). Do the same thing in mongosh. --- packages/cli-repl/src/cli-repl.spec.ts | 2 +- packages/cli-repl/src/cli-repl.ts | 3 ++- packages/cli-repl/src/mongosh-repl.ts | 9 ++++----- packages/cli-repl/test/e2e.spec.ts | 5 +++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/cli-repl/src/cli-repl.spec.ts b/packages/cli-repl/src/cli-repl.spec.ts index 774f02a48e..69d82aff6a 100644 --- a/packages/cli-repl/src/cli-repl.spec.ts +++ b/packages/cli-repl/src/cli-repl.spec.ts @@ -361,7 +361,7 @@ describe('CliRepl', () => { }); it('evaluates code passed through --eval', async() => { - cliReplOptions.shellCliOptions.eval = 'print("i am being evaluated")'; + cliReplOptions.shellCliOptions.eval = '"i am" + " being evaluated"'; cliRepl = new CliRepl(cliReplOptions); await startWithExpectedImmediateExit(cliRepl, ''); expect(output).to.include('i am being evaluated'); diff --git a/packages/cli-repl/src/cli-repl.ts b/packages/cli-repl/src/cli-repl.ts index c479fd3f5a..0123b25e21 100644 --- a/packages/cli-repl/src/cli-repl.ts +++ b/packages/cli-repl/src/cli-repl.ts @@ -190,7 +190,8 @@ class CliRepl { async loadCommandLineFilesAndEval(files: string[]) { if (this.cliOptions.eval) { this.bus.emit('mongosh:eval-cli-script'); - await this.mongoshRepl.loadExternalCode(this.cliOptions.eval, '@(shell eval)'); + const evalResult = await this.mongoshRepl.loadExternalCode(this.cliOptions.eval, '@(shell eval)'); + this.output.write(this.mongoshRepl.writer(evalResult) + '\n'); } else if (this.cliOptions.eval === '') { // This happens e.g. when --eval is followed by another option, for example // when running `mongosh --eval --shell "eval script"`, which can happen diff --git a/packages/cli-repl/src/mongosh-repl.ts b/packages/cli-repl/src/mongosh-repl.ts index 116ab440d7..970c791478 100644 --- a/packages/cli-repl/src/mongosh-repl.ts +++ b/packages/cli-repl/src/mongosh-repl.ts @@ -334,7 +334,6 @@ class MongoshNodeRepl implements EvaluationListener { } async onLoad(filename: string): Promise { - const repl = this.runtimeState().repl; const { contents, absolutePath @@ -342,7 +341,7 @@ class MongoshNodeRepl implements EvaluationListener { return { resolvedFilename: absolutePath, - evaluate: () => promisify(repl.eval.bind(repl))(contents, repl.context, absolutePath) + evaluate: async() => { await this.loadExternalCode(contents, absolutePath); } }; } @@ -350,15 +349,15 @@ class MongoshNodeRepl implements EvaluationListener { await this.runtimeState().internalState.shellApi.load(filename); } - async loadExternalCode(code: string, filename: string): Promise { + async loadExternalCode(code: string, filename: string): Promise { const { repl } = this.runtimeState(); - await promisify(repl.eval.bind(repl))(code, repl.context, filename); + return await promisify(repl.eval.bind(repl))(code, repl.context, filename); } /** * Format the result to a string so it can be written to the output stream. */ - writer(result: any): string { + writer(result: any /* Error | ShellResult */): string { // This checks for error instances. // The writer gets called immediately by the internal `repl.eval` // in case of errors. diff --git a/packages/cli-repl/test/e2e.spec.ts b/packages/cli-repl/test/e2e.spec.ts index 6d3696a5c2..24fa247a69 100644 --- a/packages/cli-repl/test/e2e.spec.ts +++ b/packages/cli-repl/test/e2e.spec.ts @@ -595,9 +595,10 @@ describe('e2e', function() { }); context('--eval', () => { + const script = 'const a = "hello", b = " one"; a + b'; it('loads a script from the command line as requested', async() => { const shell = TestShell.start({ - args: [ '--nodb', '--eval', 'print("hello one")' ] + args: [ '--nodb', '--eval', script ] }); await eventually(() => { shell.assertContainsOutput('hello one'); @@ -608,7 +609,7 @@ describe('e2e', function() { it('drops into shell if --shell is used', async() => { const shell = TestShell.start({ - args: [ '--nodb', '--eval', 'print("hello one")', '--shell' ] + args: [ '--nodb', '--eval', script, '--shell' ] }); await shell.waitForPrompt(); shell.assertContainsOutput('hello one');