| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773 |
- var should = require('chai').should(),
- Hash = require('hashish'),
- yargs = require('../');
- describe('usage', function () {
- describe('demand options', function () {
- describe('using .demand()', function () {
- it ('should show an error along with the missing arguments on demand fail', function () {
- var r = checkUsage(function () {
- return yargs('-x 10 -z 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .demand(['x','y'])
- .argv;
- });
- r.result.should.have.property('x', 10);
- r.result.should.have.property('z', 20);
- r.result.should.have.property('_').with.length(0);
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage -x NUM -y NUM',
- 'Options:',
- ' -x [required]',
- ' -y [required]',
- 'Missing required arguments: y'
- ]);
- r.logs.should.have.length(0);
- r.exit.should.be.ok;
- });
- describe('using .require()', function() {
- it ('should show an error along with the missing arguments on demand fail', function () {
- var r = checkUsage(function () {
- return yargs('-x 10 -z 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .require(['x','y'])
- .argv;
- });
- r.result.should.have.property('x', 10);
- r.result.should.have.property('z', 20);
- r.result.should.have.property('_').with.length(0);
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage -x NUM -y NUM',
- 'Options:',
- ' -x [required]',
- ' -y [required]',
- 'Missing required arguments: y'
- ]);
- r.logs.should.have.length(0);
- r.exit.should.be.ok;
- });
- });
- });
- it('should show an error along with a custom message on demand fail', function () {
- var r = checkUsage(function () {
- return yargs('-z 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .demand(['x','y'], 'x and y are both required to multiply all the things')
- .argv;
- });
- r.result.should.have.property('z', 20);
- r.result.should.have.property('_').with.length(0);
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage -x NUM -y NUM',
- 'Options:',
- ' -x [required]',
- ' -y [required]',
- 'Missing required arguments: x, y',
- 'x and y are both required to multiply all the things'
- ]);
- r.logs.should.have.length(0);
- r.exit.should.be.ok;
- });
- it('should return valid values when demand passes', function () {
- var r = checkUsage(function () {
- return yargs('-x 10 -y 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .demand(['x','y'])
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('x', 10);
- r.result.should.have.property('y', 20)
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('errors').with.length(0);
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit', false);
- });
- });
- it('should return valid values when check passes', function () {
- var r = checkUsage(function () {
- return yargs('-x 10 -y 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .check(function (argv) {
- if (!('x' in argv)) throw 'You forgot about -x';
- if (!('y' in argv)) throw 'You forgot about -y';
- })
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('x', 10);
- r.result.should.have.property('y', 20);
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('errors').with.length(0);
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit', false);
- });
- it('should display missing arguments when check fails with a thrown exception', function () {
- var r = checkUsage(function () {
- return yargs('-x 10 -z 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .check(function (argv) {
- if (!('x' in argv)) throw 'You forgot about -x';
- if (!('y' in argv)) throw 'You forgot about -y';
- })
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('x', 10);
- r.result.should.have.property('z', 20);
- r.result.should.have.property('_').with.length(0);
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage -x NUM -y NUM',
- 'You forgot about -y'
- ]);
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- });
- it('should display missing arguments when check fails with a return value', function () {
- var r = checkUsage(function () {
- return yargs('-x 10 -z 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .check(function (argv) {
- if (!('x' in argv)) return 'You forgot about -x';
- if (!('y' in argv)) return 'You forgot about -y';
- })
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('x', 10);
- r.result.should.have.property('z', 20);
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- r.should.have.property('errors');
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage -x NUM -y NUM',
- 'You forgot about -y'
- ]);
- });
- exports.checkFailReturn = function () {
- var r = checkUsage(function () {
- return yargs('-x 10 -z 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .check(function (argv) {
- if (!('x' in argv)) return 'You forgot about -x';
- if (!('y' in argv)) return 'You forgot about -y';
- })
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('x', 10);
- r.result.should.have.property('z', 20);
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- r.should.have.property('errors');
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage -x NUM -y NUM',
- 'You forgot about -y'
- ]);
- };
- it('should return a valid result when check condition passes', function () {
- function checker (argv) {
- return 'x' in argv && 'y' in argv;
- }
- var r = checkUsage(function () {
- return yargs('-x 10 -y 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .check(checker)
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('x', 10);
- r.result.should.have.property('y', 20);
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('errors').with.length(0);
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit', false);
- });
- it('should display a failed message when check condition fails', function () {
- function checker (argv) {
- return 'x' in argv && 'y' in argv;
- }
- var r = checkUsage(function () {
- return yargs('-x 10 -z 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .check(checker)
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('x', 10);
- r.result.should.have.property('z', 20);
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- r.should.have.property('errors');
- r.errors.join('\n').split(/\n+/).join('\n').should.equal(
- 'Usage: ./usage -x NUM -y NUM\n'
- + 'Argument check failed: ' + checker.toString()
- );
- });
- it('should return a valid result when demanding a count of non-hyphenated values', function () {
- var r = checkUsage(function () {
- return yargs('1 2 3 --moo'.split(' '))
- .usage('Usage: $0 [x] [y] [z] {OPTIONS}')
- .demand(3)
- .argv;
- });
- r.should.have.property('result');
- r.should.have.property('errors').with.length(0);
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit', false);
- r.result.should.have.property('_').and.deep.equal([1,2,3]);
- r.result.should.have.property('moo', true);
- });
- it('should return a failure message when not enough non-hyphenated arguments are found after a demand count', function () {
- var r = checkUsage(function () {
- return yargs('1 2 --moo'.split(' '))
- .usage('Usage: $0 [x] [y] [z] {OPTIONS}')
- .demand(3)
- .argv;
- });
- r.should.have.property('result');
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- r.result.should.have.property('_').and.deep.equal([1,2]);
- r.result.should.have.property('moo', true);
- r.should.have.property('errors');
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage [x] [y] [z] {OPTIONS}',
- 'Not enough non-option arguments: got 2, need at least 3'
- ]);
- });
- it('should return a custom failure message when not enough non-hyphenated arguments are found after a demand count', function () {
- var r = checkUsage(function () {
- return yargs('src --moo'.split(' '))
- .usage('Usage: $0 [x] [y] [z] {OPTIONS} <src> <dest> [extra_files...]')
- .demand(2, 'src and dest files are both required')
- .argv;
- });
- r.should.have.property('result');
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- r.result.should.have.property('_').and.deep.equal(['src']);
- r.result.should.have.property('moo', true);
- r.should.have.property('errors');
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage [x] [y] [z] {OPTIONS} <src> <dest> [extra_files...]',
- 'src and dest files are both required'
- ]);
- });
- it('should return a valid result when setting defaults for singles', function () {
- var r = checkUsage(function () {
- return yargs('--foo 50 --baz 70 --powsy'.split(' '))
- .default('foo', 5)
- .default('bar', 6)
- .default('baz', 7)
- .argv
- ;
- });
- r.should.have.property('result');
- r.result.should.have.property('foo', 50);
- r.result.should.have.property('bar', 6);
- r.result.should.have.property('baz', 70);
- r.result.should.have.property('powsy', true);
- r.result.should.have.property('_').with.length(0);
- });
- it('should return a valid result when default is set for an alias', function () {
- var r = checkUsage(function () {
- return yargs('')
- .alias('f', 'foo')
- .default('f', 5)
- .argv
- ;
- });
- r.should.have.property('result');
- r.result.should.have.property('f', 5);
- r.result.should.have.property('foo', 5);
- r.result.should.have.property('_').with.length(0);
- });
- it('should print a single line when failing and default is set for an alias', function() {
- var r = checkUsage(function() {
- return yargs('')
- .alias('f', 'foo')
- .default('f', 5)
- .demand(1)
- .argv
- ;
- });
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Options:',
- ' -f, --foo [default: 5]',
- 'Not enough non-option arguments: got 0, need at least 1',
- ]);
- });
- it('should allow you to set default values for a hash of options', function () {
- var r = checkUsage(function () {
- return yargs('--foo 50 --baz 70'.split(' '))
- .default({ foo : 10, bar : 20, quux : 30 })
- .argv
- ;
- });
- r.should.have.property('result');
- r.result.should.have.property('_').with.length(0);
- r.result.should.have.property('foo', 50);
- r.result.should.have.property('baz', 70);
- r.result.should.have.property('bar', 20);
- r.result.should.have.property('quux', 30);
- });
- describe('required arguments', function () {
- describe('with options object', function () {
- it('should show a failure message if a required option is missing', function () {
- var r = checkUsage(function () {
- var opts = {
- foo: { description: 'foo option', alias: 'f', requiresArg: true },
- bar: { description: 'bar option', alias: 'b', requiresArg: true }
- };
- return yargs('-f --bar 20'.split(' '))
- .usage('Usage: $0 [options]', opts)
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('errors');
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage [options]',
- 'Options:',
- ' --foo, -f foo option',
- ' --bar, -b bar option',
- 'Missing argument value: foo',
- ]);
- });
- it('should show a failure message if more than one required option is missing', function () {
- var r = checkUsage(function () {
- var opts = {
- foo: { description: 'foo option', alias: 'f', requiresArg: true },
- bar: { description: 'bar option', alias: 'b', requiresArg: true }
- };
- return yargs('-f --bar'.split(' '))
- .usage('Usage: $0 [options]', opts)
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('errors');
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage [options]',
- 'Options:',
- ' --foo, -f foo option',
- ' --bar, -b bar option',
- 'Missing argument values: foo, bar',
- ]);
- });
- });
- describe('with requiresArg method', function () {
- it('should show a failure message if a required option is missing', function () {
- var r = checkUsage(function () {
- var opts = {
- foo: { description: 'foo option', alias: 'f' },
- bar: { description: 'bar option', alias: 'b' }
- };
- return yargs('-f --bar 20'.split(' '))
- .usage('Usage: $0 [options]', opts)
- .requiresArg(['foo', 'bar'])
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('errors');
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage [options]',
- 'Options:',
- ' --foo, -f foo option',
- ' --bar, -b bar option',
- 'Missing argument value: foo',
- ]);
- });
- });
- });
- context("with strict() option set", function () {
- it('should fail given an option argument that is not demanded', function () {
- var r = checkUsage(function () {
- opts = {
- foo: { demand: 'foo option', alias: 'f' },
- bar: { demand: 'bar option', alias: 'b' }
- };
- return yargs('-f 10 --bar 20 --baz 30'.split(' '))
- .usage('Usage: $0 [options]', opts)
- .strict()
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('_').with.length(0);
- r.result.should.have.property('f', 10);
- r.result.should.have.property('foo', 10);
- r.result.should.have.property('b', 20);
- r.result.should.have.property('bar', 20);
- r.result.should.have.property('baz', 30);
- r.should.have.property('errors');
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage [options]',
- 'Options:',
- ' --foo, -f [required]',
- ' --bar, -b [required]',
- 'Unknown argument: baz',
- ]);
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- });
- it('should fail given an option argument without a corresponding description', function () {
- var r = checkUsage(function () {
- opts = {
- foo: { description: 'foo option', alias: 'f' },
- bar: { description: 'bar option', alias: 'b' }
- };
- return yargs('-f 10 --bar 20 --baz 30'.split(' '))
- .usage('Usage: $0 [options]', opts)
- .strict()
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('_').with.length(0);
- r.result.should.have.property('f', 10);
- r.result.should.have.property('foo', 10);
- r.result.should.have.property('b', 20);
- r.result.should.have.property('bar', 20);
- r.result.should.have.property('baz', 30);
- r.should.have.property('errors');
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage [options]',
- 'Options:',
- ' --foo, -f foo option',
- ' --bar, -b bar option',
- 'Unknown argument: baz',
- ]);
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- });
- it('should fail given multiple option arguments without corresponding descriptions', function () {
- var r = checkUsage(function () {
- opts = {
- foo: { description: 'foo option', alias: 'f' },
- bar: { description: 'bar option', alias: 'b' }
- };
- return yargs('-f 10 --bar 20 --baz 30 -q 40'.split(' '))
- .usage('Usage: $0 [options]', opts)
- .strict()
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('_').with.length(0);
- r.result.should.have.property('f', 10);
- r.result.should.have.property('foo', 10);
- r.result.should.have.property('b', 20);
- r.result.should.have.property('bar', 20);
- r.result.should.have.property('baz', 30);
- r.result.should.have.property('q', 40);
- r.should.have.property('errors');
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage [options]',
- 'Options:',
- ' --foo, -f foo option',
- ' --bar, -b bar option',
- 'Unknown arguments: baz, q',
- ]);
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- });
- it('should pass given option arguments with corresponding descriptions', function () {
- var r = checkUsage(function () {
- opts = {
- foo: { description: 'foo option' },
- bar: { description: 'bar option' }
- };
- return yargs('--foo 10 --bar 20'.split(' '))
- .usage('Usage: $0 [options]', opts)
- .strict()
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('foo', 10);
- r.result.should.have.property('bar', 20)
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('errors').with.length(0);
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit', false);
- });
- });
- it('should display example on fail', function () {
- var r = checkUsage(function () {
- return yargs('')
- .example("$0 something", "description")
- .example("$0 something else", "other description")
- .demand(['y'])
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('errors');
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- r.errors.join('\n').split(/\n+/).should.deep.equal([
- 'Examples:',
- ' ./usage something description',
- ' ./usage something else other description',
- 'Options:',
- ' -y [required]',
- 'Missing required arguments: y'
- ]);
- });
- describe('demand option with boolean flag', function () {
- describe('with demand option', function () {
- it('should report missing required arguments', function () {
- var r = checkUsage(function () {
- return yargs('-y 10 -z 20'.split(' '))
- .usage('Usage: $0 -x NUM [-y NUM]')
- .options({
- 'x': { description: 'an option', demand: true },
- 'y': { description: 'another option', demand: false }
- })
- .argv;
- });
- r.result.should.have.property('y', 10);
- r.result.should.have.property('z', 20);
- r.result.should.have.property('_').with.length(0);
- r.errors.join('\n').split(/\n/).should.deep.equal([
- 'Usage: ./usage -x NUM [-y NUM]',
- '',
- 'Options:',
- ' -x an option [required]',
- ' -y another option',
- '',
- 'Missing required arguments: x'
- ]);
- r.logs.should.have.length(0);
- r.exit.should.be.ok;
- });
- });
- describe('with required option', function () {
- it('should report missing required arguments', function () {
- var r = checkUsage(function () {
- return yargs('-y 10 -z 20'.split(' '))
- .usage('Usage: $0 -x NUM [-y NUM]')
- .options({
- 'x': { description: 'an option', required: true },
- 'y': { description: 'another option', required: false }
- })
- .argv;
- });
- r.result.should.have.property('y', 10);
- r.result.should.have.property('z', 20);
- r.result.should.have.property('_').with.length(0);
- r.errors.join('\n').split(/\n/).should.deep.equal([
- 'Usage: ./usage -x NUM [-y NUM]',
- '',
- 'Options:',
- ' -x an option [required]',
- ' -y another option',
- '',
- 'Missing required arguments: x'
- ]);
- r.logs.should.have.length(0);
- r.exit.should.be.ok;
- });
- });
- it('should not report missing required arguments when given an alias', function () {
- var r = checkUsage(function () {
- return yargs('-w 10'.split(' '))
- .usage('Usage: $0 --width NUM [--height NUM]')
- .options({
- 'width': { description: 'Width', alias: 'w', demand: true },
- 'height': { description: 'Height', alias: 'h', demand: false }
- })
- .argv;
- });
- r.result.should.have.property('w', 10);
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('errors').with.length(0);
- r.logs.should.have.length(0);
- });
- });
- describe('help option', function () {
- it('should display usage', function () {
- var r = checkUsage(function () {
- return yargs(['--help'])
- .demand(['y'])
- .help('help')
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('errors');
- r.should.have.property('logs').with.length(1);
- r.should.have.property('exit').and.be.ok;
- r.logs.join('\n').split(/\n+/).should.deep.equal([
- 'Options:',
- ' --help Show help',
- ' -y [required]',
- ''
- ]);
- });
- it('should not show both dashed and camelCase aliases', function () {
- var r = checkUsage(function () {
- return yargs(['--help'])
- .usage('Usage: $0 options')
- .help('help')
- .describe('some-opt', 'Some option')
- .default('some-opt', 2)
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('exit').and.be.ok;
- r.should.have.property('errors').with.length(0);
- r.should.have.property('logs');
- r.logs.join('\n').split(/\n+/).should.deep.equal([
- 'Usage: ./usage options',
- 'Options:',
- ' --help Show help ',
- ' --some-opt Some option [default: 2]',
- ''
- ]);
- });
- });
- describe('version option', function () {
- it('should display version', function () {
- var r = checkUsage(function () {
- return yargs(['--version'])
- .version('1.0.1', 'version', 'Show version number')
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('errors');
- r.should.have.property('logs').with.length(1);
- r.should.have.property('exit').and.be.ok;
- r.logs.join('\n').split(/\n+/).should.deep.equal([
- '1.0.1'
- ]);
- });
- });
- describe('showHelpOnFail', function () {
- it('should display user supplied message', function () {
- var opts = {
- foo: { desc: 'foo option', alias: 'f' },
- bar: { desc: 'bar option', alias: 'b' }
- };
- var r = checkUsage(function () {
- return yargs(['--foo'])
- .usage('Usage: $0 [options]')
- .options(opts)
- .demand(['foo', 'bar'])
- .showHelpOnFail(false, "Specify --help for available options")
- .argv;
- });
- r.should.have.property('result');
- r.result.should.have.property('_').with.length(0);
- r.should.have.property('errors');
- r.should.have.property('logs').with.length(0);
- r.should.have.property('exit').and.be.ok;
- r.errors.join('\n').split(/\n/).should.deep.equal([
- 'Missing required arguments: bar',
- '',
- 'Specify --help for available options'
- ]);
- });
- });
- it('should succeed when rebase', function () {
- yargs.rebase('/home/chevex', '/home/chevex/foo/bar/baz').should.equal('./foo/bar/baz');
- yargs.rebase('/home/chevex/foo/bar/baz', '/home/chevex').should.equal('../../..');
- yargs.rebase('/home/chevex/foo', '/home/chevex/pow/zoom.txt').should.equal('../pow/zoom.txt');
- });
- function checkUsage (f) {
- var exit = false;
- process._exit = process.exit;
- process._env = process.env;
- process._argv = process.argv;
- process.stdout._write = process.stdout.write;
- process.exit = function () { exit = true };
- process.env = Hash.merge(process.env, { _ : 'node' });
- process.argv = [ './usage' ];
- process.stdout.write = function (msg) { logs.push(msg) };
- var errors = [];
- var logs = [];
- console._error = console.error;
- console.error = function (msg) { errors.push(msg) };
- console._log = console.log;
- console.log = function (msg) { logs.push(msg) };
- var result = f();
- process.exit = process._exit;
- process.env = process._env;
- process.argv = process._argv;
- process.stdout.write = process.stdout._write;
- console.error = console._error;
- console.log = console._log;
- return {
- errors : errors,
- logs : logs,
- exit : exit,
- result : result
- };
- };
- });
|