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');
|
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 || {};
|
|
|
|
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-01-15 14:51:45 +00:00
|
|
|
}
|
2018-05-16 22:40:24 +00:00
|
|
|
|
2018-06-01 17:33:11 +00:00
|
|
|
config(options) {
|
|
|
|
this.options = utils.recursiveMerge(this.options, options);
|
|
|
|
this.simOptions = this.options.simulatorOptions || {};
|
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',
|
|
|
|
// TODO: confi will need to detect if this is a obj
|
|
|
|
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
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-21 22:05:35 +00:00
|
|
|
|
|
|
|
module.exports = Test;
|