generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 21
/
deployResultFormatter.ts
364 lines (329 loc) · 12.5 KB
/
deployResultFormatter.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
/*
* 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 * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';
import { ux } from '@oclif/core';
import { dim, underline, bold } from 'chalk';
import { DeployResult, Failures, FileResponse, RequestStatus, Successes } from '@salesforce/source-deploy-retrieve';
import { Org, SfError } from '@salesforce/core';
import { ensureArray } from '@salesforce/kit';
import {
CodeCoverageResult,
CoverageReporter,
CoverageReporterOptions,
JUnitReporter,
TestResult,
} from '@salesforce/apex-node';
import { StandardColors } from '@salesforce/sf-plugins-core';
import { DeployResultJson, isSdrFailure, isSdrSuccess, TestLevel, Verbosity, Formatter } from '../utils/types';
import {
generateCoveredLines,
getCoverageFormattersOptions,
mapTestResults,
transformCoverageToApexCoverage,
coverageOutput,
} from '../utils/coverage';
import { sortFileResponses, asRelativePaths, tableHeader, getFileResponseSuccessProps } from '../utils/output';
export class DeployResultFormatter implements Formatter<DeployResultJson> {
private relativeFiles: FileResponse[];
private absoluteFiles: FileResponse[];
private testLevel: TestLevel;
private verbosity: Verbosity;
private coverageOptions: CoverageReporterOptions;
private resultsDir: string;
private readonly junit: boolean | undefined;
public constructor(
protected result: DeployResult,
protected flags: Partial<{
'test-level': TestLevel;
verbose: boolean;
concise: boolean;
'coverage-formatters': string[];
junit: boolean;
'results-dir': string;
'target-org': Org;
}>
) {
this.absoluteFiles = sortFileResponses(this.result.getFileResponses() ?? []);
this.relativeFiles = asRelativePaths(this.absoluteFiles);
this.testLevel = this.flags['test-level'] ?? TestLevel.NoTestRun;
this.verbosity = this.determineVerbosity();
this.resultsDir = this.flags['results-dir'] ?? 'coverage';
this.coverageOptions = getCoverageFormattersOptions(this.flags['coverage-formatters']);
this.junit = this.flags.junit;
}
public getJson(): DeployResultJson {
if (this.verbosity === 'concise') {
return {
...this.result.response,
details: {
componentFailures: this.result.response.details.componentFailures,
runTestResult: this.result.response.details.runTestResult,
},
files: this.absoluteFiles.filter((f) => f.state === 'Failed'),
};
} else {
return {
...this.result.response,
files: this.absoluteFiles,
...(this.result.replacements.size
? {
replacements: Object.fromEntries(this.result.replacements),
}
: {}),
};
}
}
public display(): void {
if (this.verbosity !== 'concise') {
this.displaySuccesses();
}
this.displayFailures();
this.displayDeletes();
this.displayTestResults();
this.maybeCreateRequestedReports();
this.displayReplacements();
}
private maybeCreateRequestedReports(): void {
// only generate reports if test results are presented
if (this.result.response?.numberTestsTotal) {
if (this.coverageOptions.reportFormats?.length) {
ux.log(
`Code Coverage formats, [${this.flags['coverage-formatters']?.join(', ')}], written to ${this.resultsDir}/`
);
this.createCoverageReport('no-map');
}
if (this.junit) {
ux.log(`Junit results written to ${this.resultsDir}/junit/junit.xml`);
this.createJunitResults();
}
}
}
private createJunitResults(): void {
const testResult = this.transformDeployTestsResultsToTestResult();
if (testResult.summary.testsRan > 0) {
const jUnitReporter = new JUnitReporter();
const junitResults = jUnitReporter.format(testResult);
const junitReportPath = path.join(this.resultsDir ?? '', 'junit');
fs.mkdirSync(junitReportPath, { recursive: true });
fs.writeFileSync(path.join(junitReportPath, 'junit.xml'), junitResults, 'utf8');
}
}
private transformDeployTestsResultsToTestResult(): TestResult {
if (!this.result.response?.details?.runTestResult) {
throw new SfError('No test results found');
}
const runTestResult = this.result.response?.details?.runTestResult;
const numTestsRun = parseInt(runTestResult.numTestsRun, 10);
const numTestFailures = parseInt(runTestResult.numFailures, 10);
return {
summary: {
commandTimeInMs: 0,
failRate: ((numTestFailures / numTestsRun) * 100).toFixed(2) + '%',
failing: numTestFailures,
hostname: this.flags['target-org']?.getConnection().getConnectionOptions().instanceUrl as string,
orgId: this.flags['target-org']?.getConnection().getAuthInfoFields().orgId as string,
outcome: '',
passRate: numTestFailures === 0 ? '100%' : ((1 - numTestFailures / numTestsRun) * 100).toFixed(2) + '%',
passing: numTestsRun - numTestFailures,
skipRate: '',
skipped: 0,
testExecutionTimeInMs: parseFloat(runTestResult.totalTime),
testRunId: '',
testStartTime: new Date().toISOString(),
testTotalTimeInMs: parseFloat(runTestResult.totalTime),
testsRan: numTestsRun,
userId: this.flags['target-org']?.getConnection().getConnectionOptions().userId as string,
username: this.flags['target-org']?.getConnection().getUsername() as string,
},
tests: [
...mapTestResults(ensureArray(runTestResult.successes)),
...mapTestResults(ensureArray(runTestResult.failures)),
],
codecoverage: ensureArray(runTestResult?.codeCoverage).map((cov): CodeCoverageResult => {
const numLinesUncovered = parseInt(cov.numLocationsNotCovered, 10);
const [uncoveredLines, coveredLines] = generateCoveredLines(cov);
const numLocationsNum = parseInt(cov.numLocations, 10);
const numLocationsNotCovered: number = parseInt(cov.numLocationsNotCovered, 10);
return {
// TODO: fix this type in SDR?
type: cov.type as 'ApexClass' | 'ApexTrigger',
apexId: cov.id,
name: cov.name,
numLinesUncovered,
numLinesCovered: parseInt(cov.numLocations, 10) - numLinesUncovered,
coveredLines,
uncoveredLines,
percentage:
numLocationsNum > 0
? (((numLocationsNum - numLocationsNotCovered) / numLocationsNum) * 100).toFixed() + '%'
: '',
};
}),
};
}
private createCoverageReport(sourceDir: string): void {
if (this.resultsDir) {
const apexCoverage = transformCoverageToApexCoverage(
ensureArray(this.result.response?.details?.runTestResult?.codeCoverage)
);
fs.mkdirSync(this.resultsDir, { recursive: true });
const coverageReport = new CoverageReporter(apexCoverage, this.resultsDir, sourceDir, this.coverageOptions);
coverageReport.generateReports();
}
}
private displayReplacements(): void {
if (this.verbosity === 'verbose' && this.result.replacements?.size) {
const replacements = Array.from(this.result.replacements.entries()).flatMap(([filepath, stringsReplaced]) =>
stringsReplaced.map((replaced) => ({
filePath: path.relative(process.cwd(), filepath),
replaced,
}))
);
ux.table(
replacements,
{
filePath: { header: 'PROJECT PATH' },
replaced: { header: 'TEXT REPLACED' },
},
{
title: tableHeader('Metadata Replacements'),
}
);
}
}
private displaySuccesses(): void {
const successes = this.relativeFiles.filter((f) => f.state !== 'Failed');
if (!successes.length || this.result.response.status === RequestStatus.Failed) return;
const columns = {
state: { header: 'State' },
fullName: { header: 'Name' },
type: { header: 'Type' },
filePath: { header: 'Path' },
};
const title = this.result.response.checkOnly ? 'Validated Source' : 'Deployed Source';
const options = { title: tableHeader(title) };
ux.log();
ux.table(
successes.map((s) => ({ filePath: s.filePath, fullName: s.fullName, type: s.type, state: s.state })),
columns,
options
);
}
private displayFailures(): void {
if (this.result.response.status === RequestStatus.Succeeded) return;
const failures = this.relativeFiles.filter(isSdrFailure);
if (!failures.length) return;
const columns = {
problemType: { header: 'Type' },
fullName: { header: 'Name' },
error: { header: 'Problem' },
};
const options = { title: error(`Component Failures [${failures.length}]`) };
ux.log();
ux.table(
failures.map((f) => ({ problemType: f.problemType, fullName: f.fullName, error: f.error })),
columns,
options
);
}
private displayDeletes(): void {
const deletions = this.relativeFiles.filter(isSdrSuccess).filter((f) => f.state === 'Deleted');
if (!deletions.length) return;
const columns = {
fullName: { header: 'Name' },
type: { header: 'Type' },
filePath: { header: 'Path' },
};
const options = { title: tableHeader('Deleted Source') };
ux.log();
ux.table(getFileResponseSuccessProps(deletions), columns, options);
}
private displayTestResults(): void {
if (this.testLevel === TestLevel.NoTestRun) {
ux.log();
return;
}
this.displayVerboseTestFailures();
if (this.verbosity === 'verbose') {
this.displayVerboseTestSuccesses();
this.displayVerboseTestCoverage();
}
ux.log();
ux.log(tableHeader('Test Results Summary'));
const passing = this.result.response.numberTestsCompleted ?? 0;
const failing = this.result.response.numberTestErrors ?? 0;
const total = this.result.response.numberTestsTotal ?? 0;
const time = this.result.response.details.runTestResult?.totalTime ?? 0;
ux.log(`Passing: ${passing}`);
ux.log(`Failing: ${failing}`);
ux.log(`Total: ${total}`);
if (time) ux.log(`Time: ${time}`);
}
private displayVerboseTestSuccesses(): void {
const successes = ensureArray(this.result.response.details.runTestResult?.successes);
if (successes.length > 0) {
const testSuccesses = sortTestResults(successes);
ux.log();
ux.log(success(`Test Success [${successes.length}]`));
for (const test of testSuccesses) {
const testName = underline(`${test.name}.${test.methodName}`);
ux.log(`${check} ${testName}`);
}
}
}
private displayVerboseTestFailures(): void {
if (!this.result.response.numberTestErrors) return;
const failures = ensureArray(this.result.response.details.runTestResult?.failures);
const failureCount = this.result.response.details.runTestResult?.numFailures;
const testFailures = sortTestResults(failures);
ux.log();
ux.log(error(`Test Failures [${failureCount}]`));
for (const test of testFailures) {
const testName = underline(`${test.name}.${test.methodName}`);
const stackTrace = test.stackTrace.replace(/\n/g, `${os.EOL} `);
ux.log(`• ${testName}`);
ux.log(` ${dim('message')}: ${test.message}`);
ux.log(` ${dim('stacktrace')}: ${os.EOL} ${stackTrace}`);
ux.log();
}
}
private displayVerboseTestCoverage(): void {
const codeCoverage = ensureArray(this.result.response.details.runTestResult?.codeCoverage);
if (codeCoverage.length) {
const coverage = codeCoverage.sort((a, b) => (a.name.toUpperCase() > b.name.toUpperCase() ? 1 : -1));
ux.log();
ux.log(tableHeader('Apex Code Coverage'));
ux.table(coverage.map(coverageOutput), {
name: { header: 'Name' },
numLocations: { header: '% Covered' },
lineNotCovered: { header: 'Uncovered Lines' },
});
}
}
private determineVerbosity(): Verbosity {
if (this.flags.verbose) return 'verbose';
if (this.flags.concise) return 'concise';
return 'normal';
}
}
function sortTestResults<T extends Failures | Successes>(results: T[]): T[] {
return results.sort((a, b) => {
if (a.methodName === b.methodName) {
return a.name.localeCompare(b.name);
}
return a.methodName.localeCompare(b.methodName);
});
}
function error(message: string): string {
return StandardColors.error(bold(message));
}
function success(message: string): string {
return StandardColors.success(bold(message));
}
const check = StandardColors.success('✓');