Skip to content

Commit 56d0879

Browse files
committed
refactor
1 parent 1011053 commit 56d0879

File tree

8 files changed

+329
-296
lines changed

8 files changed

+329
-296
lines changed

packages/compass-smoke-tests/src/build-info.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import path from 'node:path';
55
import { handler as writeBuildInfo } from 'hadron-build/commands/info';
66

77
import { type PackageKind } from './packages';
8-
import { type SmokeTestsContext } from './context';
8+
import { type SmokeTestsContextWithSandbox } from './context';
99
import { pick } from 'lodash';
1010

1111
const SUPPORTED_CHANNELS = ['dev', 'beta', 'stable'] as const;
@@ -232,7 +232,7 @@ export function readPackageDetails(
232232
}
233233

234234
export function writeAndReadPackageDetails(
235-
context: SmokeTestsContext
235+
context: SmokeTestsContextWithSandbox
236236
): PackageDetails {
237237
const compassDir = path.resolve(__dirname, '../../compass');
238238
const infoArgs = {

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

Lines changed: 11 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
#!/usr/bin/env npx ts-node
2-
import assert from 'node:assert/strict';
3-
import fs from 'node:fs';
4-
import path from 'node:path';
5-
62
import yargs from 'yargs';
73
import { hideBin } from 'yargs/helpers';
84
import { pick } from 'lodash';
9-
import {
10-
type PackageDetails,
11-
readPackageDetails,
12-
writeAndReadPackageDetails,
13-
} from './build-info';
14-
import { createSandbox } from './directories';
15-
import { downloadFile } from './downloads';
16-
import { type PackageKind, SUPPORTED_PACKAGES } from './packages';
17-
import { getLatestRelease, getLatestReleaseKindByKind } from './releases';
185
import { SUPPORTED_TESTS } from './tests/types';
196
import { type SmokeTestsContext } from './context';
20-
21-
import { installMacDMG } from './installers/mac-dmg';
22-
import { installMacZIP } from './installers/mac-zip';
23-
import { installWindowsZIP } from './installers/windows-zip';
24-
import { installWindowsMSI } from './installers/windows-msi';
25-
7+
import { SUPPORTED_PACKAGES } from './packages';
268
import { testTimeToFirstQuery } from './tests/time-to-first-query';
279
import { testAutoUpdateFrom } from './tests/auto-update-from';
2810
import { testAutoUpdateTo } from './tests/auto-update-to';
@@ -118,74 +100,10 @@ const argv = yargs(hideBin(process.argv))
118100
})
119101
.default('tests', SUPPORTED_TESTS.slice());
120102

121-
type TestSubject = PackageDetails & {
122-
filepath: string;
123-
/**
124-
* Is the package unsigned?
125-
* In which case we'll expect auto-updating to fail.
126-
*/
127-
unsigned?: boolean;
128-
};
129-
130-
/**
131-
* Either finds the local package or downloads the package
132-
*/
133-
async function getTestSubject(
134-
context: SmokeTestsContext
135-
): Promise<TestSubject> {
136-
if (context.localPackage) {
137-
const compassDistPath = path.resolve(
138-
__dirname,
139-
'../../packages/compass/dist'
140-
);
141-
const buildInfoPath = path.resolve(compassDistPath, 'target.json');
142-
assert(
143-
fs.existsSync(buildInfoPath),
144-
`Expected '${buildInfoPath}' to exist`
145-
);
146-
const details = readPackageDetails(context.package, buildInfoPath);
147-
return {
148-
...details,
149-
filepath: path.resolve(compassDistPath, details.filename),
150-
unsigned: true,
151-
};
152-
} else {
153-
assert(
154-
context.bucketName !== undefined && context.bucketKeyPrefix !== undefined,
155-
'Bucket name and key prefix are needed to download'
156-
);
157-
const details = writeAndReadPackageDetails(context);
158-
const filepath = await downloadFile({
159-
url: `https://${context.bucketName}.s3.amazonaws.com/${context.bucketKeyPrefix}/${details.filename}`,
160-
targetFilename: details.filename,
161-
clearCache: context.forceDownload,
162-
});
163-
164-
return { ...details, filepath };
165-
}
166-
}
167-
168-
function getInstaller(kind: PackageKind) {
169-
if (kind === 'osx_dmg') {
170-
return installMacDMG;
171-
} else if (kind === 'osx_zip') {
172-
return installMacZIP;
173-
} else if (kind === 'windows_zip') {
174-
return installWindowsZIP;
175-
} else if (kind === 'windows_msi') {
176-
return installWindowsMSI;
177-
} else {
178-
throw new Error(`Installer for '${kind}' is not yet implemented`);
179-
}
180-
}
181-
182103
async function run() {
183-
const context: SmokeTestsContext = {
184-
...argv.parseSync(),
185-
sandboxPath: createSandbox(),
186-
};
104+
const context: SmokeTestsContext = argv.parseSync();
187105

188-
console.log(`Running tests in ${context.sandboxPath}`);
106+
console.log(`Running tests`);
189107

190108
console.log(
191109
'context',
@@ -200,81 +118,15 @@ async function run() {
200118
])
201119
);
202120

203-
const {
204-
kind,
205-
appName,
206-
filepath,
207-
buildInfo: { channel, version },
208-
autoUpdatable,
209-
} = await getTestSubject(context);
210-
211-
try {
212-
if (context.tests.length === 0) {
213-
console.log('Warning: not performing any tests. Pass --tests.');
214-
}
215-
216-
for (const testName of context.tests) {
217-
const installerPath =
218-
testName === 'auto-update-to'
219-
? await getLatestRelease(
220-
channel,
221-
context.arch,
222-
kind,
223-
context.forceDownload
224-
)
225-
: filepath;
121+
for (const testName of context.tests) {
122+
console.log(testName);
226123

227-
const install = getInstaller(
228-
testName === 'auto-update-to' ? getLatestReleaseKindByKind(kind) : kind
229-
);
230-
231-
const { appPath, uninstall } = install({
232-
appName,
233-
filepath: installerPath,
234-
destinationPath: context.sandboxPath,
235-
});
236-
237-
try {
238-
if (testName === 'time-to-first-query') {
239-
// Auto-update does not work on mac in CI at the moment. So in that case
240-
// we just run the E2E tests to make sure the app at least starts up.
241-
testTimeToFirstQuery({
242-
appName,
243-
appPath,
244-
});
245-
}
246-
if (testName === 'auto-update-from') {
247-
await testAutoUpdateFrom({
248-
appName,
249-
appPath,
250-
autoUpdatable,
251-
});
252-
}
253-
if (testName === 'auto-update-to') {
254-
assert(
255-
context.bucketKeyPrefix !== undefined,
256-
'Bucket key prefix is needed to download'
257-
);
258-
259-
await testAutoUpdateTo({
260-
appName,
261-
appPath,
262-
autoUpdatable,
263-
channel,
264-
bucketKeyPrefix: context.bucketKeyPrefix,
265-
version,
266-
});
267-
}
268-
} finally {
269-
await uninstall();
270-
}
271-
}
272-
} finally {
273-
if (context.skipCleanup) {
274-
console.log(`Skipped cleaning up sandbox: ${context.sandboxPath}`);
275-
} else {
276-
console.log(`Cleaning up sandbox: ${context.sandboxPath}`);
277-
fs.rmSync(context.sandboxPath, { recursive: true });
124+
if (testName === 'time-to-first-query') {
125+
await testTimeToFirstQuery(context);
126+
} else if (testName === 'auto-update-from') {
127+
await testAutoUpdateFrom(context);
128+
} else if (testName === 'auto-update-to') {
129+
await testAutoUpdateTo(context);
278130
}
279131
}
280132
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ export type SmokeTestsContext = {
99
package: PackageKind;
1010
forceDownload?: boolean;
1111
localPackage?: boolean;
12-
sandboxPath: string;
1312
tests: TestName[];
1413
skipCleanup: boolean;
1514
};
15+
16+
export type SmokeTestsContextWithSandbox = SmokeTestsContext & {
17+
sandboxPath: string;
18+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { type PackageKind } from '../packages';
2+
import { installMacDMG } from './mac-dmg';
3+
import { installMacZIP } from './mac-zip';
4+
import { installWindowsZIP } from './windows-zip';
5+
import { installWindowsMSI } from './windows-msi';
6+
7+
export function getInstaller(kind: PackageKind) {
8+
if (kind === 'osx_dmg') {
9+
return installMacDMG;
10+
} else if (kind === 'osx_zip') {
11+
return installMacZIP;
12+
} else if (kind === 'windows_zip') {
13+
return installWindowsZIP;
14+
} else if (kind === 'windows_msi') {
15+
return installWindowsMSI;
16+
} else {
17+
throw new Error(`Installer for '${kind}' is not yet implemented`);
18+
}
19+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import assert from 'node:assert/strict';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
5+
import { type SmokeTestsContextWithSandbox } from './context';
6+
7+
import {
8+
type PackageDetails,
9+
readPackageDetails,
10+
writeAndReadPackageDetails,
11+
} from './build-info';
12+
import { downloadFile } from './downloads';
13+
14+
type TestSubjectDetails = PackageDetails & {
15+
/**
16+
* Is the package unsigned?
17+
* In which case we'll expect auto-updating to fail.
18+
*/
19+
unsigned?: boolean;
20+
};
21+
22+
type TestSubject = TestSubjectDetails & {
23+
filepath: string;
24+
};
25+
/**
26+
* Either uses the local package details or calculates it
27+
*/
28+
export function getTestSubjectDetails(
29+
context: SmokeTestsContextWithSandbox
30+
): TestSubjectDetails {
31+
if (context.localPackage) {
32+
const compassDistPath = path.resolve(
33+
__dirname,
34+
'../../packages/compass/dist'
35+
);
36+
const buildInfoPath = path.resolve(compassDistPath, 'target.json');
37+
assert(
38+
fs.existsSync(buildInfoPath),
39+
`Expected '${buildInfoPath}' to exist`
40+
);
41+
const details = readPackageDetails(context.package, buildInfoPath);
42+
return {
43+
...details,
44+
unsigned: true,
45+
};
46+
} else {
47+
return writeAndReadPackageDetails(context);
48+
}
49+
}
50+
51+
/**
52+
* Either finds the local package or downloads the package
53+
*/
54+
export async function getTestSubject(
55+
context: SmokeTestsContextWithSandbox
56+
): Promise<TestSubject> {
57+
const subject = getTestSubjectDetails(context);
58+
if (context.localPackage) {
59+
const compassDistPath = path.resolve(
60+
__dirname,
61+
'../../packages/compass/dist'
62+
);
63+
return {
64+
...subject,
65+
filepath: path.resolve(compassDistPath, subject.filename),
66+
};
67+
} else {
68+
assert(
69+
context.bucketName !== undefined && context.bucketKeyPrefix !== undefined,
70+
'Bucket name and key prefix are needed to download'
71+
);
72+
73+
const filepath = await downloadFile({
74+
url: `https://${context.bucketName}.s3.amazonaws.com/${context.bucketKeyPrefix}/${subject.filename}`,
75+
targetFilename: subject.filename,
76+
clearCache: context.forceDownload,
77+
});
78+
79+
return { ...subject, filepath };
80+
}
81+
}

0 commit comments

Comments
 (0)