From 8a6a568dbc5762043c80c2004bf5d9f74cea8545 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sat, 15 Feb 2020 17:22:50 -0500 Subject: [PATCH] (fix): don't mutate process.argv with arr.splice - 1.7.1 introduced some arr.splice's into the code, which caused mutations in process.argv, affecting downstream code - set arr to a clone of process.argv instead so it can be freely mutated after - explicitly call the parameter processArgv so code is written more carefully when dealing with it - add a test to ensure process.argv is not mutated --- lib/index.js | 3 ++- test/index.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 3779c22..f7690d1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -105,7 +105,8 @@ class Sade { return this; } - parse(arr, opts={}) { + parse(processArgv, opts={}) { + let arr = processArgv.slice(); // DON'T mutate process.argv, clone first let offset=2, tmp, idx, isVoid, cmd; let alias = { h:'help', v:'version' }; let argv = mri(arr.slice(offset), { alias }); diff --git a/test/index.js b/test/index.js index 7f2eaf5..2863f82 100644 --- a/test/index.js +++ b/test/index.js @@ -212,6 +212,20 @@ test('prog.action (multi optional)', t => { (c=true) && run(); // +4 tests }); +test('prog.parse', t => { + t.plan(1); + + let ctx = sade('foo') + .command('build') + .action(() => {}); + + let args = ['', '', 'build']; + let argsCopy = args.slice(); + ctx.parse(argsCopy); + + t.deepEqual(argsCopy, args, '~> process.argv is not mutated'); +}); + test('prog.parse :: lazy', t => { t.plan(14);