-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathcoverage.ts
More file actions
130 lines (119 loc) · 5 KB
/
Copy pathcoverage.ts
File metadata and controls
130 lines (119 loc) · 5 KB
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
/*
* Copyright 2026, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as path from 'node:path';
import {
ApexTestResultData,
ApexTestResultOutcome,
CoverageReportFormats,
CoverageReporterOptions,
DefaultReportOptions,
ApexCodeCoverageAggregate,
ApexCodeCoverageAggregateRecord,
} from '@salesforce/apex-node';
import type { Successes, Failures, CodeCoverage } from '@salesforce/source-deploy-retrieve';
import { ensureArray } from '@salesforce/kit';
import { StandardColors } from '@salesforce/sf-plugins-core';
export const mapTestResults = <T extends Failures | Successes>(testResults: T[]): ApexTestResultData[] =>
testResults.map((testResult) => ({
apexClass: { fullName: testResult.name, id: testResult.id, name: testResult.name, namespacePrefix: '' },
apexLogId: '',
asyncApexJobId: '',
fullName: testResult.name,
id: testResult.id,
...('message' in testResult && testResult.message
? { message: testResult.message, outcome: ApexTestResultOutcome.Fail }
: { message: null, outcome: ApexTestResultOutcome.Pass }),
methodName: testResult.methodName,
queueItemId: '',
runTime: parseInt(testResult.time, 10),
stackTrace: 'stackTrace' in testResult ? testResult.stackTrace : null,
testTimestamp: '',
}));
export const generateCoveredLines = (cov: CodeCoverage): [number[], number[]] => {
const [lineCount] = getCoverageNumbers(cov);
const uncoveredLines = ensureArray(cov.locationsNotCovered).map((location) => parseInt(location.line, 10));
const minLineNumber = uncoveredLines.length ? Math.min(...uncoveredLines) : 1;
const lines = [...Array(lineCount).keys()].map((i) => i + minLineNumber);
const coveredLines = lines.filter((line) => !uncoveredLines.includes(line));
return [uncoveredLines, coveredLines];
};
export const getCoverageFormattersOptions = (formatters: string[] = []): CoverageReporterOptions => {
const reportFormats = formatters as CoverageReportFormats[];
const reportOptions = Object.fromEntries(
reportFormats.map((format) => {
const formatDefaults = DefaultReportOptions[format];
return [
format,
{
...formatDefaults,
// always join any subdir from the defaults with our custom coverage dir
...('subdir' in formatDefaults ? { subdir: path.join('coverage', formatDefaults.subdir) } : {}),
// if there is no subdir, we also put the file in the coverage dir, otherwise leave it alone
...('file' in formatDefaults && !('subdir' in formatDefaults)
? { file: path.join('coverage', formatDefaults.file) }
: {}),
},
];
})
);
return {
reportFormats,
reportOptions,
};
};
export const transformCoverageToApexCoverage = (mdCoverage: CodeCoverage[]): ApexCodeCoverageAggregate => {
const apexCoverage = mdCoverage.map((cov): ApexCodeCoverageAggregateRecord => {
const [NumLinesCovered, NumLinesUncovered] = getCoverageNumbers(cov);
const [uncoveredLines, coveredLines] = generateCoveredLines(cov);
return {
ApexClassOrTrigger: {
Id: cov.id,
Name: cov.name,
},
NumLinesCovered,
NumLinesUncovered,
Coverage: {
coveredLines,
uncoveredLines,
},
};
});
return { done: true, totalSize: apexCoverage.length, records: apexCoverage };
};
export const coverageOutput = (
cov: CodeCoverage
): Pick<CodeCoverage, 'name'> & { coveragePercent: string; linesNotCovered: string } => ({
name: cov.name,
coveragePercent: formatPercent(getCoveragePct(cov)),
linesNotCovered: cov.locationsNotCovered
? ensureArray(cov.locationsNotCovered)
.map((location) => location.line)
.join(',')
: '',
});
const color = (percent: number): typeof StandardColors.success =>
percent >= 90 ? StandardColors.success : percent >= 75 ? StandardColors.warning : StandardColors.error;
const formatPercent = (percent: number): string => color(percent)(`${percent}%`);
export const getCoveragePct = (cov: CodeCoverage): number => {
const [lineCount, uncoveredLineCount] = getCoverageNumbers(cov);
const coverageDecimal = parseFloat(((lineCount - uncoveredLineCount) / lineCount).toFixed(2));
return lineCount > 0 ? coverageDecimal * 100 : 100;
};
/** returns the number of total line for which coverage should apply, and the total uncovered line */
export const getCoverageNumbers = (cov: CodeCoverage): [lineCount: number, uncoveredLineCount: number] => [
parseInt(cov.numLocations, 10),
parseInt(cov.numLocationsNotCovered, 10),
];