-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathcli.ts
executable file
·270 lines (244 loc) · 6.47 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env npx ts-node
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { pick } from 'lodash';
import createDebug from 'debug';
import { execute } from './execute';
import {
type PackageDetails,
readPackageDetails,
writeAndReadPackageDetails,
} from './build-info';
import { createSandbox } from './directories';
import { downloadFile } from './downloads';
import { type PackageKind, SUPPORTED_PACKAGES } from './packages';
import { getLatestRelease } from './releases';
import { type SmokeTestsContext } from './context';
import { installMacDMG } from './installers/mac-dmg';
import { installMacZIP } from './installers/mac-zip';
import { installWindowsZIP } from './installers/windows-zip';
const debug = createDebug('compass-smoke-tests');
const SUPPORTED_PLATFORMS = ['win32', 'darwin', 'linux'] as const;
const SUPPORTED_ARCHS = ['x64', 'arm64'] as const;
function isSupportedPlatform(
value: unknown
): value is typeof SUPPORTED_PLATFORMS[number] {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
return SUPPORTED_PLATFORMS.includes(value as any);
}
function isSupportedArch(
value: unknown
): value is typeof SUPPORTED_ARCHS[number] {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
return SUPPORTED_ARCHS.includes(value as any);
}
function getDefaultPlatform() {
const {
platform,
env: { PLATFORM },
} = process;
if (isSupportedPlatform(PLATFORM)) {
return PLATFORM;
} else if (isSupportedPlatform(platform)) {
return platform;
}
}
function getDefaultArch() {
const {
arch,
env: { ARCH },
} = process;
if (isSupportedArch(ARCH)) {
return ARCH;
} else if (isSupportedArch(arch)) {
return arch;
}
}
const argv = yargs(hideBin(process.argv))
.scriptName('smoke-tests')
.detectLocale(false)
.version(false)
.strict()
.option('bucketName', {
type: 'string',
default: process.env.EVERGREEN_BUCKET_NAME,
})
.option('bucketKeyPrefix', {
type: 'string',
default: process.env.EVERGREEN_BUCKET_KEY_PREFIX,
})
.option('platform', {
choices: SUPPORTED_PLATFORMS,
demandOption: true,
default: getDefaultPlatform(),
})
.option('arch', {
choices: SUPPORTED_ARCHS,
demandOption: true,
default: getDefaultArch(),
})
.option('package', {
type: 'string',
choices: SUPPORTED_PACKAGES,
demandOption: true,
description: 'Which package to test',
})
.option('forceDownload', {
type: 'boolean',
description: 'Force download all assets before starting',
})
.option('localPackage', {
type: 'boolean',
description: 'Use the local package instead of downloading',
});
type TestSubject = PackageDetails & {
filepath: string;
/**
* Is the package unsigned?
* In which case we'll expect auto-updating to fail.
*/
unsigned?: boolean;
};
/**
* Either finds the local package or downloads the package
*/
async function getTestSubject(
context: SmokeTestsContext
): Promise<TestSubject> {
if (context.localPackage) {
const compassDistPath = path.resolve(
__dirname,
'../../packages/compass/dist'
);
const buildInfoPath = path.resolve(compassDistPath, 'target.json');
assert(
fs.existsSync(buildInfoPath),
`Expected '${buildInfoPath}' to exist`
);
const details = readPackageDetails(context.package, buildInfoPath);
return {
...details,
filepath: path.resolve(compassDistPath, details.filename),
unsigned: true,
};
} else {
assert(
context.bucketName !== undefined && context.bucketKeyPrefix !== undefined,
'Bucket name and key prefix are needed to download'
);
const details = writeAndReadPackageDetails(context);
const filepath = await downloadFile({
url: `https://${context.bucketName}.s3.amazonaws.com/${context.bucketKeyPrefix}/${details.filename}`,
targetFilename: details.filename,
clearCache: context.forceDownload,
});
return { ...details, filepath };
}
}
function getInstaller(kind: PackageKind) {
if (kind === 'osx_dmg') {
return installMacDMG;
} else if (kind === 'osx_zip') {
return installMacZIP;
} else if (kind === 'windows_zip') {
return installWindowsZIP;
} else {
throw new Error(`Installer for '${kind}' is not yet implemented`);
}
}
async function run() {
const context: SmokeTestsContext = {
...argv.parseSync(),
sandboxPath: createSandbox(),
};
debug(`Running tests in ${context.sandboxPath}`);
debug(
'context',
pick(context, [
'forceDownload',
'bucketName',
'bucketKeyPrefix',
'platform',
'arch',
'package',
])
);
const { kind, filepath, buildInfo, appName } = await getTestSubject(context);
const install = getInstaller(kind);
try {
const { appPath, uninstall } = install({
appName,
filepath,
destinationPath: context.sandboxPath,
});
try {
runTest({ appName, appPath });
} finally {
await uninstall();
}
} finally {
debug('Cleaning up sandbox');
fs.rmSync(context.sandboxPath, { recursive: true });
}
debug('update from latest release to this package');
const releasepath = await getLatestRelease(
buildInfo.channel,
context.arch,
kind,
context.forceDownload
);
try {
const appName = buildInfo.productName;
const { appPath, uninstall } = install({
appName,
filepath: releasepath,
destinationPath: context.sandboxPath,
});
try {
runTest({ appName, appPath });
} finally {
await uninstall();
}
} finally {
debug('Cleaning up sandbox');
fs.rmSync(context.sandboxPath, { recursive: true });
}
}
type RunTestOptions = {
appName: string;
appPath: string;
};
function runTest({ appName, appPath }: RunTestOptions) {
execute(
'npm',
[
'run',
'--unsafe-perm',
'test-packaged',
'--workspace',
'compass-e2e-tests',
'--',
'--test-filter=time-to-first-query',
],
{
// We need to use a shell to get environment variables setup correctly
shell: true,
env: {
...process.env,
COMPASS_APP_NAME: appName,
COMPASS_APP_PATH: appPath,
},
}
);
}
run()
.then(function () {
debug('done');
})
.catch(function (err) {
console.error(err.stack);
process.exitCode = 1;
});