Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not mutate process.argv #36

Merged
merged 3 commits into from
Feb 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class Sade {
}

parse(arr, opts={}) {
arr = arr.slice(); // copy
let offset=2, tmp, idx, isVoid, cmd;
let alias = { h:'help', v:'version' };
let argv = mri(arr.slice(offset), { alias });
Expand Down
64 changes: 64 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,70 @@ test('prog.action (multi optional)', t => {
(c=true) && run(); // +4 tests
});

test('prog.parse :: safe :: default', t => {
let ctx = sade('foo').command('build', '', { default: true });

let argv1 = ['', '', 'build'];
let foo = ctx.parse(argv1, { lazy: true });
t.deepEqual(argv1, ['', '', 'build'], '~> argv unchanged');
t.deepEqual(foo.args, [{ _: [] }], '~> args correct');

let argv2 = ['', ''];
let bar = ctx.parse(argv2, { lazy: true });
t.deepEqual(argv2, ['', ''], '~> argv unchanged');
t.deepEqual(bar.args, [{ _: [] }], '~> args correct');

t.end();
});

test('prog.parse :: safe :: alias', t => {
let ctx = sade('foo').command('build').alias('b');

let argv1 = ['', '', 'build'];
let foo = ctx.parse(argv1, { lazy: true });
t.deepEqual(argv1, ['', '', 'build'], '~> argv unchanged');
t.deepEqual(foo.args, [{ _: [] }], '~> args correct');

let argv2 = ['', '', 'b'];
let bar = ctx.parse(argv2, { lazy: true });
t.deepEqual(argv2, ['', '', 'b'], '~> argv unchanged');
t.deepEqual(bar.args, [{ _: [] }], '~> args correct');

t.end();
});

test('prog.parse :: safe :: default :: flags', t => {
let ctx = sade('foo').command('build <dir>', '', { default: true });

let argv1 = ['', '', '-r', 'dotenv', 'build', 'public', '--fresh'];
let foo = ctx.parse(argv1, { lazy: true });
t.deepEqual(argv1, ['', '', '-r', 'dotenv', 'build', 'public', '--fresh'], '~> argv unchanged');
t.deepEqual(foo.args, ['public', { _: [], r: 'dotenv', fresh: true }], '~> args correct');

let argv2 = ['', '', '-r', 'dotenv', 'public', '--fresh'];
let bar = ctx.parse(argv2, { lazy: true });
t.deepEqual(argv2, ['', '', '-r', 'dotenv', 'public', '--fresh'], '~> argv unchanged');
t.deepEqual(bar.args, ['public', { _: [], r: 'dotenv', fresh: true }], '~> args correct');

t.end();
});

test('prog.parse :: safe :: alias :: flags', t => {
let ctx = sade('foo').command('build <dir>').alias('b');

let argv1 = ['', '', '-r', 'dotenv', 'build', 'public', '--fresh'];
let foo = ctx.parse(argv1, { lazy: true });
t.deepEqual(argv1, ['', '', '-r', 'dotenv', 'build', 'public', '--fresh'], '~> argv unchanged');
t.deepEqual(foo.args, ['public', { _: [], r: 'dotenv', fresh: true }], '~> args correct');

let argv2 = ['', '', '-r', 'dotenv', 'b', 'public', '--fresh'];
let bar = ctx.parse(argv2, { lazy: true });
t.deepEqual(argv2, ['', '', '-r', 'dotenv', 'b', 'public', '--fresh'], '~> argv unchanged');
t.deepEqual(bar.args, ['public', { _: [], r: 'dotenv', fresh: true }], '~> args correct');

t.end();
});

test('prog.parse :: lazy', t => {
t.plan(14);

Expand Down