Skip to content

Commit 5919b08

Browse files
committed
test: replace process.execArgv parseArgs tests with spawn tests
1 parent 7fd3688 commit 5919b08

File tree

2 files changed

+42
-81
lines changed

2 files changed

+42
-81
lines changed

lib/internal/util/parse_args/parse_args.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const {
44
ArrayPrototypeForEach,
5-
ArrayPrototypeIncludes,
65
ArrayPrototypeMap,
76
ArrayPrototypePush,
87
ArrayPrototypePushApply,
@@ -54,20 +53,16 @@ const {
5453
kEmptyObject,
5554
} = require('internal/util');
5655

56+
const { getOptionValue } = require('internal/options');
5757

5858
function getMainArgs() {
59-
// Work out where to slice process.argv for user supplied arguments.
60-
61-
// Check node options for scenarios where user CLI args follow executable.
62-
const execArgv = process.execArgv;
63-
if (ArrayPrototypeIncludes(execArgv, '-e') ||
64-
ArrayPrototypeIncludes(execArgv, '--eval') ||
65-
ArrayPrototypeIncludes(execArgv, '-p') ||
66-
ArrayPrototypeIncludes(execArgv, '--print')) {
59+
// -p / --print internally sets --eval, so this works for all cases
60+
const evalValue = getOptionValue('--eval');
61+
62+
if (evalValue.length !== 0) {
6763
return ArrayPrototypeSlice(process.argv, 1);
6864
}
6965

70-
// Normally first two arguments are executable and script, then CLI arguments
7166
return ArrayPrototypeSlice(process.argv, 2);
7267
}
7368

test/parallel/test-parse-args.mjs

Lines changed: 37 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import '../common/index.mjs';
1+
import { spawnPromisified } from '../common/index.mjs';
22
import assert from 'node:assert';
3-
import { test } from 'node:test';
3+
import { suite, test } from 'node:test';
44
import { parseArgs } from 'node:util';
55

66
test('when short option used as flag then stored as flag', () => {
@@ -211,73 +211,42 @@ test('order of option and positional does not matter (per README)', () => {
211211
);
212212
});
213213

214-
test('correct default args when use node -p', () => {
215-
const holdArgv = process.argv;
216-
process.argv = [process.argv0, '--foo'];
217-
const holdExecArgv = process.execArgv;
218-
process.execArgv = ['-p', '0'];
219-
const result = parseArgs({ strict: false });
220-
221-
const expected = { values: { __proto__: null, foo: true },
222-
positionals: [] };
223-
assert.deepStrictEqual(result, expected);
224-
process.argv = holdArgv;
225-
process.execArgv = holdExecArgv;
226-
});
227-
228-
test('correct default args when use node --print', () => {
229-
const holdArgv = process.argv;
230-
process.argv = [process.argv0, '--foo'];
231-
const holdExecArgv = process.execArgv;
232-
process.execArgv = ['--print', '0'];
233-
const result = parseArgs({ strict: false });
234-
235-
const expected = { values: { __proto__: null, foo: true },
236-
positionals: [] };
237-
assert.deepStrictEqual(result, expected);
238-
process.argv = holdArgv;
239-
process.execArgv = holdExecArgv;
240-
});
241-
242-
test('correct default args when use node -e', () => {
243-
const holdArgv = process.argv;
244-
process.argv = [process.argv0, '--foo'];
245-
const holdExecArgv = process.execArgv;
246-
process.execArgv = ['-e', '0'];
247-
const result = parseArgs({ strict: false });
248-
249-
const expected = { values: { __proto__: null, foo: true },
250-
positionals: [] };
251-
assert.deepStrictEqual(result, expected);
252-
process.argv = holdArgv;
253-
process.execArgv = holdExecArgv;
254-
});
255-
256-
test('correct default args when use node --eval', () => {
257-
const holdArgv = process.argv;
258-
process.argv = [process.argv0, '--foo'];
259-
const holdExecArgv = process.execArgv;
260-
process.execArgv = ['--eval', '0'];
261-
const result = parseArgs({ strict: false });
262-
const expected = { values: { __proto__: null, foo: true },
263-
positionals: [] };
264-
assert.deepStrictEqual(result, expected);
265-
process.argv = holdArgv;
266-
process.execArgv = holdExecArgv;
267-
});
268-
269-
test('correct default args when normal arguments', () => {
270-
const holdArgv = process.argv;
271-
process.argv = [process.argv0, 'script.js', '--foo'];
272-
const holdExecArgv = process.execArgv;
273-
process.execArgv = [];
274-
const result = parseArgs({ strict: false });
214+
suite('correct default args', () => {
215+
suite('with CLI flags', () => {
216+
const evalCode = "JSON.stringify(require('util').parseArgs({ strict: false }).values)";
217+
const evalCodePrinted = `process.stdout.write(${evalCode})`;
218+
const execArgsTests = {
219+
'-e <script>': ['-e', evalCodePrinted],
220+
'-p <script>': ['-p', evalCode],
221+
'-pe <script>': ['-pe', evalCode],
222+
'--eval <script>': ['--eval', evalCodePrinted],
223+
'--eval=<script>': [`--eval=${evalCodePrinted}`],
224+
'--print <script>': ['--print', evalCode],
225+
'--print --eval <script>': ['--print', '--eval', evalCode],
226+
'--print --eval=<script>': ['--print', `--eval=${evalCode}`],
227+
};
228+
for (const description in execArgsTests) {
229+
const execArgs = execArgsTests[description];
230+
test(description, async () => {
231+
const { code, signal, stderr, stdout } = await spawnPromisified(
232+
process.execPath,
233+
[...execArgs, '--', '--foo', '--bar']);
234+
assert.strictEqual(code, 0);
235+
assert.strictEqual(signal, null);
236+
assert.strictEqual(stderr, '');
237+
assert.deepStrictEqual(JSON.parse(stdout), { foo: true, bar: true });
238+
});
239+
}
240+
});
275241

276-
const expected = { values: { __proto__: null, foo: true },
277-
positionals: [] };
278-
assert.deepStrictEqual(result, expected);
279-
process.argv = holdArgv;
280-
process.execArgv = holdExecArgv;
242+
test('without CLI flags', () => {
243+
assert.strictEqual(process.execArgv.length, 0);
244+
const holdArgv = process.argv;
245+
process.argv = [process.argv0, 'script.js', '--foo', '--bar'];
246+
const { values } = parseArgs({ strict: false });
247+
assert.deepStrictEqual(values, { __proto__: null, foo: true, bar: true });
248+
process.argv = holdArgv;
249+
});
281250
});
282251

283252
test('excess leading dashes on options are retained', () => {
@@ -1052,13 +1021,10 @@ test('allow negative options and passed multiple arguments', () => {
10521021
test('auto-detect --no-foo as negated when strict:false and allowNegative', () => {
10531022
const holdArgv = process.argv;
10541023
process.argv = [process.argv0, 'script.js', '--no-foo'];
1055-
const holdExecArgv = process.execArgv;
1056-
process.execArgv = [];
10571024
const result = parseArgs({ strict: false, allowNegative: true });
10581025

10591026
const expected = { values: { __proto__: null, foo: false },
10601027
positionals: [] };
10611028
assert.deepStrictEqual(result, expected);
10621029
process.argv = holdArgv;
1063-
process.execArgv = holdExecArgv;
10641030
});

0 commit comments

Comments
 (0)