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

fix: adds file extension to apex class and apex trigger #342

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"reporter": "./test/combinedTestReporter.js",
"reporter-options": "mochaFile=./test-results/test-results.xml",
"colors": true,
"exit": true
"exit": true,
"spec": "./test/**/*.test.ts"
}
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bugs": "https://github.com/forcedotcom/salesforcedx-apex/issues",
"main": "lib/src/index.js",
"dependencies": {
"@salesforce/core": "^6.1.0",
"@salesforce/core": "^6.4.2",
"@salesforce/kit": "^3.0.15",
"@types/istanbul-reports": "^3.0.4",
"faye": "1.4.0",
Expand All @@ -27,26 +27,26 @@
"@types/mocha": "^10.0.4",
"@types/node": "^18",
"@types/sinon": "^17.0.1",
"@typescript-eslint/eslint-plugin": "^6.10.0",
"@typescript-eslint/parser": "^6.10.0",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"chai": "^4.3.10",
"commitlint": "^18.2.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.53.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-header": "^3.0.0",
"eslint-plugin-jsdoc": "^46.8.2",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-prettier": "^5.1.2",
"husky": "^8.0.0",
"lint-staged": "^15.0.2",
"mocha": "^10.2.0",
"mocha-junit-reporter": "^2.2.1",
"nyc": "^15.1.0",
"prettier": "^3.0.3",
"prettier": "^3.1.1",
"shx": "^0.3.4",
"sinon": "^17.0.1",
"source-map-support": "^0.5.16",
"ts-node": "^10.7.0",
"ts-node": "^10.9.2",
"typescript": "^5.2.2"
},
"homepage": "https://github.com/forcedotcom/salesforcedx-apex",
Expand Down Expand Up @@ -86,4 +86,4 @@
"path": "./node_modules/cz-conventional-changelog"
}
}
}
}
31 changes: 14 additions & 17 deletions src/reporters/coverageReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class CoverageReporter {
coverageMap: this.coverageMap
});
const formats = this.options?.reportFormats || ['text-summary'];
formats.forEach((format) => {
formats.forEach(format => {
const report = reports.create(
format,
this.options?.reportOptions[format] || DefaultReportOptions[format]
Expand All @@ -121,27 +121,29 @@ export class CoverageReporter {
}

private buildCoverageMap(): libCoverage.CoverageMap {
const pathsToFiles = this.findFullPathToClass(['cls', 'trigger']);
const pathsToFiles = glob.sync('**/*.{cls,trigger}', {
cwd: this.sourceDir
});
const coverageMap = libCoverage.createCoverageMap();
this.coverage.records.forEach(
(record: ApexCodeCoverageRecord | ApexCodeCoverageAggregateRecord) => {
const fileCoverageData: libCoverage.FileCoverageData =
{} as libCoverage.FileCoverageData;
const fileRegEx = new RegExp(
`${record.ApexClassOrTrigger.Name}\.(cls|trigger)`
);
const fileCoverageData: libCoverage.FileCoverageData = {} as libCoverage.FileCoverageData;
const fileNameWithExtension = `${record.ApexClassOrTrigger.Name}.${
record.ApexClassOrTrigger.Id?.startsWith('01p') ? 'cls' : 'trigger'
}`;

fileCoverageData.fnMap = {};
fileCoverageData.branchMap = {};
fileCoverageData.path = path.join(
this.sourceDir,
pathsToFiles.find((file) => fileRegEx.test(file)) ||
record.ApexClassOrTrigger.Name
pathsToFiles.find(file => file === fileNameWithExtension) ||
fileNameWithExtension
);
fileCoverageData.f = {};
fileCoverageData.b = {};
fileCoverageData.s = [
...record.Coverage.coveredLines.map((line) => [line, 1]),
...record.Coverage.uncoveredLines.map((line) => [line, 0])
...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]) => {
Expand All @@ -160,7 +162,7 @@ export class CoverageReporter {
...record.Coverage.uncoveredLines
]
.sort()
.map((line) => {
.map(line => {
const statement: libCoverage.Range = {
start: {
line,
Expand All @@ -182,9 +184,4 @@ export class CoverageReporter {
);
return coverageMap;
}

private findFullPathToClass(listOfExtensions: string[]): string[] {
const searchPattern = `**/*.{${listOfExtensions.join(',')}}`;
return glob.sync(searchPattern, { cwd: this.sourceDir });
}
}
90 changes: 85 additions & 5 deletions test/reporters/coverageReporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,50 @@ const multipleCoverageAggregate = {
],
uncoveredLines: []
}
},
{
attributes: {
type: 'ApexCodeCoverageAggregate',
url:
'/services/data/v56.0/tooling/sobjects/ApexCodeCoverageAggregate/7153M000000ADC6QAO'
},
ApexClassOrTrigger: {
attributes: {
type: 'Name',
url:
'/services/data/v56.0/tooling/sobjects/ApexTrigger/01q3M000000CzMWQA0'
},
Id: '01q3M000000CzMWQA0',
Name: 'Bla'
},
NumLinesCovered: 1,
NumLinesUncovered: 0,
Coverage: {
coveredLines: [10],
uncoveredLines: []
}
},
{
attributes: {
type: 'ApexCodeCoverageAggregate',
url:
'/services/data/v56.0/tooling/sobjects/ApexCodeCoverageAggregate/7153M000000ADBwQAO'
},
ApexClassOrTrigger: {
attributes: {
type: 'Name',
url:
'/services/data/v56.0/tooling/sobjects/ApexClass/01p3M000000MLgJQAW'
},
Id: '01p3M000000MLgJQAW',
Name: 'Bla'
},
NumLinesCovered: 1,
NumLinesUncovered: 0,
Coverage: {
coveredLines: [3],
uncoveredLines: []
}
}
]
};
Expand All @@ -127,14 +171,13 @@ describe('coverageReports', () => {
const coverageReport = new CoverageReporter(
multipleCoverageAggregate,
testResultsDir,
'packages/apex-node/test/coverageReporters/testResources',
'test/reporters/testResources',
{
reportFormats: ['clover', 'html'],
reportOptions: {
clover: {
file: 'clover.xml',
projectRoot:
'packages/apex-node/test/coverageReporters/testResources'
projectRoot: 'test/reporters/testResources'
}
}
}
Expand All @@ -154,7 +197,7 @@ describe('coverageReports', () => {
const coverageReport = new CoverageReporter(
multipleCoverageAggregate,
testResultsDir,
'packages/apex-node/test/coverageReporters/testResources'
'test/reporters/testResources'
);
coverageReport.generateReports();
const textSummaryFile = path.join(testResultsDir, 'text-summary.txt');
Expand All @@ -173,7 +216,7 @@ describe('coverageReports', () => {
const coverageReport = new CoverageReporter(
coverageAggregate,
testResultsDir,
'packages/apex-node/test/coverageReporters/testResources'
'test/reporters/testResources'
);
coverageReport.generateReports();
const textSummaryFile = path.join(testResultsDir, 'text-summary.txt');
Expand Down Expand Up @@ -202,4 +245,41 @@ describe('coverageReports', () => {
const dirEntries = await fs.promises.readdir(testResultsDir);
expect(dirEntries).to.have.lengthOf(1);
});

describe('code coverage report', () => {
it('should contain file extension', async () => {
const coverageReport = new CoverageReporter(
multipleCoverageAggregate,
testResultsDir,
'test/reporters/testResources',
{
reportFormats: ['json'],
reportOptions: {
json: {
file: 'coverage.json'
}
}
}
);
coverageReport.generateReports();

const codeCoverage = JSON.parse(
fs.readFileSync(path.join(testResultsDir, 'coverage.json'), {
encoding: 'utf-8'
})
);

const files = multipleCoverageAggregate.records
.map(record => {
return `${record.ApexClassOrTrigger.Name}.${
record.ApexClassOrTrigger.Id.startsWith('01p') ? 'cls' : 'trigger'
}`;
})
.sort();

Object.keys(codeCoverage).forEach(codeCoverageEntry => {
expect(files).to.include(path.basename(codeCoverageEntry));
});
});
});
});
7 changes: 7 additions & 0 deletions test/reporters/testResources/main/default/classes/Bla.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class Bla{

public static void speak(){
System.debug('bla');
}

}
11 changes: 11 additions & 0 deletions test/reporters/testResources/main/default/triggers/Bla.trigger
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
trigger Bla on Bla__c(
before insert,
before update,
before delete,
after insert,
after update,
after delete,
after undelete
) {
Bla.speak();
}
Loading