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.
130 lines
3.3 KiB
130 lines
3.3 KiB
5 years ago
|
const API = require('./../lib/api');
|
||
5 years ago
|
const utils = require('./resources/plugin.utils');
|
||
|
const truffleUtils = require('./resources/truffle.utils');
|
||
|
const PluginUI = require('./resources/truffle.ui');
|
||
5 years ago
|
|
||
5 years ago
|
const pkg = require('./../package.json');
|
||
5 years ago
|
const death = require('death');
|
||
5 years ago
|
const path = require('path');
|
||
|
const Web3 = require('web3');
|
||
5 years ago
|
|
||
5 years ago
|
|
||
5 years ago
|
/**
|
||
|
* Truffle Plugin: `truffle run coverage [options]`
|
||
5 years ago
|
* @param {Object} config @truffle/config config
|
||
5 years ago
|
* @return {Promise}
|
||
|
*/
|
||
5 years ago
|
async function plugin(config){
|
||
5 years ago
|
let ui;
|
||
5 years ago
|
let api;
|
||
5 years ago
|
let error;
|
||
5 years ago
|
let truffle;
|
||
5 years ago
|
let testsErrored = false;
|
||
5 years ago
|
|
||
|
try {
|
||
5 years ago
|
death(utils.finish.bind(null, config, api)); // Catch interrupt signals
|
||
5 years ago
|
|
||
5 years ago
|
config = truffleUtils.normalizeConfig(config);
|
||
|
|
||
5 years ago
|
ui = new PluginUI(config.logger.log);
|
||
5 years ago
|
|
||
5 years ago
|
if(config.help) return ui.report('help'); // Exit if --help
|
||
5 years ago
|
|
||
5 years ago
|
truffle = truffleUtils.loadLibrary(config);
|
||
5 years ago
|
api = new API(utils.loadSolcoverJS(config));
|
||
5 years ago
|
|
||
5 years ago
|
truffleUtils.setNetwork(config, api);
|
||
5 years ago
|
|
||
5 years ago
|
// Server launch
|
||
5 years ago
|
const client = api.client || truffle.ganache;
|
||
|
const address = await api.ganache(client);
|
||
5 years ago
|
|
||
|
const web3 = new Web3(address);
|
||
5 years ago
|
const accounts = await web3.eth.getAccounts();
|
||
|
const nodeInfo = await web3.eth.getNodeInfo();
|
||
|
const ganacheVersion = nodeInfo.split('/')[1];
|
||
|
|
||
5 years ago
|
truffleUtils.setNetworkFrom(config, accounts);
|
||
5 years ago
|
|
||
5 years ago
|
// Version Info
|
||
5 years ago
|
ui.report('versions', [
|
||
|
truffle.version,
|
||
|
ganacheVersion,
|
||
|
pkg.version
|
||
|
]);
|
||
5 years ago
|
|
||
5 years ago
|
// Exit if --version
|
||
|
if (config.version) return await utils.finish(config, api);
|
||
5 years ago
|
|
||
|
ui.report('network', [
|
||
5 years ago
|
config.network,
|
||
|
config.networks[config.network].network_id,
|
||
|
config.networks[config.network].port
|
||
5 years ago
|
]);
|
||
5 years ago
|
|
||
5 years ago
|
// Run post-launch server hook;
|
||
|
await api.onServerReady(config);
|
||
|
|
||
5 years ago
|
// Instrument
|
||
5 years ago
|
const skipFiles = api.skipFiles || [];
|
||
|
skipFiles.push('Migrations.sol');
|
||
|
|
||
5 years ago
|
let {
|
||
|
targets,
|
||
|
skipped
|
||
5 years ago
|
} = utils.assembleFiles(config, skipFiles);
|
||
5 years ago
|
|
||
|
targets = api.instrument(targets);
|
||
|
utils.reportSkipped(config, skipped);
|
||
5 years ago
|
|
||
5 years ago
|
// Filesystem & Compiler Re-configuration
|
||
5 years ago
|
const {
|
||
|
tempArtifactsDir,
|
||
|
tempContractsDir
|
||
|
} = utils.getTempLocations(config);
|
||
|
|
||
5 years ago
|
utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir)
|
||
5 years ago
|
utils.save(targets, config.contracts_directory, tempContractsDir);
|
||
|
utils.save(skipped, config.contracts_directory, tempContractsDir);
|
||
5 years ago
|
|
||
5 years ago
|
config.contracts_directory = tempContractsDir;
|
||
|
config.build_directory = tempArtifactsDir;
|
||
|
|
||
|
config.contracts_build_directory = path.join(
|
||
|
tempArtifactsDir,
|
||
|
path.basename(config.contracts_build_directory)
|
||
5 years ago
|
);
|
||
|
|
||
5 years ago
|
config.all = true;
|
||
5 years ago
|
config.test_files = truffleUtils.getTestFilePaths(config);
|
||
5 years ago
|
config.compilers.solc.settings.optimizer.enabled = false;
|
||
5 years ago
|
|
||
5 years ago
|
// Compile Instrumented Contracts
|
||
5 years ago
|
await truffle.contracts.compile(config);
|
||
5 years ago
|
await api.onCompileComplete(config);
|
||
5 years ago
|
|
||
5 years ago
|
// Run tests
|
||
5 years ago
|
try {
|
||
5 years ago
|
failures = await truffle.test.run(config)
|
||
5 years ago
|
} catch (e) {
|
||
5 years ago
|
error = e.stack;
|
||
5 years ago
|
}
|
||
5 years ago
|
await api.onTestsComplete(config);
|
||
5 years ago
|
|
||
|
// Run Istanbul
|
||
5 years ago
|
await api.report();
|
||
5 years ago
|
await api.onIstanbulComplete(config);
|
||
5 years ago
|
|
||
5 years ago
|
} catch(e){
|
||
|
error = e;
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
// Finish
|
||
5 years ago
|
await utils.finish(config, api);
|
||
5 years ago
|
|
||
5 years ago
|
if (error !== undefined) throw error;
|
||
5 years ago
|
if (failures > 0) throw new Error(ui.generate('tests-fail', [failures]));
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
module.exports = plugin;
|