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/core/engine.js b/lib/core/engine.js index 1c64466e1..cdccb309a 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -212,7 +212,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/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..2bc7e133b 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 = { @@ -134,6 +139,22 @@ class Test { this.initDeployServices(); this.engine.startService("codeGenerator"); + if (this.options.node === 'embark') { + return this.engine.ipc.connect((err) => { + if (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) { + return this.engine.logger.error(err.message || err); + } + this.options.node = node; + callback(); + }); + }); + } callback(); } 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); } diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 92facc19d..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,17 @@ 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) { + 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){ @@ -482,6 +497,7 @@ module.exports = { normalizeInput, buildUrl, buildUrlFromConfig, + deconstructUrl, getWeiBalanceFromString, getHexBalanceFromString, compact,