1- import '../common/index.mjs' ;
1+ import { spawnPromisified } from '../common/index.mjs' ;
22import assert from 'node:assert' ;
3- import { test } from 'node:test' ;
3+ import { suite , test } from 'node:test' ;
44import { parseArgs } from 'node:util' ;
55
66test ( '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
283252test ( 'excess leading dashes on options are retained' , ( ) => {
@@ -1052,13 +1021,10 @@ test('allow negative options and passed multiple arguments', () => {
10521021test ( '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 ;
1064- } ) ;
1030+ } ) ;
0 commit comments