22import assert from 'node:assert/strict' ;
33import fs from 'node:fs' ;
44import path from 'node:path' ;
5+ import { once } from 'node:events' ;
56
6- import crossSpawn from 'cross-spawn' ;
7- import kill from 'tree-kill' ;
87import yargs from 'yargs' ;
98import { hideBin } from 'yargs/helpers' ;
109import { pick } from 'lodash' ;
11- import { execute } from './execute' ;
10+ import { execute , executeAsync } from './execute' ;
1211import {
1312 type PackageDetails ,
1413 readPackageDetails ,
@@ -226,7 +225,7 @@ async function run() {
226225 } ) ;
227226 }
228227 if ( testName === 'auto-update-from' ) {
229- runUpdateTest ( {
228+ await runUpdateTest ( {
230229 appName,
231230 appPath,
232231 autoUpdatable,
@@ -247,45 +246,22 @@ async function run() {
247246 }
248247}
249248
250- type AutoUpdateServerOptions = {
251- port : number ;
252- allowDowngrades ?: boolean ;
253- } ;
254-
255- function startAutoUpdateServer ( {
256- port,
257- allowDowngrades,
258- } : AutoUpdateServerOptions ) {
259- const env : Record < string , string > = {
260- ...process . env ,
261- PORT : port . toString ( ) ,
262- } ;
263- if ( allowDowngrades ) {
264- env . UPDATE_CHECKER_ALLOW_DOWNGRADES = 'true' ;
249+ async function importUpdateServer ( ) {
250+ try {
251+ return ( await import ( 'compass-mongodb-com' ) ) . default ;
252+ } catch ( err : unknown ) {
253+ console . log ( 'Remember to npm link compass-mongodb-com' ) ;
254+ throw err ;
265255 }
256+ }
266257
267- // a git repo that is not published to npm that evergreen clones for us in CI
268- // next to the Compass code
269- const cwd = path . join (
270- __dirname ,
271- '..' ,
272- '..' ,
273- '..' ,
274- '..' ,
275- 'compass-mongodb-com'
276- ) ;
277-
278- if ( ! fs . existsSync ( cwd ) ) {
279- throw new Error ( `compass-mongodb-com does not exist: ${ cwd } ` ) ;
280- }
258+ async function startAutoUpdateServer ( ) {
259+ console . log ( 'Starting auto-update server' ) ;
260+ const { httpServer, updateChecker, start } = ( await importUpdateServer ( ) ) ( ) ;
261+ start ( ) ;
262+ await once ( updateChecker , 'refreshed' ) ;
281263
282- console . log ( 'Starting auto-update server' , cwd ) ;
283- return crossSpawn . spawn ( 'npm' , [ 'run' , 'start' ] , {
284- env,
285- cwd,
286- stdio : 'inherit' ,
287- shell : true ,
288- } ) ;
264+ return httpServer ;
289265}
290266
291267type RunE2ETestOptions = {
@@ -324,18 +300,26 @@ type RunUpdateTestOptions = {
324300 testName : string ;
325301} ;
326302
327- function runUpdateTest ( {
303+ async function runUpdateTest ( {
328304 appName,
329305 appPath,
330306 autoUpdatable,
331307 testName,
332308} : RunUpdateTestOptions ) {
333- const server = startAutoUpdateServer ( {
334- allowDowngrades : true ,
335- port : 8080 ,
336- } ) ;
309+ process . env . PORT = '0' ; // dynamic port
310+ process . env . UPDATE_CHECKER_ALLOW_DOWNGRADES = 'true' ;
311+
312+ const server = await startAutoUpdateServer ( ) ;
313+
314+ const address = server . address ( ) ;
315+ assert ( typeof address === 'object' && address !== null ) ;
316+ const port = address . port ;
317+ const HADRON_AUTO_UPDATE_ENDPOINT_OVERRIDE = `http://localhost:${ port } ` ;
318+ console . log ( { HADRON_AUTO_UPDATE_ENDPOINT_OVERRIDE } ) ;
319+
337320 try {
338- execute (
321+ // must be async because the update server is running in the same process
322+ await executeAsync (
339323 'npm' ,
340324 [
341325 'run' ,
@@ -351,7 +335,7 @@ function runUpdateTest({
351335 shell : true ,
352336 env : {
353337 ...process . env ,
354- HADRON_AUTO_UPDATE_ENDPOINT_OVERRIDE : 'http://localhost:8080' ,
338+ HADRON_AUTO_UPDATE_ENDPOINT_OVERRIDE ,
355339 AUTO_UPDATE_UPDATABLE : ( ! ! autoUpdatable ) . toString ( ) ,
356340 TEST_NAME : testName ,
357341 COMPASS_APP_NAME : appName ,
@@ -360,12 +344,9 @@ function runUpdateTest({
360344 }
361345 ) ;
362346 } finally {
363- if ( server . pid ) {
364- console . log ( 'Stopping auto-update server' ) ;
365- kill ( server . pid , 'SIGINT' ) ;
366- } else {
367- console . log ( 'cannnot stop auto-update server because no pid' ) ;
368- }
347+ console . log ( 'Stopping auto-update server' ) ;
348+ server . close ( ) ;
349+ delete process . env . UPDATE_CHECKER_ALLOW_DOWNGRADES ;
369350 }
370351}
371352
0 commit comments