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: support monocart report #521

Merged
merged 25 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Here is a list of common options. Run `c8 --help` for the full list and document
| `--per-file` | check thresholds per file | `boolean` | `false` |
| `--temp-directory` | directory V8 coverage data is written to and read from | `string` | `process.env.NODE_V8_COVERAGE` |
| `--clean` | should temp files be deleted before script execution | `boolean` | `true` |
| `--experimental-monocart` | see [section below](#using-monocart-coverage-reports-experimental) for more info | `boolean` | `false` |

## Checking for "full" source coverage using `--all`

Expand Down Expand Up @@ -119,6 +120,12 @@ The `--100` flag can be set for the `check-coverage` as well:
c8 check-coverage --100
```

## Using Monocart coverage reports (experimental)
[Monocart](https://github.com/cenfun/monocart-coverage-reports) will bring additional support for native V8 coverage reports, for example:
Copy link
Owner

Choose a reason for hiding this comment

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

I think we should explain what monocart is a bit better. It's an alternate tool for converting raw V8 coverage reports to other coverage formats, as an alternate to v8-to-istanbul and istanbul reports.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added some explanations, please take a look.

```sh
c8 --experimental-monocart --reporter=v8 --reporter=console-details node foo.js
```

## Ignoring Uncovered Lines, Functions, and Blocks

Sometimes you might find yourself wanting to ignore uncovered portions of your
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ exports.outputReport = async function (argv) {
src: argv.src,
skipFull: argv.skipFull,
excludeNodeModules: argv.excludeNodeModules,
mergeAsync: argv.mergeAsync
mergeAsync: argv.mergeAsync,
monocartArgv: (argv.experimentalMonocart || process.env.EXPERIMENTAL_MONOCART) ? argv : null
})
await report.run()
if (argv.checkCoverage) await checkCoverages(argv, report)
Expand Down
5 changes: 5 additions & 0 deletions lib/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ function buildYargs (withCommands = false) {
describe: 'supplying --merge-async will merge all v8 coverage reports asynchronously and incrementally. ' +
'This is to avoid OOM issues with Node.js runtime.'
})
.option('experimental-monocart', {
default: false,
type: 'boolean',
describe: 'Using Monocart coverage reports'
cenfun marked this conversation as resolved.
Show resolved Hide resolved
})
.pkgConf('c8')
.demandCommand(1)
.check((argv) => {
Expand Down
130 changes: 129 additions & 1 deletion lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const getSourceMapFromFile = require('./source-map-from-file')
const v8toIstanbul = require('v8-to-istanbul')
const util = require('util')
const debuglog = util.debuglog('c8')
const { CoverageReport } = require('monocart-coverage-reports')
Copy link
Owner

Choose a reason for hiding this comment

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

we should move this into a try/catch.

Copy link
Owner

Choose a reason for hiding this comment

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

Perhaps we should move if into a try/catch in a separate monocart.js helper file, then we can mock that file to test the help output.


class Report {
constructor ({
Expand All @@ -36,7 +37,8 @@ class Report {
allowExternal = false,
skipFull,
excludeNodeModules,
mergeAsync
mergeAsync,
monocartArgv
}) {
this.reporter = reporter
this.reporterOptions = reporterOptions || {}
Expand All @@ -60,6 +62,7 @@ class Report {
this.src = this._getSrc(src)
this.skipFull = skipFull
this.mergeAsync = mergeAsync
this.monocartArgv = monocartArgv
}

_getSrc (src) {
Expand All @@ -73,6 +76,9 @@ class Report {
}

async run () {
if (this.monocartArgv) {
return this.runMonocart()
Copy link
Owner

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.

Copy link
Contributor Author

@cenfun cenfun Jun 11, 2024

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

  • Move the import library to try/catch. see function getMonocart ()
  • Use the 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).
  • Print the install instructions and exit with 1 when failed to load the lib.

Please review, thanks.

}
const context = libReport.createContext({
dir: this.reportsDirectory,
watermarks: this.watermarks,
Expand All @@ -89,6 +95,128 @@ class Report {
}
}

async runMonocart () {
Copy link
Owner

Choose a reason for hiding this comment

The 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 report.js.

const argv = this.monocartArgv
const exclude = this.exclude

function getEntryFilter () {
if (argv.entryFilter) {
return argv.entryFilter
}
if (argv.filter) {
return argv.filter
}
return (entry) => {
return exclude.shouldInstrument(fileURLToPath(entry.url))
}
}

function getSourceFilter () {
if (argv.sourceFilter) {
return argv.sourceFilter
}
if (argv.filter) {
return argv.filter
}
return (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,
inline: argv.inline,
lcov: argv.lcov,
outputDir: argv.reportsDir,
clean: argv.clean,

reports: getReports(),
all: getAllOptions(),

// use default value for istanbul
defaultSummarizer: 'pkg',

entryFilter: getEntryFilter(),
sourceFilter: getSourceFilter(),

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 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
Expand Down
Loading