embark-area-51/lib/tests/test.js

200 lines
6.8 KiB
JavaScript
Raw Normal View History

2018-06-01 17:42:05 +00:00
const async = require('async');
2018-06-01 17:33:11 +00:00
const Engine = require('../core/engine.js');
const TestLogger = require('./test_logger.js');
const Web3 = require('web3');
const utils = require('../utils/utils');
2018-06-01 17:42:05 +00:00
const constants = require('../constants');
const Events = require('../core/events');
2018-06-01 17:44:49 +00:00
const cloneDeep = require('clone-deep');
2017-06-26 13:01:54 +00:00
2018-06-01 17:33:11 +00:00
function getSimulator() {
2017-03-04 02:06:44 +00:00
try {
2018-06-01 17:33:11 +00:00
return require('ganache-cli');
2017-03-04 02:06:44 +00:00
} catch (e) {
2018-06-01 17:33:11 +00:00
const moreInfo = 'For more information see https://github.com/trufflesuite/ganache-cli';
2017-03-04 02:06:44 +00:00
if (e.code === 'MODULE_NOT_FOUND') {
2018-06-01 17:33:11 +00:00
console.error(__('Simulator not found; Please install it with "%s"', 'npm install ganache-cli --save'));
console.error(moreInfo);
throw e;
2017-03-04 02:06:44 +00:00
}
2018-06-01 17:33:11 +00:00
console.error("==============");
console.error(__("Tried to load Ganache CLI (testrpc), but an error occurred. This is a problem with Ganache CLI"));
console.error(moreInfo);
console.error("==============");
2017-03-04 02:06:44 +00:00
throw e;
}
2018-06-01 17:33:11 +00:00
}
class Test {
constructor(options) {
this.options = options || {};
this.simOptions = this.options.simulatorOptions || {};
2018-06-01 17:43:43 +00:00
this.contracts = {};
this.events = new Events();
this.ready = true;
this.builtContracts = {};
2018-06-04 14:45:50 +00:00
this.compiledContracts = {};
2018-06-01 17:43:43 +00:00
2018-06-01 17:33:11 +00:00
this.web3 = new Web3();
if (this.simOptions.node) {
this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node));
} else {
this.sim = getSimulator();
this.web3.setProvider(this.sim.provider(this.simOptions));
}
2018-05-16 23:24:08 +00:00
2018-06-01 17:33:11 +00:00
this.engine = new Engine({
env: this.options.env || 'test',
2018-06-01 17:42:05 +00:00
// TODO: config will need to detect if this is a obj
2018-06-01 17:33:11 +00:00
embarkConfig: this.options.embarkConfig || 'embark.json',
interceptLogs: false
});
2017-10-13 09:56:42 +00:00
2018-06-01 17:33:11 +00:00
this.engine.init({
logger: new TestLogger({logLevel: 'debug'})
2017-06-26 13:01:54 +00:00
});
2018-06-01 17:33:11 +00:00
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};
2018-06-01 17:42:05 +00:00
this.engine.startService("libraryManager");
this.engine.startService("codeRunner");
this.engine.startService("web3", {
web3: this.web3
});
this.engine.startService("deployment", {
2018-06-04 17:12:51 +00:00
trackContracts: false,
ipcRole: 'client'
2018-06-01 17:42:05 +00:00
});
this.engine.startService("codeGenerator");
}
2018-06-01 17:43:43 +00:00
init(callback) {
2018-06-01 17:44:49 +00:00
const self = this;
2018-06-01 17:43:43 +00:00
this.engine.contractsManager.build(() => {
2018-06-01 17:44:49 +00:00
self.builtContracts = cloneDeep(self.engine.contractsManager.contracts);
2018-06-04 14:45:50 +00:00
self.compiledContracts = cloneDeep(self.engine.contractsManager.compiledContracts);
2018-06-01 17:43:43 +00:00
callback();
});
}
onReady(callback) {
if (this.ready) {
return callback();
}
this.events.once('ready', () => {
callback();
});
}
2018-06-01 17:42:05 +00:00
config(options, callback) {
if (!callback) {
callback = function () {
};
}
2018-05-30 21:35:54 +00:00
if (!options.contracts) {
throw new Error(__('No contracts specified in the options'));
}
2018-06-01 17:42:05 +00:00
this.options = utils.recursiveMerge(this.options, options);
this.simOptions = this.options.simulatorOptions || {};
this.ready = false;
2018-06-01 17:42:05 +00:00
2018-06-01 17:44:49 +00:00
// Reset contracts
this.engine.contractsManager.contracts = cloneDeep(this.builtContracts);
2018-06-04 14:45:50 +00:00
this.engine.contractsManager.compiledContracts = cloneDeep(this.compiledContracts);
2018-06-01 17:44:49 +00:00
2018-06-01 17:42:05 +00:00
this._deploy(options, (err, accounts) => {
this.ready = true;
this.events.emit('ready');
2018-06-01 17:42:05 +00:00
if (err) {
2018-06-01 17:44:49 +00:00
console.error(err.red);
2018-06-01 17:42:05 +00:00
return callback(err);
}
callback(null, accounts);
});
}
2018-06-01 17:43:43 +00:00
_deploy(config, callback) {
2018-06-01 17:42:05 +00:00
const self = this;
async.waterfall([
2018-06-01 17:43:43 +00:00
function getConfig(next) {
self.engine.config.contractsConfig = {contracts: config.contracts, versions: self.versions_default};
2018-06-01 17:42:05 +00:00
self.engine.events.emit(constants.events.contractConfigChanged, self.engine.config.contractsConfig);
2018-06-01 17:43:43 +00:00
next();
2018-06-01 17:42:05 +00:00
},
2018-06-01 17:43:43 +00:00
function deploy(next) {
2018-06-01 17:42:05 +00:00
self.engine.deployManager.gasLimit = 6000000;
self.engine.contractsManager.gasLimit = 6000000;
self.engine.deployManager.fatalErrors = true;
self.engine.deployManager.deployOnlyOnConfig = true;
self.engine.events.request('deploy:contracts', next);
2018-06-01 17:43:43 +00:00
},
function getAccounts(next) {
self.web3.eth.getAccounts(function (err, accounts) {
2018-06-01 17:42:05 +00:00
if (err) {
2018-06-01 17:43:43 +00:00
return next(err);
2018-06-01 17:42:05 +00:00
}
2018-06-01 17:43:43 +00:00
self.accounts = accounts;
self.web3.eth.defaultAccount = accounts[0];
next();
2018-06-01 17:42:05 +00:00
});
2018-06-01 17:43:43 +00:00
},
function createContractObject(next) {
2018-06-01 17:44:49 +00:00
async.each(Object.keys(self.engine.contractsManager.contracts), (contractName, eachCb) => {
2018-06-01 17:43:43 +00:00
const contract = self.engine.contractsManager.contracts[contractName];
2018-06-01 17:44:49 +00:00
if (!self.contracts[contractName]) {
self.contracts[contractName] = {};
}
Object.assign(self.contracts[contractName], new self.web3.eth.Contract(contract.abiDefinition, contract.deployedAddress,
2018-05-30 13:06:38 +00:00
{from: self.web3.eth.defaultAccount, gas: 6000000}));
2018-06-06 16:47:16 +00:00
self.contracts[contractName].address = contract.deployedAddress;
2018-06-01 17:43:43 +00:00
eachCb();
}, next);
2018-06-01 17:42:05 +00:00
}
2018-06-01 17:43:43 +00:00
], function (err) {
if (err) {
2018-06-01 17:42:05 +00:00
console.log(__('terminating due to error'));
2018-06-01 17:44:49 +00:00
return callback(err);
}
2018-06-01 17:43:43 +00:00
callback();
});
2018-06-01 17:35:15 +00:00
}
2018-06-01 17:43:43 +00:00
require(module) {
2018-05-30 21:35:54 +00:00
if (module.startsWith('Embark/contracts/')) {
const contractName = module.substr(17);
2018-06-01 17:43:43 +00:00
if (this.contracts[contractName]) {
return this.contracts[contractName];
}
2018-05-31 13:52:37 +00:00
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"'));
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]];
}
}
2018-05-30 13:06:38 +00:00
this.contracts[contractName] = new this.web3.eth.Contract(contract.abiDefinition, contract.address,
{from: this.web3.eth.defaultAccount, gas: 6000000});
2018-06-06 16:47:16 +00:00
this.contracts[contractName].address = contract.address;
2018-06-01 17:43:43 +00:00
return this.contracts[contractName];
}
throw new Error(__('Unknown module %s', module));
}
2018-06-01 17:33:11 +00:00
}
2016-08-21 22:05:35 +00:00
module.exports = Test;