From 3f561f525735e967351c3eebb164d6aa13ecbd87 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 22 Aug 2018 16:46:39 -0400 Subject: [PATCH 1/3] use node option for tests --- cmd/cmd.js | 3 ++- lib/tests/run_tests.js | 2 +- lib/tests/test.js | 11 ++++++++--- lib/utils/utils.js | 11 +++++++++++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index 97a3ff66c..53ba06f1b 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -207,13 +207,14 @@ class Cmd { test() { program .command('test [file]') + .option('-n , --node [node]', __('Node to connect to (default: vm)')) .option('-d , --gasDetails', __('When set, will print the gas cost for each contract deploy')) .option('--locale [locale]', __('language to use (default: en)')) .option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'warn') .description(__('run tests')) .action(function (file, options) { i18n.setOrDetectLocale(options.locale); - embark.runTests({file, loglevel: options.loglevel, gasDetails: options.gasDetails}); + embark.runTests({file, loglevel: options.loglevel, gasDetails: options.gasDetails, node: options.node}); }); } diff --git a/lib/tests/run_tests.js b/lib/tests/run_tests.js index a4e9d258a..97af4f24f 100644 --- a/lib/tests/run_tests.js +++ b/lib/tests/run_tests.js @@ -61,7 +61,7 @@ module.exports = { }, function setupGlobalNamespace(files, next) { // TODO put default config - const test = new Test({loglevel}); + const test = new Test({loglevel, node: options.node}); global.embark = test; global.assert = assert; global.config = test.config.bind(test); diff --git a/lib/tests/test.js b/lib/tests/test.js index 64cd218d9..e22db40bf 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -58,10 +58,15 @@ class Test { }); } - if (this.simOptions.host) { - let {host, port, type, protocol, accounts} = this.simOptions; + if (this.simOptions.host || this.options.node) { + let options = this.simOptions; + if (this.options.node) { + options = utils.deconstructUrl(this.options.node); + } + + let {host, port, type, protocol, accounts} = options; if (!protocol) { - protocol = (this.simOptions.type === "rpc") ? 'http' : 'ws'; + protocol = (options.type === "rpc") ? 'http' : 'ws'; } const endpoint = `${protocol}://${host}:${port}`; const providerOptions = { diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 92facc19d..c1ccbfca6 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -364,6 +364,16 @@ function buildUrlFromConfig(configObj) { return this.buildUrl(configObj.protocol, canonicalHost(configObj.host), configObj.port); } +function deconstructUrl(endpoint) { + const matches = endpoint.match(/(ws|https?):\/\/([a-zA-Z0-9_.-]*):?([0-9]*)?/); + return { + protocol: matches[1], + host: matches[2], + port: matches[3], + type: matches[1] === 'ws' ? 'ws' : 'rpc' + }; +} + function getWeiBalanceFromString(balanceString, web3){ if(!web3){ throw new Error(__('[utils.getWeiBalanceFromString]: Missing parameter \'web3\'')); @@ -482,6 +492,7 @@ module.exports = { normalizeInput, buildUrl, buildUrlFromConfig, + deconstructUrl, getWeiBalanceFromString, getHexBalanceFromString, compact, From 86f1cf51d2dda7df2d0e893e481a9c7a50edce6d Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 23 Aug 2018 12:54:43 -0400 Subject: [PATCH 2/3] enable node=embark --- lib/core/engine.js | 3 ++- lib/modules/blockchain_process/index.js | 8 +++++++- lib/modules/solidity/solcW.js | 4 ++++ lib/pipeline/pipeline.js | 2 +- lib/tests/test.js | 14 ++++++++++++++ lib/utils/utils.js | 11 ++++++++--- 6 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index 56dd62df6..0e0957ef2 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -213,7 +213,8 @@ class Engine { web3Service(options) { this.registerModule('blockchain_process', { locale: this.locale, - isDev: this.isDev + isDev: this.isDev, + ipc: this.ipc }); this.registerModule('blockchain_connector', { diff --git a/lib/modules/blockchain_process/index.js b/lib/modules/blockchain_process/index.js index dd7de6775..66fafe469 100644 --- a/lib/modules/blockchain_process/index.js +++ b/lib/modules/blockchain_process/index.js @@ -13,6 +13,7 @@ class BlockchainModule { this.embark = embark; this.locale = options.locale; this.isDev = options.isDev; + this.ipc = options.ipc; this.registerBlockchainProcess(); } @@ -25,6 +26,11 @@ class BlockchainModule { self.startBlockchainNode(cb); }); }); + + if (!this.ipc.isServer()) return; + self.ipc.on('blockchain:node', (_message, cb) => { + cb(null, utils.buildUrlFromConfig(self.contractsConfig.deployment)); + }); } assertNodeConnection(noLogs, cb) { @@ -47,7 +53,7 @@ class BlockchainModule { if (!self.contractsConfig || !self.contractsConfig.deployment || !self.contractsConfig.deployment.host) { return next(); } - const {host, port, type, protocol} = self.contractsConfig.deployment; + const {host, port, type, protocol} = self.contractsConfig.deployment; utils.pingEndpoint(host, port, type, protocol, self.blockchainConfig.wsOrigins.split(',')[0], next); } ], function (err) { diff --git a/lib/modules/solidity/solcW.js b/lib/modules/solidity/solcW.js index 8c3260ca2..355508e33 100644 --- a/lib/modules/solidity/solcW.js +++ b/lib/modules/solidity/solcW.js @@ -21,6 +21,10 @@ class SolcW { return self.load_compiler_internally(done); } + if (self.ipc.connected) { + self.compilerLoaded = true; + return done(); + } self.ipc.connect((err) => { if (err) { return self.load_compiler_internally(done); diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index b1b818cce..76f69e427 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -136,7 +136,7 @@ class Pipeline { ], function (err, contentFile) { if (err) { - self.logger.error(err); + self.logger.error(err.message || err); return fileCb(err); } diff --git a/lib/tests/test.js b/lib/tests/test.js index e22db40bf..d4b8e1d2b 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -139,6 +139,20 @@ class Test { this.initDeployServices(); this.engine.startService("codeGenerator"); + if (this.options.node === 'embark') { + return this.engine.ipc.connect((err) => { + if (err) { + return this.engine.logger.error(err.message || err); + } + this.engine.ipc.request('blockchain:node', {}, (err, node) => { + if (err) { + return this.engine.logger.error(err.message || err); + } + this.options.node = node; + callback(); + }); + }); + } callback(); } diff --git a/lib/utils/utils.js b/lib/utils/utils.js index c1ccbfca6..21b2ec2e5 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -340,13 +340,18 @@ function normalizeInput(input) { * The URL host, required. * @param {string} port * The URL port, default to empty string. + * @param {string} [type] + * Type of connection * @returns {string} the constructued URL, with defaults */ -function buildUrl(protocol, host, port) { +function buildUrl(protocol, host, port, type) { if (!host) throw new Error('utils.buildUrl: parameter \'host\' is required'); if (port) port = ':' + port; else port = ''; - return `${protocol || 'http'}://${host}${port}`; + if (!protocol) { + protocol = type === 'ws' ? 'ws' : 'http'; + } + return `${protocol}://${host}${port}`; } /** @@ -361,7 +366,7 @@ function buildUrl(protocol, host, port) { function buildUrlFromConfig(configObj) { if (!configObj) throw new Error('[utils.buildUrlFromConfig]: config object must cannot be null'); if (!configObj.host) throw new Error('[utils.buildUrlFromConfig]: object must contain a \'host\' property'); - return this.buildUrl(configObj.protocol, canonicalHost(configObj.host), configObj.port); + return this.buildUrl(configObj.protocol, canonicalHost(configObj.host), configObj.port, configObj.type); } function deconstructUrl(endpoint) { From 36c6de4b99c020497b7a1f910c873571560293aa Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 23 Aug 2018 13:01:22 -0400 Subject: [PATCH 3/3] add error message if no connection --- lib/tests/test.js | 4 +++- lib/tests/test_logger.js | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/tests/test.js b/lib/tests/test.js index d4b8e1d2b..2bc7e133b 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -142,7 +142,9 @@ class Test { if (this.options.node === 'embark') { return this.engine.ipc.connect((err) => { if (err) { - return this.engine.logger.error(err.message || err); + this.engine.logger.error(err.message || err); + this.engine.logger.error("Could not connect to Embark's IPC. Is embark running?"); + process.exit(1); } this.engine.ipc.request('blockchain:node', {}, (err, node) => { if (err) { diff --git a/lib/tests/test_logger.js b/lib/tests/test_logger.js index 9d9fe39fb..165d20b38 100644 --- a/lib/tests/test_logger.js +++ b/lib/tests/test_logger.js @@ -16,7 +16,6 @@ class TestLogger { if (!(this.shouldLog('error'))) { return; } - console.error(txt); this.logFunction(txt.red); }