From e71f474b88cefcec823f2543da6f4126bc72bde8 Mon Sep 17 00:00:00 2001 From: Casper Lans Date: Sat, 15 Sep 2018 00:18:41 +0200 Subject: [PATCH 1/2] Read settings.js from current working directory --- index.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 05fe146..d4d4b22 100644 --- a/index.js +++ b/index.js @@ -156,9 +156,18 @@ class NodeTestHelper extends EventEmitter { } }; - var settings = { - available: function() { return false; } - }; + var settings; + try { + // Try to read settings.js from the current working directory. + var filePath = path.join(process.cwd(), "settings.js"); + require.resolve(filePath); + settings = require(filePath); + } catch (err) { + // settings.js not found, set settings to not available. + settings = { + available: function() { return false; } + }; + } var red = { _: v => v From 1557a4f8dba07cc1ee26a04f07ee4b9ce5af3e9d Mon Sep 17 00:00:00 2001 From: Casper Lans Date: Sun, 16 Sep 2018 17:19:21 +0200 Subject: [PATCH 2/2] Pass settings as optional parameter to load() instead of config file --- index.js | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index d4d4b22..1d9b963 100644 --- a/index.js +++ b/index.js @@ -58,7 +58,6 @@ function findRuntimePath() { } } - // As we have prerelease tags in development version, they need stripping off // before semver will do a sensible comparison with a range. function checkSemver(localVersion,testRange) { @@ -118,7 +117,30 @@ class NodeTestHelper extends EventEmitter { } } - load(testNode, testFlow, testCredentials, cb) { + /** + * Loads a flow then starts the flow. + * + * @param {Object|Object[]} testNode - Module object of a node to be tested returned by require function. This node will be registered, and can be used in testFlows. + * @param {Object[]} testFlow - Flow data to test a node. If you want to use flow data exported from Node-RED editor, export the flow to the clipboard and paste the content into your test scripts. + * @param {Object} [testCredentials] - Optional node credentials. (Not optional when testSettings parameter is supplied, but may be undefined.) + * @param {Object} [testSettings] - Optional settings. + * @param {function} cb - Function to call back when testFlows has been started. + */ + load(testNode, testFlow, testCredentials, testSettings, cb) { + if (arguments.length === 3) { + // function call was: load(testNode, testFlow, cb) + cb = testCredentials; + testSettings = { available: function() { return false; } }; + testCredentials = {}; + } else if (arguments.length === 4) { + // function call was: load(testNode, testFlow, testCredentials, cb) + cb = testSettings; + testSettings = { available: function() { return false; } }; + } else if (testCredentials === undefined) { + // function call was: load(testNode, testFlow, undefined, testSettings, cb) + testCredentials = {}; + } + const log = this._log; const logSpy = this._logSpy = this._sandbox.spy(log, 'log'); logSpy.FATAL = log.FATAL; @@ -143,32 +165,12 @@ class NodeTestHelper extends EventEmitter { }); }); - - - if (typeof testCredentials === 'function') { - cb = testCredentials; - testCredentials = {}; - } - var storage = { getFlows: function () { return when.resolve({flows:testFlow,credentials:testCredentials}); } }; - var settings; - try { - // Try to read settings.js from the current working directory. - var filePath = path.join(process.cwd(), "settings.js"); - require.resolve(filePath); - settings = require(filePath); - } catch (err) { - // settings.js not found, set settings to not available. - settings = { - available: function() { return false; } - }; - } - var red = { _: v => v }; @@ -182,7 +184,7 @@ class NodeTestHelper extends EventEmitter { const redNodes = this._redNodes; redNodes.init({ events: this._events, - settings: settings, + settings: testSettings, storage: storage, log: this._log });