-
Notifications
You must be signed in to change notification settings - Fork 91
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: support monocart report #521
Merged
Merged
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2308d4a
feat: support monocart report
cenfun caae21f
Update version 2.7.3
cenfun 1a7d020
2.7.10
cenfun 59a6161
Merge branch 'main' into monocart-report
bcoe 4e856a2
better integration into the report.js
cenfun 28be91c
doc: explain for monocart
cenfun 6c20c54
Update README.md
cenfun 7e60c5c
move MCR to peerDependencies
cenfun aecd2b3
Update lib/parse-args.js
cenfun 2192d25
doc(CONTRIBUTING): remove dead link, update broken link (#526)
msimerson 1a82a95
better integration into the report.js
cenfun 4dbfe35
doc: explain for monocart
cenfun 27966b3
Update README.md
cenfun 5aa4422
move MCR to peerDependencies
cenfun 9566904
Update lib/parse-args.js
cenfun 2218b53
Merge branch 'monocart-report' of https://github.com/cenfun/c8 into m…
cenfun 237d262
resolve conflicts
cenfun 4830f62
Merge remote-tracking branch 'origin/main' into monocart-report
cenfun 1d2ff88
resolve conflicts
cenfun 85303b4
move to try/catch
cenfun 714cdd6
test case for monocart installation
cenfun c88abbf
optimize arguments
cenfun 6fd00e5
Update lib/report.js
bcoe 4b0947a
fixed unit test
cenfun 67bbd57
updated the test case for import-mcr
cenfun 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,8 @@ class Report { | |
allowExternal = false, | ||
skipFull, | ||
excludeNodeModules, | ||
mergeAsync | ||
mergeAsync, | ||
monocartArgv | ||
}) { | ||
this.reporter = reporter | ||
this.reporterOptions = reporterOptions || {} | ||
|
@@ -60,6 +61,7 @@ class Report { | |
this.src = this._getSrc(src) | ||
this.skipFull = skipFull | ||
this.mergeAsync = mergeAsync | ||
this.monocartArgv = monocartArgv | ||
} | ||
|
||
_getSrc (src) { | ||
|
@@ -73,6 +75,9 @@ class Report { | |
} | ||
|
||
async run () { | ||
if (this.monocartArgv) { | ||
return this.runMonocart() | ||
} | ||
const context = libReport.createContext({ | ||
dir: this.reportsDirectory, | ||
watermarks: this.watermarks, | ||
|
@@ -89,6 +94,137 @@ class Report { | |
} | ||
} | ||
|
||
async getMonocart () { | ||
let MCR | ||
try { | ||
MCR = await import('monocart-coverage-reports') | ||
} catch (e) { | ||
console.error('ERROR: Failed to import module "monocart-coverage-reports". Please try to install it: npm i monocart-coverage-reports --save-dev') | ||
bcoe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
process.exit(1) | ||
} | ||
return MCR | ||
} | ||
|
||
async runMonocart () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I prefer now that we're sharing some of the same behaviour in |
||
const MCR = await this.getMonocart() | ||
if (!MCR) { | ||
return | ||
} | ||
|
||
const argv = this.monocartArgv | ||
const exclude = this.exclude | ||
|
||
function getEntryFilter () { | ||
return argv.entryFilter || argv.filter || function (entry) { | ||
return exclude.shouldInstrument(fileURLToPath(entry.url)) | ||
} | ||
} | ||
|
||
function getSourceFilter () { | ||
return argv.sourceFilter || argv.filter || function (sourcePath) { | ||
if (argv.excludeAfterRemap) { | ||
// console.log(sourcePath) | ||
return exclude.shouldInstrument(sourcePath) | ||
} | ||
return true | ||
} | ||
} | ||
|
||
function getReports () { | ||
const reports = Array.isArray(argv.reporter) ? argv.reporter : [argv.reporter] | ||
const reporterOptions = argv.reporterOptions || {} | ||
|
||
return reports.map((reportName) => { | ||
const reportOptions = { | ||
...reporterOptions[reportName] | ||
} | ||
if (reportName === 'text') { | ||
reportOptions.skipEmpty = false | ||
reportOptions.skipFull = argv.skipFull | ||
reportOptions.maxCols = process.stdout.columns || 100 | ||
} | ||
return [reportName, reportOptions] | ||
}) | ||
} | ||
|
||
// --all: add empty coverage for all files | ||
function getAllOptions () { | ||
if (!argv.all) { | ||
return | ||
} | ||
|
||
const src = argv.src | ||
const workingDirs = Array.isArray(src) ? src : (typeof src === 'string' ? [src] : [process.cwd()]) | ||
return { | ||
dir: workingDirs, | ||
filter: (filePath) => { | ||
return exclude.shouldInstrument(filePath) | ||
} | ||
} | ||
} | ||
|
||
function initPct (summary) { | ||
Object.keys(summary).forEach(k => { | ||
if (summary[k].pct === '') { | ||
summary[k].pct = 100 | ||
} | ||
}) | ||
return summary | ||
} | ||
|
||
// adapt coverage options | ||
const coverageOptions = { | ||
logging: argv.logging, | ||
name: argv.name, | ||
|
||
reports: getReports(), | ||
|
||
outputDir: argv.reportsDir, | ||
baseDir: argv.baseDir, | ||
|
||
entryFilter: getEntryFilter(), | ||
sourceFilter: getSourceFilter(), | ||
|
||
inline: argv.inline, | ||
lcov: argv.lcov, | ||
|
||
all: getAllOptions(), | ||
|
||
clean: argv.clean, | ||
|
||
// use default value for istanbul | ||
defaultSummarizer: 'pkg', | ||
|
||
onEnd: (coverageResults) => { | ||
// for check coverage | ||
this._allCoverageFiles = { | ||
files: () => { | ||
return coverageResults.files.map(it => it.sourcePath) | ||
}, | ||
fileCoverageFor: (file) => { | ||
const fileCoverage = coverageResults.files.find(it => it.sourcePath === file) | ||
return { | ||
toSummary: () => { | ||
return initPct(fileCoverage.summary) | ||
} | ||
} | ||
}, | ||
getCoverageSummary: () => { | ||
return initPct(coverageResults.summary) | ||
} | ||
} | ||
} | ||
} | ||
const coverageReport = new MCR.CoverageReport(coverageOptions) | ||
coverageReport.cleanCache() | ||
|
||
// read v8 coverage data from tempDirectory | ||
await coverageReport.addFromDir(argv.tempDirectory) | ||
|
||
// generate report | ||
await coverageReport.generate() | ||
} | ||
|
||
async getCoverageMapFromAllCoverageFiles () { | ||
// the merge process can be very expensive, and it's often the case that | ||
// check-coverage is called immediately after a report. We memoize the | ||
|
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.
If we were unable to require Monocart, we should print the install instructions here and exit with 1.
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 have made the following improvements based on your suggestion
getMonocart ()
import("xxx")
function to load the lib asynchronously. It should be good enough without creating a new help file, and added new test case to cover it (rename the lib entry file can mock it).Please review, thanks.