From 104459011ab5b093f946cfb71b5b0334ac4a833d Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Sun, 26 May 2024 00:06:01 +0900 Subject: [PATCH] test_runner: refactor to use min/max of `validateInteger` Instead of additional `if` statement, use min/max of `validateInteger` for `shard.index`. --- lib/internal/test_runner/runner.js | 7 +------ test/parallel/test-runner-cli.js | 2 +- test/parallel/test-runner-run.mjs | 6 ++---- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 9e01eb2fbb66ed..d95619a773c3b6 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -41,7 +41,6 @@ const { codes: { ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, - ERR_OUT_OF_RANGE, ERR_TEST_FAILURE, }, } = require('internal/errors'); @@ -495,11 +494,7 @@ function run(options = kEmptyObject) { shard = { __proto__: null, index: shard.index, total: shard.total }; validateInteger(shard.total, 'options.shard.total', 1); - validateInteger(shard.index, 'options.shard.index'); - - if (shard.index <= 0 || shard.total < shard.index) { - throw new ERR_OUT_OF_RANGE('options.shard.index', `>= 1 && <= ${shard.total} ("options.shard.total")`, shard.index); - } + validateInteger(shard.index, 'options.shard.index', 1, shard.total); if (watch) { throw new ERR_INVALID_ARG_VALUE('options.shard', watch, 'shards not supported with watch mode'); diff --git a/test/parallel/test-runner-cli.js b/test/parallel/test-runner-cli.js index ab6078a4a05d74..f165a509c995cc 100644 --- a/test/parallel/test-runner-cli.js +++ b/test/parallel/test-runner-cli.js @@ -238,7 +238,7 @@ const testFixtures = fixtures.path('test-runner'); assert.strictEqual(child.status, 1); assert.strictEqual(child.signal, null); - assert.match(child.stderr.toString(), /The value of "options\.shard\.index" is out of range\. It must be >= 1 && <= 3 \("options\.shard\.total"\)\. Received 0/); + assert.match(child.stderr.toString(), /The value of "options\.shard\.index" is out of range\. It must be >= 1 && <= 3\. Received 0/); const stdout = child.stdout.toString(); assert.strictEqual(stdout, ''); } diff --git a/test/parallel/test-runner-run.mjs b/test/parallel/test-runner-run.mjs index dd98b5a2d050db..54e882c4ecabe3 100644 --- a/test/parallel/test-runner-run.mjs +++ b/test/parallel/test-runner-run.mjs @@ -345,8 +345,7 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { }), { name: 'RangeError', code: 'ERR_OUT_OF_RANGE', - // eslint-disable-next-line @stylistic/js/max-len - message: 'The value of "options.shard.index" is out of range. It must be >= 1 && <= 6 ("options.shard.total"). Received 0' + message: 'The value of "options.shard.index" is out of range. It must be >= 1 && <= 6. Received 0' }); }); @@ -360,8 +359,7 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { }), { name: 'RangeError', code: 'ERR_OUT_OF_RANGE', - // eslint-disable-next-line @stylistic/js/max-len - message: 'The value of "options.shard.index" is out of range. It must be >= 1 && <= 6 ("options.shard.total"). Received 7' + message: 'The value of "options.shard.index" is out of range. It must be >= 1 && <= 6. Received 7' }); });