Code coverage for Solidity smart-contracts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
solidity-coverage/runCoveredTests.js

60 lines
2.8 KiB

8 years ago
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var shell = require('shelljs');
var SolidityCoder = require("web3/lib/solidity/coder.js");
var fs = require('fs');
var path = require('path');
var getInstrumentedVersion = require('./instrumentSolidity.js');
var CoverageMap = require('./coverageMap.js');
var coverage = new CoverageMap();
var mkdirp = require('mkdirp');
8 years ago
var childprocess = require('child_process');
8 years ago
//PAtch our local testrpc if necessary
if (!shell.test('-e','./node_modules/ethereumjs-vm/lib/opFns.js.orig')){
console.log('patch local testrpc...')
shell.exec('patch -b ./node_modules/ethereumjs-vm/lib/opFns.js ./hookIntoEvents.patch')
}
//Run the modified testrpc with large block limit
var testrpcProcess = childprocess.exec('./node_modules/ethereumjs-testrpc/bin/testrpc --gasLimit 0xfffffffffff')
if (shell.test('-d','../originalContracts')){
console.log("There is already an 'originalContracts' directory in your truffle directory.\nThis is probably due to a previous solcover failure.\nPlease make sure the ./contracts/ directory contains your contracts (perhaps by copying them from originalContracts), and then delete the originalContracts directory.")
process.exit(1);
}
shell.mv('./../contracts/', './../originalContracts/');
shell.mkdir('./../contracts/');
//For each contract in originalContracts, get the instrumented version
shell.ls('./../originalContracts/**/*.sol').forEach(function(file) {
8 years ago
if (file !== 'originalContracts/Migrations.sol') {
console.log("instrumenting ", file);
var contract = fs.readFileSync("./" + file).toString();
var fileName = path.basename(file);
var instrumentedContractInfo = getInstrumentedVersion(contract, file, true);
mkdirp.sync(path.dirname(file.replace('originalContracts', 'contracts')));
fs.writeFileSync(file.replace('originalContracts','contracts'), instrumentedContractInfo.contract);
var canonicalContractPath = path.resolve(file);
coverage.addContract(instrumentedContractInfo, canonicalContractPath);
8 years ago
}
});
shell.cp("./../originalContracts/Migrations.sol", "./../contracts/Migrations.sol");
shell.rm('./allFiredEvents'); //Delete previous results
shell.exec('truffle test --network coverage');
8 years ago
events = fs.readFileSync('./allFiredEvents').toString().split('\n')
events.pop();
//The pop here isn't a bug - there is an empty line at the end of this file, so we
//don't want to include it as an event.
coverage.generate(events, './../originalContracts/');
8 years ago
fs.writeFileSync('./coverage.json', JSON.stringify(coverage.coverage));
8 years ago
shell.exec("./node_modules/istanbul/lib/cli.js report html")
testrpcProcess.kill();
shell.rm('-rf', './../contracts');
shell.mv('./../originalContracts', './../contracts');