Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add apex code coverage reporters #284

Merged
merged 21 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"printWidth": 80,
"tabWidth": 2,
"singleQuote": true
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "salesforcedx-apex",
"dependencies": {
"node": "12.4.0",
"node": "^14.14.0",
"npm": "^6.9.0"
},
"devDependencies": {
Expand All @@ -22,7 +22,7 @@
"lint-staged": "^10.2.11",
"mocha": "^5",
"prettier": "1.19.1",
"typescript": "3.7.5"
"typescript": "4.6.4"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -56,7 +56,7 @@
},
"private": true,
"volta": {
"node": "12.4.0",
"node": "14.0.0",
"yarn": "1.22.4"
},
"config": {
Expand Down
18 changes: 14 additions & 4 deletions packages/apex-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,34 @@
"main": "lib/src/index.js",
"dependencies": {
"@salesforce/core": "^2.35.0",
"faye": "1.4.0"
"faye": "1.4.0",
"glob": "^8.0.1",
"istanbul-lib-coverage": "^3.2.0",
"istanbul-lib-report": "^3.0.0",
"istanbul-reports": "^3.1.4"
},
"devDependencies": {
"@salesforce/ts-sinon": "^1.1.2",
"@salesforce/ts-types": "1.2.2",
"@types/chai": "^4",
"@types/fs-extra": "^9.0.13",
"@types/istanbul-lib-coverage": "^2.0.4",
"@types/istanbul-lib-report": "^3.0.0",
"@types/istanbul-reports": "^3.0.1",
"@types/mkdirp": "0.5.2",
"@types/mocha": "^5",
"@types/node": "^10",
"@types/sinon": "^7.5.2",
"chai": "^4",
"fs-extra": "^10.1.0",
"mocha": "^5",
"mocha-junit-reporter": "^1.23.3",
"nyc": "^14.1.1",
"shx": "0.2.2",
"shx": "^0.3.4",
"sinon": "^7.3.1",
"source-map-support": "^0.5.16",
"typescript": "4.3.2"
"ts-node": "^10.7.0",
"typescript": "4.6.4"
},
"homepage": "https://github.com/forcedotcom/salesforcedx-apex",
"keywords": [
Expand Down Expand Up @@ -56,6 +66,6 @@
},
"license": "BSD-3-Clause",
"engines": {
"node": ">=10.17.0"
"node": ">=14.14.0"
}
}
Empty file.
Empty file.
Empty file.
163 changes: 163 additions & 0 deletions packages/apex-node/src/coverageReporters/covergeReporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Copyright (c) 2022, 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 {
ApexCodeCoverage,
ApexCodeCoverageAggregate,
ApexCodeCoverageAggregateRecord,
ApexCodeCoverageRecord
} from '../tests/types';
import * as libReport from 'istanbul-lib-report';
import * as reports from 'istanbul-reports';
import * as libCoverage from 'istanbul-lib-coverage';
import * as path from 'path';
import { glob } from 'glob';
import * as fs from 'fs';

const startOfSource = (source: string): number => {
if (source) {
return source.search(/\S/) || 0;
}
return 0;
};
const endOfSource = (source: string): number => {
if (source) {
return source.search(/\S$/) || 0;
}
return 0;
};

export type CoverageReportFormats = reports.ReportType;

export const DefaultWatermarks: libReport.Watermarks = {
statements: [50, 75],
functions: [50, 75],
branches: [50, 75],
lines: [50, 75]
};

export const DefaultReportOptions: Partial<reports.ReportOptions> = {
clover: { file: 'clover.xml', projectRoot: '.' },
cobertura: { file: 'cobertura.xml', projectRoot: '.' },
'html-spa': {
verbose: false,
skipEmpty: false,
subdir: 'html-spa',
linkMapper: undefined,
metricsToShow: ['lines', 'statements', 'branches']
},
html: {
verbose: false,
skipEmpty: false,
subdir: 'html',
linkMapper: undefined
},
json: { file: 'coverage.json' },
'json-summary': { file: 'coverage-summary.json' },
lcovonly: { file: 'lcovonly.info', projectRoot: '.' },
none: {} as never,
teamcity: { file: 'teamcity.txt', blockName: 'coverage' },
text: { file: 'text.txt', maxCols: 160, skipEmpty: false, skipFull: false },
'text-summary': { file: 'text-summary.txt' }
};

export interface CoverageReporterOptions {
reportFormats?: CoverageReportFormats[];
reportOptions?: Partial<typeof DefaultReportOptions>;
watermark?: typeof DefaultWatermarks;
}

export class CoverageReporter {
private readonly coverageMap: libCoverage.CoverageMap;
constructor(
private readonly coverage: ApexCodeCoverageAggregate | ApexCodeCoverage,
private readonly reportDir: string,
private readonly sourceDir: string,
private readonly options?: CoverageReporterOptions
) {
this.coverageMap = this.buildCoverageMap();
}

public generateReports(): void {
const context = libReport.createContext({
dir: this.reportDir,
defaultSummarizer: 'nested',
watermarks: this.options?.watermark || DefaultWatermarks,
coverageMap: this.coverageMap
});
const formats = this.options?.reportFormats || ['text-summary'];
formats.forEach(format => {
const report = reports.create(
format,
this.options?.reportOptions[format] || DefaultReportOptions[format]
);
report.execute(context);
});
}

private buildCoverageMap(): libCoverage.CoverageMap {
const coverageMap = libCoverage.createCoverageMap();
this.coverage.records.forEach(
(record: ApexCodeCoverageRecord | ApexCodeCoverageAggregateRecord) => {
const fileCoverageData: libCoverage.FileCoverageData = {} as libCoverage.FileCoverageData;
fileCoverageData.fnMap = {};
fileCoverageData.branchMap = {};
fileCoverageData.path = path.join(
this.sourceDir,
this.findFullPathToClass(record.ApexClassOrTrigger.Name)
);
fileCoverageData.f = {};
fileCoverageData.b = {};
fileCoverageData.s = [
...record.Coverage.coveredLines.map(line => [line, 1]),
...record.Coverage.uncoveredLines.map(line => [line, 0])
]
.map(([line, covered]) => [Number(line).toString(10), covered])
.reduce((acc, [line, value]) => {
return Object.assign(acc, { [line]: value });
}, {});
let sourceLines: string[] = [];
try {
sourceLines = fs
.readFileSync(fileCoverageData.path, 'utf8')
.split('\n');
} catch {
// file not found
}
fileCoverageData.statementMap = [
...record.Coverage.coveredLines,
...record.Coverage.uncoveredLines
]
.sort()
.map(line => {
const statement: libCoverage.Range = {
start: {
line,
column: startOfSource(sourceLines[line - 1])
},
end: {
line,
column: endOfSource(sourceLines[line - 1])
}
};

return [Number(line).toString(10), statement];
})
.reduce((acc, [line, value]) => {
return Object.assign(acc, { [Number(line).toString()]: value });
}, {});
coverageMap.addFileCoverage(fileCoverageData);
}
);
return coverageMap;
}

private findFullPathToClass(classOrTriggerName: string): string {
const searchPattern = `**/${classOrTriggerName}.{cls,trigger}`;
const files = glob.sync(searchPattern, { cwd: this.sourceDir });
return files[0] ? files[0] : classOrTriggerName;
}
}
14 changes: 14 additions & 0 deletions packages/apex-node/src/coverageReporters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last review question on this @peternhale, more of a semantic one - could this be included under src/reporters? Should it be? Or do you feel this is distinct enough to merit the separation? Just thinking for future maintenance, we may wonder why we have two different reporter directories.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had considered using reporters dir, but felt like it was a bit too different. I an on the fence, so I don't mind moving it to reporters.

* Copyright (c) 2020, salesforce.com, inc.
peternhale marked this conversation as resolved.
Show resolved Hide resolved
* 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
*/

export {
CoverageReporter,
CoverageReporterOptions,
CoverageReportFormats,
DefaultReportOptions,
DefaultWatermarks
} from './covergeReporter';
peternhale marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 9 additions & 0 deletions packages/apex-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ export {
export { LogService, ApexLogGetOptions, LogRecord, LogResult } from './logs';
export { JUnitReporter, TapReporter, HumanReporter } from './reporters';
export {
CoverageReporterOptions,
CoverageReporter,
CoverageReportFormats,
DefaultReportOptions,
DefaultWatermarks
} from './coverageReporters';
export {
ApexCodeCoverageAggregate,
ApexCodeCoverageAggregateRecord,
ApexTestProgressValue,
ApexTestResultData,
ApexTestResultOutcome,
Expand Down
16 changes: 8 additions & 8 deletions packages/apex-node/src/tests/codeCoverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ export class CodeCoverage {
return perClassCoverageMap;
}

private async queryPerClassCodeCov(
apexTestClassSet: Set<string>
): Promise<ApexCodeCoverage[]> {
const perClassCodeCovQuery =
'SELECT ApexTestClassId, ApexClassOrTrigger.Id, ApexClassOrTrigger.Name, TestMethodName, NumLinesCovered, NumLinesUncovered, Coverage FROM ApexCodeCoverage WHERE ApexTestClassId IN (%s)';
return this.fetchResults(apexTestClassSet, perClassCodeCovQuery);
}

/**
* Returns the aggregate code coverage information from ApexCodeCoverageAggregate entity for a given set of Apex classes
* @param apexClassIdSet Set of ids for Apex classes
Expand Down Expand Up @@ -160,6 +152,14 @@ export class CodeCoverage {
};
}

private async queryPerClassCodeCov(
apexTestClassSet: Set<string>
): Promise<ApexCodeCoverage[]> {
const perClassCodeCovQuery =
'SELECT ApexTestClassId, ApexClassOrTrigger.Id, ApexClassOrTrigger.Name, TestMethodName, NumLinesCovered, NumLinesUncovered, Coverage FROM ApexCodeCoverage WHERE ApexTestClassId IN (%s)';
return this.fetchResults(apexTestClassSet, perClassCodeCovQuery);
}

private async queryAggregateCodeCov(
apexClassIdSet: Set<string>
): Promise<ApexCodeCoverageAggregate[]> {
Expand Down
2 changes: 2 additions & 0 deletions packages/apex-node/src/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

export { TestService } from './testService';
export {
ApexCodeCoverageAggregate,
ApexCodeCoverageAggregateRecord,
ApexTestProgressValue,
ApexTestRunResultStatus,
AsyncTestConfiguration,
Expand Down
Loading