Skip to content

Commit b0f06a8

Browse files
committed
run the update server programmatically
1 parent 57f87f0 commit b0f06a8

File tree

6 files changed

+97
-62
lines changed

6 files changed

+97
-62
lines changed

.evergreen/functions.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,9 @@ functions:
677677
echo "Using g++ version:"
678678
g++ --version;
679679
npm ci --engine-strict=false
680+
npm link --engine-strict=false
681+
cd ../compass
682+
npm link compass-mongodb-com --engine-strict=false
680683
681684
smoketest-packaged-app:
682685
- command: shell.exec

packages/compass-smoke-tests/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,20 @@
3434
"@mongodb-js/eslint-config-compass": "^1.2.5",
3535
"@mongodb-js/prettier-config-compass": "^1.1.5",
3636
"@mongodb-js/tsconfig-compass": "^1.1.5",
37-
"cross-spawn": "^7.0.5",
3837
"depcheck": "^1.4.1",
3938
"eslint": "^7.25.0",
4039
"hadron-build": "^25.6.5",
4140
"lodash": "^4.17.21",
4241
"prettier": "^2.7.1",
43-
"tree-kill": "^1.2.2",
4442
"typescript": "^5.0.4",
4543
"yargs": "^17.7.2"
44+
},
45+
"peerDependencies": {
46+
"compass-mongodb-com": "*"
47+
},
48+
"peerDependenciesMeta": {
49+
"compass-mongodb-com": {
50+
"optional": true
51+
}
4652
}
4753
}

packages/compass-smoke-tests/src/cli.ts

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
import assert from 'node:assert/strict';
33
import fs from 'node:fs';
44
import path from 'node:path';
5+
import { once } from 'node:events';
56

6-
import crossSpawn from 'cross-spawn';
7-
import kill from 'tree-kill';
87
import yargs from 'yargs';
98
import { hideBin } from 'yargs/helpers';
109
import { pick } from 'lodash';
11-
import { execute } from './execute';
10+
import { execute, executeAsync } from './execute';
1211
import {
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

291267
type 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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare module 'compass-mongodb-com' {
2+
import type http from 'http';
3+
function updateServer(): {
4+
start: () => void;
5+
httpServer: http.Server;
6+
updateChecker: NodeJS.EventEmitter;
7+
};
8+
export = updateServer;
9+
}

packages/compass-smoke-tests/src/execute.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { spawnSync, type SpawnOptions } from 'node:child_process';
1+
import { spawn, spawnSync, type SpawnOptions } from 'node:child_process';
22

33
export class ExecuteFailure extends Error {
44
constructor(
@@ -13,16 +13,11 @@ export class ExecuteFailure extends Error {
1313
super(`${commandDetails} exited with ${statusDetails} ${signalDetails}`);
1414
}
1515
}
16-
1716
export function execute(
1817
command: string,
1918
args: string[],
2019
options?: SpawnOptions
2120
) {
22-
// print the command so that when it outputs to stdout we can see where it
23-
// comes from
24-
console.log(command, args);
25-
2621
const { status, signal } = spawnSync(command, args, {
2722
stdio: 'inherit',
2823
...options,
@@ -31,3 +26,40 @@ export function execute(
3126
throw new ExecuteFailure(command, args, status, signal);
3227
}
3328
}
29+
30+
export function executeAsync(
31+
command: string,
32+
args: string[],
33+
options?: SpawnOptions
34+
): Promise<void> {
35+
return new Promise((resolve, reject) => {
36+
console.log(command, ...args);
37+
const child = spawn(command, args, {
38+
stdio: 'inherit',
39+
...options,
40+
});
41+
const killChild = () => child.kill();
42+
const interruptChild = () => child.kill('SIGINT');
43+
process.once('exit', killChild);
44+
process.once('SIGINT', interruptChild);
45+
child.once('error', reject);
46+
child.once('exit', (code, signal) => {
47+
process.off('exit', killChild);
48+
process.off('SIGINT', interruptChild);
49+
if (code !== null) {
50+
if (code === 0) {
51+
resolve();
52+
} else {
53+
reject(new ExecuteFailure(command, args, code, null));
54+
}
55+
} else {
56+
if (signal !== null) {
57+
reject(new ExecuteFailure(command, args, null, signal));
58+
} else {
59+
// shouldn't happen
60+
reject(new ExecuteFailure(command, args, null, null));
61+
}
62+
}
63+
});
64+
});
65+
}

packages/compass-smoke-tests/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
"compilerOptions": {
44
"noEmit": true,
55
"types": ["node"],
6-
"lib": ["ES2021"]
6+
"lib": ["ES2021"],
7+
"module": "NodeNext",
8+
"paths": {
9+
"compass-mongodb-com": ["./src/compass-mongodb-com.d.ts"]
10+
}
711
},
812
"include": ["src/**/*"]
913
}

0 commit comments

Comments
 (0)