Skip to content

Commit

Permalink
fix!: remove covered line adjustment in deploy coverage reports
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Dec 22, 2024
1 parent cddded4 commit 0bf5e1e
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 541 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ sf apex get test --test-run-id <test run id> --code-coverage --result-format jso

The code coverage JSONs created by the Salesforce CLI aren't accepted automatically for Salesforce DX repositories and needs to be converted using this plugin.

**Disclaimer**: Due to existing bugs with how the Salesforce CLI reports covered lines during deployments (see [5511](https://github.com/forcedotcom/salesforcedx-vscode/issues/5511) and [1568](https://github.com/forcedotcom/cli/issues/1568)), to add support for covered lines in this plugin for deployment coverage files, I had to add a function to re-number out-of-range covered lines the CLI may report (ex: line 100 in a 98-line Apex Class is reported back as covered by the Salesforce CLI deploy command). Salesforce's coverage result may also include extra lines as covered (ex: 120 lines are included in the coverage report for a 100 line file), so the coverage percentage may vary based on how many lines the API returns in the coverage report. Once Salesforce fixes the API to correctly return covered lines in the deploy command, this function will be removed.

## Command

The `apex-code-coverage-transformer` has 1 command:
Expand Down
8 changes: 0 additions & 8 deletions src/helpers/getTotalLines.ts

This file was deleted.

48 changes: 48 additions & 0 deletions src/helpers/handleCloverFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
/* eslint-disable no-param-reassign */

import { CloverCoverageObject, CloverFile } from './types.js';
import { normalizePathToUnix } from './normalizePathToUnix.js';

export function handleCloverFormat(
filePath: string,
fileName: string,
lines: Record<string, number>,
uncoveredLines: number[],
coveredLines: number[],
coverageObj: CloverCoverageObject
): void {
const cloverFile: CloverFile = {
'@name': fileName,
'@path': normalizePathToUnix(filePath),
metrics: {
'@statements': uncoveredLines.length + coveredLines.length,
'@coveredstatements': coveredLines.length,
'@conditionals': 0,
'@coveredconditionals': 0,
'@methods': 0,
'@coveredmethods': 0,
},
line: [],
};

for (const [lineNumber, isCovered] of Object.entries(lines)) {
cloverFile.line.push({
'@num': Number(lineNumber),
'@count': isCovered === 1 ? 1 : 0,
'@type': 'stmt',
});
}

coverageObj.coverage.project.file.push(cloverFile);
const projectMetrics = coverageObj.coverage.project.metrics;

projectMetrics['@statements'] += uncoveredLines.length + coveredLines.length;
projectMetrics['@coveredstatements'] += coveredLines.length;
projectMetrics['@elements'] += uncoveredLines.length + coveredLines.length;
projectMetrics['@coveredelements'] += coveredLines.length;
projectMetrics['@files'] += 1;
projectMetrics['@classes'] += 1;
projectMetrics['@loc'] += uncoveredLines.length + coveredLines.length;
projectMetrics['@ncloc'] += uncoveredLines.length + coveredLines.length;
}
43 changes: 43 additions & 0 deletions src/helpers/handleCoberturaFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';
/* eslint-disable no-param-reassign */

import { CoberturaCoverageObject, CoberturaClass, CoberturaPackage } from './types.js';
import { normalizePathToUnix } from './normalizePathToUnix.js';

export function handleCoberturaFormat(
filePath: string,
fileName: string,
lines: Record<string, number>,
uncoveredLines: number[],
coveredLines: number[],
coverageObj: CoberturaCoverageObject,
packageObj: CoberturaPackage
): void {
const classObj: CoberturaClass = {
'@name': fileName,
'@filename': normalizePathToUnix(filePath),
'@line-rate': (coveredLines.length / (coveredLines.length + uncoveredLines.length)).toFixed(4),
'@branch-rate': '1',
methods: {},
lines: {
line: [],
},
};

for (const [lineNumber, isCovered] of Object.entries(lines)) {
classObj.lines.line.push({
'@number': Number(lineNumber),
'@hits': isCovered === 1 ? 1 : 0,
'@branch': 'false',
});
}

coverageObj.coverage['@lines-valid'] += uncoveredLines.length + coveredLines.length;
coverageObj.coverage['@lines-covered'] += coveredLines.length;
packageObj.classes.class.push(classObj);

packageObj['@line-rate'] = Number(
(coverageObj.coverage['@lines-covered'] / coverageObj.coverage['@lines-valid']).toFixed(4)
);
coverageObj.coverage['@line-rate'] = packageObj['@line-rate'];
}
27 changes: 27 additions & 0 deletions src/helpers/handleSonarFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
/* eslint-disable no-param-reassign */

import { SonarCoverageObject, SonarClass } from './types.js';
import { normalizePathToUnix } from './normalizePathToUnix.js';

export function handleSonarFormat(
filePath: string,
lines: Record<string, number>,
coverageObj: SonarCoverageObject
): void {
const fileObj: SonarClass = {
'@path': normalizePathToUnix(filePath),
lineToCover: [],
};

for (const lineNumberString in lines) {
if (!Object.hasOwn(lines, lineNumberString)) continue;
const covered = lines[lineNumberString] === 1 ? 'true' : 'false';
fileObj.lineToCover.push({
'@lineNumber': Number(lineNumberString),
'@covered': covered,
});
}

coverageObj.coverage.file.push(fileObj);
}
45 changes: 0 additions & 45 deletions src/helpers/setCoveredLinesClover.ts

This file was deleted.

45 changes: 0 additions & 45 deletions src/helpers/setCoveredLinesCobertura.ts

This file was deleted.

40 changes: 0 additions & 40 deletions src/helpers/setCoveredLinesSonar.ts

This file was deleted.

Loading

0 comments on commit 0bf5e1e

Please sign in to comment.