From 8b18cd0ab29bb2743aa2d4d7aac0dd59539c43dd Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 27 Apr 2021 11:54:37 +0200 Subject: [PATCH] fix(history): do not modify empty history MONGOSH-722 --- packages/cli-repl/src/mongosh-repl.spec.ts | 7 +++++++ packages/history/src/history.spec.ts | 6 ++++++ packages/history/src/history.ts | 1 + 3 files changed, 14 insertions(+) diff --git a/packages/cli-repl/src/mongosh-repl.spec.ts b/packages/cli-repl/src/mongosh-repl.spec.ts index 1d2a38a883..b59e611c43 100644 --- a/packages/cli-repl/src/mongosh-repl.spec.ts +++ b/packages/cli-repl/src/mongosh-repl.spec.ts @@ -415,6 +415,13 @@ describe('MongoshNodeRepl', () => { 'const a = 20' ]); }); + + it('does not crash if hitting enter and then up', async() => { + input.write('\n'); + await once(mongoshRepl.runtimeState().repl, 'flushHistory'); + input.write(`${arrowUp}`); + await tick(); + }); }); } }); diff --git a/packages/history/src/history.spec.ts b/packages/history/src/history.spec.ts index 1ee98757f6..ffe50a6b58 100644 --- a/packages/history/src/history.spec.ts +++ b/packages/history/src/history.spec.ts @@ -33,6 +33,12 @@ describe('changeHistory', () => { changeHistory(cloned); expect(cloned).to.deep.equal(i); }); + + it('does not modify an empty history', () => { + const i: string[] = []; + changeHistory(i); + expect(i).to.deep.equal([]); + }); }); context('when redact option is false', () => { diff --git a/packages/history/src/history.ts b/packages/history/src/history.ts index b5c258c3a6..e67192533a 100644 --- a/packages/history/src/history.ts +++ b/packages/history/src/history.ts @@ -17,6 +17,7 @@ export function removeCommand(history: string, redact = false): string { * @param {boolean} redact - Option to redact sensitive info. */ export function changeHistory(history: string[], redact = false): void { + if (history.length === 0) return; const hiddenCommands = new RegExp(HIDDEN_COMMANDS, 'g'); if (hiddenCommands.test(history[0])) {