Skip to content

Commit d464748

Browse files
authored
chore(e2e): few suggestions to smoke-test.ts (#6607)
Check values from process before use
1 parent 6f8fd32 commit d464748

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

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

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,47 @@ import {
1919
assertCommonBuildInfo,
2020
} from './helpers/buildinfo';
2121

22+
const SUPPORTED_PLATFORMS = ['win32', 'darwin', 'linux'] as const;
23+
const SUPPORTED_ARCHS = ['x64', 'arm64'] as const;
24+
25+
function isSupportedPlatform(
26+
value: unknown
27+
): value is typeof SUPPORTED_PLATFORMS[number] {
28+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
29+
return SUPPORTED_PLATFORMS.includes(value as any);
30+
}
31+
32+
function isSupportedArch(
33+
value: unknown
34+
): value is typeof SUPPORTED_ARCHS[number] {
35+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
36+
return SUPPORTED_ARCHS.includes(value as any);
37+
}
38+
39+
function getDefaultPlatform() {
40+
const {
41+
platform,
42+
env: { PLATFORM },
43+
} = process;
44+
if (isSupportedPlatform(PLATFORM)) {
45+
return PLATFORM;
46+
} else if (isSupportedPlatform(platform)) {
47+
return platform;
48+
}
49+
}
50+
51+
function getDefaultArch() {
52+
const {
53+
arch,
54+
env: { ARCH },
55+
} = process;
56+
if (isSupportedArch(ARCH)) {
57+
return ARCH;
58+
} else if (isSupportedArch(arch)) {
59+
return arch;
60+
}
61+
}
62+
2263
const argv = yargs(hideBin(process.argv))
2364
.scriptName('smoke-tests')
2465
.detectLocale(false)
@@ -42,16 +83,14 @@ const argv = yargs(hideBin(process.argv))
4283
'Will be read from packages/compass/package.json if not specified',
4384
})
4485
.option('platform', {
45-
type: 'string',
46-
choices: ['win32', 'darwin', 'linux'],
86+
choices: SUPPORTED_PLATFORMS,
4787
demandOption: true,
48-
default: process.env.PLATFORM ?? process.platform,
88+
default: getDefaultPlatform(),
4989
})
5090
.option('arch', {
51-
type: 'string',
52-
choices: ['x64', 'arm64'],
91+
choices: SUPPORTED_ARCHS,
5392
demandOption: true,
54-
default: process.env.ARCH ?? process.arch,
93+
default: getDefaultArch(),
5594
})
5695
.option('package', {
5796
type: 'string',
@@ -65,7 +104,7 @@ const argv = yargs(hideBin(process.argv))
65104
'linux_tar',
66105
'linux_rpm',
67106
'rhel_tar',
68-
],
107+
] as const,
69108
demandOption: true,
70109
description: 'Which package to test',
71110
})
@@ -122,9 +161,7 @@ async function readPackageVersion(packagePath: string) {
122161
}
123162

124163
async function run() {
125-
const parsedArgs = argv.parseSync();
126-
127-
const context = parsedArgs as SmokeTestsContext;
164+
const context: SmokeTestsContext = argv.parseSync();
128165

129166
console.log(
130167
'context',

0 commit comments

Comments
 (0)