Skip to content

Commit

Permalink
Merge pull request #2 from dougluce/add-coverage
Browse files Browse the repository at this point in the history
Add code coverage stats
  • Loading branch information
Johnny-Malizia authored Jul 8, 2016
2 parents 4d09e66 + 605cecf commit b42092e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
coverage/
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"test": "electron-mocha",
"testmon": "nodemon --exec '(electron-mocha && growlnotify -n nodemon --image ~/green.png -m good) || (growlnotify -n nodemon --image ~/red.png -m crash && exit 1)'"
"testmon": "nodemon --exec '(electron-mocha && growlnotify -n nodemon --image ~/green.png -m good) || (growlnotify -n nodemon --image ~/red.png -m crash && exit 1)'",
"coverage": "electron-mocha --require test/coverage && istanbul report lcov"
},
"bin": {
"scan": "./bin/scan"
Expand All @@ -14,8 +15,10 @@
"license": "ISC",
"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"electron-mocha": "^2.3.0",
"express": "^4.14.0",
"istanbul": "^0.4.4",
"mocha": "^2.5.3",
"nodemon": "^1.9.2"
},
Expand Down
57 changes: 57 additions & 0 deletions test/coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict'

// Largely cribbed from
// https://raw.githubusercontent.com/tropy/tropy/master/test/support/coverage.js

const glob = require('glob')
const { readFileSync: read, realpathSync } = require('fs')
const { Reporter, Instrumenter, Collector, hook } = require('istanbul')
const { keys } = Object

function match () {
const map = {}
const fn = function (file) { return map[file] }
fn.files = []

for (let file of glob.sync(pattern)) {
let fullpath = realpathSync(file)
fn.files.push(fullpath)
map[fullpath] = true
}

return fn
}

function report () {
const cov = global.__coverage__
for (let file of matched.files) {
if (!cov[file]) {
// Add uncovered files to the report.
transformer(read(file, 'utf-8'), file)
for (let key of keys(instrumenter.coverState.s)) {
instrumenter.coverState.s[key] = 0
}
cov[file] = instrumenter.coverState
}
}

const collector = new Collector()
collector.add(cov)

const reporter = new Reporter()
reporter.addAll(['text-summary', 'json'])
reporter.write(collector, true, () => {})
}

// The source files to cover. Avoid node_modules/, coverage/, and
// */coverage.js (supposed to be test/coverage.js)

const pattern = '{!(node_modules|coverage)/**,.}/!(coverage).js'
const matched = match()

const instrumenter = new Instrumenter()
const transformer = instrumenter.instrumentSync.bind(instrumenter)

hook.hookRequire(matched, transformer, {})

process.on('exit', report)

0 comments on commit b42092e

Please sign in to comment.