From 383b3c31a519059c753994139ee1070f364c639b Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Sun, 12 Mar 2017 12:23:30 +0900 Subject: [PATCH 1/2] speed up test class - unstable --- lib/core/test.js | 105 ++++++++++++++++---------- lib/index.js | 21 +----- test/cmd.js | 13 +++- test_app/test/another_storage_spec.js | 5 +- test_app/test/simple_storage_spec.js | 4 +- test_app/test/token_spec.js | 4 +- 6 files changed, 89 insertions(+), 63 deletions(-) diff --git a/lib/core/test.js b/lib/core/test.js index fe3b04063..3c0a186c3 100644 --- a/lib/core/test.js +++ b/lib/core/test.js @@ -1,14 +1,9 @@ var async = require('async'); var Web3 = require('web3'); -var Engine = require('./engine.js'); -var RunCode = require('./runCode.js'); -var TestLogger = require('./test_logger.js'); - var getSimulator = function() { try { - var sim = require('ethereumjs-testrpc'); - return sim; + return require('ethereumjs-testrpc'); } catch (e) { if (e.code === 'MODULE_NOT_FOUND') { console.log('Simulator not found; Please install it with "npm install ethereumjs-testrpc --save"'); @@ -25,32 +20,58 @@ var getSimulator = function() { } }; + var Test = function(options) { - this.options = options || {}; - var simOptions = this.options.simulatorOptions || {}; + var opts = options === undefined ? {} : options; + opts.logLevel = opts.hasOwnProperty('logLevel') ? opts.logLevel : 'debug'; + opts.simulatorOptions = opts.hasOwnProperty('simulatorOptions') ? opts.simulatorOptions : {}; - 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 - }); + function newWebThree () { + try { + var Web3 = require('web3'); + var web3 = new Web3(); + web3.setProvider(getSimulator()); + } 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'); + // TODO: should throw exception instead + return process.exit(); + } + 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 new Error(e); + } + } - this.engine.init({ - logger: new TestLogger({logLevel: this.options.logLevel || 'debug'}) - }); + function deployAll (contractsConfig, cb) { + var RunCode = require('./runCode.js'); + var self = this; - this.sim = getSimulator(); - this.web3 = new Web3(); - this.web3.setProvider(this.sim.provider(simOptions)); -}; + function newEngine () { + var Engine = require('./engine.js'); + return new Engine({ + env: opts.env || 'test', + // TODO: confi will need to detect if this is a obj + embarkConfig: opts.embarkConfig || 'embark.json', + interceptLogs: false + }).init(); + } -Test.prototype.deployAll = function(contractsConfig, cb) { - var self = this; + function newLogger() { + var TestLogger = require('./test_logger.js'); + new TestLogger({logLevel: opts.logLevel}) + } - async.waterfall([ + this.engine = newEngine(); + this.web3 = newWebThree(); + + async.waterfall([ function getConfig(callback) { - self.engine.config.contractsConfig = {contracts: contractsConfig}; + contractsConfig = { contracts: contractsConfig }; callback(); }, function startServices(callback) { @@ -73,22 +94,28 @@ Test.prototype.deployAll = function(contractsConfig, cb) { } }); } - ], function(err, result) { - if (err) { - console.log("got error"); - process.exit(); - } - // this should be part of the waterfall and not just something done at the - // end - self.web3.eth.getAccounts(function(err, accounts) { + ], function(err, result) { if (err) { - throw new Error(err); + console.log("got error"); + process.exit(); } - self.web3.eth.defaultAccount = accounts[0]; - RunCode.doEval(result, self.web3); // jshint ignore:line - cb(); + // this should be part of the waterfall and not just something done at the + // end + self.web3.eth.getAccounts(function(err, accounts) { + if (err) { + throw new Error(err); + } + self.web3.eth.defaultAccount = accounts[0]; + RunCode.doEval(result, self.web3); // jshint ignore:line + cb(); + }); }); - }); -}; + } + + + return { + deployAll: deployAll + } +}(); module.exports = Test; diff --git a/lib/index.js b/lib/index.js index e60b26813..468a4987c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,19 +1,17 @@ /*jshint esversion: 6 */ var async = require('async'); -//require("./utils/debug_util.js")(__filename, async); +//require("./core/debug_util.js")(__filename, async); var colors = require('colors'); var Engine = require('./core/engine.js'); -var Test = require('./core/test.js'); - var IPFS = require('./upload/ipfs.js'); var Swarm = require('./upload/swarm.js'); var version = require('../package.json').version; -var Embark = function () { +var Embark = (function () { function initConfig (env, options) { var Events = require('./core/events.js'); var Logger = require('./core/logger.js'); @@ -167,6 +165,7 @@ var Embark = function () { } function initTests (options) { + var Test = require('./core/test.js'); return new Test(options); } @@ -196,19 +195,7 @@ var Embark = function () { upload: upload }; -}(); - -Embark.initTests = function() { - console.error("=============================".green); - console.error("deprecated: Starting with Embark 2.5.0 the Embark object needs to be initialized".red); - console.log("replace:"); - console.log("var Embark = require('embark');"); - console.log("with:"); - console.log("var Embark = require('embark')();"); - console.error("=============================".green); - var embark = Embark(); - return embark.initTests(); -}; +})(); module.exports = Embark; diff --git a/test/cmd.js b/test/cmd.js index 8b64a97dd..8565ab3d7 100644 --- a/test/cmd.js +++ b/test/cmd.js @@ -2,8 +2,8 @@ var Embark = require('../lib/index'); var Cmd = require('../lib/cmd'); describe('embark.Cmd', function () { + this.timeout(0); var cmd = new Cmd(Embark); - describe('#new', function () { it('it should not create an app without a name', function (done) { cmd.newApp(undefined, function (output) { @@ -27,4 +27,15 @@ describe('embark.Cmd', function () { done(); }); }); + + // describe("#help", function () { + // it('it should spit out helpful text if no arguments are supplied', function (done) { + // cmd.process([], function (output) { + // var lines = output.split('\n'); + // assert.equal(lines[0], '\n'); + // assert.equal(lines[1], 'Usage:'); + // done(); + // }); + // }) + // }) }); \ No newline at end of file diff --git a/test_app/test/another_storage_spec.js b/test_app/test/another_storage_spec.js index ad12778d2..bb5f8c911 100644 --- a/test_app/test/another_storage_spec.js +++ b/test_app/test/another_storage_spec.js @@ -1,6 +1,7 @@ var assert = require('assert'); -var Embark = require('embark')(); -var EmbarkSpec = Embark.initTests(); +var Embark = require('embark'); +//TODO: this path is temporary to handle the scope of Embark within an app +var EmbarkSpec = require('../node_modules/embark/lib/core/test.js'); var web3 = EmbarkSpec.web3; describe("AnotherStorage", function() { diff --git a/test_app/test/simple_storage_spec.js b/test_app/test/simple_storage_spec.js index 55dfe63fd..9a24bc69f 100644 --- a/test_app/test/simple_storage_spec.js +++ b/test_app/test/simple_storage_spec.js @@ -1,6 +1,6 @@ var assert = require('assert'); -var Embark = require('embark')(); -var EmbarkSpec = Embark.initTests(); +var Embark = require('embark'); +var EmbarkSpec = require('../node_modules/embark/lib/core/test.js'); var web3 = EmbarkSpec.web3; describe("SimpleStorage", function() { diff --git a/test_app/test/token_spec.js b/test_app/test/token_spec.js index 8bbb4d2b1..6dfdff78e 100644 --- a/test_app/test/token_spec.js +++ b/test_app/test/token_spec.js @@ -1,6 +1,6 @@ var assert = require('assert'); -var Embark = require('embark')(); -var EmbarkSpec = Embark.initTests(); +var Embark = require('embark'); +var EmbarkSpec = require('../node_modules/embark/lib/core/test.js'); var web3 = EmbarkSpec.web3; describe("Token", function() { From 124177c8fc4a8288f923508ea1a0774e0d4ffc7c Mon Sep 17 00:00:00 2001 From: Todd Baur Date: Mon, 13 Mar 2017 00:21:19 +0900 Subject: [PATCH 2/2] speed up testing --- lib/core/test.js | 56 ++++++++++----------------- lib/index.js | 2 +- test_app/package.json | 2 +- test_app/test/another_storage_spec.js | 5 +-- test_app/test/simple_storage_spec.js | 4 +- test_app/test/token_spec.js | 6 +-- 6 files changed, 26 insertions(+), 49 deletions(-) diff --git a/lib/core/test.js b/lib/core/test.js index 3c0a186c3..d4b216e45 100644 --- a/lib/core/test.js +++ b/lib/core/test.js @@ -1,6 +1,3 @@ -var async = require('async'); -var Web3 = require('web3'); - var getSimulator = function() { try { return require('ethereumjs-testrpc'); @@ -20,34 +17,26 @@ var getSimulator = function() { } }; - -var Test = function(options) { +var Test; +Test = (function (options) { + var async = require('async'); var opts = options === undefined ? {} : options; opts.logLevel = opts.hasOwnProperty('logLevel') ? opts.logLevel : 'debug'; opts.simulatorOptions = opts.hasOwnProperty('simulatorOptions') ? opts.simulatorOptions : {}; + var sim = getSimulator(); - function newWebThree () { + function newWebThree() { try { var Web3 = require('web3'); var web3 = new Web3(); - web3.setProvider(getSimulator()); + web3.setProvider(sim.provider(opts.simulatorOptions)); + return web3; } 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'); - // TODO: should throw exception instead - return process.exit(); - } - 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 new Error(e); } } - function deployAll (contractsConfig, cb) { + function deployAll(contractsConfig, cb) { var RunCode = require('./runCode.js'); var self = this; @@ -58,20 +47,16 @@ var Test = function(options) { // TODO: confi will need to detect if this is a obj embarkConfig: opts.embarkConfig || 'embark.json', interceptLogs: false - }).init(); + }); } - function newLogger() { - var TestLogger = require('./test_logger.js'); - new TestLogger({logLevel: opts.logLevel}) - } - - this.engine = newEngine(); - this.web3 = newWebThree(); + self.web3 = newWebThree(); + self.engine = newEngine(); + self.engine.init(); async.waterfall([ function getConfig(callback) { - contractsConfig = { contracts: contractsConfig }; + self.engine.config.contractsConfig = {contracts: contractsConfig}; callback(); }, function startServices(callback) { @@ -84,24 +69,24 @@ var Test = function(options) { callback(); }, function deploy(callback) { - self.engine.events.on('abi-contracts-vanila', function(vanillaABI) { + self.engine.events.on('abi-contracts-vanila', function (vanillaABI) { callback(null, vanillaABI); }); - self.engine.deployManager.deployContracts(function(err, result) { + self.engine.deployManager.deployContracts(function (err, result) { if (err) { console.log(err); callback(err); } }); } - ], function(err, result) { + ], function (err, result) { if (err) { console.log("got error"); process.exit(); } // this should be part of the waterfall and not just something done at the // end - self.web3.eth.getAccounts(function(err, accounts) { + self.web3.eth.getAccounts(function (err, accounts) { if (err) { throw new Error(err); } @@ -114,8 +99,9 @@ var Test = function(options) { return { - deployAll: deployAll - } -}(); + deployAll: deployAll, + sim: sim + }; +}()); module.exports = Test; diff --git a/lib/index.js b/lib/index.js index 468a4987c..7037f2246 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ /*jshint esversion: 6 */ var async = require('async'); -//require("./core/debug_util.js")(__filename, async); +// require("./utils/debug_util.js")(__filename, async); var colors = require('colors'); diff --git a/test_app/package.json b/test_app/package.json index 29cd34ffa..042f454f1 100644 --- a/test_app/package.json +++ b/test_app/package.json @@ -10,7 +10,7 @@ "license": "ISC", "homepage": "", "devDependencies": { - "embark": "../", + "embark": "file:../", "mocha": "^2.2.5" }, "dependencies": { diff --git a/test_app/test/another_storage_spec.js b/test_app/test/another_storage_spec.js index bb5f8c911..b60297b57 100644 --- a/test_app/test/another_storage_spec.js +++ b/test_app/test/another_storage_spec.js @@ -1,8 +1,5 @@ var assert = require('assert'); -var Embark = require('embark'); -//TODO: this path is temporary to handle the scope of Embark within an app -var EmbarkSpec = require('../node_modules/embark/lib/core/test.js'); -var web3 = EmbarkSpec.web3; +var EmbarkSpec = require('embark/lib/core/test.js'); describe("AnotherStorage", function() { before(function(done) { diff --git a/test_app/test/simple_storage_spec.js b/test_app/test/simple_storage_spec.js index 9a24bc69f..b330ced62 100644 --- a/test_app/test/simple_storage_spec.js +++ b/test_app/test/simple_storage_spec.js @@ -1,7 +1,5 @@ var assert = require('assert'); -var Embark = require('embark'); -var EmbarkSpec = require('../node_modules/embark/lib/core/test.js'); -var web3 = EmbarkSpec.web3; +var EmbarkSpec = require('embark/lib/core/test.js'); describe("SimpleStorage", function() { before(function(done) { diff --git a/test_app/test/token_spec.js b/test_app/test/token_spec.js index a043f84ab..93eb28b5f 100644 --- a/test_app/test/token_spec.js +++ b/test_app/test/token_spec.js @@ -1,9 +1,5 @@ var assert = require('assert'); -var Embark = require('embark'); -var EmbarkSpec = require('../node_modules/embark/lib/core/test.js'); -var Embark = require('embark'); -var EmbarkSpec = Embark.initTests(); -var web3 = EmbarkSpec.web3; +var EmbarkSpec = require('embark/lib/core/test.js'); describe("Token", function() { before(function(done) {