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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class OpenContextRuntime implements Runtime {
) {
this.interpreterEnvironment = interpreterEnvironment;
this.internalState = new ShellInternalState(serviceProvider, messageBus || new EventEmitter());
this.internalState.isInteractive = true;
this.shellEvaluator = new ShellEvaluator(this.internalState);
this.internalState.setCtx(this.interpreterEnvironment.getContextObject());
this.interpreter = new Interpreter(this.interpreterEnvironment);
Expand Down
55 changes: 55 additions & 0 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,61 @@ describe('CliRepl', () => {
expect(output).to.include('--eval requires an argument, but no argument was given');
expect(exitCode).to.equal(0);
});

it('isInteractive() is false for --eval without --shell', async() => {
const filename1 = path.resolve(__dirname, '..', 'test', 'fixtures', 'load', 'printisinteractive.js');
cliReplOptions.shellCliOptions.eval = await fs.readFile(filename1, 'utf8');
cliRepl = new CliRepl(cliReplOptions);
await startWithExpectedImmediateExit(cliRepl, await testServer.connectionString());
expect(output).to.match(/isInteractive=false/);
expect(exitCode).to.equal(0);
});

it('isInteractive() is true for --eval with --shell', async() => {
const filename1 = path.resolve(__dirname, '..', 'test', 'fixtures', 'load', 'printisinteractive.js');
cliReplOptions.shellCliOptions.eval = await fs.readFile(filename1, 'utf8');
cliReplOptions.shellCliOptions.shell = true;
cliRepl = new CliRepl(cliReplOptions);
await cliRepl.start(await testServer.connectionString(), {});
expect(output).to.match(/isInteractive=true/);
expect(exitCode).to.equal(null);

input.write('exit\n');
await waitBus(cliRepl.bus, 'mongosh:closed');
expect(exitCode).to.equal(0);
});

it('isInteractive() is false for loaded file without --shell', async() => {
const filename1 = path.resolve(__dirname, '..', 'test', 'fixtures', 'load', 'printisinteractive.js');
cliReplOptions.shellCliOptions._.push(filename1);
cliRepl = new CliRepl(cliReplOptions);
await startWithExpectedImmediateExit(cliRepl, await testServer.connectionString());
expect(output).to.match(/isInteractive=false/);
expect(exitCode).to.equal(0);
});

it('isInteractive() is true for --eval with --shell', async() => {
const filename1 = path.resolve(__dirname, '..', 'test', 'fixtures', 'load', 'printisinteractive.js');
cliReplOptions.shellCliOptions._.push(filename1);
cliReplOptions.shellCliOptions.shell = true;
cliRepl = new CliRepl(cliReplOptions);
await cliRepl.start(await testServer.connectionString(), {});
expect(output).to.match(/isInteractive=true/);
expect(exitCode).to.equal(null);

input.write('exit\n');
await waitBus(cliRepl.bus, 'mongosh:closed');
expect(exitCode).to.equal(0);
});

it('isInteractive() is true for plain shell', async() => {
cliRepl = new CliRepl(cliReplOptions);
await cliRepl.start(await testServer.connectionString(), {});

input.write('print("isInteractive=" + isInteractive())\n');
await waitEval(cliRepl.bus);
expect(output).to.match(/isInteractive=true/);
});
});

context('with a user-provided prompt', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,15 @@ class CliRepl {
const initialized = await this.mongoshRepl.initialize(initialServiceProvider);
const commandLineLoadFiles = this.listCommandLineLoadFiles();
if (commandLineLoadFiles.length > 0 || this.cliOptions.eval !== undefined) {
this.mongoshRepl.setIsInteractive(!!this.cliOptions.shell);
this.bus.emit('mongosh:start-loading-cli-scripts', { usesShellOption: !!this.cliOptions.shell });
await this.loadCommandLineFilesAndEval(commandLineLoadFiles);
if (!this.cliOptions.shell) {
await this.exit(0);
return;
}
} else {
this.mongoshRepl.setIsInteractive(true);
}
await this.loadRcFiles();
this.bus.emit('mongosh:start-mongosh-repl', { version });
Expand Down
4 changes: 4 additions & 0 deletions packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ class MongoshNodeRepl implements EvaluationListener {
this._runtimeState = null;
}

setIsInteractive(value: boolean): void {
this.runtimeState().internalState.isInteractive = value;
}

async initialize(serviceProvider: ServiceProvider): Promise<InitializationToken> {
const internalState = new ShellInternalState(serviceProvider, this.bus, this.shellCliOptions);
const shellEvaluator = new ShellEvaluator(internalState);
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-repl/test/fixtures/load/printisinteractive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable */
print(`isInteractive=${isInteractive()}`)
3 changes: 3 additions & 0 deletions packages/i18n/src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ const translations: Catalog = {
},
printjson: {
description: 'Alias for print()'
},
isInteractive: {
description: 'Returns whether the shell will enter or has entered interactive mode'
}
}
},
Expand Down
7 changes: 7 additions & 0 deletions packages/shell-api/src/shell-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,13 @@ describe('ShellApi', () => {
expect(version).to.equal(expected);
});
});
describe('isInteractive', () => {
it('returns a boolean', () => {
expect(internalState.context.isInteractive()).to.equal(false);
internalState.isInteractive = true;
expect(internalState.context.isInteractive()).to.equal(true);
});
});
for (const cmd of ['exit', 'quit']) {
// eslint-disable-next-line no-loop-func
describe(cmd, () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/shell-api/src/shell-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,8 @@ export default class ShellApi extends ShellApiClass {
const { evaluationListener } = this._internalState;
await evaluationListener.onClearCommand?.();
}

isInteractive(): boolean {
return this._internalState.isInteractive;
}
}
1 change: 1 addition & 0 deletions packages/shell-api/src/shell-internal-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export default class ShellInternalState {
public evaluationListener: EvaluationListener;
public mongocryptdSpawnPath: string | null;
public batchSizeFromDBQuery: number | undefined = undefined;
public isInteractive = false;

public readonly interrupted = new InterruptFlag();
public resumeMongosAfterInterrupt: Array<{
Expand Down