-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
37b3dfb
chore: wip
peternhale 03a36ad
chore: wip adding code coverage reporting
peternhale a7ff933
chore: revert prettier line width
peternhale 1272279
chore: wip
peternhale 947370a
chore: fix tests
peternhale 782539d
chore: wip
peternhale 16cbf0a
chore: apply review commets
peternhale 018fbf4
chore: (maybe) fix tests
peternhale 62acfd4
chore: apply review comments
peternhale 0b31334
chore: remove skip lib check for apex-node
peternhale 0637d0c
chore: remove node 12 from builds
peternhale 94b8e99
chore: fix job refs
peternhale d02fc2e
chore: more review suggestions applied
peternhale c3c910a
chore: add aditional tests and error handling
peternhale 0251fa8
chore: move coverage reporter to reporters dir
peternhale 99592af
chore: move coverage reporter tests to reporters dir
peternhale 1edcef5
chore: make readonly for users and groups
peternhale 22c449c
chore: try to fix test failures
peternhale ddabaca
chore: debug
peternhale 72d3e06
chore: try no access
peternhale 5280141
chore: remove the test
peternhale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
"printWidth": 80, | ||
"tabWidth": 2, | ||
"singleQuote": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
163 changes: 163 additions & 0 deletions
163
packages/apex-node/src/coverageReporters/covergeReporter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* 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
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.