From cd8980151b4b464bba98e185b34765b8b4614c31 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Fri, 14 Sep 2018 13:16:02 -0400 Subject: [PATCH 1/4] Update coverage report after each step --- lib/modules/coverage/index.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/modules/coverage/index.js b/lib/modules/coverage/index.js index a9070a1d..a7dbbb7d 100644 --- a/lib/modules/coverage/index.js +++ b/lib/modules/coverage/index.js @@ -24,12 +24,8 @@ class CodeCoverage { this.coverageReport = {}; this.contractSources = new ContractSources([]); - var self = this; - - process.on('exit', (_code) => { - const coverageReportPath = fs.dappPath('.embark', 'coverage.json'); - fs.writeFileSync(coverageReportPath, JSON.stringify(self.coverageReport)); - }); + this.dotEmbarkPath = fs.dappPath('.embark'); + this.coverageReportPath = fs.dappPath('.embark', 'coverage.json'); } compileSolc(input) { @@ -42,6 +38,11 @@ class CodeCoverage { this.contractSources.parseSolcOutput(output); } + updateCoverageReport() { + fs.mkdirpSync(this.dotEmbarkPath); + fs.writeFileSync(this.coverageReportPath, JSON.stringify(this.coverageReport)); + } + async runSolc(receipt) { let block = await web3.eth.getBlock(receipt.number); if(block.transactions.length == 0) return; @@ -61,6 +62,8 @@ class CodeCoverage { for(let i in traces) { this.coverageReport = this.contractSources.generateCodeCoverage(traces[i]); } + + this.updateCoverageReport(); } } From d8becfe54f4208e4031f90381e4fdc686e5ebc90 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Fri, 14 Sep 2018 13:21:47 -0400 Subject: [PATCH 2/4] Lint lyfe --- lib/modules/coverage/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/modules/coverage/index.js b/lib/modules/coverage/index.js index a7dbbb7d..7126f4ac 100644 --- a/lib/modules/coverage/index.js +++ b/lib/modules/coverage/index.js @@ -1,6 +1,4 @@ /*global web3*/ -const process = require('process'); - const fs = require('../../core/fs'); const ContractSources = require('./contract_sources'); From a8a9f3e9a806f41d650ecc113220232e34307af6 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Fri, 14 Sep 2018 16:31:40 -0400 Subject: [PATCH 3/4] Setup coverage to listen to the test embark events This allows us to catch an event to know when the tests are done running. In turn, this will tell us when to save the coverage file and run the report. --- lib/modules/coverage/index.js | 9 ++++++--- lib/tests/run_tests.js | 26 ++++++++++++++------------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/modules/coverage/index.js b/lib/modules/coverage/index.js index 7126f4ac..8dfe6583 100644 --- a/lib/modules/coverage/index.js +++ b/lib/modules/coverage/index.js @@ -18,6 +18,10 @@ class CodeCoverage { embark.events.on('contracts:run:solc', this.runSolc.bind(this)); embark.events.on('block:header', this.runSolc.bind(this)); + // These events are emitted from a test-specific Embark instance, so we need to + // pull it in from global. + global.embark.events.on('tests:finished', this.updateCoverageReport.bind(this)); + this.seenTransactions = {}; this.coverageReport = {}; this.contractSources = new ContractSources([]); @@ -36,9 +40,10 @@ class CodeCoverage { this.contractSources.parseSolcOutput(output); } - updateCoverageReport() { + updateCoverageReport(cb) { fs.mkdirpSync(this.dotEmbarkPath); fs.writeFileSync(this.coverageReportPath, JSON.stringify(this.coverageReport)); + cb(); } async runSolc(receipt) { @@ -60,8 +65,6 @@ class CodeCoverage { for(let i in traces) { this.coverageReport = this.contractSources.generateCodeCoverage(traces[i]); } - - this.updateCoverageReport(); } } diff --git a/lib/tests/run_tests.js b/lib/tests/run_tests.js index c2b5c2c8..87451ee7 100644 --- a/lib/tests/run_tests.js +++ b/lib/tests/run_tests.js @@ -130,18 +130,20 @@ module.exports = { return next(); } - runCmd(`${fs.embarkPath('node_modules/.bin/istanbul')} report --root .embark --format html`, - {silent: false, exitOnError: false}, (err) => { - if (err) { - return next(err); - } - console.log(`Coverage report created. You can find it here: ${fs.dappPath('coverage/index.html')}\n`); - const opn = require('opn'); - const _next = () => { next(); }; - opn(fs.dappPath('coverage/index.html'), {wait: false}) - .then(() => new Promise(resolve => setTimeout(resolve, 1000))) - .then(_next, _next); - }); + global.embark.events.emit('tests:finished', function() { + runCmd(`${fs.embarkPath('node_modules/.bin/istanbul')} report --root .embark --format html`, + {silent: false, exitOnError: false}, (err) => { + if (err) { + return next(err); + } + console.log(`Coverage report created. You can find it here: ${fs.dappPath('coverage/index.html')}\n`); + const opn = require('opn'); + const _next = () => { next(); }; + opn(fs.dappPath('coverage/index.html'), {wait: false}) + .then(() => new Promise(resolve => setTimeout(resolve, 1000))) + .then(_next, _next); + }); + }); } ], (err) => { if (err) { From 103262f435a530d3deb3d50eece7fddaa8332bfa Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Fri, 14 Sep 2018 16:43:58 -0400 Subject: [PATCH 4/4] Write coverage report asynchronously --- lib/modules/coverage/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/modules/coverage/index.js b/lib/modules/coverage/index.js index 8dfe6583..f71c30b8 100644 --- a/lib/modules/coverage/index.js +++ b/lib/modules/coverage/index.js @@ -41,9 +41,9 @@ class CodeCoverage { } updateCoverageReport(cb) { - fs.mkdirpSync(this.dotEmbarkPath); - fs.writeFileSync(this.coverageReportPath, JSON.stringify(this.coverageReport)); - cb(); + fs.mkdirp(this.dotEmbarkPath, () => { + fs.writeFile(this.coverageReportPath, JSON.stringify(this.coverageReport), cb); + }); } async runSolc(receipt) {