-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement Istanbul reporting (#8)
- Loading branch information
Showing
11 changed files
with
2,156 additions
and
225 deletions.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.DS_Store | ||
node_modules | ||
.nyc_output | ||
coverage |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const Exclude = require('test-exclude') | ||
const findUp = require('find-up') | ||
const {readFileSync} = require('fs') | ||
const yargs = require('yargs') | ||
const parser = require('yargs-parser') | ||
|
||
const configPath = findUp.sync(['.c8rc', '.c8rc.json']) | ||
const config = configPath ? JSON.parse(readFileSync(configPath)) : {} | ||
|
||
yargs | ||
.usage('$0 [opts] [script] [opts]') | ||
.option('reporter', { | ||
alias: 'r', | ||
describe: 'coverage reporter(s) to use', | ||
default: 'text' | ||
}) | ||
.option('exclude', { | ||
alias: 'x', | ||
default: Exclude.defaultExclude, | ||
describe: 'a list of specific files and directories that should be excluded from coverage, glob patterns are supported.' | ||
}) | ||
.option('include', { | ||
alias: 'n', | ||
default: [], | ||
describe: 'a list of specific files that should be covered, glob patterns are supported' | ||
}) | ||
.option('coverage-directory', { | ||
default: './coverage', | ||
describe: 'directory to output coverage JSON and reports' | ||
}) | ||
.pkgConf('c8') | ||
.config(config) | ||
.demandCommand(1) | ||
.epilog('visit https://git.io/vHysA for list of available reporters') | ||
|
||
function hideInstrumenterArgs (yargv) { | ||
var argv = process.argv.slice(1) | ||
argv = argv.slice(argv.indexOf(yargv._[0])) | ||
if (argv[0][0] === '-') { | ||
argv.unshift(process.execPath) | ||
} | ||
return argv | ||
} | ||
|
||
function hideInstrumenteeArgs () { | ||
let argv = process.argv.slice(2) | ||
const yargv = parser(argv) | ||
|
||
if (!yargv._.length) return argv | ||
|
||
// drop all the arguments after the bin being | ||
// instrumented by c8. | ||
argv = argv.slice(0, argv.indexOf(yargv._[0])) | ||
argv.push(yargv._[0]) | ||
|
||
return argv | ||
} | ||
|
||
module.exports = { | ||
yargs, | ||
hideInstrumenterArgs, | ||
hideInstrumenteeArgs | ||
} |
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,51 @@ | ||
const libCoverage = require('istanbul-lib-coverage') | ||
const libReport = require('istanbul-lib-report') | ||
const reports = require('istanbul-reports') | ||
const {readdirSync, readFileSync} = require('fs') | ||
const {resolve} = require('path') | ||
|
||
class Report { | ||
constructor ({reporter, coverageDirectory, watermarks}) { | ||
this.reporter = reporter | ||
this.coverageDirectory = coverageDirectory | ||
this.watermarks = watermarks | ||
} | ||
run () { | ||
const map = this._getCoverageMapFromAllCoverageFiles() | ||
var context = libReport.createContext({ | ||
dir: './coverage', | ||
watermarks: this.watermarks | ||
}) | ||
|
||
const tree = libReport.summarizers.pkg(map) | ||
|
||
this.reporter.forEach(function (_reporter) { | ||
tree.visit(reports.create(_reporter), context) | ||
}) | ||
} | ||
_getCoverageMapFromAllCoverageFiles () { | ||
const map = libCoverage.createCoverageMap({}) | ||
|
||
this._loadReports().forEach(function (report) { | ||
map.merge(report) | ||
}) | ||
|
||
return map | ||
} | ||
_loadReports () { | ||
const tmpDirctory = resolve(this.coverageDirectory, './tmp') | ||
const files = readdirSync(tmpDirctory) | ||
|
||
return files.map((f) => { | ||
return JSON.parse(readFileSync( | ||
resolve(tmpDirctory, f), | ||
'utf8' | ||
)) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = function (opts) { | ||
const report = new Report(opts) | ||
report.run() | ||
} |
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.