Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,31 +334,30 @@ class MongoshNodeRepl implements EvaluationListener {
}

async onLoad(filename: string): Promise<OnLoadResult> {
const repl = this.runtimeState().repl;
const {
contents,
absolutePath
} = await this.ioProvider.readFileUTF8(filename);

return {
resolvedFilename: absolutePath,
evaluate: () => promisify(repl.eval.bind(repl))(contents, repl.context, absolutePath)
evaluate: async() => { await this.loadExternalCode(contents, absolutePath); }
};
}

async loadExternalFile(filename: string): Promise<void> {
await this.runtimeState().internalState.shellApi.load(filename);
}

async loadExternalCode(code: string, filename: string): Promise<void> {
async loadExternalCode(code: string, filename: string): Promise<ShellResult> {
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.
Expand Down
5 changes: 3 additions & 2 deletions packages/cli-repl/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down