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
15 changes: 14 additions & 1 deletion packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ describe('CliRepl', () => {
});

context('with a user-provided prompt', () => {
it('allows prompts that interact with shell API methods', async() => {
beforeEach(async() => {
await cliRepl.start(await testServer.connectionString(), {});

input.write('use clirepltest\n');
Expand All @@ -973,10 +973,23 @@ describe('CliRepl', () => {
await waitEval(cliRepl.bus);

output = '';
});

it('allows prompts that interact with shell API methods', async() => {
input.write('1 + 2\n');
await waitEval(cliRepl.bus);
expect(output).to.include('on clirepltest> ');
});

it('renders the prompt correctly on interrupt', async() => {
input.write('while(true) { sleep(500); }\n');
process.kill(process.pid, 'SIGINT');

await waitBus(cliRepl.bus, 'mongosh:interrupt-complete');

expect(output).to.contain('Stopping execution');
expect(output).to.contain('on clirepltest> ');
});
});

context('pressing CTRL-C', () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ class MongoshNodeRepl implements EvaluationListener {
}

const { repl, shellEvaluator } = this.runtimeState();
let interrupted = false;

try {
const shellResult = await shellEvaluator.customEval(originalEval, input, context, filename);
Expand All @@ -424,6 +425,7 @@ class MongoshNodeRepl implements EvaluationListener {
.filter(([key]) => !key.startsWith('_')));
} catch (err) {
if (this.runtimeState().internalState.interrupted.isSet()) {
interrupted = true;
this.bus.emit('mongosh:eval-interrupted');
// The shell is interrupted by CTRL-C - so we ignore any errors
// that happened during evaluation.
Expand All @@ -442,7 +444,8 @@ class MongoshNodeRepl implements EvaluationListener {
}
throw err;
} finally {
if (!this.insideAutoCompleteOrGetPrompt) {
if (!this.insideAutoCompleteOrGetPrompt && !interrupted) {
// In case of an interrupt, onAsyncSigint will print the prompt when completed
repl.setPrompt(await this.getShellPrompt());
}

Expand Down Expand Up @@ -512,6 +515,10 @@ class MongoshNodeRepl implements EvaluationListener {
}));
}
this.bus.emit('mongosh:interrupt-complete'); // For testing purposes.

const { repl } = this.runtimeState();
repl.setPrompt(await this.getShellPrompt());

return true;
}

Expand Down