-
Notifications
You must be signed in to change notification settings - Fork 0
/
testdependencies.ts
420 lines (365 loc) · 19.5 KB
/
testdependencies.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
/* eslint-disable no-console */
/* eslint-disable complexity */
import * as fs from 'node:fs'
import * as path from 'node:path'
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Messages, Org, SfProject, SfProjectJson } from '@salesforce/core';
import { AnyJson } from '@salesforce/ts-types';
import * as fastFuzzy from 'fast-fuzzy';
import * as glob from 'glob';
import * as jsonQuery from 'json-query';
import * as prompts from 'prompts';
import * as sqlstring from 'sqlstring';
import { MetadataTypeInfos } from '../../../metadataTypeInfos';
import { parseXml } from '../../../xmlUtility';
import { MetadataType } from '../../../metadataTypeInfos';
// Initialize Messages with the current plugin directory
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('sfdx-metadata-patcher', 'mdata');
type TestDependenciesFlags = {
config: boolean;
nameconv: string;
depth: number;
manifest: string;
destructivemanifest: string;
fuzzythreshold: string;
usecodecoverage: boolean;
prod: boolean;
loglevel: string;
};
type PluginConfig = {
[metadataType: string]: 's' | 'f' | 'd' | MetadataSpecificAction;
}
type MetadataSpecificAction = {
onDeploy: 's' | 'f' | 'd';
onDestroy: 's' | 'f' | 'd';
}
export default class TestDependencies extends SfCommand<AnyJson> {
public static readonly summary = messages.getMessage('apex.testdependencies.description');
public static readonly examples = [
`To configure the plugin:
$ sfdx mdata:apex:testdependencies --config`,
`To find all test dependecies for classes in a delta package in a SFDX project with a specific naming convention for test classes:
$ sfdx mdata:apex:testdependencies -x package.xml -d destructiveChanges.xml --nameconv _Test`,
`To find all test dependecies for classes in a delta package in a SFDX project when deploying into production:
$ sfdx mdata:apex:testdependencies -x package.xml -d destructiveChanges.xml --prod`,
`To find all test dependecies for classes in a delta package in a SFDX project with a custom fuzzy threshold score:
$ sfdx mdata:apex:testdependencies -x package.xml -d destructiveChanges.xml --fuzzythreshold 0.75`,
`To find all test dependecies up to a certain depth in a SFDX project:
$ sfdx mdata:apex:testdependencies -x package.xml -d destructiveChanges.xml -l 1`,
`To find all test dependecies for classes in a delta package including ApexCodeCoverage in target org in a SFDX project:
$ sfdx mdata:apex:testdependencies -x package.xml -d destructiveChanges.xml --usecodecoverage`
];
public static readonly flags = {
config: Flags.boolean({
summary: messages.getMessage('apex.testdependencies.flags.config'),
exclusive: ['nameconv', 'depth', 'manifest', 'destructivemanifest', 'fuzzythreshold', 'usecodecoverage']
}),
nameconv: Flags.string({
char: 'n',
summary: messages.getMessage('apex.testdependencies.flags.nameconv'),
default: 'Test'
}),
depth: Flags.integer({
char: 'l',
summary: messages.getMessage('apex.testdependencies.flags.depth'),
default: -1
}),
manifest: Flags.string({
char: 'x',
summary: messages.getMessage('apex.testdependencies.flags.manifest'),
required: false
}),
destructivemanifest: Flags.string({
char: 'd',
summary: messages.getMessage('apex.testdependencies.flags.destructivemanifest'),
required: false
}),
fuzzythreshold: Flags.string({
summary: messages.getMessage('apex.testdependencies.flags.fuzzythreshold'),
default: '.6',
}),
usecodecoverage: Flags.boolean({
summary: messages.getMessage('apex.testdependencies.flags.usecodecoverage'),
default: false
}),
prod: Flags.boolean({
summary: messages.getMessage('apex.testdependencies.flags.prod'),
default: false
}),
'target-org': Flags.optionalOrg({
summary: messages.getMessage('general.flags.targetorg'),
char: 'o',
}),
loglevel: Flags.string({
summary: messages.getMessage('general.flags.loglevel'),
default: 'info',
required: false,
options: [
'trace',
'debug',
'info',
'warn',
'error',
'fatal',
'TRACE',
'DEBUG',
'INFO',
'WARN',
'ERROR',
'FATAL'
]
})
};
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
public static readonly requiresProject = true;
// Comment this out if your command does not require an org username
protected static requiresUsername = false;
// Comment this out if your command does not support a hub org username
protected static supportsDevhubUsername = false;
protected static supportsUsername = true;
public project: SfProject;
protected metadataTypeInfos: MetadataTypeInfos;
protected sfdxProjectJson: SfProjectJson;
protected actualFlags: TestDependenciesFlags;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private static eqSet(as: Set<any>, bs: Set<any>): boolean {
if (as.size !== bs.size) return false;
for (const a of as) if (!bs.has(a)) return false;
return true;
}
public async run(): Promise<AnyJson> {
this.actualFlags = ((await this.parse(TestDependencies)).flags as unknown) as TestDependenciesFlags;
this.metadataTypeInfos = JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', '..', '..', 'cfg', 'metadata', 'metadataTypeInfos.json')).toString()) as MetadataTypeInfos;
this.project = await SfProject.resolve();
this.sfdxProjectJson = this.project.getSfProjectJson();
if (this.actualFlags.config) {
return this.configurePlugin();
}
return this.computeDeltaTestClassesList();
}
private async computeDeltaTestClassesList(): Promise<AnyJson> {
const allApexClasses = glob.sync(`${this.project.getPackageDirectories()[0].fullPath}main/default/classes/*.cls`);
const apexClassesContentsByName: {
[key: string]: string;
} = allApexClasses.filter(c => !path.basename(c, '.cls').endsWith(this.actualFlags.nameconv)).reduce((acc, f) => {
acc[path.basename(f, '.cls')] = fs.readFileSync(f).toString();
return acc;
}, {});
const allApexTestClasses = new Set(allApexClasses.filter(c => path.basename(c, '.cls').endsWith(this.actualFlags.nameconv)).map(c => path.basename(c, '.cls')));
const deltaApexCodeClasses: Set<string> = new Set<string>();
const deltaPackageXml = await parseXml(this.actualFlags.manifest);
const destructivePackageXml = await parseXml(this.actualFlags.destructivemanifest);
let strategy = 'full';
const frontier: Set<string> = new Set<string>();
const closedList: Set<string> = new Set<string>();
const apexTestClasses: Set<string> = new Set<string>();
let testLevel = 'RunLocalTests';
const plugins = this.sfdxProjectJson.get('plugins');
const pluginConfig = plugins['mdataDeltaTests'] as PluginConfig;
if (pluginConfig) {
const metataTypesToCheck: string[] = Object.keys(pluginConfig);
for (const metadataType of metataTypesToCheck) {
if (pluginConfig[metadataType] === 's') {
continue;
}
const metadataTypePc: string = metadataType[0].toUpperCase() + metadataType.substring(1);
const deployMembers: string[] = (jsonQuery(`Package.types[name=${metadataTypePc}].members`, { data: deltaPackageXml }).value as string[]) || [];
const destroyMembers: string[] = (jsonQuery(`Package.types[name=${metadataTypePc}].members`, { data: destructivePackageXml }).value as string[]) || [];
if ((deployMembers.length > 0 && (pluginConfig[metadataType] === 'f' || (pluginConfig[metadataType] as MetadataSpecificAction).onDeploy === 'f')) ||
destroyMembers.length > 0 && (pluginConfig[metadataType] === 'f' || (pluginConfig[metadataType] as MetadataSpecificAction).onDestroy === 'f')) {
strategy = 'full';
break;
}
strategy = 'delta';
deployMembers.forEach((member: string) => {
if (metadataTypePc === 'ApexClass' && !member.endsWith(this.actualFlags.nameconv)) {
deltaApexCodeClasses.add(member);
}
if (metadataTypePc === 'ApexClass' && member.endsWith(this.actualFlags.nameconv)) {
apexTestClasses.add(member);
} else {
frontier.add(member);
while (member.includes('.')) {
member = member.substring(member.indexOf('.') + 1);
frontier.add(member);
}
}
});
destroyMembers.forEach((member: string) => {
frontier.add(member);
while (member.includes('.')) {
member = member.substring(member.indexOf('.') + 1);
frontier.add(member);
}
});
}
}
if (strategy === 'full') {
if (!this.jsonEnabled()) {
this.log(`-l ${testLevel}`);
}
return { testLevel, classList: [] };
}
if (!frontier.size) {
testLevel = this.actualFlags.prod ? 'RunLocalTests' : 'NoTestRun';
if (!this.jsonEnabled()) {
this.log(`-l ${testLevel}`);
}
return { testLevel, classList: [] };
}
if (strategy === 'delta') {
const curFrontier = new Set<string>(frontier);
let curDepth = 0;
while ((this.actualFlags.depth < 0 || curDepth < this.actualFlags.depth) && curFrontier.size) {
const frontierQueue = Array.from(curFrontier);
curFrontier.clear();
while (frontierQueue.length) {
const curMember = frontierQueue.splice(0, 1)[0];
closedList.add(curMember);
Object.keys(apexClassesContentsByName).forEach(apexClass => {
let fuzzyRes: fastFuzzy.MatchData<string>;
const potentialTestClass = `${apexClass}${this.actualFlags.nameconv}`;
if (allApexTestClasses.has(potentialTestClass) && !apexTestClasses.has(potentialTestClass) && apexClassesContentsByName[apexClass].includes(curMember)) {
this.log(`Adding Test Class ${potentialTestClass} (exact match) since it depends on ${curMember}`);
if (!curFrontier.has(apexClass) && !closedList.has(apexClass)) {
curFrontier.add(apexClass);
}
deltaApexCodeClasses.add(apexClass);
apexTestClasses.add(potentialTestClass);
} else if (allApexTestClasses.has(potentialTestClass) && !apexTestClasses.has(potentialTestClass) && apexClassesContentsByName[apexClass].match(new RegExp(curMember, 'ig'))) {
this.log(`Adding Test Class ${potentialTestClass} (regex match) since it depends on ${curMember}`);
if (!curFrontier.has(apexClass) && !closedList.has(apexClass)) {
curFrontier.add(apexClass);
}
deltaApexCodeClasses.add(apexClass);
apexTestClasses.add(potentialTestClass);
} else if (allApexTestClasses.has(potentialTestClass) && !apexTestClasses.has(potentialTestClass) && ((fuzzyRes = fastFuzzy.fuzzy(curMember, apexClassesContentsByName[apexClass], { useSellers: false, returnMatchData: true })).score >= Number(this.actualFlags.fuzzythreshold))) {
this.log(`Adding Test Class ${potentialTestClass} (fuzzy match, score: ${fuzzyRes.score}) since it depends on ${curMember}`);
if (!frontier.has(apexClass) && !closedList.has(apexClass)) {
curFrontier.add(apexClass);
}
deltaApexCodeClasses.add(apexClass);
apexTestClasses.add(potentialTestClass);
}
});
}
curDepth++;
}
}
if (this.actualFlags.usecodecoverage && deltaApexCodeClasses.size > 0) {
const coverageRecords: Array<{
[key: string]: {
Name: string;
};
}> = await (new Promise((resolve, reject) => {
const records: Array<{
[key: string]: {
Name: string;
};
}> = [];
void (this.actualFlags['target-org'] as Org).getConnection(this.actualFlags['api-version'] as string).tooling.query(`SELECT ApexTestClass.Name, ApexClassOrTrigger.Name FROM ApexCodeCoverage WHERE ApexClassOrTrigger.Name IN (${sqlstring.escape(Array.from(deltaApexCodeClasses))})`)
.on('record', (record: {
[key: string]: {
Name: string;
};
}) => {
records.push(record);
})
.on('error', err => {
reject(err);
})
.on('end', () => {
resolve(records);
})
.run({ autoFetch: true, maxFetch: 1000000 });
}));
coverageRecords.filter(coverageRecord => !apexTestClasses.has(coverageRecord['ApexTestClass'].Name) && allApexTestClasses.has(coverageRecord['ApexTestClass'].Name)).forEach(coverageRecord => {
this.log(`Adding Test Class ${coverageRecord['ApexTestClass'].Name} (ApexCodeCoverage) since it covers ${coverageRecord['ApexClassOrTrigger'].Name}`);
apexTestClasses.add(coverageRecord['ApexTestClass'].Name);
});
}
if (apexTestClasses.size === 0 || TestDependencies.eqSet(allApexTestClasses, apexTestClasses)) {
if (!this.jsonEnabled()) {
this.log(`-l ${testLevel}`);
}
return { testLevel, classList: [] };
} else {
testLevel = 'RunSpecifiedTests';
if (!this.jsonEnabled()) {
this.log(`-l ${testLevel} -r ${Array.from(apexTestClasses).join(',')}`);
}
return { testLevel, classList: Array.from(apexTestClasses) };
}
}
private async configurePlugin(): Promise<AnyJson> {
const pluginConfig = (this.sfdxProjectJson.get('plugins') && this.sfdxProjectJson.get('plugins')['mdataDeltaTests'] || {}) as PluginConfig;
const baseDir = this.project.getPackageDirectories()[0].fullPath;
for (const metadataTypeName of Object.keys(this.metadataTypeInfos.typeDefs).sort()) {
const metadataType = this.metadataTypeInfos.typeDefs[metadataTypeName];
const folderChunks: string[] = [];
let curMetadataType: MetadataType | undefined = metadataType;
do {
folderChunks.unshift(`**/${curMetadataType.defaultDirectory}`);
curMetadataType = curMetadataType.parent;
} while (curMetadataType);
if (glob.sync(path.join(baseDir, ...folderChunks)).length) {
const sfdxKeyName = metadataType.metadataName[0].toString().toLowerCase() + metadataType.metadataName.substring(1);
const choices = [
{ title: 'Full', value: 'f' },
{ title: 'Delta', value: 'd' },
{ title: 'Skip', value: 's' }
];
const initial = Object.prototype.hasOwnProperty.call(pluginConfig, sfdxKeyName) ? choices.findIndex(c => c.value === pluginConfig[sfdxKeyName] || c.value === (pluginConfig[sfdxKeyName] as MetadataSpecificAction).onDeploy) : 0;
// eslint-disable-next-line no-await-in-loop
const testResponse = await prompts({
type: 'select',
name: 'strat',
message: `Pick the Apex Test Strategy to apply when the MetadataType "${metadataType.metadataName}" appears in the Delta Deployment.`,
choices,
initial
});
if (testResponse.strat !== 's' && metadataType.deleteSupported) {
// eslint-disable-next-line no-await-in-loop
const sepDeleteResponse = await prompts({
type: 'toggle',
name: 'strat',
message: `Do you want to specify a different strategy when a component of MetadataType "${metadataType.metadataName}" gets deleted?`,
initial: (Object.prototype.hasOwnProperty.call(pluginConfig, sfdxKeyName) && Object.prototype.hasOwnProperty.call(pluginConfig[sfdxKeyName] as MetadataSpecificAction, 'onDestroy')) as boolean,
active: 'yes',
inactive: 'no'
});
if (sepDeleteResponse.strat) {
const deleteInitial = (Object.prototype.hasOwnProperty.call(pluginConfig, sfdxKeyName) && Object.prototype.hasOwnProperty.call(pluginConfig[sfdxKeyName], 'onDestroy')) ? choices.findIndex(c => c.value === (pluginConfig[sfdxKeyName] as MetadataSpecificAction).onDestroy) : 0;
// eslint-disable-next-line no-await-in-loop
const deleteResponse = await prompts({
type: 'select',
name: 'strat',
message: `Pick the Apex Test Strategy to apply when the MetadataType "${metadataType.metadataName}" appears in the Destructive Changes Deployment.`,
choices,
initial: deleteInitial
});
pluginConfig[sfdxKeyName] = {
onDeploy: testResponse.strat as 's' | 'd' | 'f',
onDestroy: deleteResponse.strat as 's' | 'd' | 'f'
};
} else {
pluginConfig[sfdxKeyName] = testResponse.strat as 's' | 'd' | 'f';
}
} else {
pluginConfig[sfdxKeyName] = testResponse.strat as 's' | 'd' | 'f';
}
}
}
if (!this.sfdxProjectJson.has('plugins')) {
this.sfdxProjectJson.set('plugins', {});
}
const plugins = this.sfdxProjectJson.get('plugins');
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
plugins['mdataDeltaTests'] = pluginConfig;
this.sfdxProjectJson.set('plugins', plugins);
await this.sfdxProjectJson.write(this.sfdxProjectJson.getContents());
this.log('Configuration written to sfdx-project.json');
return pluginConfig;
}
}