@@ -2,7 +2,8 @@ import { MongoshInternalError } from '@mongosh/errors';
22import { bson } from '@mongosh/service-provider-core' ;
33import { once } from 'events' ;
44import { 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' ;
67import path from 'path' ;
78import type { Duplex } from 'stream' ;
89import { PassThrough } from 'stream' ;
@@ -29,6 +30,7 @@ import type { CliReplOptions } from './cli-repl';
2930import { CliRepl } from './cli-repl' ;
3031import { CliReplErrors } from './error-codes' ;
3132import type { DevtoolsConnectOptions } from '@mongosh/service-provider-server' ;
33+ import type { AddressInfo } from 'net' ;
3234const { EJSON } = bson ;
3335
3436const 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 ( {
0 commit comments