From b77315a1127b8c6156dee7d9fbfbebdf10850f5d Mon Sep 17 00:00:00 2001 From: RedYetiDev <38299977+RedYetiDev@users.noreply.github.com> Date: Thu, 26 Sep 2024 17:38:24 -0400 Subject: [PATCH 1/6] test_runner: support custom arguments in `run()` --- doc/api/test.md | 6 +++++ lib/internal/test_runner/runner.js | 23 +++++++++++++++++++- test/fixtures/test-runner/print-arguments.js | 5 +++++ test/parallel/test-runner-run.mjs | 14 ++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/test-runner/print-arguments.js diff --git a/doc/api/test.md b/doc/api/test.md index 8152e90101bd3c..d3faab258d17a6 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -1299,6 +1299,12 @@ changes: * `setup` {Function} A function that accepts the `TestsStream` instance and can be used to setup listeners before any tests are run. **Default:** `undefined`. + * `execArgv` {Array} An array of CLI flags to pass to the `node` executable when + spawning the subprocesses. This option has no effect when `isolation` is `'none`'. + **Default:** `[]` + * `argv` {Array} An array of CLI flags to pass to each test file when spawning the + subprocesses. This option has no effect when `isolation` is `'none'`. + **Default:** `[]`. * `signal` {AbortSignal} Allows aborting an in-progress test execution. * `testNamePatterns` {string|RegExp|Array} A String, RegExp or a RegExp Array, that can be used to only run tests whose name matches the provided pattern. diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 4a19e13d9a029c..09c2aad6f054c9 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -130,7 +130,13 @@ function filterExecArgv(arg, i, arr) { !ArrayPrototypeSome(kFilterArgValues, (p) => arg === p || (i > 0 && arr[i - 1] === p) || StringPrototypeStartsWith(arg, `${p}=`)); } -function getRunArgs(path, { forceExit, inspectPort, testNamePatterns, testSkipPatterns, only }) { +function getRunArgs(path, { forceExit, + inspectPort, + testNamePatterns, + testSkipPatterns, + only, + argv: suppliedArgs, + execArgv }) { const argv = ArrayPrototypeFilter(process.execArgv, filterExecArgv); if (forceExit === true) { ArrayPrototypePush(argv, '--test-force-exit'); @@ -148,12 +154,20 @@ function getRunArgs(path, { forceExit, inspectPort, testNamePatterns, testSkipPa ArrayPrototypePush(argv, '--test-only'); } + for (let i = 0; i < execArgv.length; i++) { + ArrayPrototypePush(argv, execArgv[i]); + } + if (path === kIsolatedProcessName) { ArrayPrototypePush(argv, '--test', ...ArrayPrototypeSlice(process.argv, 1)); } else { ArrayPrototypePush(argv, path); } + for (let i = 0; i < suppliedArgs.length; i++) { + ArrayPrototypePush(argv, suppliedArgs[i]); + } + return argv; } @@ -548,6 +562,8 @@ function run(options = kEmptyObject) { lineCoverage = 0, branchCoverage = 0, functionCoverage = 0, + execArgv = [], + argv = [], } = options; if (files != null) { @@ -643,6 +659,9 @@ function run(options = kEmptyObject) { validateInteger(branchCoverage, 'options.branchCoverage', 0, 100); validateInteger(functionCoverage, 'options.functionCoverage', 0, 100); + validateStringArray(argv, 'options.argv'); + validateStringArray(execArgv, 'options.execArgv'); + const rootTestOptions = { __proto__: null, concurrency, timeout, signal }; const globalOptions = { __proto__: null, @@ -685,6 +704,8 @@ function run(options = kEmptyObject) { forceExit, cwd, isolation, + argv, + execArgv, }; if (isolation === 'process') { diff --git a/test/fixtures/test-runner/print-arguments.js b/test/fixtures/test-runner/print-arguments.js new file mode 100644 index 00000000000000..106f6a9addc0e2 --- /dev/null +++ b/test/fixtures/test-runner/print-arguments.js @@ -0,0 +1,5 @@ +const { test } = require('node:test'); + +test('process.argv is setup', (t) => { + t.assert.deepStrictEqual(process.argv.slice(2), ['--a-custom-argument']); +}); \ No newline at end of file diff --git a/test/parallel/test-runner-run.mjs b/test/parallel/test-runner-run.mjs index c423465ab849ae..b74ee7d35b3e63 100644 --- a/test/parallel/test-runner-run.mjs +++ b/test/parallel/test-runner-run.mjs @@ -33,6 +33,20 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { for await (const _ of stream); }); + const argPrintingFile = join(testFixtures, 'print-arguments.js'); + it('should allow custom arguments via execArgv', async () => { + const result = await run({ files: [argPrintingFile], execArgv: ['-p', '"Printed"'] }).compose(spec).toArray(); + assert.strictEqual(result[0].toString(), 'Printed\n'); + }); + + it('should allow custom arguments via argv', async () => { + const stream = run({ files: [argPrintingFile], argv: ['--a-custom-argument'] }); + stream.on('test:fail', common.mustNotCall()); + stream.on('test:pass', common.mustCall(1)); + // eslint-disable-next-line no-unused-vars + for await (const _ of stream); + }); + it('should run same file twice', async () => { const stream = run({ files: [ From 5f0ad553b4d2373de2e02e0e6f975105cabb7988 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Fri, 27 Sep 2024 10:59:21 -0400 Subject: [PATCH 2/6] fixup! Co-authored-by: Antoine du Hamel --- lib/internal/test_runner/runner.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 09c2aad6f054c9..09e76e70661a0c 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -154,9 +154,7 @@ function getRunArgs(path, { forceExit, ArrayPrototypePush(argv, '--test-only'); } - for (let i = 0; i < execArgv.length; i++) { - ArrayPrototypePush(argv, execArgv[i]); - } + ArrayPrototypePushApply(argv, execArgv); if (path === kIsolatedProcessName) { ArrayPrototypePush(argv, '--test', ...ArrayPrototypeSlice(process.argv, 1)); From 951dd8015f2f500500152e937130062c22f7ca58 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Fri, 27 Sep 2024 10:59:43 -0400 Subject: [PATCH 3/6] fixup! Co-authored-by: Antoine du Hamel --- lib/internal/test_runner/runner.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 09e76e70661a0c..411061cedfde1b 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -162,9 +162,7 @@ function getRunArgs(path, { forceExit, ArrayPrototypePush(argv, path); } - for (let i = 0; i < suppliedArgs.length; i++) { - ArrayPrototypePush(argv, suppliedArgs[i]); - } + ArrayPrototypePushApply(argv, suppliedArgs); return argv; } From ed22f77df2996193c8f783d0813519c54cbc4802 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Fri, 27 Sep 2024 11:00:15 -0400 Subject: [PATCH 4/6] fixup! --- test/fixtures/test-runner/print-arguments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fixtures/test-runner/print-arguments.js b/test/fixtures/test-runner/print-arguments.js index 106f6a9addc0e2..6140b3b8d26060 100644 --- a/test/fixtures/test-runner/print-arguments.js +++ b/test/fixtures/test-runner/print-arguments.js @@ -2,4 +2,4 @@ const { test } = require('node:test'); test('process.argv is setup', (t) => { t.assert.deepStrictEqual(process.argv.slice(2), ['--a-custom-argument']); -}); \ No newline at end of file +}); From e1c438c7ee706fa1045d4eb26e8be7820069c559 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Fri, 27 Sep 2024 11:01:40 -0400 Subject: [PATCH 5/6] fixup! --- lib/internal/test_runner/runner.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 411061cedfde1b..b8d7740614cee5 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -10,6 +10,7 @@ const { ArrayPrototypeJoin, ArrayPrototypeMap, ArrayPrototypePush, + ArrayPrototypePushApply, ArrayPrototypeShift, ArrayPrototypeSlice, ArrayPrototypeSome, From e5c71b4d68ff6cf22d33b626b94e8f2f65248d80 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Fri, 27 Sep 2024 11:42:46 -0400 Subject: [PATCH 6/6] Update test-runner-run.mjs Co-authored-by: Antoine du Hamel --- test/parallel/test-runner-run.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-runner-run.mjs b/test/parallel/test-runner-run.mjs index b74ee7d35b3e63..1460ef15ed8dea 100644 --- a/test/parallel/test-runner-run.mjs +++ b/test/parallel/test-runner-run.mjs @@ -42,7 +42,7 @@ describe('require(\'node:test\').run', { concurrency: true }, () => { it('should allow custom arguments via argv', async () => { const stream = run({ files: [argPrintingFile], argv: ['--a-custom-argument'] }); stream.on('test:fail', common.mustNotCall()); - stream.on('test:pass', common.mustCall(1)); + stream.on('test:pass', common.mustCall()); // eslint-disable-next-line no-unused-vars for await (const _ of stream); });