Skip to content

Commit

Permalink
fix(cli-repl): properly skip update notification fetches in quiet mode
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax authored Apr 3, 2024
1 parent a38a173 commit 81d0eac
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
64 changes: 61 additions & 3 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { MongoshInternalError } from '@mongosh/errors';
import { bson } from '@mongosh/service-provider-core';
import { once } from 'events';
import { promises as fs } from 'fs';
import http from 'http';
import type { Server as HTTPServer } from 'http';
import http, { createServer as createHTTPServer } from 'http';
import path from 'path';
import type { Duplex } from 'stream';
import { PassThrough } from 'stream';
Expand All @@ -29,6 +30,7 @@ import type { CliReplOptions } from './cli-repl';
import { CliRepl } from './cli-repl';
import { CliReplErrors } from './error-codes';
import type { DevtoolsConnectOptions } from '@mongosh/service-provider-server';
import type { AddressInfo } from 'net';
const { EJSON } = bson;

const delay = promisify(setTimeout);
Expand Down Expand Up @@ -138,7 +140,7 @@ describe('CliRepl', function () {
const content = await fs.readFile(path.join(tmpdir.path, 'config'), {
encoding: 'utf8',
});
expect((EJSON.parse(content) as any).enableTelemetry).to.be.false;
expect(EJSON.parse(content).enableTelemetry).to.be.false;
});

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

it('emits exit when asked to, Node.js-style', async function () {
Expand Down Expand Up @@ -992,6 +994,62 @@ describe('CliRepl', function () {
);
});
});

context('showing update information', function () {
let httpServer: HTTPServer;
let httpServerUrl: string;

beforeEach(async function () {
httpServer = createHTTPServer((req, res) => {
res.end(
JSON.stringify({
versions: [{ version: '2023.4.15' }],
})
);
});
httpServer.listen(0);
await once(httpServer, 'listening');
httpServerUrl = `http://127.0.0.1:${
(httpServer.address() as AddressInfo).port
}`;
});

afterEach(async function () {
httpServer.close();
await once(httpServer, 'close');
});

it('does not attempt to load information about new releases with --eval and no explicit --no-quiet', async function () {
cliReplOptions.shellCliOptions.eval = ['1+1'];
cliRepl = new CliRepl(cliReplOptions);
cliRepl.fetchMongoshUpdateUrlRegardlessOfCiEnvironment = true;
cliRepl.config.updateURL = httpServerUrl;
let fetchingUpdateMetadataCalls = 0;
cliRepl.bus.on(
'mongosh:fetching-update-metadata',
() => fetchingUpdateMetadataCalls++
);
await startWithExpectedImmediateExit(cliRepl, '');
expect(fetchingUpdateMetadataCalls).to.equal(0);
});

it('does attempt to load information about new releases in --no-quiet mode', async function () {
cliReplOptions.shellCliOptions.eval = ['1+1'];
cliReplOptions.shellCliOptions.quiet = false;
cliRepl = new CliRepl(cliReplOptions);
cliRepl.fetchMongoshUpdateUrlRegardlessOfCiEnvironment = true;
cliRepl.config.updateURL = httpServerUrl;
let fetchingUpdateMetadataCalls = 0;
cliRepl.bus.on(
'mongosh:fetching-update-metadata',
() => fetchingUpdateMetadataCalls++
);
const requestPromise = once(httpServer, 'request');
await startWithExpectedImmediateExit(cliRepl, '');
expect(fetchingUpdateMetadataCalls).to.equal(1);
await requestPromise;
});
});
});

verifyAutocompletion({
Expand Down
13 changes: 8 additions & 5 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class CliRepl implements MongoshIOProvider {
isContainerizedEnvironment = false;
hasOnDiskTelemetryId = false;
updateNotificationManager = new UpdateNotificationManager();
fetchMongoshUpdateUrlRegardlessOfCiEnvironment = false; // for testing

/**
* Instantiate the new CLI Repl.
Expand Down Expand Up @@ -1187,11 +1188,13 @@ export class CliRepl implements MongoshIOProvider {
}

async fetchMongoshUpdateUrl() {
const { quiet } = CliRepl.getFileAndEvalInfo(this.cliOptions);
if (
this.cliOptions.quiet ||
process.env.CI ||
process.env.IS_CI ||
this.isContainerizedEnvironment
quiet ||
(!this.fetchMongoshUpdateUrlRegardlessOfCiEnvironment &&
(process.env.CI ||
process.env.IS_CI ||
this.isContainerizedEnvironment))
) {
// No point in telling users about new versions if we are in
// a CI or Docker-like environment. or the user has explicitly
Expand Down Expand Up @@ -1224,7 +1227,7 @@ export class CliRepl implements MongoshIOProvider {
}
}

async getMoreRecentMongoshVersion() {
async getMoreRecentMongoshVersion(): Promise<string | null> {
const { version } = require('../package.json');
return await this.updateNotificationManager.getLatestVersionIfMoreRecent(
process.env
Expand Down

0 comments on commit 81d0eac

Please sign in to comment.