1- import child_process from ' child_process' ;
2- import { promises as fs } from 'fs' ;
3- import { MongoClient , MongoClientOptions } from ' mongodb' ;
4- import path from ' path' ;
5- import semver from ' semver' ;
6- import { promisify } from ' util' ;
7- import which from ' which' ;
8- import { ConnectionString } from ' mongodb-connection-string-url' ;
9- import { MongoCluster , MongoClusterOptions } from ' mongodb-runner' ;
10- import { downloadCryptLibrary } from ' ../packages/build/src/packaging/download-crypt-library' ;
1+ import child_process from " child_process" ;
2+ import { promises as fs } from "fs" ;
3+ import { MongoClient , MongoClientOptions } from " mongodb" ;
4+ import path from " path" ;
5+ import semver from " semver" ;
6+ import { promisify } from " util" ;
7+ import which from " which" ;
8+ import { ConnectionString } from " mongodb-connection-string-url" ;
9+ import { MongoCluster , MongoClusterOptions } from " mongodb-runner" ;
10+ import { downloadCryptLibrary } from " ../packages/build/src/packaging/download-crypt-library" ;
1111
1212const execFile = promisify ( child_process . execFile ) ;
1313
@@ -20,7 +20,7 @@ function ciLog(...args: any[]) {
2020
2121// Return the path to the temporary directory and ensure that it exists.
2222async function getTmpdir ( ) : Promise < string > {
23- const tmpdir = path . resolve ( __dirname , '..' , ' tmp' ) ;
23+ const tmpdir = path . resolve ( __dirname , ".." , " tmp" ) ;
2424 await fs . mkdir ( tmpdir , { recursive : true } ) ;
2525 return tmpdir ;
2626}
@@ -31,11 +31,11 @@ export class MongodSetup {
3131 _setConnectionString : ( connectionString : string ) => void ;
3232 _serverVersion : string | null = null ;
3333 _isCommunityServer : boolean | null = null ;
34- _bindir = '' ;
34+ _bindir = "" ;
3535
3636 constructor ( connectionString ?: string ) {
37- this . _setConnectionString = ( connectionString : string ) => { } ; // Make TypeScript happy.
38- this . _connectionString = new Promise ( resolve => {
37+ this . _setConnectionString = ( connectionString : string ) => { } ; // Make TypeScript happy.
38+ this . _connectionString = new Promise ( ( resolve ) => {
3939 this . _setConnectionString = resolve ;
4040 } ) ;
4141
@@ -45,15 +45,21 @@ export class MongodSetup {
4545 }
4646
4747 async start ( ) : Promise < void > {
48- throw new Error ( ' Server not managed' ) ;
48+ throw new Error ( " Server not managed" ) ;
4949 }
5050
5151 async stop ( ) : Promise < void > {
52- throw new Error ( ' Server not managed' ) ;
52+ throw new Error ( " Server not managed" ) ;
5353 }
5454
55- async connectionString ( searchParams : Partial < Record < keyof MongoClientOptions , string > > = { } , uriOptions : Partial < ConnectionString > = { } ) : Promise < string > {
56- if ( Object . keys ( searchParams ) . length + Object . keys ( uriOptions ) . length === 0 ) {
55+ async connectionString (
56+ searchParams : Partial < Record < keyof MongoClientOptions , string > > = { } ,
57+ uriOptions : Partial < ConnectionString > = { }
58+ ) : Promise < string > {
59+ if (
60+ Object . keys ( searchParams ) . length + Object . keys ( uriOptions ) . length ===
61+ 0
62+ ) {
5763 return this . _connectionString ;
5864 }
5965
@@ -70,7 +76,7 @@ export class MongodSetup {
7076 }
7177
7278 async port ( ) : Promise < string > {
73- return ( await this . hostport ( ) ) . split ( ':' ) . reverse ( ) [ 0 ] ;
79+ return ( await this . hostport ( ) ) . split ( ":" ) . reverse ( ) [ 0 ] ;
7480 }
7581
7682 async hostport ( ) : Promise < string > {
@@ -82,8 +88,8 @@ export class MongodSetup {
8288 return this . _serverVersion ;
8389 }
8490
85- const { version } = await this . withClient ( async client => {
86- return await client . db ( ' db1' ) . admin ( ) . serverStatus ( ) ;
91+ const { version } = await this . withClient ( async ( client ) => {
92+ return await client . db ( " db1" ) . admin ( ) . serverStatus ( ) ;
8793 } ) ;
8894 this . _serverVersion = version ;
8995 return version ;
@@ -94,10 +100,10 @@ export class MongodSetup {
94100 return this . _isCommunityServer ;
95101 }
96102
97- const { modules } = await this . withClient ( async client => {
98- return await client . db ( ' db1' ) . admin ( ) . command ( { buildInfo : 1 } ) ;
103+ const { modules } = await this . withClient ( async ( client ) => {
104+ return await client . db ( " db1" ) . admin ( ) . command ( { buildInfo : 1 } ) ;
99105 } ) ;
100- const isCommunityServer = ! modules . includes ( ' enterprise' ) ;
106+ const isCommunityServer = ! modules . includes ( " enterprise" ) ;
101107 this . _isCommunityServer = isCommunityServer ;
102108 return isCommunityServer ;
103109 }
@@ -123,8 +129,15 @@ export class MongodSetup {
123129export class MongoRunnerSetup extends MongodSetup {
124130 private static _usedDirPrefix : Record < string , 0 > = { } ;
125131
126- private static _buildDirPath ( id : string , version ?: string , topology ?: string ) {
127- const prefix = [ id , version , topology ] . filter ( Boolean ) . join ( '-' ) . replace ( / [ ^ a - z A - Z 0 - 9 _ . - ] / g, '' ) ;
132+ private static _buildDirPath (
133+ id : string ,
134+ version ?: string ,
135+ topology ?: string
136+ ) {
137+ const prefix = [ id , version , topology ]
138+ . filter ( Boolean )
139+ . join ( "-" )
140+ . replace ( / [ ^ a - z A - Z 0 - 9 _ . - ] / g, "" ) ;
128141
129142 this . _usedDirPrefix [ prefix ] ??= 0 ;
130143
@@ -148,15 +161,19 @@ export class MongoRunnerSetup extends MongodSetup {
148161 if ( this . _cluster ) return ;
149162 const tmpDir = await getTmpdir ( ) ;
150163 const version = process . env . MONGOSH_SERVER_TEST_VERSION ;
151- const dirPath = MongoRunnerSetup . _buildDirPath ( this . _id , version , this . _opts . topology ) ;
164+ const dirPath = MongoRunnerSetup . _buildDirPath (
165+ this . _id ,
166+ version ,
167+ this . _opts . topology
168+ ) ;
152169
153170 this . _cluster = await MongoCluster . start ( {
154- topology : ' standalone' ,
155- tmpDir : path . join ( tmpDir , ' mongodb-runner' , ' dbs' , dirPath ) ,
156- logDir : path . join ( tmpDir , ' mongodb-runner' , ' logs' , dirPath ) ,
171+ topology : " standalone" ,
172+ tmpDir : path . join ( tmpDir , " mongodb-runner" , " dbs" , dirPath ) ,
173+ logDir : path . join ( tmpDir , " mongodb-runner" , " logs" , dirPath ) ,
157174 version : version ,
158- ...this . _opts
159- } )
175+ ...this . _opts ,
176+ } ) ;
160177
161178 this . _setConnectionString ( this . _cluster . connectionString ) ;
162179 }
@@ -168,17 +185,25 @@ export class MongoRunnerSetup extends MongodSetup {
168185}
169186
170187async function getInstalledMongodVersion ( ) : Promise < string > {
171- await Promise . all ( [ which ( 'mongod' ) , which ( 'mongos' ) ] ) ;
172- const { stdout } = await execFile ( 'mongod' , [ '--version' ] ) ;
173- const { version } = stdout . match ( / ^ d b v e r s i o n (?< version > .+ ) $ / m) ! . groups as any ;
188+ await Promise . all ( [ which ( "mongod" ) , which ( "mongos" ) ] ) ;
189+ const { stdout } = await execFile ( "mongod" , [ "--version" ] ) ;
190+ const { version } = stdout . match ( / ^ d b v e r s i o n (?< version > .+ ) $ / m) !
191+ . groups as any ;
174192 return version ;
175193}
176194
177- export async function downloadCurrentCryptSharedLibrary ( versionSpec ?: string ) : Promise < string > {
178- if ( process . platform === 'linux' ) {
179- return ( await downloadCryptLibrary ( `linux-${ process . arch . replace ( 'ppc64' , 'ppc64le' ) } ` as any , versionSpec ) ) . cryptLibrary ;
195+ export async function downloadCurrentCryptSharedLibrary (
196+ versionSpec ?: string
197+ ) : Promise < string > {
198+ if ( process . platform === "linux" ) {
199+ return (
200+ await downloadCryptLibrary (
201+ `linux-${ process . arch . replace ( "ppc64" , "ppc64le" ) } ` as any ,
202+ versionSpec
203+ )
204+ ) . cryptLibrary ;
180205 }
181- return ( await downloadCryptLibrary ( ' host' , versionSpec ) ) . cryptLibrary ;
206+ return ( await downloadCryptLibrary ( " host" , versionSpec ) ) . cryptLibrary ;
182207}
183208
184209/**
@@ -188,23 +213,25 @@ export async function downloadCurrentCryptSharedLibrary(versionSpec?: string): P
188213 * @export
189214 * @returns {MongodSetup } - Object with information about the started server.
190215 */
191- let sharedSetup : MongodSetup | null = null ;
192- export function startTestServer ( id : string , args : Partial < MongoClusterOptions > = { } ) : MongodSetup {
216+ let sharedSetup : MongodSetup | null = null ;
217+ export function startTestServer (
218+ id : string ,
219+ args : Partial < MongoClusterOptions > = { }
220+ ) : MongodSetup {
193221 const server = new MongoRunnerSetup ( id , args ) ;
194- before ( async function ( ) {
195- this . timeout ( 120_000 ) ; // Include potential mongod download time.
222+ before ( async function ( ) {
223+ this . timeout ( 120_000 ) ; // Include potential mongod download time.
196224 await server . start ( ) ;
197225 } ) ;
198226
199- after ( async function ( ) {
227+ after ( async function ( ) {
200228 this . timeout ( 30_000 ) ;
201229 await server . stop ( ) ;
202230 } ) ;
203231
204232 return server ;
205233}
206234
207-
208235/**
209236 * Starts or reuse an existing shared local server managed by this process.
210237 *
@@ -221,10 +248,10 @@ export function startSharedTestServer(): MongodSetup {
221248 return new MongodSetup ( process . env . MONGOSH_TEST_SERVER_URL ) ;
222249 }
223250
224- const server = sharedSetup ?? ( sharedSetup = new MongoRunnerSetup ( ' shared' ) ) ;
251+ const server = sharedSetup ?? ( sharedSetup = new MongoRunnerSetup ( " shared" ) ) ;
225252
226- before ( async function ( ) {
227- this . timeout ( 120_000 ) ; // Include potential mongod download time.
253+ before ( async function ( ) {
254+ this . timeout ( 120_000 ) ; // Include potential mongod download time.
228255 await server . start ( ) ;
229256 } ) ;
230257
@@ -233,7 +260,7 @@ export function startSharedTestServer(): MongodSetup {
233260 return server ;
234261}
235262
236- global . after ?.( async function ( ) {
263+ global . after ?.( async function ( ) {
237264 if ( sharedSetup !== null ) {
238265 this . timeout ( 30_000 ) ;
239266 await sharedSetup . stop ( ) ;
@@ -242,24 +269,35 @@ global.after?.(async function() {
242269
243270// The same as startTestServer(), except that this starts multiple servers
244271// in parallel in the same before() call.
245- export function startTestCluster ( id : string , ...argLists : Partial < MongoClusterOptions > [ ] ) : MongodSetup [ ] {
246- const servers = argLists . map ( args => new MongoRunnerSetup ( id , args ) ) ;
272+ export function startTestCluster (
273+ id : string ,
274+ ...argLists : Partial < MongoClusterOptions > [ ]
275+ ) : MongodSetup [ ] {
276+ const servers = argLists . map ( ( args ) => new MongoRunnerSetup ( id , args ) ) ;
247277
248- before ( async function ( ) {
278+ before ( async function ( ) {
249279 this . timeout ( 90_000 + 30_000 * servers . length ) ;
250280 await Promise . all ( servers . map ( ( server : MongodSetup ) => server . start ( ) ) ) ;
251281 } ) ;
252282
253- after ( async function ( ) {
283+ after ( async function ( ) {
254284 this . timeout ( 30_000 * servers . length ) ;
255285 await Promise . all ( servers . map ( ( server : MongodSetup ) => server . stop ( ) ) ) ;
256286 } ) ;
257287
258288 return servers ;
259289}
260290
261- function skipIfVersion ( test : any , testServerVersion : string , semverCondition : string ) : void {
262- if ( semver . satisfies ( testServerVersion , semverCondition , { includePrerelease : true } ) ) {
291+ function skipIfVersion (
292+ test : any ,
293+ testServerVersion : string ,
294+ semverCondition : string
295+ ) : void {
296+ if (
297+ semver . satisfies ( testServerVersion , semverCondition , {
298+ includePrerelease : true ,
299+ } )
300+ ) {
263301 test . skip ( ) ;
264302 }
265303}
@@ -272,8 +310,11 @@ function skipIfVersion(test: any, testServerVersion: string, semverCondition: st
272310 * e.g. skipIfServerVersion(testServer, '< 4.4')
273311 * });
274312 */
275- export function skipIfServerVersion ( server : MongodSetup , semverCondition : string ) : void {
276- before ( async function ( ) {
313+ export function skipIfServerVersion (
314+ server : MongodSetup ,
315+ semverCondition : string
316+ ) : void {
317+ before ( async function ( ) {
277318 skipIfVersion ( this , await server . serverVersion ( ) , semverCondition ) ;
278319 } ) ;
279320}
@@ -286,7 +327,7 @@ export function skipIfServerVersion(server: MongodSetup, semverCondition: string
286327 * });
287328 */
288329export function skipIfCommunityServer ( server : MongodSetup ) : void {
289- before ( async function ( ) {
330+ before ( async function ( ) {
290331 if ( await server . isCommunityServer ( ) ) {
291332 this . skip ( ) ;
292333 }
@@ -298,7 +339,7 @@ export function skipIfCommunityServer(server: MongodSetup): void {
298339 * --apiStrict.
299340 */
300341export function skipIfApiStrict ( ) : void {
301- before ( function ( ) {
342+ before ( function ( ) {
302343 if ( process . env . MONGOSH_TEST_FORCE_API_STRICT ) {
303344 this . skip ( ) ;
304345 }
@@ -320,19 +361,21 @@ export function skipIfApiStrict(): void {
320361 * @param semverCondition Semver condition
321362 */
322363export function skipIfEnvServerVersion ( semverCondition : string ) : void {
323- before ( async function ( ) {
364+ before ( async function ( ) {
324365 let testServerVersion = process . env . MONGOSH_SERVER_TEST_VERSION ;
325366 if ( ! testServerVersion ) {
326367 try {
327368 testServerVersion = await getInstalledMongodVersion ( ) ;
328- } catch ( e : any ) {
369+ } catch ( e : any ) {
329370 // no explicitly specified version but also no local mongod installation
330- testServerVersion = ' 9999.9999.9999' ;
371+ testServerVersion = " 9999.9999.9999" ;
331372 }
332373 } else {
333- testServerVersion = testServerVersion . split ( '-' ) [ 0 ] . split ( '.' )
334- . map ( num => / [ 0 - 9 ] + / . test ( num ) ? num : '0' )
335- . join ( '.' ) ;
374+ testServerVersion = testServerVersion
375+ . split ( "-" ) [ 0 ]
376+ . split ( "." )
377+ . map ( ( num ) => ( / [ 0 - 9 ] + / . test ( num ) ? num : "0" ) )
378+ . join ( "." ) ;
336379 }
337380 skipIfVersion ( this , testServerVersion , semverCondition ) ;
338381 } ) ;
0 commit comments