Skip to content

Commit 5d1188a

Browse files
committed
Cleanup on exit
1 parent 8886861 commit 5d1188a

File tree

5 files changed

+184
-177
lines changed

5 files changed

+184
-177
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import { SUPPORTED_PACKAGES } from './packages';
99
import { testTimeToFirstQuery } from './tests/time-to-first-query';
1010
import { testAutoUpdateFrom } from './tests/auto-update-from';
1111
import { testAutoUpdateTo } from './tests/auto-update-to';
12+
import {
13+
deleteSandboxesDirectory,
14+
sandboxesDirectoryExists,
15+
} from './directories';
1216

1317
const debug = createDebug('compass:smoketests');
1418

@@ -92,7 +96,7 @@ const argv = yargs(hideBin(process.argv))
9296
})
9397
.option('skipCleanup', {
9498
type: 'boolean',
95-
description: 'Do not delete the sandbox after a run',
99+
description: 'Do not delete the sandboxes after a run',
96100
default: false,
97101
})
98102
.option('tests', {
@@ -106,6 +110,27 @@ const argv = yargs(hideBin(process.argv))
106110
async function run() {
107111
const context: SmokeTestsContext = argv.parseSync();
108112

113+
function cleanupMaybe() {
114+
if (context.skipCleanup) {
115+
console.log('Skipped cleanup of sandboxes');
116+
} else {
117+
debug('Cleaning up sandboxes');
118+
try {
119+
deleteSandboxesDirectory();
120+
} catch (err) {
121+
if (err instanceof Error) {
122+
console.warn(`Failed cleaning sandboxes: ${err.message}`);
123+
} else {
124+
throw err;
125+
}
126+
}
127+
}
128+
}
129+
130+
process.once('SIGTERM', cleanupMaybe);
131+
process.once('SIGINT', cleanupMaybe);
132+
process.once('exit', cleanupMaybe);
133+
109134
debug(`Running tests`);
110135

111136
debug(

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ import crypto from 'node:crypto';
33
import fs from 'node:fs';
44
import path from 'node:path';
55

6+
const SANDBOXES_PATH = path.resolve(__dirname, '../.sandboxes');
7+
8+
export function deleteSandboxesDirectory() {
9+
fs.rmSync(SANDBOXES_PATH, { recursive: true, force: true });
10+
}
11+
12+
export function sandboxesDirectoryExists() {
13+
return fs.existsSync(SANDBOXES_PATH);
14+
}
15+
616
function ensureSandboxesDirectory() {
7-
const sandboxesPath = path.resolve(__dirname, '../.sandboxes');
8-
if (!fs.existsSync(sandboxesPath)) {
9-
fs.mkdirSync(sandboxesPath, { recursive: true });
17+
if (!sandboxesDirectoryExists()) {
18+
fs.mkdirSync(SANDBOXES_PATH, { recursive: true });
1019
}
11-
return sandboxesPath;
20+
return SANDBOXES_PATH;
1221
}
1322

1423
export function createSandbox() {

packages/compass-smoke-tests/src/tests/auto-update-from.ts

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,66 +19,57 @@ export async function testAutoUpdateFrom(context: SmokeTestsContext) {
1919

2020
const { kind, filepath, autoUpdatable } = subject;
2121

22-
try {
23-
const install = getInstaller(kind);
22+
const install = getInstaller(kind);
2423

25-
const { appName, appPath, uninstall } = install({
26-
...subject,
27-
filepath,
28-
sandboxPath,
29-
});
24+
const { appName, appPath, uninstall } = install({
25+
...subject,
26+
filepath,
27+
sandboxPath,
28+
});
3029

31-
try {
32-
process.env.PORT = '0'; // dynamic port
33-
process.env.UPDATE_CHECKER_ALLOW_DOWNGRADES = 'true';
30+
try {
31+
process.env.PORT = '0'; // dynamic port
32+
process.env.UPDATE_CHECKER_ALLOW_DOWNGRADES = 'true';
3433

35-
const server = await startAutoUpdateServer();
34+
const server = await startAutoUpdateServer();
3635

37-
const address = server.address();
38-
assert(typeof address === 'object' && address !== null);
39-
const port = address.port;
40-
const HADRON_AUTO_UPDATE_ENDPOINT_OVERRIDE = `http://localhost:${port}`;
36+
const address = server.address();
37+
assert(typeof address === 'object' && address !== null);
38+
const port = address.port;
39+
const HADRON_AUTO_UPDATE_ENDPOINT_OVERRIDE = `http://localhost:${port}`;
4140

42-
try {
43-
// must be async because the update server is running in the same process
44-
await executeAsync(
45-
'npm',
46-
[
47-
'run',
48-
'--unsafe-perm',
49-
'test-packaged',
50-
'--workspace',
51-
'compass-e2e-tests',
52-
'--',
53-
'--test-filter=auto-update',
54-
],
55-
{
56-
// We need to use a shell to get environment variables setup correctly
57-
shell: true,
58-
env: {
59-
...process.env,
60-
HADRON_AUTO_UPDATE_ENDPOINT_OVERRIDE,
61-
AUTO_UPDATE_UPDATABLE: (!!autoUpdatable).toString(),
62-
TEST_NAME: 'auto-update-from',
63-
COMPASS_APP_NAME: appName,
64-
COMPASS_APP_PATH: appPath,
65-
},
66-
}
67-
);
68-
} finally {
69-
debug('Stopping auto-update server');
70-
server.close();
71-
delete process.env.UPDATE_CHECKER_ALLOW_DOWNGRADES;
72-
}
41+
try {
42+
// must be async because the update server is running in the same process
43+
await executeAsync(
44+
'npm',
45+
[
46+
'run',
47+
'--unsafe-perm',
48+
'test-packaged',
49+
'--workspace',
50+
'compass-e2e-tests',
51+
'--',
52+
'--test-filter=auto-update',
53+
],
54+
{
55+
// We need to use a shell to get environment variables setup correctly
56+
shell: true,
57+
env: {
58+
...process.env,
59+
HADRON_AUTO_UPDATE_ENDPOINT_OVERRIDE,
60+
AUTO_UPDATE_UPDATABLE: (!!autoUpdatable).toString(),
61+
TEST_NAME: 'auto-update-from',
62+
COMPASS_APP_NAME: appName,
63+
COMPASS_APP_PATH: appPath,
64+
},
65+
}
66+
);
7367
} finally {
74-
await uninstall();
68+
debug('Stopping auto-update server');
69+
server.close();
70+
delete process.env.UPDATE_CHECKER_ALLOW_DOWNGRADES;
7571
}
7672
} finally {
77-
if (context.skipCleanup) {
78-
debug(`Skipped cleaning up sandbox: ${sandboxPath}`);
79-
} else {
80-
debug(`Cleaning up sandbox: ${sandboxPath}`);
81-
fs.rmSync(sandboxPath, { recursive: true });
82-
}
73+
await uninstall();
8374
}
8475
}

packages/compass-smoke-tests/src/tests/auto-update-to.ts

Lines changed: 73 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -23,98 +23,89 @@ export async function testAutoUpdateTo(context: SmokeTestsContext) {
2323
buildInfo: { channel, version },
2424
} = subject;
2525

26-
try {
27-
// Derive the kind of package needed to install the latest release
28-
const latestApp = getTestSubjectDetails({
29-
...context,
30-
sandboxPath,
31-
package: getLatestReleaseKindByKind(subject.kind),
32-
});
26+
// Derive the kind of package needed to install the latest release
27+
const latestApp = getTestSubjectDetails({
28+
...context,
29+
sandboxPath,
30+
package: getLatestReleaseKindByKind(subject.kind),
31+
});
3332

34-
assert.equal(latestApp.buildInfo.channel, subject.buildInfo.channel);
33+
assert.equal(latestApp.buildInfo.channel, subject.buildInfo.channel);
3534

36-
const install = getInstaller(latestApp.kind);
37-
const filepath = await getLatestRelease(
38-
channel,
39-
context.arch,
40-
latestApp.kind,
41-
context.forceDownload
42-
);
35+
const install = getInstaller(latestApp.kind);
36+
const filepath = await getLatestRelease(
37+
channel,
38+
context.arch,
39+
latestApp.kind,
40+
context.forceDownload
41+
);
4342

44-
const { appPath, appName, uninstall } = install({
45-
...latestApp,
46-
filepath,
47-
filename: subject.filename,
48-
sandboxPath,
49-
});
43+
const { appPath, appName, uninstall } = install({
44+
...latestApp,
45+
filepath,
46+
filename: subject.filename,
47+
sandboxPath,
48+
});
5049

51-
try {
52-
process.env.PORT = '0'; // dynamic port
53-
process.env.UPDATE_CHECKER_ALLOW_DOWNGRADES = 'true';
50+
try {
51+
process.env.PORT = '0'; // dynamic port
52+
process.env.UPDATE_CHECKER_ALLOW_DOWNGRADES = 'true';
5453

55-
if (channel === 'dev') {
56-
process.env.DEV_RELEASE = JSON.stringify({
57-
version: version,
58-
bucket_key_prefix: context.bucketKeyPrefix,
59-
});
60-
} else {
61-
process.env.PUBLISHED_RELEASES = JSON.stringify({
62-
name: version,
63-
body: version,
64-
bucket_key_prefix: context.bucketKeyPrefix,
65-
});
66-
}
54+
if (channel === 'dev') {
55+
process.env.DEV_RELEASE = JSON.stringify({
56+
version: version,
57+
bucket_key_prefix: context.bucketKeyPrefix,
58+
});
59+
} else {
60+
process.env.PUBLISHED_RELEASES = JSON.stringify({
61+
name: version,
62+
body: version,
63+
bucket_key_prefix: context.bucketKeyPrefix,
64+
});
65+
}
6766

68-
const server = await startAutoUpdateServer();
67+
const server = await startAutoUpdateServer();
6968

70-
const address = server.address();
71-
assert(typeof address === 'object' && address !== null);
72-
const port = address.port;
73-
const HADRON_AUTO_UPDATE_ENDPOINT_OVERRIDE = `http://localhost:${port}`;
69+
const address = server.address();
70+
assert(typeof address === 'object' && address !== null);
71+
const port = address.port;
72+
const HADRON_AUTO_UPDATE_ENDPOINT_OVERRIDE = `http://localhost:${port}`;
7473

75-
try {
76-
// must be async because the update server is running in the same process
77-
await executeAsync(
78-
'npm',
79-
[
80-
'run',
81-
'--unsafe-perm',
82-
'test-packaged',
83-
'--workspace',
84-
'compass-e2e-tests',
85-
'--',
86-
'--test-filter=auto-update',
87-
],
88-
{
89-
// We need to use a shell to get environment variables setup correctly
90-
shell: true,
91-
env: {
92-
...process.env,
93-
HADRON_AUTO_UPDATE_ENDPOINT_OVERRIDE,
94-
AUTO_UPDATE_UPDATABLE: (!!subject.autoUpdatable).toString(),
95-
TEST_NAME: 'auto-update-to',
96-
EXPECTED_UPDATE_VERSION: version,
97-
COMPASS_APP_NAME: appName,
98-
COMPASS_APP_PATH: appPath,
99-
},
100-
}
101-
);
102-
} finally {
103-
debug('Stopping auto-update server');
104-
server.close();
105-
delete process.env.DEV_RELEASE;
106-
delete process.env.PUBLISHED_RELEASES;
107-
delete process.env.UPDATE_CHECKER_ALLOW_DOWNGRADES;
108-
}
74+
try {
75+
// must be async because the update server is running in the same process
76+
await executeAsync(
77+
'npm',
78+
[
79+
'run',
80+
'--unsafe-perm',
81+
'test-packaged',
82+
'--workspace',
83+
'compass-e2e-tests',
84+
'--',
85+
'--test-filter=auto-update',
86+
],
87+
{
88+
// We need to use a shell to get environment variables setup correctly
89+
shell: true,
90+
env: {
91+
...process.env,
92+
HADRON_AUTO_UPDATE_ENDPOINT_OVERRIDE,
93+
AUTO_UPDATE_UPDATABLE: (!!subject.autoUpdatable).toString(),
94+
TEST_NAME: 'auto-update-to',
95+
EXPECTED_UPDATE_VERSION: version,
96+
COMPASS_APP_NAME: appName,
97+
COMPASS_APP_PATH: appPath,
98+
},
99+
}
100+
);
109101
} finally {
110-
await uninstall();
102+
debug('Stopping auto-update server');
103+
server.close();
104+
delete process.env.DEV_RELEASE;
105+
delete process.env.PUBLISHED_RELEASES;
106+
delete process.env.UPDATE_CHECKER_ALLOW_DOWNGRADES;
111107
}
112108
} finally {
113-
if (context.skipCleanup) {
114-
debug(`Skipped cleaning up sandbox: ${sandboxPath}`);
115-
} else {
116-
debug(`Cleaning up sandbox: ${sandboxPath}`);
117-
fs.rmSync(sandboxPath, { recursive: true });
118-
}
109+
await uninstall();
119110
}
120111
}

0 commit comments

Comments
 (0)