Do not connect to the node until it is needed in test

This commit is contained in:
Anthony Laibe 2018-07-30 09:27:59 +01:00
parent 1ec291dd46
commit 4b119918c3
2 changed files with 66 additions and 99 deletions

View File

@ -93,7 +93,6 @@ module.exports = {
return Mocha.describe(describeName, callback);
};
console.info('Compiling contracts'.cyan);
test.init((err) => {
next(err, files);
});
@ -115,7 +114,6 @@ module.exports = {
done(err);
});
});
mocha.run(function (fails) {
failures += fails;
mocha.suite.removeAllListeners();

View File

@ -33,10 +33,11 @@ class Test {
constructor(options) {
this.options = options || {};
this.simOptions = {};
this.contracts = {};
this.events = new Events();
this.ready = true;
this.firstRunConfig = true;
this.error = false;
this.contracts = {};
this.builtContracts = {};
this.compiledContracts = {};
this.logsSubscription = null;
@ -77,7 +78,6 @@ class Test {
if (err) {
return callback(err);
}
self.subscribeToPendingTransactions();
callback();
});
});
@ -124,8 +124,6 @@ class Test {
}
init(callback) {
const self = this;
this.engine = new Engine({
env: this.options.env || 'test',
// TODO: config will need to detect if this is a obj
@ -133,37 +131,23 @@ class Test {
interceptLogs: false
});
this.initWeb3Provider((err) => {
if (err) {
return callback(err);
}
this.engine.init({
logger: new TestLogger({logLevel: this.options.loglevel})
});
this.versions_default = this.engine.config.contractsConfig.versions;
// Reset contract config to nothing to make sure we deploy only what we want
this.engine.config.contractsConfig = {
contracts: {},
versions: this.versions_default
};
this.engine.startService("libraryManager");
this.engine.startService("codeRunner");
this.initDeployServices();
this.engine.startService("codeGenerator");
this.engine.contractsManager.build(() => {
self.builtContracts = cloneDeep(self.engine.contractsManager.contracts);
let className;
for (className in self.builtContracts) {
self.builtContracts[className].dependencyCount = null;
}
self.compiledContracts = cloneDeep(self.engine.contractsManager.compiledContracts);
callback();
});
this.engine.init({
logger: new TestLogger({logLevel: this.options.loglevel})
});
this.versions_default = this.engine.config.contractsConfig.versions;
// Reset contract config to nothing to make sure we deploy only what we want
this.engine.config.contractsConfig = {
contracts: {},
versions: this.versions_default
};
this.engine.startService("libraryManager");
this.engine.startService("codeRunner");
this.initDeployServices();
this.engine.startService("codeGenerator");
callback();
}
onReady(callback) {
@ -193,14 +177,9 @@ class Test {
checkDeploymentOptions(options, callback) {
const self = this;
if (!options.deployment) {
if (!self.simOptions.host && !self.simOptions.accounts) {
return callback();
}
self.simOptions = {};
} else {
self.simOptions = {};
let resetServices = false;
self.simOptions = {};
let resetServices = false;
if (options.deployment) {
if (options.deployment.accounts) {
// Account setup
self.simOptions.accounts = AccountParser.parseAccountsConfig(options.deployment.accounts, self.web3);
@ -217,14 +196,17 @@ class Test {
});
resetServices = true;
}
if (!resetServices) {
return callback();
}
}
if (!resetServices && !self.firstRunConfig) {
return callback();
}
self.initWeb3Provider((err) => {
if (err) {
return callback(err);
}
self.firstRunConfig = false;
self.initDeployServices();
callback();
});
@ -255,6 +237,21 @@ class Test {
self.engine.contractsManager.contractDependencies = {};
next();
},
function compileContracts(next) {
if (Object.keys(self.builtContracts).length > 0) {
return next();
}
console.info('Compiling contracts'.cyan);
self.engine.contractsManager.build(() => {
self.builtContracts = cloneDeep(self.engine.contractsManager.contracts);
let className;
for (className in self.builtContracts) {
self.builtContracts[className].dependencyCount = null;
}
self.compiledContracts = cloneDeep(self.engine.contractsManager.compiledContracts);
next();
});
},
function deploy(next) {
self._deploy(options, (err, accounts) => {
if (err) {
@ -314,27 +311,27 @@ class Test {
function createContractObject(accounts, next) {
async.each(Object.keys(self.engine.contractsManager.contracts), (contractName, eachCb) => {
const contract = self.engine.contractsManager.contracts[contractName];
let data;
if (!self.contracts[contractName]) {
self.contracts[contractName] = {};
data = "";
} else {
data = self.contracts[contractName].options.data;
}
Object.assign(self.contracts[contractName], new EmbarkJS.Contract({
let newContract = new EmbarkJS.Contract({
abi: contract.abiDefinition,
address: contract.deployedAddress,
from: self.web3.eth.defaultAccount,
gas: 6000000,
web3: self.web3
}));
});
self.contracts[contractName].address = contract.deployedAddress;
if (self.contracts[contractName].options) {
self.contracts[contractName].options.from = self.contracts[contractName].options.from || self.web3.eth.defaultAccount;
self.contracts[contractName].options.data = data;
self.contracts[contractName].options.gas = 6000000;
if (newContract.options) {
newContract.options.from = self.web3.eth.defaultAccount;
newContract.options.data = contract.code;
newContract.options.gas = 6000000;
}
Object.setPrototypeOf(self.contracts[contractName], newContract);
Object.assign(self.contracts[contractName], newContract);
eachCb();
}, (err) => {
next(err, accounts);
@ -349,48 +346,20 @@ class Test {
});
}
require(module) {
if (module.startsWith('Embark/contracts/')) {
const contractName = module.substr(17);
if (this.contracts[contractName]) {
return this.contracts[contractName];
}
let contract = this.engine.contractsManager.contracts[contractName];
if (!contract) {
const contractNames = Object.keys(this.engine.contractsManager.contracts);
// It is probably an instanceof
contractNames.find(contrName => {
// Find a contract with a similar name
if (contractName.indexOf(contrName) > -1) {
contract = this.engine.contractsManager.contracts[contrName];
return true;
}
return false;
});
// If still nothing, assign bogus one, we will redefine it anyway on deploy
if (!contract) {
console.warn(__('Could not recognize the contract name "%s"', contractName));
console.warn(__('If it is an instance of another contract, it will be reassigned on deploy'));
console.warn(__('Otherwise, you can rename the contract to contain the parent contract in the name eg: Token2 for Token'));
contract = this.engine.contractsManager.contracts[contractNames[0]];
}
}
this.contracts[contractName] = new EmbarkJS.Contract({
abi: contract.abiDefinition,
address: contract.address,
from: this.web3.eth.defaultAccount,
gas: 6000000,
web3: this.web3
});
this.contracts[contractName].address = contract.address;
this.contracts[contractName].options.data = contract.code;
this.contracts[contractName].options.gas = 6000000;
this.web3.eth.getAccounts().then((accounts) => {
this.contracts[contractName].options.from = contract.from || accounts[0];
});
return this.contracts[contractName];
require(path) {
const prefix = 'Embark/contracts/';
if (!path.startsWith(prefix)) {
throw new Error(__('Unknown module %s', path));
}
throw new Error(__('Unknown module %s', module));
let contractName = path.replace(prefix, "");
let contract = this.contracts[contractName];
if (contract) {
return contract;
}
let newContract = {};
this.contracts[contractName] = newContract;
return newContract;
}
}