From 1919c9a7ba2532b1dd469aa4b63b55c62e3ecfa9 Mon Sep 17 00:00:00 2001 From: Raz Luvaton <16746759+rluvaton@users.noreply.github.com> Date: Thu, 28 Sep 2023 19:32:47 +0300 Subject: [PATCH 1/2] test_runner: disallow array in `run` options --- lib/internal/test_runner/runner.js | 7 +++++-- test/parallel/test-runner-run.mjs | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 08f9b48dda10d3..6a4ba5bf51a902 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -424,9 +424,12 @@ function watchFiles(testFiles, opts) { } function run(options) { - if (options === null || typeof options !== 'object') { - options = kEmptyObject; + if (options != null) { + validateObject(options, 'options'); } + + options = options ?? kEmptyObject; + let { testNamePatterns, shard } = options; const { concurrency, timeout, signal, files, inspectPort, watch, setup, only } = options; diff --git a/test/parallel/test-runner-run.mjs b/test/parallel/test-runner-run.mjs index 02fed7a3659162..3f4f7f215b33e9 100644 --- a/test/parallel/test-runner-run.mjs +++ b/test/parallel/test-runner-run.mjs @@ -8,6 +8,11 @@ import assert from 'node:assert'; const testFixtures = fixtures.path('test-runner'); describe('require(\'node:test\').run', { concurrency: true }, () => { + it('should fail when passing array as options', () => { + assert.throws(() => run([]), { + code: 'ERR_INVALID_ARG_TYPE' + }); + }); it('should run with no tests', async () => { const stream = run({ files: [] }); From 9ffad8667e7afc2c13ec52f019cebc895ef403fc Mon Sep 17 00:00:00 2001 From: Raz Luvaton <16746759+rluvaton@users.noreply.github.com> Date: Fri, 6 Oct 2023 12:36:24 +0300 Subject: [PATCH 2/2] test_runner: add more run validation --- lib/internal/test_runner/runner.js | 8 ++------ test/parallel/test-runner-run.mjs | 29 ++++++++++++++++------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 6a4ba5bf51a902..68efa81614b7aa 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -423,12 +423,8 @@ function watchFiles(testFiles, opts) { return filesWatcher; } -function run(options) { - if (options != null) { - validateObject(options, 'options'); - } - - options = options ?? kEmptyObject; +function run(options = kEmptyObject) { + validateObject(options, 'options'); let { testNamePatterns, shard } = options; const { concurrency, timeout, signal, files, inspectPort, watch, setup, only } = options; diff --git a/test/parallel/test-runner-run.mjs b/test/parallel/test-runner-run.mjs index 3f4f7f215b33e9..3201da25f87a1d 100644 --- a/test/parallel/test-runner-run.mjs +++ b/test/parallel/test-runner-run.mjs @@ -8,12 +8,6 @@ import assert from 'node:assert'; const testFixtures = fixtures.path('test-runner'); describe('require(\'node:test\').run', { concurrency: true }, () => { - it('should fail when passing array as options', () => { - assert.throws(() => run([]), { - code: 'ERR_INVALID_ARG_TYPE' - }); - }); - it('should run with no tests', async () => { const stream = run({ files: [] }); stream.on('test:fail', common.mustNotCall()); @@ -70,13 +64,6 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { for await (const _ of stream); }); - it('should validate files', async () => { - [Symbol(), {}, () => {}, 0, 1, 0n, 1n, '', '1', Promise.resolve([]), true, false] - .forEach((files) => assert.throws(() => run({ files }), { - code: 'ERR_INVALID_ARG_TYPE' - })); - }); - it('should be piped with dot', async () => { const result = await run({ files: [join(testFixtures, 'default-behavior/test/random.cjs')] @@ -442,4 +429,20 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { assert.deepStrictEqual(executedTestFiles.sort(), [...shardsTestsFiles].sort()); }); }); + + describe('validation', () => { + it('should only allow array in options.files', async () => { + [Symbol(), {}, () => {}, 0, 1, 0n, 1n, '', '1', Promise.resolve([]), true, false] + .forEach((files) => assert.throws(() => run({ files }), { + code: 'ERR_INVALID_ARG_TYPE' + })); + }); + + it('should only allow object as options', () => { + [Symbol(), [], () => {}, 0, 1, 0n, 1n, '', '1', true, false] + .forEach((options) => assert.throws(() => run(options), { + code: 'ERR_INVALID_ARG_TYPE' + })); + }); + }); });