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/dist/truffle.plugin.js

99 lines
3.1 KiB

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