2016-10-02 20:57:13 +00:00
|
|
|
var async = require('async');
|
2016-08-21 22:05:35 +00:00
|
|
|
var Web3 = require('web3');
|
2016-10-02 20:57:13 +00:00
|
|
|
var Embark = require('./index.js');
|
2016-08-21 22:05:35 +00:00
|
|
|
var ContractsManager = require('./contracts.js');
|
2016-10-02 20:57:13 +00:00
|
|
|
var Deploy = require('./deploy.js');
|
2016-10-02 21:26:48 +00:00
|
|
|
var TestLogger = require('./test_logger.js');
|
2016-10-02 20:57:13 +00:00
|
|
|
var Config = require('./config.js');
|
|
|
|
var ABIGenerator = require('./abi.js');
|
2016-08-21 22:05:35 +00:00
|
|
|
|
2017-01-29 02:31:09 +00:00
|
|
|
// TODO: load config file
|
|
|
|
// TODO: include plugins
|
2016-08-21 22:05:35 +00:00
|
|
|
var Test = function(options) {
|
|
|
|
try {
|
2016-10-02 20:57:13 +00:00
|
|
|
this.sim = require('ethereumjs-testrpc');
|
2016-08-21 22:05:35 +00:00
|
|
|
} catch(e) {
|
2016-10-02 20:57:13 +00:00
|
|
|
this.sim = false;
|
2016-08-21 22:05:35 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:13 +00:00
|
|
|
if (this.sim === false) {
|
|
|
|
console.log('Simulator not found; Please install it with "npm install -g ethereumjs-testrpc');
|
|
|
|
console.log('For more information see https://github.com/ethereumjs/testrpc');
|
2016-08-21 22:05:35 +00:00
|
|
|
exit();
|
|
|
|
}
|
2016-11-08 09:31:02 +00:00
|
|
|
|
|
|
|
this.web3 = new Web3();
|
2017-02-05 03:57:46 +00:00
|
|
|
this.web3.setProvider(this.sim.provider(options));
|
2016-08-21 22:05:35 +00:00
|
|
|
};
|
|
|
|
|
2016-10-02 20:57:13 +00:00
|
|
|
Test.prototype.deployAll = function(contractsConfig, cb) {
|
2016-08-21 22:05:35 +00:00
|
|
|
var self = this;
|
2016-10-02 20:57:13 +00:00
|
|
|
var logger = new TestLogger({logLevel: 'debug'});
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
function buildContracts(callback) {
|
2016-10-15 19:54:19 +00:00
|
|
|
var config = new Config({env: 'test'});
|
2016-10-02 20:57:13 +00:00
|
|
|
config.contractsFiles = config.loadFiles(["app/contracts/**"]);
|
|
|
|
config.contractsConfig = {contracts: contractsConfig} ;
|
|
|
|
|
|
|
|
var contractsManager = new ContractsManager({
|
|
|
|
contractFiles: config.contractsFiles,
|
|
|
|
contractsConfig: config.contractsConfig,
|
|
|
|
logger: logger
|
|
|
|
});
|
2017-02-04 23:04:38 +00:00
|
|
|
contractsManager.build(callback);
|
2016-10-02 20:57:13 +00:00
|
|
|
},
|
|
|
|
function deployContracts(contractsManager, callback) {
|
|
|
|
var deploy = new Deploy({
|
|
|
|
web3: self.web3,
|
|
|
|
contractsManager: contractsManager,
|
|
|
|
logger: logger,
|
|
|
|
chainConfig: false,
|
|
|
|
env: 'test'
|
|
|
|
});
|
|
|
|
deploy.deployAll(function() {
|
|
|
|
callback(null, contractsManager);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function generateABI(contractsManager, callback) {
|
2016-12-07 02:33:31 +00:00
|
|
|
var abiGenerator = new ABIGenerator({contractsManager: contractsManager});
|
2016-10-02 20:57:13 +00:00
|
|
|
var ABI = abiGenerator.generateContracts(false);
|
|
|
|
callback(null, ABI);
|
|
|
|
}
|
|
|
|
], function(err, result) {
|
2016-10-31 01:35:08 +00:00
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
2016-10-02 20:57:13 +00:00
|
|
|
self.web3.eth.getAccounts(function(err, accounts) {
|
2016-10-31 01:35:08 +00:00
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
2016-10-02 20:57:13 +00:00
|
|
|
var web3 = self.web3;
|
|
|
|
web3.eth.defaultAccount = accounts[0];
|
2016-10-22 21:29:06 +00:00
|
|
|
// TODO: replace evals with separate process so it's isolated and with
|
|
|
|
// a callback
|
|
|
|
eval(result); // jshint ignore:line
|
2016-10-02 20:57:13 +00:00
|
|
|
cb();
|
2016-08-21 22:05:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Test;
|