pull/381/head
cgewecke 5 years ago
parent f6e4c24d8c
commit 9e78ae8d78
  1. 60
      dist/truffle.plugin.js
  2. 4
      lib/injector.js

@ -14,8 +14,8 @@
Test options override Test options override
--------------------- ---------------------
contracts_directory, /users/myPath/to/someProject/.coverageContracts contracts_directory, /users/myPath/to/someProject/.coverageContracts
contracts_build_directory, /users/myPath/to/someProject/.coverageArtifact/contracts contracts_build_directory, /users/myPath/to/someProject/.coverageArtifacts/contracts
provider ganache.provider (w/ vm resolved) provider ganache.provider (async b/c vm must be resolved)
logger add filter for unused variables... logger add filter for unused variables...
Truffle Lib API Truffle Lib API
@ -26,62 +26,72 @@
*/ */
const SolidityCoverage = require('./../lib/app.js'); const SolidityCoverage = require('./../lib/app.js');
const req = require('req-cwd');
module.exports = async (config) => module.exports = async (config) =>
let truffle; let error;
try { try {
truffle = loadTruffleLibrary();
const coverageConfigPath = coveragePaths.config(config) // Load truffle lib & coverage config
const coverageConfig = req.silent(coverageConfigPath) || {}; const truffle = loadTruffleLibrary();
const coverageConfig = req.silent('./.solcover.js') || {};
app.contractsDir = coveragePaths.contracts(config, coverageConfig);
// Start
const app = new SolidityCoverage(coverageConfig); const app = new SolidityCoverage(coverageConfig);
app.generateEnvironment();
app.instrument(config.contracts_directory);
// Compile instrumented sources / optimization off // Write instrumented sources to temp folder
config.contracts_directory = app.contractsDir; app.contractsDirectory = coveragePaths.contracts(config, app);
config.build_directory = coveragePaths.artifacts.root(config, coverageConfig); app.generateEnvironment(config.contracts_directory, app.contractsDirectory);
config.contracts_build_directory = coveragePaths.artifacts.contracts(config, coverageConfig); app.instrument();
// Have truffle use temp folders
config.contracts_directory = app.contractsDirectory;
config.build_directory = coveragePaths.artifacts.root(config, app);
config.contracts_build_directory = coveragePaths.artifacts.contracts(config, app);
// Compile w/out optimization
config.compilers.solc.settings.optimization.enabled = false; config.compilers.solc.settings.optimization.enabled = false;
await truffle.contracts.compile(config); await truffle.contracts.compile(config);
// Test using compiled, instrumented sources // Launch provider & run tests
config.provider = await app.getCoverageProvider(); config.provider = await app.getCoverageProvider(truffle);
try { try {
await truffle.test.run(config) await truffle.test.run(config)
} catch (err) { } catch (e) {
error = e;
app.testsErrored = true; app.testsErrored = true;
} }
// Produce report
app.generateCoverage(); app.generateCoverage();
} catch(err){ } catch(e){
return app.cleanUp(err); error = e;
} }
return app.cleanUp(); // Finish
return app.cleanUp(error);
} }
// -------------------------------------- Helpers -------------------------------------------------- // -------------------------------------- Helpers --------------------------------------------------
function loadTruffleLibrary(){ function loadTruffleLibrary(){
try { return require("truffle") } catch(err) {}; try { return require("truffle") } catch(err) {};
try { return require("sc-truffle")} catch(err) {}; try { return require("./truffle.library")} catch(err) {};
throw new Error(utils.errors.NO_TRUFFLE_LIB) throw new Error(utils.errors.NO_TRUFFLE_LIB)
} }
const coveragePaths = { const coveragePaths = {
contracts: (t, c) => path.join(path.dirname(t.contracts_directory), c.contractsDirName)), contracts: (t, c) => path.join(path.dirname(t.contracts_directory), c.contractsDirName)),
config: (t) => path.join(t.working_directory, '.solcover.js'),
artifacts: { artifacts: {
root: (t, c) => path.join(path.dirname(t.build_directory), c.artifactsDirName), root: (t, c) => path.join(path.dirname(t.build_directory), c.artifactsDirName),
contracts: (c, t) => path.join(c.build_directory, path.basename(t.contracts_build_directory)) contracts: (t, c) => {
const root = path.join(path.dirname(t.build_directory), c.artifactsDirName);
return path.join(root, path.basename(t.contracts_build_directory));
}
} }
} }

@ -12,13 +12,11 @@ class Injector {
* @param {String} memoryVariable * @param {String} memoryVariable
* @param {String} hash hash key to an instrumentationData entry (see _getHash) * @param {String} hash hash key to an instrumentationData entry (see _getHash)
* @param {String} type instrumentation type, e.g. line, statement * @param {String} type instrumentation type, e.g. line, statement
* @return {String} ex: _sc_82e0891[0] = bytes32(0xdc08...08ed1); // function // @return {String} ex: _sc_82e0891[0] = bytes32(0xdc08...08ed1); /* function */
*/
_getInjectable(memoryVariable, hash, type){ _getInjectable(memoryVariable, hash, type){
return `${memoryVariable}[0] = bytes32(${hash}); /* ${type} */ \n`; return `${memoryVariable}[0] = bytes32(${hash}); /* ${type} */ \n`;
} }
_getHash(fileName) { _getHash(fileName) {
this.hashCounter++; this.hashCounter++;
return web3Utils.keccak256(`${fileName}:${this.hashCounter}`); return web3Utils.keccak256(`${fileName}:${this.hashCounter}`);

Loading…
Cancel
Save