Skip to content

Commit 81d0eac

Browse files
authored
fix(cli-repl): properly skip update notification fetches in quiet mode MONGOSH-1751 (#1919)
1 parent a38a173 commit 81d0eac

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

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

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { MongoshInternalError } from '@mongosh/errors';
22
import { bson } from '@mongosh/service-provider-core';
33
import { once } from 'events';
44
import { promises as fs } from 'fs';
5-
import http from 'http';
5+
import type { Server as HTTPServer } from 'http';
6+
import http, { createServer as createHTTPServer } from 'http';
67
import path from 'path';
78
import type { Duplex } from 'stream';
89
import { PassThrough } from 'stream';
@@ -29,6 +30,7 @@ import type { CliReplOptions } from './cli-repl';
2930
import { CliRepl } from './cli-repl';
3031
import { CliReplErrors } from './error-codes';
3132
import type { DevtoolsConnectOptions } from '@mongosh/service-provider-server';
33+
import type { AddressInfo } from 'net';
3234
const { EJSON } = bson;
3335

3436
const delay = promisify(setTimeout);
@@ -138,7 +140,7 @@ describe('CliRepl', function () {
138140
const content = await fs.readFile(path.join(tmpdir.path, 'config'), {
139141
encoding: 'utf8',
140142
});
141-
expect((EJSON.parse(content) as any).enableTelemetry).to.be.false;
143+
expect(EJSON.parse(content).enableTelemetry).to.be.false;
142144
});
143145

144146
it('does not store config options on disk that have not been changed', async function () {
@@ -188,7 +190,7 @@ describe('CliRepl', function () {
188190
const content = await fs.readFile(path.join(tmpdir.path, 'config'), {
189191
encoding: 'utf8',
190192
});
191-
expect((EJSON.parse(content) as any).inspectDepth).equal(Infinity);
193+
expect(EJSON.parse(content).inspectDepth).equal(Infinity);
192194
});
193195

194196
it('emits exit when asked to, Node.js-style', async function () {
@@ -992,6 +994,62 @@ describe('CliRepl', function () {
992994
);
993995
});
994996
});
997+
998+
context('showing update information', function () {
999+
let httpServer: HTTPServer;
1000+
let httpServerUrl: string;
1001+
1002+
beforeEach(async function () {
1003+
httpServer = createHTTPServer((req, res) => {
1004+
res.end(
1005+
JSON.stringify({
1006+
versions: [{ version: '2023.4.15' }],
1007+
})
1008+
);
1009+
});
1010+
httpServer.listen(0);
1011+
await once(httpServer, 'listening');
1012+
httpServerUrl = `http://127.0.0.1:${
1013+
(httpServer.address() as AddressInfo).port
1014+
}`;
1015+
});
1016+
1017+
afterEach(async function () {
1018+
httpServer.close();
1019+
await once(httpServer, 'close');
1020+
});
1021+
1022+
it('does not attempt to load information about new releases with --eval and no explicit --no-quiet', async function () {
1023+
cliReplOptions.shellCliOptions.eval = ['1+1'];
1024+
cliRepl = new CliRepl(cliReplOptions);
1025+
cliRepl.fetchMongoshUpdateUrlRegardlessOfCiEnvironment = true;
1026+
cliRepl.config.updateURL = httpServerUrl;
1027+
let fetchingUpdateMetadataCalls = 0;
1028+
cliRepl.bus.on(
1029+
'mongosh:fetching-update-metadata',
1030+
() => fetchingUpdateMetadataCalls++
1031+
);
1032+
await startWithExpectedImmediateExit(cliRepl, '');
1033+
expect(fetchingUpdateMetadataCalls).to.equal(0);
1034+
});
1035+
1036+
it('does attempt to load information about new releases in --no-quiet mode', async function () {
1037+
cliReplOptions.shellCliOptions.eval = ['1+1'];
1038+
cliReplOptions.shellCliOptions.quiet = false;
1039+
cliRepl = new CliRepl(cliReplOptions);
1040+
cliRepl.fetchMongoshUpdateUrlRegardlessOfCiEnvironment = true;
1041+
cliRepl.config.updateURL = httpServerUrl;
1042+
let fetchingUpdateMetadataCalls = 0;
1043+
cliRepl.bus.on(
1044+
'mongosh:fetching-update-metadata',
1045+
() => fetchingUpdateMetadataCalls++
1046+
);
1047+
const requestPromise = once(httpServer, 'request');
1048+
await startWithExpectedImmediateExit(cliRepl, '');
1049+
expect(fetchingUpdateMetadataCalls).to.equal(1);
1050+
await requestPromise;
1051+
});
1052+
});
9951053
});
9961054

9971055
verifyAutocompletion({

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export class CliRepl implements MongoshIOProvider {
120120
isContainerizedEnvironment = false;
121121
hasOnDiskTelemetryId = false;
122122
updateNotificationManager = new UpdateNotificationManager();
123+
fetchMongoshUpdateUrlRegardlessOfCiEnvironment = false; // for testing
123124

124125
/**
125126
* Instantiate the new CLI Repl.
@@ -1187,11 +1188,13 @@ export class CliRepl implements MongoshIOProvider {
11871188
}
11881189

11891190
async fetchMongoshUpdateUrl() {
1191+
const { quiet } = CliRepl.getFileAndEvalInfo(this.cliOptions);
11901192
if (
1191-
this.cliOptions.quiet ||
1192-
process.env.CI ||
1193-
process.env.IS_CI ||
1194-
this.isContainerizedEnvironment
1193+
quiet ||
1194+
(!this.fetchMongoshUpdateUrlRegardlessOfCiEnvironment &&
1195+
(process.env.CI ||
1196+
process.env.IS_CI ||
1197+
this.isContainerizedEnvironment))
11951198
) {
11961199
// No point in telling users about new versions if we are in
11971200
// a CI or Docker-like environment. or the user has explicitly
@@ -1224,7 +1227,7 @@ export class CliRepl implements MongoshIOProvider {
12241227
}
12251228
}
12261229

1227-
async getMoreRecentMongoshVersion() {
1230+
async getMoreRecentMongoshVersion(): Promise<string | null> {
12281231
const { version } = require('../package.json');
12291232
return await this.updateNotificationManager.getLatestVersionIfMoreRecent(
12301233
process.env

0 commit comments

Comments
 (0)