2016-10-02 20:57:13 +00:00
var async = require ( 'async' ) ;
2016-08-21 22:05:35 +00:00
var Web3 = require ( 'web3' ) ;
2017-02-19 18:17:28 +00:00
2017-02-19 17:51:32 +00:00
var Embark = require ( '../index.js' ) ;
2017-02-19 18:17:28 +00:00
var ABIGenerator = require ( '../contracts/abi.js' ) ;
2017-02-19 17:51:32 +00:00
var ContractsManager = require ( '../contracts/contracts.js' ) ;
var Deploy = require ( '../contracts/deploy.js' ) ;
2017-02-19 18:17:28 +00:00
2016-10-02 20:57:13 +00:00
var Config = require ( './config.js' ) ;
2017-02-18 21:53:49 +00:00
var RunCode = require ( './runCode.js' ) ;
2017-02-19 18:17:28 +00:00
var TestLogger = require ( './test_logger.js' ) ;
2016-08-21 22:05:35 +00:00
2017-02-26 22:33:14 +00:00
var Test = function ( options ) {
this . options = _options || { } ;
var simOptions = this . options . simulatorOptions || { } ;
2017-02-06 11:42:58 +00:00
try {
2016-10-02 20:57:13 +00:00
this . sim = require ( 'ethereumjs-testrpc' ) ;
2017-02-06 11:42:58 +00:00
} catch ( e ) {
if ( e . code === 'MODULE_NOT_FOUND' ) {
console . log ( 'Simulator not found; Please install it with "npm install ethereumjs-testrpc --save"' ) ;
console . log ( 'IMPORTANT: if you using a NodeJS version older than 6.9.1 then you need to install an older version of testrpc "npm install ethereumjs-testrpc@2.0 --save"' ) ;
console . log ( 'For more information see https://github.com/ethereumjs/testrpc' ) ;
2017-02-18 21:06:39 +00:00
// TODO: should throw exception instead
process . exit ( ) ;
2017-02-06 11:42:58 +00:00
} else {
console . log ( "==============" ) ;
console . log ( "Tried to load testrpc but an error occurred. This is a problem with testrpc" ) ;
console . log ( 'IMPORTANT: if you using a NodeJS version older than 6.9.1 then you need to install an older version of testrpc "npm install ethereumjs-testrpc@2.0 --save". Alternatively install node 6.9.1 and the testrpc 3.0' ) ;
console . log ( "==============" ) ;
throw e ;
}
}
2016-08-21 22:05:35 +00:00
2016-11-08 09:31:02 +00:00
this . web3 = new Web3 ( ) ;
2017-02-12 14:54:09 +00:00
this . web3 . setProvider ( this . sim . provider ( simOptions ) ) ;
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 ;
2017-02-26 22:33:14 +00:00
var logger = new TestLogger ( { logLevel : this . options . logLevel || 'debug' } ) ;
2016-10-02 20:57:13 +00:00
async . waterfall ( [
2017-02-03 11:30:08 +00:00
function getConfig ( callback ) {
var config = new Config ( { env : 'test' , logger : logger } ) ;
2017-02-26 22:33:14 +00:00
config . loadConfigFiles ( {
2017-02-26 22:33:58 +00:00
embarkConfig : self . options . embarkConfig || 'embark.json' ,
2017-02-26 22:33:14 +00:00
interceptLogs : false
} ) ;
2017-02-06 11:42:58 +00:00
config . contractsConfig = { contracts : contractsConfig } ;
2017-02-03 11:30:08 +00:00
callback ( null , config ) ;
} ,
2017-02-06 11:42:58 +00:00
function buildContracts ( config , callback ) {
2016-10-02 20:57:13 +00:00
var contractsManager = new ContractsManager ( {
contractFiles : config . contractsFiles ,
contractsConfig : config . contractsConfig ,
2017-02-03 11:30:08 +00:00
logger : logger ,
plugins : config . plugins
2016-10-02 20:57:13 +00:00
} ) ;
2017-02-06 11:42:58 +00:00
contractsManager . build ( function ( ) {
callback ( null , contractsManager ) ;
} ) ;
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 ) ;
}
2017-02-18 21:53:49 +00:00
self . web3 . eth . defaultAccount = accounts [ 0 ] ;
RunCode . doEval ( result , self . web3 ) ; // jshint ignore:line
2017-02-26 22:33:58 +00:00
if ( self . options . logging ) {
2017-02-26 22:33:14 +00:00
cb ( logger ) ;
} else {
cb ( ) ;
}
2016-08-21 22:05:35 +00:00
} ) ;
} ) ;
} ;
module . exports = Test ;