From 5b5d51ecf4523ed997ee6b3a8e8438acac2c9c8c Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 2 Aug 2016 11:03:47 -0700 Subject: [PATCH] Add helper to instrument electron.asar --- spec/static/coverage.js | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/spec/static/coverage.js b/spec/static/coverage.js index 3308e2630f73..151824f34800 100644 --- a/spec/static/coverage.js +++ b/spec/static/coverage.js @@ -1,12 +1,16 @@ +const asar = require('asar') const fs = require('fs') const glob = require('glob') +const mkdirp = require('mkdirp') const path = require('path') -const {ipcRenderer} = require('electron') const {Collector, Instrumenter, Reporter} = require('istanbul') +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 const addUnrequiredFiles = (coverage) => { const instrumenter = new Instrumenter() - const transformer = instrumenter.instrumentSync.bind(instrumenter) const libPath = path.join(__dirname, '..', '..', 'lib') glob.sync('**/*.js', {cwd: libPath}).map(function (relativePath) { @@ -14,7 +18,7 @@ const addUnrequiredFiles = (coverage) => { }).filter(function (filePath) { return coverage[filePath] == null }).forEach(function (filePath) { - transformer(fs.readFileSync(filePath, 'utf8'), filePath) + instrumenter.instrumentSync(fs.readFileSync(filePath, 'utf8'), filePath) // When instrumenting the code, istanbul will give each FunctionDeclaration // a value of 1 in coverState.s,presumably to compensate for function @@ -28,6 +32,7 @@ const addUnrequiredFiles = (coverage) => { }) } +// Generate a code coverage report in out/coverage/lcov-report exports.generate = () => { const coverage = window.__coverage__ if (coverage == null) return @@ -36,10 +41,35 @@ exports.generate = () => { const collector = new Collector() collector.add(coverage) + + const {ipcRenderer} = require('electron') collector.add(ipcRenderer.sendSync('get-coverage')) - const outPath = path.join(__dirname, '..', '..', 'out', 'coverage') - const reporter = new Reporter(null, outPath) + const reporter = new Reporter(null, outputPath) reporter.addAll(['text', 'lcov']) reporter.write(collector, true, function () {}) } + +// Generate an instrumented .asar file for all the files in lib/ and save it +// to out/coverage/electron-instrumented.asar +exports.instrument = () => { + const instrumenter = new Instrumenter() + + glob.sync('**/*.js', {cwd: libPath}).forEach(function (relativePath) { + const rawPath = path.join(libPath, relativePath) + const raw = fs.readFileSync(rawPath, 'utf8') + + const generatedPath = path.join(outputPath, 'lib', relativePath) + const generated = instrumenter.instrumentSync(raw, rawPath) + mkdirp.sync(path.dirname(generatedPath)) + fs.writeFileSync(generatedPath, generated) + }) + + const asarPath = path.join(outputPath, 'electron-instrumented.asar') + asar.createPackageWithOptions(path.join(outputPath, 'lib'), asarPath, {}, function (error) { + if (error) { + console.error(error.stack || error) + process.exit(1) + } + }) +}