electron/spec/coverage/reporter.js

125 lines
4 KiB
JavaScript
Raw Normal View History

2016-08-02 18:03:47 +00:00
const asar = require('asar')
2016-08-02 17:38:51 +00:00
const fs = require('fs')
const glob = require('glob')
2016-08-02 18:03:47 +00:00
const mkdirp = require('mkdirp')
2016-08-02 17:38:51 +00:00
const path = require('path')
const rimraf = require('rimraf')
2016-08-02 17:38:51 +00:00
const {Collector, Instrumenter, Reporter} = require('istanbul')
2016-08-02 18:03:47 +00:00
const outputPath = path.join(__dirname, '..', '..', 'out', 'coverage')
const libPath = path.join(__dirname, '..', '..', 'lib')
// Add unrequired files to the coverage report so all files are present there
2016-08-02 17:38:51 +00:00
const addUnrequiredFiles = (coverage) => {
const instrumenter = new Instrumenter()
const libPath = path.join(__dirname, '..', '..', 'lib')
glob.sync('**/*.js', {cwd: libPath}).map(function (relativePath) {
return path.join(libPath, relativePath)
}).filter(function (filePath) {
return coverage[filePath] == null
}).forEach(function (filePath) {
2016-08-02 18:03:47 +00:00
instrumenter.instrumentSync(fs.readFileSync(filePath, 'utf8'), filePath)
2016-08-02 17:38:51 +00:00
// When instrumenting the code, istanbul will give each FunctionDeclaration
// a value of 1 in coverState.s,presumably to compensate for function
// hoisting. We need to reset this, as the function was not hoisted, as it
// was never loaded.
Object.keys(instrumenter.coverState.s).forEach(function (key) {
instrumenter.coverState.s[key] = 0
});
coverage[filePath] = instrumenter.coverState
})
}
// Add coverage data to collector for all opened browser windows
const addBrowserWindowsData = (collector) => {
const dataPath = path.join(outputPath, 'data')
glob.sync('*.json', {cwd: dataPath}).map(function (relativePath) {
return path.join(dataPath, relativePath)
}).forEach(function (filePath) {
collector.add(JSON.parse(fs.readFileSync(filePath)));
})
}
2016-08-02 18:03:47 +00:00
// Generate a code coverage report in out/coverage/lcov-report
exports.generateReport = () => {
2016-08-02 17:38:51 +00:00
const coverage = window.__coverage__
if (coverage == null) return
addUnrequiredFiles(coverage)
const collector = new Collector()
collector.add(coverage)
2016-08-02 18:03:47 +00:00
const {ipcRenderer} = require('electron')
2016-08-02 17:38:51 +00:00
collector.add(ipcRenderer.sendSync('get-coverage'))
addBrowserWindowsData(collector)
2016-08-02 18:03:47 +00:00
const reporter = new Reporter(null, outputPath)
2016-08-02 17:38:51 +00:00
reporter.addAll(['text', 'lcov'])
reporter.write(collector, true, function () {})
}
2016-08-02 21:04:21 +00:00
// Save coverage data from the browser window with the given pid
const saveCoverageData = (coverage, pid) => {
if (coverage && pid) {
const dataPath = path.join(outputPath, 'data', `${pid}-${Date.now()}.json`)
mkdirp.sync(path.dirname(dataPath))
fs.writeFileSync(dataPath, JSON.stringify(coverage))
}
}
const getCoverageFromWebContents = (webContents, callback) => {
webContents.executeJavaScript('[window.__coverage__, window.process && window.process.pid]', (results) => {
const coverage = results[0]
const pid = results[1] || webContents.getId()
2016-08-02 21:04:21 +00:00
callback(coverage, pid)
})
}
2016-08-02 21:04:21 +00:00
// Save coverage data when a BrowserWindow is closed manually
const patchBrowserWindow = () => {
const {BrowserWindow} = require('electron')
2016-08-02 21:04:21 +00:00
const {destroy} = BrowserWindow.prototype
BrowserWindow.prototype.destroy = function () {
2016-08-02 23:14:42 +00:00
if (this.isDestroyed() || !this.getURL()) {
return destroy.call(this)
}
2016-08-02 21:04:21 +00:00
getCoverageFromWebContents(this.webContents, (coverage, pid) => {
saveCoverageData(coverage, pid)
destroy.call(this)
})
}
}
2016-08-02 21:04:21 +00:00
// Save coverage data when beforeunload fires on the webContent's window object
const saveCoverageOnBeforeUnload = () => {
const {app, ipcMain} = require('electron')
ipcMain.on('save-coverage', function (event, coverage, pid) {
saveCoverageData(coverage, pid || event.sender.getId())
2016-08-02 21:04:21 +00:00
})
app.on('web-contents-created', function (event, webContents) {
webContents.executeJavaScript(`
window.addEventListener('beforeunload', function () {
require('electron').ipcRenderer.send('save-coverage', window.__coverage__, window.process && window.process.pid)
2016-08-02 21:04:21 +00:00
})
`)
})
}
exports.setupCoverage = () => {
const coverage = global.__coverage__
if (coverage == null) return
rimraf.sync(path.join(outputPath, 'data'))
patchBrowserWindow()
saveCoverageOnBeforeUnload()
}