123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- var Hash = require('hashish');
- var optimist = require('../index');
- var test = require('tap').test;
- test('usageFail', function (t) {
- var r = checkUsage(function () {
- return optimist('-x 10 -z 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .demand(['x','y'])
- .argv;
- });
- t.same(
- r.result,
- { x : 10, z : 20, _ : [], $0 : './usage' }
- );
- t.same(
- r.errors.join('\n').split(/\n+/),
- [
- 'Usage: ./usage -x NUM -y NUM',
- 'Options:',
- ' -x [required]',
- ' -y [required]',
- 'Missing required arguments: y',
- ]
- );
- t.same(r.logs, []);
- t.ok(r.exit);
- t.end();
- });
- test('usagePass', function (t) {
- var r = checkUsage(function () {
- return optimist('-x 10 -y 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .demand(['x','y'])
- .argv;
- });
- t.same(r, {
- result : { x : 10, y : 20, _ : [], $0 : './usage' },
- errors : [],
- logs : [],
- exit : false,
- });
- t.end();
- });
- test('checkPass', function (t) {
- var r = checkUsage(function () {
- return optimist('-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;
- });
- t.same(r, {
- result : { x : 10, y : 20, _ : [], $0 : './usage' },
- errors : [],
- logs : [],
- exit : false,
- });
- t.end();
- });
- test('checkFail', function (t) {
- var r = checkUsage(function () {
- return optimist('-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;
- });
- t.same(
- r.result,
- { x : 10, z : 20, _ : [], $0 : './usage' }
- );
- t.same(
- r.errors.join('\n').split(/\n+/),
- [
- 'Usage: ./usage -x NUM -y NUM',
- 'You forgot about -y'
- ]
- );
- t.same(r.logs, []);
- t.ok(r.exit);
- t.end();
- });
- test('checkCondPass', function (t) {
- function checker (argv) {
- return 'x' in argv && 'y' in argv;
- }
- var r = checkUsage(function () {
- return optimist('-x 10 -y 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .check(checker)
- .argv;
- });
- t.same(r, {
- result : { x : 10, y : 20, _ : [], $0 : './usage' },
- errors : [],
- logs : [],
- exit : false,
- });
- t.end();
- });
- test('checkCondFail', function (t) {
- function checker (argv) {
- return 'x' in argv && 'y' in argv;
- }
- var r = checkUsage(function () {
- return optimist('-x 10 -z 20'.split(' '))
- .usage('Usage: $0 -x NUM -y NUM')
- .check(checker)
- .argv;
- });
- t.same(
- r.result,
- { x : 10, z : 20, _ : [], $0 : './usage' }
- );
- t.same(
- r.errors.join('\n').split(/\n+/).join('\n'),
- 'Usage: ./usage -x NUM -y NUM\n'
- + 'Argument check failed: ' + checker.toString()
- );
- t.same(r.logs, []);
- t.ok(r.exit);
- t.end();
- });
- test('countPass', function (t) {
- var r = checkUsage(function () {
- return optimist('1 2 3 --moo'.split(' '))
- .usage('Usage: $0 [x] [y] [z] {OPTIONS}')
- .demand(3)
- .argv;
- });
- t.same(r, {
- result : { _ : [ '1', '2', '3' ], moo : true, $0 : './usage' },
- errors : [],
- logs : [],
- exit : false,
- });
- t.end();
- });
- test('countFail', function (t) {
- var r = checkUsage(function () {
- return optimist('1 2 --moo'.split(' '))
- .usage('Usage: $0 [x] [y] [z] {OPTIONS}')
- .demand(3)
- .argv;
- });
- t.same(
- r.result,
- { _ : [ '1', '2' ], moo : true, $0 : './usage' }
- );
- t.same(
- r.errors.join('\n').split(/\n+/),
- [
- 'Usage: ./usage [x] [y] [z] {OPTIONS}',
- 'Not enough non-option arguments: got 2, need at least 3',
- ]
- );
- t.same(r.logs, []);
- t.ok(r.exit);
- t.end();
- });
- test('defaultSingles', function (t) {
- var r = checkUsage(function () {
- return optimist('--foo 50 --baz 70 --powsy'.split(' '))
- .default('foo', 5)
- .default('bar', 6)
- .default('baz', 7)
- .argv
- ;
- });
- t.same(r.result, {
- foo : '50',
- bar : 6,
- baz : '70',
- powsy : true,
- _ : [],
- $0 : './usage',
- });
- t.end();
- });
- test('defaultAliases', function (t) {
- var r = checkUsage(function () {
- return optimist('')
- .alias('f', 'foo')
- .default('f', 5)
- .argv
- ;
- });
- t.same(r.result, {
- f : '5',
- foo : '5',
- _ : [],
- $0 : './usage',
- });
- t.end();
- });
- test('defaultHash', function (t) {
- var r = checkUsage(function () {
- return optimist('--foo 50 --baz 70'.split(' '))
- .default({ foo : 10, bar : 20, quux : 30 })
- .argv
- ;
- });
- t.same(r.result, {
- _ : [],
- $0 : './usage',
- foo : 50,
- baz : 70,
- bar : 20,
- quux : 30,
- });
- t.end();
- });
- test('rebase', function (t) {
- t.equal(
- optimist.rebase('/home/substack', '/home/substack/foo/bar/baz'),
- './foo/bar/baz'
- );
- t.equal(
- optimist.rebase('/home/substack/foo/bar/baz', '/home/substack'),
- '../../..'
- );
- t.equal(
- optimist.rebase('/home/substack/foo', '/home/substack/pow/zoom.txt'),
- '../pow/zoom.txt'
- );
- t.end();
- });
- function checkUsage (f) {
- var exit = false;
- process._exit = process.exit;
- process._env = process.env;
- process._argv = process.argv;
- process.exit = function (t) { exit = true };
- process.env = Hash.merge(process.env, { _ : 'node' });
- process.argv = [ './usage' ];
- 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;
- console.error = console._error;
- console.log = console._log;
- return {
- errors : errors,
- logs : logs,
- exit : exit,
- result : result,
- };
- };
|