|
1 | | -import '../common/index.mjs'; |
| 1 | +import { spawnPromisified } from '../common/index.mjs'; |
2 | 2 | import assert from 'node:assert'; |
3 | | -import { test } from 'node:test'; |
| 3 | +import { suite, test } from 'node:test'; |
4 | 4 | import { parseArgs } from 'node:util'; |
5 | 5 |
|
6 | 6 | 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)', () => { |
211 | 211 | ); |
212 | 212 | }); |
213 | 213 |
|
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 | + }); |
275 | 241 |
|
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 | + }); |
281 | 250 | }); |
282 | 251 |
|
283 | 252 | test('excess leading dashes on options are retained', () => { |
@@ -1052,13 +1021,10 @@ test('allow negative options and passed multiple arguments', () => { |
1052 | 1021 | test('auto-detect --no-foo as negated when strict:false and allowNegative', () => { |
1053 | 1022 | const holdArgv = process.argv; |
1054 | 1023 | process.argv = [process.argv0, 'script.js', '--no-foo']; |
1055 | | - const holdExecArgv = process.execArgv; |
1056 | | - process.execArgv = []; |
1057 | 1024 | const result = parseArgs({ strict: false, allowNegative: true }); |
1058 | 1025 |
|
1059 | 1026 | const expected = { values: { __proto__: null, foo: false }, |
1060 | 1027 | positionals: [] }; |
1061 | 1028 | assert.deepStrictEqual(result, expected); |
1062 | 1029 | process.argv = holdArgv; |
1063 | | - process.execArgv = holdExecArgv; |
1064 | 1030 | }); |
0 commit comments