-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestkit.ts
593 lines (539 loc) · 20.2 KB
/
testkit.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
* Copyright (c) 2021, 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
*/
/* eslint-disable no-console */
/* eslint-disable no-await-in-loop */
import * as path from 'node:path';
import * as os from 'node:os';
import * as fs from 'node:fs';
import fg from 'fast-glob';
import { exec, find, mv, rm } from 'shelljs';
import { CLI, execCmd, ScratchOrgConfig, TestSession } from '@salesforce/cli-plugins-testkit';
import { AsyncCreatable, Env, parseJsonMap, set } from '@salesforce/kit';
import { AnyJson, Dictionary, ensureString, JsonMap, Nullable } from '@salesforce/ts-types';
import { AuthInfo, Connection, NamedPackageDir, SfdxPropertyKeys, SfProject } from '@salesforce/core';
import { debug, Debugger } from 'debug';
import { MetadataResolver } from '@salesforce/source-deploy-retrieve';
import { Commands, Result, StatusResult } from './types';
import { Assertions } from './assertions';
import { ExecutionLog } from './executionLog';
import { FileTracker, traverseForFiles } from './fileTracker';
/**
* SourceTestkit is a class that is designed to make composing source NUTs easy and painless
*
* It provides the following functionality:
* 1. Methods that wrap around source commands, e.g. SourceTestkit.deploy wraps around force:source:deploy
* 2. Access to commonly used assertions (provided by the Assertions class)
* 3. Ability to track file history (provided by the FileTracker class)
* 4. Ability to modify remote metadata
* 5. Ability to add local metadata
* 6. Miscellaneous helper methods
*
* To see debug logs for command executions set these env vars:
* - DEBUG=sourceTestkit:* (for logs from all nuts)
* - DEBUG=sourceTestkit:<filename.nut.ts> (for logs from specific nut)
* - DEBUG_DEPTH=8
*/
export class SourceTestkit extends AsyncCreatable<SourceTestkit.Options> {
public static Env = new Env();
private static DefaultCmdOpts: SourceTestkit.CommandOpts = {
exitCode: 0,
args: '',
json: true,
cli: 'inherit',
};
public packages!: NamedPackageDir[];
public packageNames!: string[];
public packagePaths!: string[];
public packageGlobs!: string[];
public projectDir!: string;
public expect!: Assertions;
public testMetadataFolder!: string;
public testMetadataFiles!: string[];
public username!: string;
private commands!: Commands;
private connection: Nullable<Connection>;
private debug: Debugger;
private executableName!: Executable;
private executionLog!: ExecutionLog;
private fileTracker!: FileTracker;
private metadataResolver!: MetadataResolver;
private nut: string;
private orgless: boolean;
private repository: string;
private session!: TestSession;
private scratchOrgs: ScratchOrgConfig[];
public constructor(options: SourceTestkit.Options) {
super(options);
this.repository = options.repository;
this.orgless = !!options.orgless;
this.scratchOrgs = options.scratchOrgs ?? [];
this.nut = path.basename(options.nut);
this.debug = debug(`sourceTestkit:${this.nut}`);
}
/**
* Cleans the test session
*/
public async clean(): Promise<void> {
SourceTestkit.Env.unset('TESTKIT_EXECUTABLE_PATH');
await this.session?.clean();
}
/**
* Executes force:source:convert
*/
public async convert(options: Partial<SourceTestkit.CommandOpts> = {}): Promise<Result> {
return this.execute('force:source:convert', options);
}
/**
* Executes force:source:deploy
*/
public async deploy<T = { id: string }>(options: Partial<SourceTestkit.CommandOpts> = {}): Promise<Result<T>> {
return this.execute<T>(this.getCommandString('deploy'), options);
}
/**
* Executes force:source:deploy:report
*/
public async deployReport(options: Partial<SourceTestkit.CommandOpts> = {}): Promise<Result<{ id: string }>> {
return this.execute<{ id: string }>(this.getCommandString('deployReport'), options);
}
/**
* Executes force:source:deploy:cancel
*/
public async deployCancel(options: Partial<SourceTestkit.CommandOpts> = {}): Promise<Result<{ id: string }>> {
return this.execute<{ id: string }>(this.getCommandString('deployCancel'), options);
}
/**
* Executes force:source:retrieve
*/
public async retrieve(options: Partial<SourceTestkit.CommandOpts> = {}): Promise<Result> {
return this.execute(this.getCommandString('retrieve'), options);
}
/**
* Executes force:source:push
*/
public async push(options: Partial<SourceTestkit.CommandOpts> = {}): Promise<Result> {
return this.execute(this.getCommandString('push'), options);
}
/**
* Executes force:source:pull
*/
public async pull(options: Partial<SourceTestkit.CommandOpts> = {}): Promise<Result> {
return this.execute(this.getCommandString('pull'), options);
}
/**
* Executes force:source:status
*/
public async status(options: Partial<SourceTestkit.CommandOpts> = {}): Promise<Result<StatusResult>> {
return this.execute<StatusResult>(this.getCommandString('status'), options);
}
/**
* Create an apex class
*/
public async createApexClass(
options: Partial<SourceTestkit.CommandOpts> = {}
): Promise<Result<{ created: string[]; outputDir: string }>> {
return this.execute('force:apex:class:create', options);
}
/**
* Create a Lightning Web Component
*/
public async createLWC(
options: Partial<SourceTestkit.CommandOpts> = {}
): Promise<Result<{ created: string[]; outputDir: string }>> {
return this.execute('force:lightning:component:create', options);
}
/**
* Installs a package into the scratch org. This method uses shelljs instead of testkit because
* we can't add plugin-package as a dev plugin yet.
*/
// eslint-disable-next-line class-methods-use-this
public installPackage(id: string): void {
exec(`sfdx force:package:install --noprompt --package ${id} --wait 5 --json 2> /dev/null`, { silent: true });
}
/**
* Runs the permset:assign command with the passed in options
*
* @param options
*/
public async assignPermissionSet(
options: Partial<SourceTestkit.CommandOpts> = {}
): Promise<Result<{ success: [{ name: string; value: string }]; failures: [{ name: string; value: string }] }>> {
return this.execute('force:user:permset:assign', options);
}
/**
* Adds given files to FileTracker for tracking
*/
public async trackFiles(files: string[]): Promise<void> {
for (const file of files) {
await this.fileTracker.track(file);
}
}
/**
* Adds files found by globs to FileTracker for tracking
*/
public async trackGlobs(globs: string[]): Promise<void> {
const files = await this.doGlob(globs);
for (const file of files) {
await this.fileTracker.track(file);
}
}
/**
* Read files found by globs
*/
public async readGlobs(globs: string[]): Promise<Dictionary<string>> {
const files = await this.doGlob(globs);
const returnValue: Dictionary<string> = {};
for (const file of files) {
returnValue[file] = await fs.promises.readFile(file, 'utf8');
}
return returnValue;
}
/**
* Read the org's sourcePathInfos.json
*/
public async readSourcePathInfos(): Promise<AnyJson> {
const sourcePathInfosPath = path.join(this.projectDir, '.sfdx', 'orgs', this.username, 'sourcePathInfos.json');
return JSON.parse(await fs.promises.readFile(sourcePathInfosPath, 'utf8')) as AnyJson;
}
/**
* Read the org's maxRevision.json
*/
public async readMaxRevision(): Promise<{ sourceMembers: JsonMap }> {
const maxRevisionPath = path.join(this.projectDir, '.sfdx', 'orgs', this.username, 'maxRevision.json');
return JSON.parse(await fs.promises.readFile(maxRevisionPath, 'utf8')) as { sourceMembers: JsonMap };
}
/**
* Write the org's maxRevision.json
*/
public async writeMaxRevision(contents: JsonMap): Promise<void> {
const maxRevisionPath = path.join(this.projectDir, '.sfdx', 'orgs', this.username, 'maxRevision.json');
return fs.promises.writeFile(maxRevisionPath, JSON.stringify(contents));
}
/**
* Write file
*/
// eslint-disable-next-line class-methods-use-this
public async writeFile(filename: string, contents: string): Promise<void> {
return fs.promises.writeFile(filename, contents);
}
/**
* Create a package.xml
*/
public async createPackageXml(xml: string): Promise<string> {
const packageXml = `<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
${xml}
<version>51.0</version>
</Package>
`;
const packageXmlPath = path.join(this.projectDir, 'package.xml');
await fs.promises.writeFile(packageXmlPath, packageXml);
return packageXmlPath;
}
/**
* Delete the org's sourcePathInfos.json
*/
public async deleteSourcePathInfos(): Promise<void> {
const sourcePathInfosPath = path.join(this.projectDir, '.sfdx', 'orgs', this.username, 'sourcePathInfos.json');
return fs.promises.unlink(sourcePathInfosPath);
}
/**
* Delete the org's maxRevision.json
*/
public async deleteMaxRevision(): Promise<void> {
const maxRevisionPath = path.join(this.projectDir, '.sfdx', 'orgs', this.username, 'maxRevision.json');
return fs.promises.unlink(maxRevisionPath);
}
/**
* Delete the files found by the given globs
*/
public async deleteGlobs(globs: string[]): Promise<void> {
const files = await this.doGlob(globs);
for (const file of files) {
await fs.promises.unlink(file);
}
}
/**
* Delete all source files in the project directory
*/
public async deleteAllSourceFiles(): Promise<void> {
for (const pkg of this.packagePaths) {
await fs.promises.rm(pkg, { recursive: true });
await fs.promises.mkdir(pkg, { recursive: true });
}
}
/**
* Modify files found by given globs
*/
public async modifyLocalGlobs(globs: string[], append = os.EOL): Promise<void> {
const allFiles = await this.doGlob(globs);
for (const file of allFiles) {
await this.modifyLocalFile(path.normalize(file), append);
}
}
/**
* Modify file by appending to the end of the file. Defaults to appending a new line.
*/
public async modifyLocalFile(file: string, append = os.EOL): Promise<void> {
const fullPath = file.startsWith(this.projectDir) ? file : path.join(this.projectDir, file);
let contents = await fs.promises.readFile(fullPath, 'utf-8');
contents += append;
await fs.promises.writeFile(fullPath, contents);
await this.fileTracker.update(fullPath, 'modified file');
}
/**
* Modify a remote file
*
* This presumes that there is a QuickAction called NutAction in
* the test metadata. Ideally this method would be able to update
* any metadata type with any name
*/
public async modifyRemoteFile(): Promise<string> {
const result: Array<{ Id?: string | undefined }> = (await this.connection?.tooling
.sobject('QuickActionDefinition')
.find({ DeveloperName: 'NutAction' }, ['ID']))!;
const updateRequest = {
Id: result[0].Id as string,
Description: 'updated description',
};
await this.connection?.tooling.sobject('QuickActionDefinition').update(updateRequest);
return this.testMetadataFiles.find((f) => f.endsWith('NutAction.quickAction-meta.xml'))!;
}
/**
* Adds test files (located in the test/nuts/metadata folder) to the project directory
*/
public async addTestFiles(): Promise<void> {
for (const file of this.testMetadataFiles) {
const dest = path.join(this.projectDir, file);
const src = path.join(this.testMetadataFolder, file);
this.debug(`addTestFiles: ${src} -> ${dest}`);
try {
fs.copyFileSync(src, dest);
} catch {
await fs.promises.mkdir(path.dirname(dest), { recursive: true });
fs.copyFileSync(src, dest);
}
}
await this.trackFiles(this.testMetadataFiles);
}
/**
* Spoof a remote change in the org by setting the lastRetrievedFromServer
* property in the local maxRevision.json to null
*/
public async spoofRemoteChange(globs: string[]): Promise<void> {
const files = await this.doGlob(globs);
const maxRevision = await this.readMaxRevision();
for (const file of files) {
const component = this.metadataResolver.getComponentsFromPath(file)[0];
const parent = component.parent?.name;
const type = component.type.name;
const name = component.name;
if (!type.includes('CustomLabel')) {
const maxRevisionKey = parent ? `${type}__${parent}.${name}` : `${type}__${name}`;
set(maxRevision.sourceMembers, `${maxRevisionKey}.lastRetrievedFromServer`, null);
} else {
const labels = Object.keys(maxRevision.sourceMembers).filter((k) => k.startsWith('CustomLabel'));
labels.forEach((label) => {
set(maxRevision.sourceMembers, `${label}.lastRetrievedFromServer`, null);
});
}
}
await this.writeMaxRevision(maxRevision);
}
/**
* Move the manifest file to the current working directory.
*
* This is used because the SDR library does not output the package.xml
* in the same location as toolbelt so we have to find it within the output
* dir, move it, and delete the generated dir.
*/
// eslint-disable-next-line class-methods-use-this
public findAndMoveManifest(dir: string): void {
const manifest = find(dir).filter((file) => file.endsWith('package.xml'));
if (!manifest?.length) {
throw Error(`Did not find package.xml within ${dir}`);
}
mv(manifest[0], path.join(process.cwd()));
rm('-rf', dir);
}
/**
* Execute a command using testkit. Adds --json to every command to ensure json output.
*/
public async execute<T>(cmd: string, options: Partial<SourceTestkit.CommandOpts> = {}): Promise<Nullable<Result<T>>> {
try {
const { args, exitCode, json } = Object.assign({}, SourceTestkit.DefaultCmdOpts, options);
const command = (json ? [cmd, args, '--json'] : [cmd, args]).join(' ');
this.debug(`${command} (expecting exit code: ${exitCode})`);
await this.fileTracker.updateAll(`PRE: ${command}`);
await this.executionLog.add(command);
const result = execCmd<T>(command, { ensureExitCode: exitCode, cli: options.cli ?? 'inherit' });
await this.fileTracker.updateAll(`POST: ${command}`);
if (json) {
const jsonOutput = result.jsonOutput as Result<T>;
this.debug('%O', jsonOutput);
if (!jsonOutput) {
console.error(`${command} returned null jsonOutput`);
console.error(result);
} else {
this.expect.toHaveProperty(jsonOutput, 'status');
if (jsonOutput.status === 0) {
this.expect.toHaveProperty(jsonOutput, 'result');
}
}
return jsonOutput;
}
} catch (err) {
const error = err as Error;
await this.handleError(error);
}
}
protected async init(): Promise<void> {
if (!SourceTestkit.Env.getString('TESTKIT_HUB_USERNAME') && !SourceTestkit.Env.getString('TESTKIT_AUTH_URL')) {
ensureString(SourceTestkit.Env.getString('TESTKIT_JWT_KEY'));
ensureString(SourceTestkit.Env.getString('TESTKIT_JWT_CLIENT_ID'));
ensureString(SourceTestkit.Env.getString('TESTKIT_HUB_INSTANCE'));
}
try {
this.executableName = await getExecutableName();
this.commands = COMMANDS[this.executableName];
this.metadataResolver = new MetadataResolver();
this.session = await this.createSession();
this.projectDir = this.session.project.dir;
const sfProject = await SfProject.resolve(this.projectDir);
this.packages = sfProject.getPackageDirectories();
this.packageNames = this.packages.map((p) => p.name);
this.packagePaths = this.packages.map((p) => p.fullPath);
this.packageGlobs = this.packages.map((p) => `${p.path}/**/*`);
this.username = await getDefaultUsername();
this.connection = await this.createConnection();
const context = {
connection: this.connection,
projectDir: this.projectDir,
nut: this.nut,
commands: this.commands,
};
this.fileTracker = new FileTracker(context);
this.executionLog = new ExecutionLog(context);
this.expect = new Assertions(context, this.executionLog, this.fileTracker);
this.testMetadataFolder = path.join(__dirname, 'metadata');
this.testMetadataFiles = (await traverseForFiles(this.testMetadataFolder))
.filter((f) => !f.endsWith('.DS_Store'))
.map((f) => f.replace(`${this.testMetadataFolder}${path.sep}`, ''));
} catch (err) {
const error = err as Error;
await this.handleError(error, true);
}
}
private getCommandString(cmdKey: string): string {
const cmd = this.commands[cmdKey];
if (cmd) {
return cmd;
} else {
throw new Error(`${cmdKey} command not implemented for this executable`);
}
}
/**
* Log error to console with helpful debugging information
*/
private async handleError(err: Error, clean = false): Promise<never> {
const header = ` ENCOUNTERED ERROR IN: ${this.debug.namespace} `;
console.log('-'.repeat(header.length));
console.log(header);
console.log('-'.repeat(header.length));
console.log('session:', this.session?.dir);
console.log('username:', this.username);
console.log(err);
console.log('-'.repeat(header.length));
if (clean) await this.clean();
throw err;
}
private async createSession(): Promise<TestSession> {
const scratchOrgs: ScratchOrgConfig[] = this.orgless
? []
: [{ duration: 1, setDefault: true, config: path.join('config', 'project-scratch-def.json') }];
return TestSession.create({
project: { gitClone: this.repository },
devhubAuthStrategy: 'AUTO',
scratchOrgs: [...scratchOrgs, ...this.scratchOrgs],
retries: 2,
});
}
private async createConnection(): Promise<Nullable<Connection>> {
if (this.orgless) return;
return Connection.create({
authInfo: await AuthInfo.create({ username: this.username }),
});
}
private async doGlob(globs: string[]): Promise<string[]> {
const dir = this.projectDir.replace(/\\/g, '/');
const fullGlobs = globs.map((g) => {
g = g.replace(/\\/g, '/');
if (g.startsWith('!')) {
g = g.substr(1).startsWith(dir) ? `!${g}` : [`!${dir}`, g].join('/');
} else {
g = g.startsWith(dir) ? g : [dir, g].join('/');
}
return g;
});
return fg(fullGlobs);
}
}
export namespace SourceTestkit {
export type Options = {
readonly executable?: string;
readonly repository: string;
readonly nut: string;
readonly orgless?: boolean;
readonly scratchOrgs?: ScratchOrgConfig[];
};
export type CommandOpts = {
exitCode: number | 'nonZero';
args: string;
json: boolean;
cli: CLI;
};
}
export enum Executable {
SFDX = 'sfdx',
SF = 'sf',
}
export const COMMANDS = {
[Executable.SFDX]: {
deploy: 'force:source:deploy',
deployReport: 'force:source:deploy:report',
deployCancel: 'force:source:deploy:cancel',
retrieve: 'force:source:retrieve',
convert: 'force:source:convert',
push: 'force:source:push',
pull: 'force:source:pull',
status: 'force:source:status',
},
[Executable.SF]: {
deploy: 'deploy metadata',
retrieve: 'retrieve metadata',
},
};
const getDefaultUsername = async (): Promise<string> => {
const configVar = 'target-org';
const configResult = execCmd<Array<{ key?: string; name?: string; value: string }>>(
`config:get ${configVar} --json`,
{ ensureExitCode: 0, cli: 'sf' }
).jsonOutput?.result;
// depending on which version of config:get the user has available, there may be a name or key
// eventually, drop the `key` option and the deprecated SfdxPropertyKeys
const possibleKeys = [configVar, SfdxPropertyKeys.DEFAULT_USERNAME];
const username = configResult?.find(
(r) =>
(typeof r.key === 'string' && possibleKeys.includes(r.key)) ||
(typeof r.name === 'string' && possibleKeys.includes(r.name))
)?.value;
return username!;
};
const getExecutableName = async (): Promise<Executable> => {
const pkgJsonPath = path.join(process.cwd(), 'package.json');
const pkgJson = parseJsonMap<{ oclif: { bin: string } }>(await fs.promises.readFile(pkgJsonPath, 'utf8'));
return pkgJson.oclif.bin as Executable;
};