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
19 changes: 14 additions & 5 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MongoshInternalError } from '@mongosh/errors';
import bson from 'bson';
import { once } from 'events';
import { promises as fs } from 'fs';
import http from 'http';
Expand All @@ -11,6 +10,8 @@ import { expect, fakeTTYProps, readReplLogfile, tick, useTmpdir, waitBus, waitCo
import { eventually } from '../../../testing/eventually';
import CliRepl, { CliReplOptions } from './cli-repl';
import { CliReplErrors } from './error-codes';
import { bson } from '@mongosh/service-provider-core';
const { EJSON } = bson;

const delay = promisify(setTimeout);

Expand Down Expand Up @@ -109,32 +110,40 @@ describe('CliRepl', () => {

await evalComplete; // eval-complete includes the fs.writeFile() call.
const content = await fs.readFile(path.join(tmpdir.path, 'config'), { encoding: 'utf8' });
expect(JSON.parse(content).enableTelemetry).to.be.false;
expect((EJSON.parse(content) as any).enableTelemetry).to.be.false;
});

it('does not store config options on disk that have not been changed', async() => {
let content = await fs.readFile(path.join(tmpdir.path, 'config'), { encoding: 'utf8' });
expect(Object.keys(JSON.parse(content))).to.deep.equal([
expect(Object.keys(EJSON.parse(content))).to.deep.equal([
'userId', 'enableTelemetry', 'disableGreetingMessage'
]);

input.write('config.set("inspectDepth", config.get("inspectDepth"))\n');

await waitEval(cliRepl.bus);
content = await fs.readFile(path.join(tmpdir.path, 'config'), { encoding: 'utf8' });
expect(Object.keys(JSON.parse(content))).to.deep.equal([
expect(Object.keys(EJSON.parse(content))).to.deep.equal([
'userId', 'enableTelemetry', 'disableGreetingMessage', 'inspectDepth'
]);

// When a new REPL is created:
cliRepl = new CliRepl(cliReplOptions);
await cliRepl.start('', {});
content = await fs.readFile(path.join(tmpdir.path, 'config'), { encoding: 'utf8' });
expect(Object.keys(JSON.parse(content))).to.deep.equal([
expect(Object.keys(EJSON.parse(content))).to.deep.equal([
'userId', 'enableTelemetry', 'disableGreetingMessage', 'inspectDepth'
]);
});

it('store config options on disk cannot be represented in traditional JSON', async() => {
input.write('config.set("inspectDepth", Infinity)\n');

await waitEval(cliRepl.bus);
const content = await fs.readFile(path.join(tmpdir.path, 'config'), { encoding: 'utf8' });
expect((EJSON.parse(content) as any).inspectDepth).equal(Infinity);
});

it('emits exit when asked to, Node.js-style', async() => {
input.write('.exit\n');
await exitPromise;
Expand Down
5 changes: 4 additions & 1 deletion packages/cli-repl/src/config-directory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { promisify } from 'util';
import chai, { expect } from 'chai';
import sinon from 'ts-sinon';
import sinonChai from 'sinon-chai';
import { bson } from '@mongosh/service-provider-core';
const { EJSON } = bson;
chai.use(sinonChai);

class ExampleConfig {
someProperty = 42;
someOtherProperty?: number = Infinity;
}

describe('home directory management', () => {
Expand Down Expand Up @@ -62,7 +65,7 @@ describe('home directory management', () => {
const configPath = manager.path();
await manager.writeConfigFile(new ExampleConfig());
const contents = await fs.readFile(configPath, { encoding: 'utf8' });
expect(JSON.parse(contents)).to.deep.equal(new ExampleConfig());
expect(EJSON.parse(contents)).to.deep.equal(new ExampleConfig());

expect(onError).to.not.have.been.called;
expect(onNewConfig).to.not.have.been.called;
Expand Down
7 changes: 5 additions & 2 deletions packages/cli-repl/src/config-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { promises as fs } from 'fs';
import path from 'path';
import os from 'os';
import { EventEmitter } from 'events';
import { bson } from '@mongosh/service-provider-core';
const { EJSON } = bson;

export type ShellHomePaths = {
shellRoamingDataPath: string;
Expand Down Expand Up @@ -75,7 +77,7 @@ export class ConfigManager<Config> extends EventEmitter {
if (fd !== undefined) {
// Not the first access. Read the file and return it.
try {
const config: Config = JSON.parse(await fd.readFile({ encoding: 'utf8' }));
const config: Config = EJSON.parse(await fd.readFile({ encoding: 'utf8' })) as any;
this.emit('update-config', config);
return { ...defaultConfig, ...config };
} catch (err) {
Expand All @@ -99,7 +101,8 @@ export class ConfigManager<Config> extends EventEmitter {
async writeConfigFile(config: Config): Promise<void> {
await this.shellHomeDirectory.ensureExists();
try {
await fs.writeFile(this.path(), JSON.stringify(config), { mode: 0o600 });
// TODO: { relaxed: false } can be removed once NODE-3390 is done.
await fs.writeFile(this.path(), EJSON.stringify(config, { relaxed: false }), { mode: 0o600 });
} catch (err) {
this.emit('error', err);
throw err;
Expand Down
4 changes: 3 additions & 1 deletion packages/cli-repl/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { promisify } from 'util';
import rimraf from 'rimraf';
import path from 'path';
import { readReplLogfile } from './repl-helpers';
import { bson } from '@mongosh/service-provider-core';
const { EJSON } = bson;

describe('e2e', function() {
const testServer = startTestServer('shared');
Expand Down Expand Up @@ -682,7 +684,7 @@ describe('e2e', function() {
configPath = path.resolve(homedir, '.mongodb', 'mongosh', 'config');
historyPath = path.resolve(homedir, '.mongodb', 'mongosh', 'mongosh_repl_history');
}
readConfig = async() => JSON.parse(await fs.readFile(configPath, 'utf8'));
readConfig = async() => EJSON.parse(await fs.readFile(configPath, 'utf8'));
readLogfile = async() => readReplLogfile(logPath);
startTestShell = async(...extraArgs: string[]) => {
const shell = TestShell.start({
Expand Down