generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.ts
479 lines (428 loc) · 15.6 KB
/
test.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import path from 'node:path';
import os from 'node:os';
import fs from 'node:fs/promises';
import shelljs from 'shelljs';
import { Flags, SfCommand, Ux } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
import { ensure } from '@salesforce/ts-types';
import got from 'got';
import chalk from 'chalk';
import { Channel, CLI, ServiceAvailability } from '../../../types.js';
import { AmazonS3, download } from '../../../amazonS3.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-release-management', 'cli.install.test');
const ux = new Ux();
type Results = Record<string, Record<CLI, boolean>>;
namespace Method {
export enum Type {
INSTALLER = 'installer',
NPM = 'npm',
TARBALL = 'tarball',
}
export type Options = {
cli: CLI;
channel: Channel;
method: Type;
directory: string;
};
export abstract class Base {
private static TEST_TARGETS = {
[CLI.SF]: [CLI.SF],
[CLI.SFDX]: [CLI.SFDX, CLI.SF],
};
public constructor(protected options: Method.Options) {}
public async execute(): Promise<Results> {
const { service, available } = await this.ping();
if (!available) {
ux.warn(`${service} is not currently available.`);
const results: Results = {
[this.options.method]: {} as Record<CLI, boolean>,
};
for (const cli of this.getTargets()) {
results[this.options.method][cli] = false;
}
return results;
}
switch (process.platform) {
case 'darwin': {
return this.darwin();
}
case 'win32': {
return this.win32();
}
case 'linux': {
return this.linux();
}
default:
break;
}
return {};
}
// eslint-disable-next-line class-methods-use-this
protected async ping(): Promise<ServiceAvailability> {
return Promise.resolve({ available: true, service: 'Service' });
}
// eslint-disable-next-line class-methods-use-this
protected logResult(cli: CLI, success: boolean): void {
const msg = success ? chalk.green('true') : chalk.red('false');
ux.log(`${chalk.bold(`${cli} Success`)}: ${msg}`);
}
protected getTargets(): CLI[] {
return Base.TEST_TARGETS[this.options.cli];
}
public abstract darwin(): Promise<Results>;
public abstract win32(): Promise<Results>;
public abstract linux(): Promise<Results>;
}
}
class Tarball extends Method.Base {
private s3: AmazonS3;
private paths = {
darwin: ['x64.tar.gz', 'x64.tar.xz'],
win32: [
'x64.tar.gz',
'x86.tar.gz',
// .xz is not supported by powershell's tar command
// 'x64.tar.xz',
// 'x86.tar.xz'
],
linux: ['x64.tar.gz', 'x64.tar.xz'],
'linux-arm': ['arm.tar.gz', 'arm.tar.xz'],
};
public constructor(protected options: Method.Options) {
super(options);
this.s3 = new AmazonS3({ cli: options.cli, channel: options.channel });
}
public async darwin(): Promise<Results> {
return this.installAndTest('darwin');
}
public async win32(): Promise<Results> {
return this.installAndTest('win32');
}
public async linux(): Promise<Results> {
return this.installAndTest('linux');
}
protected async ping(): Promise<ServiceAvailability> {
return this.s3.ping();
}
private async installAndTest(platform: Extract<NodeJS.Platform, 'darwin' | 'linux' | 'win32'>): Promise<Results> {
const tarballs = this.getTarballs(platform);
const results: Results = {};
for (const [tarball, location] of Object.entries(tarballs)) {
try {
// eslint-disable-next-line no-await-in-loop
await download(tarball, location);
// eslint-disable-next-line no-await-in-loop
const extracted = await this.extract(location);
const testResults = this.test(extracted);
for (const [cli, success] of Object.entries(testResults)) {
this.logResult(cli as CLI, success);
}
results[tarball] = testResults;
} catch {
results[tarball] = {} as Record<CLI, boolean>;
for (const cli of this.getTargets()) {
results[tarball][cli] = false;
}
}
ux.log();
}
return results;
}
private getTarballs(platform: Extract<NodeJS.Platform, 'darwin' | 'linux' | 'win32'>): Record<string, string> {
const paths = platform === 'linux' && os.arch().includes('arm') ? this.paths['linux-arm'] : this.paths[platform];
const s3Tarballs = paths.map(
(p) => `${this.s3.directory}/channels/${this.options.channel}/${this.options.cli}-${platform}-${p}`
);
const tarballs: Record<string, string> = {};
for (const tarball of s3Tarballs) {
const name = path.basename(tarball);
const location = path.join(this.options.directory, name);
tarballs[tarball] = location;
}
return tarballs;
}
private async extract(file: string): Promise<string> {
const dir = path.join(this.options.directory, path.basename(file).replace(/\./g, '-'));
await fs.mkdir(dir, { recursive: true });
return new Promise((resolve, reject) => {
ux.spinner.start(`Unpacking ${chalk.cyan(path.basename(file))} to ${dir}`);
const cmd =
process.platform === 'win32'
? `tar -xf ${file} -C ${dir} --strip-components 1 --exclude node_modules/.bin`
: `tar -xf ${file} -C ${dir} --strip-components 1`;
const opts = process.platform === 'win32' ? { shell: 'powershell.exe' } : {};
shelljs.exec(cmd, { ...opts, silent: true }, (code: number, stdout: string, stderr: string) => {
if (code === 0) {
ux.spinner.stop();
ux.log(stdout);
resolve(dir);
} else {
ux.log('stdout:', stdout);
ux.log('stderr:', stderr);
reject();
}
});
});
}
private test(directory: string): Record<CLI, boolean> {
const results = {} as Record<CLI, boolean>;
for (const cli of this.getTargets()) {
const executable = path.join(directory, 'bin', cli);
ux.log(`Testing ${chalk.cyan(executable)}`);
const result =
process.platform === 'win32'
? shelljs.exec(`cmd /c "${executable}.cmd" --version`)
: shelljs.exec(`${executable} --version`);
results[cli] = result.code === 0;
}
return results;
}
}
class Npm extends Method.Base {
private static STATUS_URL = 'https://status.npmjs.org/api/v2/status.json';
private package: string;
public constructor(protected options: Method.Options) {
super(options);
const name = options.cli === CLI.SF ? '@salesforce/cli' : 'sfdx-cli';
const tag = options.channel === Channel.STABLE ? 'latest' : 'latest-rc';
this.package = `${name}@${tag}`;
}
public async darwin(): Promise<Results> {
return this.installAndTest();
}
public async win32(): Promise<Results> {
return this.installAndTest();
}
public async linux(): Promise<Results> {
return this.installAndTest();
}
// eslint-disable-next-line class-methods-use-this
protected async ping(): Promise<ServiceAvailability> {
// I'm not confident that this is the best way to preempt any issues related to Npm's availability. Mainly
// because I couldn't find any documentation related to what status indicators might be used and when.
const response = await got.get(Npm.STATUS_URL).json<{ status: { indicator: string; description: string } }>();
return { service: 'Npm', available: ['none', 'minor'].includes(response.status.indicator) };
}
private async installAndTest(): Promise<Results> {
try {
await this.install();
} catch {
const results = {} as Record<CLI, boolean>;
for (const cli of this.getTargets()) {
results[cli] = false;
}
return { [this.package]: results };
}
const testResults = this.test();
for (const [cli, success] of Object.entries(testResults)) {
this.logResult(cli as CLI, success);
}
ux.log();
return { [this.package]: testResults };
}
private async install(): Promise<void> {
ux.spinner.start(`Installing: ${chalk.cyan(this.package)}`);
return new Promise((resolve, reject) => {
shelljs.exec(
`npm install ${this.package}`,
{ silent: true, cwd: this.options.directory },
(code, stdout, stderr) => {
if (code === 0) {
ux.spinner.stop();
ux.log(stdout);
resolve();
} else {
ux.spinner.stop('Failed');
ux.log(stdout);
ux.log(stderr);
reject();
}
}
);
});
}
private test(): Record<CLI, boolean> {
const results = {} as Record<CLI, boolean>;
const executable = path.join(this.options.directory, 'node_modules', '.bin', this.options.cli);
ux.log(`Testing ${chalk.cyan(executable)}`);
const result =
process.platform === 'win32'
? shelljs.exec(process.platform === 'win32' ? `cmd /c "${executable}" --version` : `${executable} --version`)
: shelljs.exec(`${executable} --version`);
results[this.options.cli] = result.code === 0;
return results;
}
}
class Installer extends Method.Base {
private s3: AmazonS3;
public constructor(protected options: Method.Options) {
super(options);
this.s3 = new AmazonS3({ cli: options.cli, channel: options.channel });
}
public async darwin(): Promise<Results> {
const pkg = `${this.options.cli}.pkg`;
const url = `${this.s3.directory}/channels/${this.options.channel}/${pkg}`;
const location = path.join(this.options.directory, pkg);
await download(url, location);
const result = shelljs.exec(`sudo installer -pkg ${location} -target /`);
const results: Results = {};
if (result.code === 0) {
const testResults = this.nixTest();
for (const [cli, success] of Object.entries(testResults)) {
this.logResult(cli as CLI, success);
}
results[url] = testResults;
} else {
results[url] = {} as Record<CLI, boolean>;
for (const cli of this.getTargets()) {
this.logResult(this.options.cli, false);
results[url][cli] = false;
}
}
ux.log();
return results;
}
public async win32(): Promise<Results> {
const executables = [`${this.options.cli}-x64.exe`, `${this.options.cli}-x86.exe`];
const results: Results = {};
for (const exe of executables) {
const url = `${this.s3.directory}/channels/${this.options.channel}/${exe}`;
const location = path.join(this.options.directory, exe);
// eslint-disable-next-line no-await-in-loop
await download(url, location);
const installLocation = `C:\\install-test\\${this.options.cli}\\${exe.includes('x86') ? 'x86' : 'x64'}`;
const cmd = `Start-Process -Wait -FilePath "${location}" -ArgumentList "/S", "/D=${installLocation}" -PassThru`;
ux.log(`Installing ${chalk.cyan(exe)} to ${installLocation}...`);
const result = shelljs.exec(cmd, { shell: 'powershell.exe' });
if (result.code === 0) {
const testResults = this.win32Test(installLocation);
for (const [cli, success] of Object.entries(testResults)) {
this.logResult(cli as CLI, success);
}
results[url] = testResults;
} else {
results[url] = {} as Record<CLI, boolean>;
for (const cli of this.getTargets()) {
this.logResult(this.options.cli, false);
results[url][cli] = false;
}
}
}
return results;
}
// eslint-disable-next-line @typescript-eslint/require-await, class-methods-use-this
public async linux(): Promise<Results> {
throw new Error('Installers not supported for linux.');
}
protected async ping(): Promise<ServiceAvailability> {
return this.s3.ping();
}
private win32Test(installLocation: string): Record<CLI, boolean> {
const results = {} as Record<CLI, boolean>;
for (const cli of this.getTargets()) {
const binaryPath = path.join(installLocation, 'bin', `${cli}.cmd`);
ux.log(`Testing ${chalk.cyan(binaryPath)}`);
const result = shelljs.exec(`cmd /c "${binaryPath}" --version`);
results[cli] =
result.code === 0 && binaryPath.includes('x86')
? result.stdout.includes('win32-x86')
: result.stdout.includes('win32-x64');
}
return results;
}
private nixTest(): Record<CLI, boolean> {
const results = {} as Record<CLI, boolean>;
for (const cli of this.getTargets()) {
const binaryPath = `/usr/local/bin/${cli}`;
ux.log(`Testing ${chalk.cyan(binaryPath)}`);
const result = shelljs.exec(`${binaryPath} --version`);
results[cli] = result.code === 0;
}
return results;
}
}
export default class Test extends SfCommand<void> {
public static readonly description = messages.getMessage('description');
public static readonly summary = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly flags = {
cli: Flags.string({
summary: messages.getMessage('flags.cli.summary'),
options: Object.values(CLI),
char: 'c',
required: true,
}),
method: Flags.string({
summary: messages.getMessage('flags.method.summary'),
options: Object.values(Method.Type),
char: 'm',
required: true,
}),
channel: Flags.string({
summary: messages.getMessage('flags.channel.summary'),
options: Object.values(Channel),
default: 'stable',
}),
'output-file': Flags.string({
summary: messages.getMessage('flags.output-file.summary'),
default: 'test-results.json',
}),
};
public async run(): Promise<void> {
const { flags } = await this.parse(Test);
const cli = ensure<CLI>(flags.cli as CLI);
const method = ensure<Method.Type>(flags.method as Method.Type);
const channel = ensure<Channel>(flags.channel as Channel);
const outputFile = ensure<string>(flags['output-file']);
const directory = await makeWorkingDir(cli, channel, method);
ux.log(`Working Directory: ${directory}`);
ux.log();
let results: Results = {};
switch (method) {
case Method.Type.TARBALL: {
const tarball = new Tarball({ cli, channel, directory, method });
results = await tarball.execute();
break;
}
case Method.Type.NPM: {
const npm = new Npm({ cli, channel, directory, method });
results = await npm.execute();
break;
}
case Method.Type.INSTALLER: {
const installer = new Installer({ cli, channel, directory, method });
results = await installer.execute();
break;
}
}
const hasFailures = Object.values(results)
.flatMap(Object.values)
.some((r) => !r);
if (hasFailures) process.exitCode = 1;
const fileData: string = JSON.stringify({ status: process.exitCode ?? 0, results }, null, 2);
await fs.writeFile(outputFile, fileData, {
encoding: 'utf8',
mode: '600',
});
ux.styledJSON(results);
ux.log(`Results written to ${outputFile}`);
}
}
const makeWorkingDir = async (cli: CLI, channel: Channel, method: Method.Type): Promise<string> => {
const tmpDir = path.join(os.tmpdir(), 'cli-install-test', cli, channel, method);
// ensure that we are starting with a clean directory
try {
await fs.rm(tmpDir, { recursive: true, force: true });
} catch {
// error means that folder doesn't exist which is okay
}
await fs.mkdir(tmpDir, { recursive: true });
return tmpDir;
};