Skip to content

Commit bc43c4d

Browse files
authored
Merge branch 'main' into silkbomb-update
2 parents 6414834 + e22a843 commit bc43c4d

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

THIRD_PARTY_NOTICES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The following third-party software is used by and included in **mongosh**.
2-
This document was automatically generated on Tue Feb 18 2025.
2+
This document was automatically generated on Wed Feb 19 2025.
33

44
## List of dependencies
55

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { Duplex } from 'stream';
1010
import { PassThrough } from 'stream';
1111
import type { StubbedInstance } from 'ts-sinon';
1212
import { stubInterface } from 'ts-sinon';
13-
import { promisify } from 'util';
13+
import { inspect, promisify } from 'util';
1414
import {
1515
expect,
1616
fakeTTYProps,
@@ -526,18 +526,48 @@ describe('MongoshNodeRepl', function () {
526526
]) {
527527
context(mode, function () {
528528
let getHistory: () => string[];
529+
let getAllHistoryItems: () => string[];
529530

530531
beforeEach(function () {
531532
const { history } = mongoshRepl.runtimeState().repl as unknown as {
532533
history: string[];
533534
};
534535
getHistory = () =>
535536
history.filter((line) => !line.startsWith('prefill-'));
537+
getAllHistoryItems = () => history;
536538
for (let i = 0; i < prefill; i++) {
537539
history.unshift(`prefill-${i}`);
538540
}
539541
});
540542

543+
describe('history() command', function () {
544+
it('returns a formatted array of history', async function () {
545+
output = '';
546+
input.write('history()\n');
547+
await waitEval(bus);
548+
expect(output).includes(
549+
inspect(
550+
getAllHistoryItems()
551+
.slice(1, getAllHistoryItems().length)
552+
.reverse(),
553+
{ maxArrayLength: Infinity }
554+
)
555+
);
556+
});
557+
558+
it('works with array operations', async function () {
559+
output = '';
560+
input.write('history().slice(history().length-10).reverse()\n');
561+
await waitEval(bus);
562+
const history = getAllHistoryItems().slice(1).reverse();
563+
expect(output).includes(
564+
inspect(history.slice(history.length - 10).reverse(), {
565+
maxArrayLength: Infinity,
566+
})
567+
);
568+
});
569+
});
570+
541571
it('looks up existing entries, if there are any', async function () {
542572
output = '';
543573
input.write(arrowUp);

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { markTime } from './startup-timing';
4747
import type { Context } from 'vm';
4848
import { Script, createContext, runInContext } from 'vm';
4949
import { installPasteSupport } from './repl-paste-support';
50+
import util from 'util';
5051

5152
declare const __non_webpack_require__: any;
5253

@@ -365,6 +366,8 @@ class MongoshNodeRepl implements EvaluationListener {
365366
await this.finishInitializingNodeRepl();
366367
instanceState.setCtx(context);
367368

369+
this.setupHistoryCommand();
370+
368371
if (!this.shellCliOptions.nodb && !this.shellCliOptions.quiet) {
369372
// cf. legacy shell:
370373
// https://github.com/mongodb/mongo/blob/a6df396047a77b90bf1ce9463eecffbee16fb864/src/mongo/shell/mongo_main.cpp#L1003-L1026
@@ -391,6 +394,35 @@ class MongoshNodeRepl implements EvaluationListener {
391394
return { __initialized: 'yes' };
392395
}
393396

397+
setupHistoryCommand(): void {
398+
const history = () => {
399+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
400+
const replHistory: string[] = (this.runtimeState().repl as any).history;
401+
const formattedHistory =
402+
// Remove the history call from the formatted history
403+
replHistory.slice(1).reverse();
404+
405+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
406+
formattedHistory[util.inspect.custom as any] = ((
407+
depth: number | null,
408+
options: util.InspectOptions,
409+
inspect: typeof util.inspect
410+
) => {
411+
// We pass a copy of the array without the util.inspect.custom set
412+
// to prevent infinite recursion.
413+
return inspect(formattedHistory.concat(), {
414+
...options,
415+
depth,
416+
maxArrayLength: Infinity,
417+
});
418+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
419+
}) as any;
420+
return formattedHistory;
421+
};
422+
423+
this.runtimeState().context.history = history;
424+
}
425+
394426
private async finishInitializingNodeRepl(): Promise<void> {
395427
const { repl, instanceState } = this.runtimeState();
396428
if (!repl) return;

0 commit comments

Comments
 (0)