embark/lib/modules/coverage/index.js

61 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-08-08 18:26:40 +00:00
/*global web3*/
const fs = require('fs');
const process = require('process');
2018-08-07 19:26:39 +00:00
const ContractSources = require('./contract_sources');
2018-08-08 18:26:40 +00:00
// Set up the web3 extension
web3.extend({
property: 'debug',
methods: [{name: 'traceTransaction', call: 'debug_traceTransaction', params: 2}]
});
2018-08-01 15:48:17 +00:00
class CodeCoverage {
2018-08-01 16:08:47 +00:00
constructor(embark, _options) {
2018-08-01 15:48:17 +00:00
this.events = embark.events;
this.logger = embark.logger;
2018-08-01 16:08:47 +00:00
2018-08-07 19:26:39 +00:00
embark.events.on('contracts:compile:solc', this.compileSolc.bind(this));
embark.events.on('contracts:compiled:solc', this.compiledSolc.bind(this));
embark.events.on('contracts:run:solc', this.runSolc.bind(this));
embark.events.on('block:header', this.runSolc.bind(this));
2018-08-08 18:26:40 +00:00
this.seenTransactions = {};
var self = this;
process.on('exit', (code) => {
fs.writeFileSync('/tmp/coverage.json', JSON.stringify(this.coverageReport));
});
2018-08-07 19:26:39 +00:00
}
compileSolc(input) {
var sources = {};
Object.keys(input.sources).forEach((path) => {
sources[path] = input.sources[path].content;
});
this.contractSources = new ContractSources(sources);
}
compiledSolc(output) {
this.contractSources.parseSolcOutput(output);
}
2018-08-08 18:26:40 +00:00
async runSolc(receipt) {
var block = await web3.eth.getBlock(receipt.number);
for(var i in block.transactions) {
var txHash = block.transactions[i];
2018-08-01 16:08:47 +00:00
2018-08-08 18:26:40 +00:00
if(this.seenTransactions[txHash]) return;
this.seenTransactions[txHash] = true;
var trace = await web3.debug.traceTransaction(txHash, {});
var cov = this.contractSources.generateCodeCoverage(trace);
this.coverageReport = cov;
}
2018-08-01 15:48:17 +00:00
}
}
2018-08-01 16:08:47 +00:00
module.exports = CodeCoverage;