Skip to content

Use ganache server (instead of provider) #402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ workflows:
build:
jobs:
- unit-test
# TODO: re-enable when server logic is added
#- e2e-zeppelin
- e2e-zeppelin
- e2e-metacoin
# TODO: re-enable.
# At the moment we're using forks so this is pointless
Expand Down
24 changes: 22 additions & 2 deletions dist/plugin-assets/truffle.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ class PluginUI extends UI {

const kinds = {

'sol-tests': `\n${w} ${c.red("This plugin cannot run Truffle's native solidity tests: ")}`+
`${args[0]} test(s) will be skipped.\n`,
'sol-tests': `${w} ${c.red("This plugin cannot run Truffle's native solidity tests: ")}`+
`${args[0]} test(s) will be skipped.\n`,

'id-clash': `${w} ${c.red("The 'network_id' values in your truffle network ")}` +
`${c.red("and .solcover.js are different. Using truffle's: ")} ${c.bold(args[0])}.\n`,

'port-clash': `${w} ${c.red("The 'port' values in your truffle network ")}` +
`${c.red("and .solcover.js are different. Using truffle's: ")} ${c.bold(args[0])}.\n`,

'no-port': `${w} ${c.red("No 'port' was declared in your truffle network. ")}` +
`${c.red("Using solidity-coverage's: ")} ${c.bold(args[0])}.\n`,

'lib-local': `\n${ct} ${c.grey('Using Truffle library from local node_modules.')}\n`,
'lib-global': `\n${ct} ${c.grey('Using Truffle library from global node_modules.')}\n`,
Expand All @@ -43,6 +51,15 @@ class PluginUI extends UI {
'truffle-version': `${ct} ${c.bold('truffle')}: v${args[0]}`,
'ganache-version': `${ct} ${c.bold('ganache-core')}: ${args[0]}`,
'coverage-version': `${ct} ${c.bold('solidity-coverage')}: v${args[0]}`,

'network': `\n${c.bold('Network Info')}` +
`\n${c.bold('============')}\n` +
`${ct} ${c.bold('id')}: ${args[1]}\n` +
`${ct} ${c.bold('port')}: ${args[2]}\n` +
`${ct} ${c.bold('network')}: ${args[0]}\n`,



}

this._write(kinds[kind]);
Expand All @@ -66,6 +83,9 @@ class PluginUI extends UI {
`${c.red('This can happen if it has a syntax error or ')}` +
`${c.red('the path you specified for it is wrong.')}`,

'no-network': `${c.red('Network: ')} ${args[0]} ` +
`${c.red(' is not defined in your truffle-config networks. ')}`,

}


Expand Down
141 changes: 114 additions & 27 deletions dist/truffle.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const util = require('util');
const globby = require('globby');
const shell = require('shelljs');
const globalModules = require('global-modules');
const TruffleProvider = require('@truffle/provider');

/**
* Truffle Plugin: `truffle run coverage [options]`
Expand All @@ -25,12 +26,12 @@ async function plugin(truffleConfig){
let solcoverjs;
let testsErrored = false;

// This needs it's own try block because this logic
// Separate try block because this logic
// runs before app.cleanUp is defined.
try {
ui = new PluginUI(truffleConfig.logger.log);

if(truffleConfig.help) return ui.report('help'); // Bail if --help
if(truffleConfig.help) return ui.report('help'); // Exit if --help

truffle = loadTruffleLibrary(ui, truffleConfig);
app = new App(loadSolcoverJS(ui, truffleConfig));
Expand All @@ -41,19 +42,30 @@ async function plugin(truffleConfig){
// Catch interrupt signals
death(app.cleanUp);

setNetwork(ui, app, truffleConfig);

// Provider / Server launch
const provider = await app.provider(truffle.ganache);
const web3 = new Web3(provider);
const address = await app.ganache(truffle.ganache);

const web3 = new Web3(address);
const accounts = await web3.eth.getAccounts();
const nodeInfo = await web3.eth.getNodeInfo();
const ganacheVersion = nodeInfo.split('/')[1];

setNetworkFrom(truffleConfig, accounts);

// Version Info
ui.report('truffle-version', [truffle.version]);
ui.report('ganache-version', [ganacheVersion]);
ui.report('coverage-version',[pkg.version]);

if (truffleConfig.version) return app.cleanUp(); // Bail if --version
if (truffleConfig.version) return app.cleanUp(); // Exit if --version

ui.report('network', [
truffleConfig.network,
truffleConfig.networks[truffleConfig.network].network_id,
truffleConfig.networks[truffleConfig.network].port
]);

// Instrument
app.sanityCheckContext();
Expand All @@ -76,25 +88,6 @@ async function plugin(truffleConfig){
// Compile Instrumented Contracts
await truffle.contracts.compile(truffleConfig);

// Network Re-configuration
const networkName = 'soliditycoverage';
truffleConfig.network = networkName;

// Truffle complains that these keys *are not* set when running plugin fn directly.
// But throws saying they *cannot* be manually set when running as truffle command.
try {
truffleConfig.network_id = "*";
truffleConfig.provider = provider;
} catch (err){}

truffleConfig.networks[networkName] = {
network_id: "*",
provider: provider,
gas: app.gasLimit,
gasPrice: app.gasPrice,
from: accounts[0]
}

// Run tests
try {
failures = await truffle.test.run(truffleConfig)
Expand Down Expand Up @@ -143,7 +136,101 @@ function getTestFilePaths(ui, truffle){
return target.filter(f => f.match(testregex) != null);
}

/**
* Configures the network. Runs before the server is launched.
* User can request a network from truffle-config with "--network <name>".
* There are overlapping options in solcoverjs (like port and providerOptions.network_id).
* Where there are mismatches user is warned & the truffle network settings are preferred.
*
* Also generates a default config & sets the default gas high / gas price low.
*
* @param {SolidityCoverage} app
* @param {TruffleConfig} config
*/
function setNetwork(ui, app, config){

// --network <network-name>
if (config.network){
const network = config.networks[config.network];

// Check network:
if (!network){
throw new Error(ui.generate('no-network', [config.network]));
}

// Check network id
if (!isNaN(parseInt(network.network_id))){

// Warn: non-matching provider options id and network id
if (app.providerOptions.network_id &&
app.providerOptions.network_id !== parseInt(network.network_id)){

ui.report('id-clash', [ parseInt(network.network_id) ]);
}

// Prefer network defined id.
app.providerOptions.network_id = parseInt(network.network_id);

} else {
network.network_id = "*";
}

// Check port: use solcoverjs || default if undefined
if (!network.port) {
ui.report('no-port', [app.port]);
network.port = app.port;
}

// Warn: port conflicts
if (app.port !== app.defaultPort && app.port !== network.port){
ui.report('port-clash', [ network.port ])
}

// Prefer network port if defined;
app.port = network.port;

network.gas = app.gasLimit;
network.gasPrice = app.gasPrice;

setOuterConfigKeys(config, app, network.network_id);
return;
}

// Default Network Configuration
config.network = 'soliditycoverage';
setOuterConfigKeys(config, app, "*");

config.networks[config.network] = {
network_id: "*",
port: app.port,
host: app.host,
gas: app.gasLimit,
gasPrice: app.gasPrice
}
}

/**
* Sets the default `from` account field in the truffle network that will be used.
* This needs to be done after accounts are fetched from the launched client.
* @param {TruffleConfig} config
* @param {Array} accounts
*/
function setNetworkFrom(config, accounts){
if (!config.networks[config.network].from){
config.networks[config.network].from = accounts[0];
}
}

// Truffle complains that these outer keys *are not* set when running plugin fn directly.
// But throws saying they *cannot* be manually set when running as truffle command.
function setOuterConfigKeys(config, app, id){
try {
config.network_id = id;
config.port = app.port;
config.host = app.host;
config.provider = TruffleProvider.create(config);
} catch (err){}
}

/**
* Tries to load truffle module library and reports source. User can force use of
Expand Down Expand Up @@ -188,8 +275,7 @@ function loadTruffleLibrary(ui, truffleConfig){
return require("./plugin-assets/truffle.library")

} catch(err) {
const msg = ui.generate('lib-fail', [err]);
throw new Error(msg);
throw new Error(ui.generate('lib-fail', [err]));
};

}
Expand Down Expand Up @@ -218,11 +304,12 @@ function loadSolcoverJS(ui, truffleConfig){
coverageConfig = {};
}

// Truffle writes to coverage config
coverageConfig.log = truffleConfig.logger.log;
coverageConfig.cwd = truffleConfig.working_directory;
coverageConfig.originalContractsDir = truffleConfig.contracts_directory;

//Merge solcoverjs mocha opts into truffle's
// Solidity-Coverage writes to Truffle config
truffleConfig.mocha = truffleConfig.mocha || {};

if (coverageConfig.mocha && typeof coverageConfig.mocha === 'object'){
Expand Down
Loading