From 0bb63dd303996ae9c257470d61692014032efdaa Mon Sep 17 00:00:00 2001 From: SindreXie Date: Wed, 23 Nov 2022 10:56:47 +0800 Subject: [PATCH 1/6] benchmark: add default value for settings params --- benchmark/_cli.js | 23 ++++++++++----------- test/parallel/test-benchmark-cli.js | 31 ++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/benchmark/_cli.js b/benchmark/_cli.js index 33ba2e0963f2fe..b5ad06565ed6c0 100644 --- a/benchmark/_cli.js +++ b/benchmark/_cli.js @@ -3,6 +3,7 @@ const fs = require('fs'); const path = require('path'); +const kEmptyObject = Object.freeze(Object.create(null)); // Create an object of all benchmark scripts const benchmarks = {}; fs.readdirSync(__dirname) @@ -15,7 +16,7 @@ fs.readdirSync(__dirname) .filter((filename) => filename[0] !== '.' && filename[0] !== '_'); }); -function CLI(usage, settings) { +function CLI(usage, settings = kEmptyObject) { if (process.argv.length < 3) { this.abort(usage); // Abort will exit the process } @@ -25,8 +26,10 @@ function CLI(usage, settings) { this.items = []; this.test = false; - for (const argName of settings.arrayArgs) { - this.optional[argName] = []; + if (settings.arrayArgs) { + for (const argName of settings.arrayArgs) { + this.optional[argName] = []; + } } let currentOptional = null; @@ -39,13 +42,9 @@ function CLI(usage, settings) { } else if (mode === 'both' && arg[0] === '-') { // Optional arguments declaration - if (arg[1] === '-') { - currentOptional = arg.slice(2); - } else { - currentOptional = arg.slice(1); - } + currentOptional = arg.split('-').pop(); - if (settings.boolArgs && settings.boolArgs.includes(currentOptional)) { + if (settings.boolArgs?.includes(currentOptional)) { this.optional[currentOptional] = true; mode = 'both'; } else { @@ -55,7 +54,7 @@ function CLI(usage, settings) { } else if (mode === 'option') { // Optional arguments value - if (settings.arrayArgs.includes(currentOptional)) { + if (settings.arrayArgs?.includes(currentOptional)) { this.optional[currentOptional].push(arg); } else { this.optional[currentOptional] = arg; @@ -107,8 +106,8 @@ CLI.prototype.benchmarks = function() { }; CLI.prototype.shouldSkip = function(scripts) { - const filters = this.optional.filter || []; - const excludes = this.optional.exclude || []; + const filters = this.optional.filter ? [...this.optional.filter] : []; + const excludes = this.optional.exclude ? [...this.optional.exclude] : []; let skip = filters.length > 0; for (const filter of filters) { diff --git a/test/parallel/test-benchmark-cli.js b/test/parallel/test-benchmark-cli.js index 49069fecac43ce..8e3a3058371c68 100644 --- a/test/parallel/test-benchmark-cli.js +++ b/test/parallel/test-benchmark-cli.js @@ -1,6 +1,6 @@ 'use strict'; -require('../common'); +const common = require('../common'); // This tests the CLI parser for our benchmark suite. @@ -36,3 +36,32 @@ testFilterPattern([], ['foo', 'bar'], 'bar', true); testFilterPattern(['foo'], ['bar'], 'foo', false); testFilterPattern(['foo'], ['bar'], 'foo-bar', true); + +function testNoSettingsPattern(filters, excludes, filename, expectedResult) { + process.argv = process.argv.concat(...filters.map((p) => ['--filter', p])); + process.argv = process.argv.concat(...excludes.map((p) => ['--exclude', p])); + process.argv = process.argv.concat(['bench']); + try { + const cli = new CLI(''); + assert.deepStrictEqual(cli.shouldSkip(filename), expectedResult); + } catch { + common.mustNotCall('If settings param is null, shouldn\'t throw an error'); + } + process.argv = originalArgv; +} + +testNoSettingsPattern([], []); +testNoSettingsPattern([], [], 'foo', false); + +testNoSettingsPattern(['foo'], [], 'foo', false); +testNoSettingsPattern(['foo'], [], 'bar', true); +testNoSettingsPattern(['foo', 'bar'], [], 'foo', false); +testNoSettingsPattern(['foo', 'bar'], [], 'bar', false); + +testNoSettingsPattern([], ['foo'], 'foo', true); +testNoSettingsPattern([], ['foo'], 'bar', false); +testNoSettingsPattern([], ['foo', 'bar'], 'foo', true); +testNoSettingsPattern([], ['foo', 'bar'], 'bar', true); + +testNoSettingsPattern(['foo'], ['bar'], 'foo', false); +testNoSettingsPattern(['foo'], ['bar'], 'foo-bar', true); From 5cd9022b86e7d61f948ae4e63ae74da31b32ba27 Mon Sep 17 00:00:00 2001 From: SindreXie Date: Tue, 11 Jun 2024 17:46:48 +0800 Subject: [PATCH 2/6] lint&benchmark: change lint errors & benchmark update --- benchmark/_cli.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmark/_cli.js b/benchmark/_cli.js index b5ad06565ed6c0..8e6854efb09f5d 100644 --- a/benchmark/_cli.js +++ b/benchmark/_cli.js @@ -3,7 +3,7 @@ const fs = require('fs'); const path = require('path'); -const kEmptyObject = Object.freeze(Object.create(null)); +const kEmptyObject = Object.freeze({__proto__: null}); // Create an object of all benchmark scripts const benchmarks = {}; fs.readdirSync(__dirname) @@ -106,8 +106,8 @@ CLI.prototype.benchmarks = function() { }; CLI.prototype.shouldSkip = function(scripts) { - const filters = this.optional.filter ? [...this.optional.filter] : []; - const excludes = this.optional.exclude ? [...this.optional.exclude] : []; + const filters = this.optional.filter || []; + const excludes = this.optional.exclude || []; let skip = filters.length > 0; for (const filter of filters) { From 9aaf17ff5be7c8b019e3ef6d998dab3fbb8cad8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=8F=9C?= Date: Tue, 11 Jun 2024 16:40:25 +0800 Subject: [PATCH 3/6] Update benchmark/_cli.js Co-authored-by: Antoine du Hamel --- benchmark/_cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/_cli.js b/benchmark/_cli.js index 8e6854efb09f5d..06058620759662 100644 --- a/benchmark/_cli.js +++ b/benchmark/_cli.js @@ -42,7 +42,7 @@ function CLI(usage, settings = kEmptyObject) { } else if (mode === 'both' && arg[0] === '-') { // Optional arguments declaration - currentOptional = arg.split('-').pop(); + currentOptional = arg.slice(arg[1] === '-' ? 2 : 1); if (settings.boolArgs?.includes(currentOptional)) { this.optional[currentOptional] = true; From 504d0c3641c41b19e5a5e5acfb410c9211d95aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=8F=9C?= Date: Tue, 11 Jun 2024 17:39:53 +0800 Subject: [PATCH 4/6] Update test/parallel/test-benchmark-cli.js Co-authored-by: Antoine du Hamel --- test/parallel/test-benchmark-cli.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-benchmark-cli.js b/test/parallel/test-benchmark-cli.js index 8e3a3058371c68..17f3382001f240 100644 --- a/test/parallel/test-benchmark-cli.js +++ b/test/parallel/test-benchmark-cli.js @@ -38,9 +38,11 @@ testFilterPattern(['foo'], ['bar'], 'foo', false); testFilterPattern(['foo'], ['bar'], 'foo-bar', true); function testNoSettingsPattern(filters, excludes, filename, expectedResult) { - process.argv = process.argv.concat(...filters.map((p) => ['--filter', p])); - process.argv = process.argv.concat(...excludes.map((p) => ['--exclude', p])); - process.argv = process.argv.concat(['bench']); + process.argv = process.argv.concat( + filters.flatMap((p) => ['--filter', p]), + excludes.flatMap((p) => ['--exclude', p]), + ['bench'], + ); try { const cli = new CLI(''); assert.deepStrictEqual(cli.shouldSkip(filename), expectedResult); From 622815d0e0637262f07ef1e8bb10a54bee628de8 Mon Sep 17 00:00:00 2001 From: SindreXie Date: Wed, 19 Jun 2024 19:55:36 +0800 Subject: [PATCH 5/6] test: add optional test case for benchmark cli --- test/parallel/test-benchmark-cli.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-benchmark-cli.js b/test/parallel/test-benchmark-cli.js index 17f3382001f240..1cf098b5d37c5e 100644 --- a/test/parallel/test-benchmark-cli.js +++ b/test/parallel/test-benchmark-cli.js @@ -11,9 +11,11 @@ const CLI = require('../../benchmark/_cli.js'); const originalArgv = process.argv; function testFilterPattern(filters, excludes, filename, expectedResult) { - process.argv = process.argv.concat(...filters.map((p) => ['--filter', p])); - process.argv = process.argv.concat(...excludes.map((p) => ['--exclude', p])); - process.argv = process.argv.concat(['bench']); + process.argv = process.argv.concat( + filters.flatMap((p) => ['--filter', p]), + excludes.flatMap((p) => ['--exclude', p]), + ['bench'], + ); const cli = new CLI('', { 'arrayArgs': ['filter', 'exclude'] }); assert.deepStrictEqual(cli.shouldSkip(filename), expectedResult); @@ -67,3 +69,15 @@ testNoSettingsPattern([], ['foo', 'bar'], 'bar', true); testNoSettingsPattern(['foo'], ['bar'], 'foo', false); testNoSettingsPattern(['foo'], ['bar'], 'foo-bar', true); + +function testNormalOption(options = [], expectedResult = []) { + process.argv = process.argv.concat(options); + const cli = new CLI('', { boolArgs: ['foo', 'bar', 'foo-bar'] }); + const optional = Object.keys(cli.optional); + assert.deepEqual(optional, expectedResult); + process.argv = originalArgv; +} + +testNormalOption(['--foo'], ['foo']) +testNormalOption(['--foo', '--bar'], ['foo', 'bar']) +testNormalOption(['--foo-bar'], ['foo-bar']) From cd555f7f4cb3f913b1d68ee249ce501ed20406c9 Mon Sep 17 00:00:00 2001 From: SindreXie Date: Wed, 19 Jun 2024 20:06:24 +0800 Subject: [PATCH 6/6] lint: lint fix --- benchmark/_cli.js | 2 +- test/parallel/test-benchmark-cli.js | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/benchmark/_cli.js b/benchmark/_cli.js index 06058620759662..442730ac6e5255 100644 --- a/benchmark/_cli.js +++ b/benchmark/_cli.js @@ -3,7 +3,7 @@ const fs = require('fs'); const path = require('path'); -const kEmptyObject = Object.freeze({__proto__: null}); +const kEmptyObject = Object.freeze({ __proto__: null }); // Create an object of all benchmark scripts const benchmarks = {}; fs.readdirSync(__dirname) diff --git a/test/parallel/test-benchmark-cli.js b/test/parallel/test-benchmark-cli.js index 1cf098b5d37c5e..91dbe07d87f78f 100644 --- a/test/parallel/test-benchmark-cli.js +++ b/test/parallel/test-benchmark-cli.js @@ -12,9 +12,9 @@ const originalArgv = process.argv; function testFilterPattern(filters, excludes, filename, expectedResult) { process.argv = process.argv.concat( - filters.flatMap((p) => ['--filter', p]), - excludes.flatMap((p) => ['--exclude', p]), - ['bench'], + filters.flatMap((p) => ['--filter', p]), + excludes.flatMap((p) => ['--exclude', p]), + ['bench'], ); const cli = new CLI('', { 'arrayArgs': ['filter', 'exclude'] }); @@ -74,10 +74,10 @@ function testNormalOption(options = [], expectedResult = []) { process.argv = process.argv.concat(options); const cli = new CLI('', { boolArgs: ['foo', 'bar', 'foo-bar'] }); const optional = Object.keys(cli.optional); - assert.deepEqual(optional, expectedResult); + assert.deepStrictEqual(optional, expectedResult); process.argv = originalArgv; } -testNormalOption(['--foo'], ['foo']) -testNormalOption(['--foo', '--bar'], ['foo', 'bar']) -testNormalOption(['--foo-bar'], ['foo-bar']) +testNormalOption(['--foo'], ['foo']); +testNormalOption(['--foo', '--bar'], ['foo', 'bar']); +testNormalOption(['--foo-bar'], ['foo-bar']);