Skip to content

Commit 21f515d

Browse files
authored
chore(e2e): Smoke test cli cleanup (#6594)
* Use parseSync * Using default constants over functions * Add separate functions to read JSON * Add an assertCommonBuildInfo function
1 parent f0ef4a9 commit 21f515d

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

packages/compass-e2e-tests/smoke-test.ts

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env npx ts-node
2-
import { createWriteStream, existsSync, promises as fs } from 'fs';
3-
import path from 'path';
2+
import assert from 'node:assert/strict';
3+
import { createWriteStream, existsSync, promises as fs } from 'node:fs';
4+
import path from 'node:path';
5+
46
import yargs from 'yargs';
5-
import type { Argv } from 'yargs';
67
import { hideBin } from 'yargs/helpers';
78
import https from 'https';
89
import { pick } from 'lodash';
@@ -18,40 +19,40 @@ const argv = yargs(hideBin(process.argv))
1819
.strict()
1920
.option('bucketName', {
2021
type: 'string',
21-
default: () => process.env.EVERGREEN_BUCKET_NAME,
22+
default: process.env.EVERGREEN_BUCKET_NAME,
2223
})
2324
.option('bucketKeyPrefix', {
2425
type: 'string',
25-
default: () => process.env.EVERGREEN_BUCKET_KEY_PREFIX,
26+
default: process.env.EVERGREEN_BUCKET_KEY_PREFIX,
2627
})
2728
.option('devVersion', {
2829
type: 'string',
2930
// For dev versions we need this from evergreen. For beta or stable (or by
3031
// default, ie. when testing a locally packaged app) we get it from the
3132
// package.json
32-
default: () => process.env.DEV_VERSION_IDENTIFIER,
33+
default: process.env.DEV_VERSION_IDENTIFIER,
3334
})
3435
.option('isWindows', {
3536
type: 'boolean',
36-
default: () => process.env.IS_WINDOWS === 'true',
37+
default: process.env.IS_WINDOWS === 'true',
3738
})
3839
.option('isOSX', {
3940
type: 'boolean',
40-
default: () => process.env.IS_OSX === 'true',
41+
default: process.env.IS_OSX === 'true',
4142
})
4243
.option('isRHEL', {
4344
type: 'boolean',
44-
default: () => process.env.IS_RHEL === 'true',
45+
default: process.env.IS_RHEL === 'true',
4546
})
4647
.option('isUbuntu', {
4748
type: 'boolean',
48-
default: () => process.env.IS_UBUNTU === 'true',
49+
default: process.env.IS_UBUNTU === 'true',
4950
})
5051
.option('arch', {
5152
type: 'string',
5253
choices: ['x64', 'arm64'],
5354
demandOption: true,
54-
default: () => process.env.ARCH ?? process.arch,
55+
default: process.env.ARCH ?? process.arch,
5556
})
5657
.option('skipDownload', {
5758
type: 'boolean',
@@ -87,17 +88,27 @@ const argv = yargs(hideBin(process.argv))
8788
return true;
8889
});
8990

90-
type BuilderCallbackParsedArgs<A extends (...args: any[]) => Argv<any>> =
91-
ReturnType<ReturnType<A>['parseSync']>;
91+
type SmokeTestsContext = ReturnType<typeof argv['parseSync']>;
9292

93-
type SmokeTestsContext = BuilderCallbackParsedArgs<typeof argv>;
93+
async function readJson<T extends object>(...segments: string[]): Promise<T> {
94+
const result = JSON.parse(
95+
await fs.readFile(path.join(...segments), 'utf8')
96+
) as unknown;
97+
assert(typeof result === 'object' && result !== null, 'Expected an object');
98+
return result as T;
99+
}
94100

95-
async function run() {
96-
const parsedArgs = argv.parse();
101+
async function readPackageVersion(packagePath: string) {
102+
const pkg = await readJson(packagePath, 'package.json');
103+
assert(
104+
'version' in pkg && typeof pkg.version === 'string',
105+
'Expected a package version'
106+
);
107+
return pkg.version;
108+
}
97109

98-
if ('then' in parsedArgs && typeof parsedArgs.then === 'function') {
99-
throw new Error('Async args parser is not allowed');
100-
}
110+
async function run() {
111+
const parsedArgs = argv.parseSync();
101112

102113
const context = parsedArgs as SmokeTestsContext;
103114

@@ -119,11 +130,7 @@ async function run() {
119130

120131
const compassDir = path.resolve(__dirname, '..', '..', 'packages', 'compass');
121132
// use the specified DEV_VERSION_IDENTIFIER if set or load version from packages/compass/package.json
122-
const version = context.devVersion
123-
? context.devVersion
124-
: (JSON.parse(
125-
await fs.readFile(path.join(compassDir, 'package.json'), 'utf8')
126-
).version as string);
133+
const version = context.devVersion ?? (await readPackageVersion(compassDir));
127134
const platform = platformFromContext(context);
128135
const outPath = path.resolve(__dirname, 'hadron-build-info.json');
129136

@@ -138,11 +145,9 @@ async function run() {
138145
};
139146
console.log('infoArgs', infoArgs);
140147
writeBuildInfo(infoArgs);
141-
const buildInfo = JSON.parse(await fs.readFile(infoArgs.out, 'utf8'));
148+
const buildInfo = await readJson(infoArgs.out);
142149

143-
if (!buildInfoIsCommon(buildInfo)) {
144-
throw new Error('buildInfo is missing');
145-
}
150+
assertCommonBuildInfo(buildInfo);
146151

147152
// filter the extensions given the platform (isWindows, isOSX, isUbuntu, isRHEL) and extension
148153
const { isWindows, isOSX, isRHEL, isUbuntu, extension } = context;
@@ -217,13 +222,16 @@ type PackageFilterConfig = Pick<
217222
const commonKeys = ['productName'];
218223
type CommonBuildInfo = Record<typeof commonKeys[number], string>;
219224

220-
function buildInfoIsCommon(buildInfo: any): buildInfo is CommonBuildInfo {
225+
function assertCommonBuildInfo(
226+
buildInfo: unknown
227+
): asserts buildInfo is CommonBuildInfo {
228+
assert(
229+
typeof buildInfo === 'object' && buildInfo !== null,
230+
'Expected buildInfo to be an object'
231+
);
221232
for (const key of commonKeys) {
222-
if (!buildInfo[key]) {
223-
return false;
224-
}
233+
assert(key in buildInfo, `Expected buildInfo to have '${key}'`);
225234
}
226-
return true;
227235
}
228236

229237
const windowsFilenameKeys = [

0 commit comments

Comments
 (0)