Skip to content

Commit 647f031

Browse files
authored
fix(cli-repl): render prompt correctly on interrupt MONGOSH-882 (#999)
1 parent bb1e8b2 commit 647f031

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/cli-repl/src/cli-repl.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ describe('CliRepl', () => {
963963
});
964964

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

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

975975
output = '';
976+
});
977+
978+
it('allows prompts that interact with shell API methods', async() => {
976979
input.write('1 + 2\n');
977980
await waitEval(cliRepl.bus);
978981
expect(output).to.include('on clirepltest> ');
979982
});
983+
984+
it('renders the prompt correctly on interrupt', async() => {
985+
input.write('while(true) { sleep(500); }\n');
986+
process.kill(process.pid, 'SIGINT');
987+
988+
await waitBus(cliRepl.bus, 'mongosh:interrupt-complete');
989+
990+
expect(output).to.contain('Stopping execution');
991+
expect(output).to.contain('on clirepltest> ');
992+
});
980993
});
981994

982995
context('pressing CTRL-C', () => {

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ class MongoshNodeRepl implements EvaluationListener {
404404
}
405405

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

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

@@ -512,6 +515,10 @@ class MongoshNodeRepl implements EvaluationListener {
512515
}));
513516
}
514517
this.bus.emit('mongosh:interrupt-complete'); // For testing purposes.
518+
519+
const { repl } = this.runtimeState();
520+
repl.setPrompt(await this.getShellPrompt());
521+
515522
return true;
516523
}
517524

0 commit comments

Comments
 (0)