From ac930d0be03e8860fb357df8756527a268f70965 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 14 Sep 2018 19:58:23 -0500 Subject: [PATCH 1/6] report error and exit if --node option value is not valid --- cmd/cmd.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index 6e1981b3..bfed9688 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -262,20 +262,24 @@ class Cmd { } test() { - const urlRegexExp = /^(vm|embark|((ws|https?):\/\/([a-zA-Z0-9_.-]*):?([0-9]*)?))$/i; program .command('test [file]') - .option('-n , --node ', __('Node to connect to. Valid values are ["vm", "embark", ""]: \n') + + .option('-n , --node ', __('Node to connect to. Valid values are ["vm", "embark", ""]: \n') + ' vm - ' + __('Starts an Ethereum simulator (ganache) and runs the tests using the simulator') + '\n' + - ' embark - ' + __('Uses the node associated with an already running embark process') + '\n' + - ' ' + __(' - Connects to a running node available at the end point and uses it to run the tests'), - urlRegexExp, 'vm') + ' embark - ' + __('Uses the node associated with an already running embark process') + '\n' + + ' ' + __(' - Connects to a running node available at the end point and uses it to run the tests')) .option('-d , --gasDetails', __('When set, will print the gas cost for each contract deploy')) .option('-c , --coverage', __('When set, will generate the coverage after the tests')) .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) { + const node = options.node; + const urlRegexExp = /^(vm|embark|((ws|https?):\/\/([a-zA-Z0-9_.-]*):?([0-9]*)?))$/i; + if (node && !(node === 'embark' || node === 'vm' || node.match(urlRegexExp))) { + console.error(`invalid --node option: must be 'vm', 'embark' or a valid URL`.red); + process.exit(1); + } checkDeps(); i18n.setOrDetectLocale(options.locale); embark.runTests({file, loglevel: options.loglevel, gasDetails: options.gasDetails, From f790ecdcbbd9e022a1ad47c3d4679cdb48066666 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 14 Sep 2018 20:15:48 -0500 Subject: [PATCH 2/6] be more explicit about setting the default option value to 'vm' --- cmd/cmd.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index bfed9688..499741bf 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -274,12 +274,13 @@ class Cmd { .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) { - const node = options.node; + const node = options.node || 'vm'; const urlRegexExp = /^(vm|embark|((ws|https?):\/\/([a-zA-Z0-9_.-]*):?([0-9]*)?))$/i; if (node && !(node === 'embark' || node === 'vm' || node.match(urlRegexExp))) { console.error(`invalid --node option: must be 'vm', 'embark' or a valid URL`.red); process.exit(1); } + options.node = node; checkDeps(); i18n.setOrDetectLocale(options.locale); embark.runTests({file, loglevel: options.loglevel, gasDetails: options.gasDetails, From 203039763d358b7a0279c04b20a98404e2357a22 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 14 Sep 2018 20:25:22 -0500 Subject: [PATCH 3/6] `node` won't ever be falsy owing to `|| 'vm'` --- cmd/cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index 499741bf..516d1182 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -276,7 +276,7 @@ class Cmd { .action(function(file, options) { const node = options.node || 'vm'; const urlRegexExp = /^(vm|embark|((ws|https?):\/\/([a-zA-Z0-9_.-]*):?([0-9]*)?))$/i; - if (node && !(node === 'embark' || node === 'vm' || node.match(urlRegexExp))) { + if (!(node === 'embark' || node === 'vm' || node.match(urlRegexExp))) { console.error(`invalid --node option: must be 'vm', 'embark' or a valid URL`.red); process.exit(1); } From 269d544ab36f337bfdb7cfcd2b67c15ed7ef4d7a Mon Sep 17 00:00:00 2001 From: emizzle Date: Sat, 15 Sep 2018 11:32:16 +1000 Subject: [PATCH 4/6] =?UTF-8?q?show=20option=20usage=20help=20with=20inval?= =?UTF-8?q?id=20`=E2=80=94node`=20option=20and=20simplied=20the=20valid=20?= =?UTF-8?q?option=20check=20to=20pure=20regex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/cmd.js | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/cmd.js b/cmd/cmd.js index 516d1182..a8ba75ae 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -278,6 +278,7 @@ class Cmd { const urlRegexExp = /^(vm|embark|((ws|https?):\/\/([a-zA-Z0-9_.-]*):?([0-9]*)?))$/i; if (!(node === 'embark' || node === 'vm' || node.match(urlRegexExp))) { console.error(`invalid --node option: must be 'vm', 'embark' or a valid URL`.red); + options.outputHelp(); process.exit(1); } options.node = node; From 6e444aaf652bad01d9a61a009c24549d032f1d6c Mon Sep 17 00:00:00 2001 From: emizzle Date: Sat, 15 Sep 2018 11:38:48 +1000 Subject: [PATCH 5/6] simplied the valid option check to pure regex --- cmd/cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index a8ba75ae..84586c36 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -276,7 +276,7 @@ class Cmd { .action(function(file, options) { const node = options.node || 'vm'; const urlRegexExp = /^(vm|embark|((ws|https?):\/\/([a-zA-Z0-9_.-]*):?([0-9]*)?))$/i; - if (!(node === 'embark' || node === 'vm' || node.match(urlRegexExp))) { + if (!urlRegexExp.test(node)) { console.error(`invalid --node option: must be 'vm', 'embark' or a valid URL`.red); options.outputHelp(); process.exit(1); From 6e60566b2750230a2746c95d6342bb75f1ba7cb6 Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Fri, 14 Sep 2018 20:44:22 -0500 Subject: [PATCH 6/6] add line break between error message and help output --- cmd/cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cmd.js b/cmd/cmd.js index 84586c36..ea415d4a 100644 --- a/cmd/cmd.js +++ b/cmd/cmd.js @@ -277,7 +277,7 @@ class Cmd { const node = options.node || 'vm'; const urlRegexExp = /^(vm|embark|((ws|https?):\/\/([a-zA-Z0-9_.-]*):?([0-9]*)?))$/i; if (!urlRegexExp.test(node)) { - console.error(`invalid --node option: must be 'vm', 'embark' or a valid URL`.red); + console.error(`invalid --node option: must be 'vm', 'embark' or a valid URL\n`.red); options.outputHelp(); process.exit(1); }