usage.js (6789B)
1 var Hash = require('hashish'); 2 var optimist = require('../index'); 3 var test = require('tap').test; 4 5 test('usageFail', function (t) { 6 var r = checkUsage(function () { 7 return optimist('-x 10 -z 20'.split(' ')) 8 .usage('Usage: $0 -x NUM -y NUM') 9 .demand(['x','y']) 10 .argv; 11 }); 12 t.same( 13 r.result, 14 { x : 10, z : 20, _ : [], $0 : './usage' } 15 ); 16 17 t.same( 18 r.errors.join('\n').split(/\n+/), 19 [ 20 'Usage: ./usage -x NUM -y NUM', 21 'Options:', 22 ' -x [required]', 23 ' -y [required]', 24 'Missing required arguments: y', 25 ] 26 ); 27 t.same(r.logs, []); 28 t.ok(r.exit); 29 t.end(); 30 }); 31 32 33 test('usagePass', function (t) { 34 var r = checkUsage(function () { 35 return optimist('-x 10 -y 20'.split(' ')) 36 .usage('Usage: $0 -x NUM -y NUM') 37 .demand(['x','y']) 38 .argv; 39 }); 40 t.same(r, { 41 result : { x : 10, y : 20, _ : [], $0 : './usage' }, 42 errors : [], 43 logs : [], 44 exit : false, 45 }); 46 t.end(); 47 }); 48 49 test('checkPass', function (t) { 50 var r = checkUsage(function () { 51 return optimist('-x 10 -y 20'.split(' ')) 52 .usage('Usage: $0 -x NUM -y NUM') 53 .check(function (argv) { 54 if (!('x' in argv)) throw 'You forgot about -x'; 55 if (!('y' in argv)) throw 'You forgot about -y'; 56 }) 57 .argv; 58 }); 59 t.same(r, { 60 result : { x : 10, y : 20, _ : [], $0 : './usage' }, 61 errors : [], 62 logs : [], 63 exit : false, 64 }); 65 t.end(); 66 }); 67 68 test('checkFail', function (t) { 69 var r = checkUsage(function () { 70 return optimist('-x 10 -z 20'.split(' ')) 71 .usage('Usage: $0 -x NUM -y NUM') 72 .check(function (argv) { 73 if (!('x' in argv)) throw 'You forgot about -x'; 74 if (!('y' in argv)) throw 'You forgot about -y'; 75 }) 76 .argv; 77 }); 78 79 t.same( 80 r.result, 81 { x : 10, z : 20, _ : [], $0 : './usage' } 82 ); 83 84 t.same( 85 r.errors.join('\n').split(/\n+/), 86 [ 87 'Usage: ./usage -x NUM -y NUM', 88 'You forgot about -y' 89 ] 90 ); 91 92 t.same(r.logs, []); 93 t.ok(r.exit); 94 t.end(); 95 }); 96 97 test('checkCondPass', function (t) { 98 function checker (argv) { 99 return 'x' in argv && 'y' in argv; 100 } 101 102 var r = checkUsage(function () { 103 return optimist('-x 10 -y 20'.split(' ')) 104 .usage('Usage: $0 -x NUM -y NUM') 105 .check(checker) 106 .argv; 107 }); 108 t.same(r, { 109 result : { x : 10, y : 20, _ : [], $0 : './usage' }, 110 errors : [], 111 logs : [], 112 exit : false, 113 }); 114 t.end(); 115 }); 116 117 test('checkCondFail', function (t) { 118 function checker (argv) { 119 return 'x' in argv && 'y' in argv; 120 } 121 122 var r = checkUsage(function () { 123 return optimist('-x 10 -z 20'.split(' ')) 124 .usage('Usage: $0 -x NUM -y NUM') 125 .check(checker) 126 .argv; 127 }); 128 129 t.same( 130 r.result, 131 { x : 10, z : 20, _ : [], $0 : './usage' } 132 ); 133 134 t.same( 135 r.errors.join('\n').split(/\n+/).join('\n'), 136 'Usage: ./usage -x NUM -y NUM\n' 137 + 'Argument check failed: ' + checker.toString() 138 ); 139 140 t.same(r.logs, []); 141 t.ok(r.exit); 142 t.end(); 143 }); 144 145 test('countPass', function (t) { 146 var r = checkUsage(function () { 147 return optimist('1 2 3 --moo'.split(' ')) 148 .usage('Usage: $0 [x] [y] [z] {OPTIONS}') 149 .demand(3) 150 .argv; 151 }); 152 t.same(r, { 153 result : { _ : [ '1', '2', '3' ], moo : true, $0 : './usage' }, 154 errors : [], 155 logs : [], 156 exit : false, 157 }); 158 t.end(); 159 }); 160 161 test('countFail', function (t) { 162 var r = checkUsage(function () { 163 return optimist('1 2 --moo'.split(' ')) 164 .usage('Usage: $0 [x] [y] [z] {OPTIONS}') 165 .demand(3) 166 .argv; 167 }); 168 t.same( 169 r.result, 170 { _ : [ '1', '2' ], moo : true, $0 : './usage' } 171 ); 172 173 t.same( 174 r.errors.join('\n').split(/\n+/), 175 [ 176 'Usage: ./usage [x] [y] [z] {OPTIONS}', 177 'Not enough non-option arguments: got 2, need at least 3', 178 ] 179 ); 180 181 t.same(r.logs, []); 182 t.ok(r.exit); 183 t.end(); 184 }); 185 186 test('defaultSingles', function (t) { 187 var r = checkUsage(function () { 188 return optimist('--foo 50 --baz 70 --powsy'.split(' ')) 189 .default('foo', 5) 190 .default('bar', 6) 191 .default('baz', 7) 192 .argv 193 ; 194 }); 195 t.same(r.result, { 196 foo : '50', 197 bar : 6, 198 baz : '70', 199 powsy : true, 200 _ : [], 201 $0 : './usage', 202 }); 203 t.end(); 204 }); 205 206 test('defaultAliases', function (t) { 207 var r = checkUsage(function () { 208 return optimist('') 209 .alias('f', 'foo') 210 .default('f', 5) 211 .argv 212 ; 213 }); 214 t.same(r.result, { 215 f : '5', 216 foo : '5', 217 _ : [], 218 $0 : './usage', 219 }); 220 t.end(); 221 }); 222 223 test('defaultHash', function (t) { 224 var r = checkUsage(function () { 225 return optimist('--foo 50 --baz 70'.split(' ')) 226 .default({ foo : 10, bar : 20, quux : 30 }) 227 .argv 228 ; 229 }); 230 t.same(r.result, { 231 _ : [], 232 $0 : './usage', 233 foo : 50, 234 baz : 70, 235 bar : 20, 236 quux : 30, 237 }); 238 t.end(); 239 }); 240 241 test('rebase', function (t) { 242 t.equal( 243 optimist.rebase('/home/substack', '/home/substack/foo/bar/baz'), 244 './foo/bar/baz' 245 ); 246 t.equal( 247 optimist.rebase('/home/substack/foo/bar/baz', '/home/substack'), 248 '../../..' 249 ); 250 t.equal( 251 optimist.rebase('/home/substack/foo', '/home/substack/pow/zoom.txt'), 252 '../pow/zoom.txt' 253 ); 254 t.end(); 255 }); 256 257 function checkUsage (f) { 258 259 var exit = false; 260 261 process._exit = process.exit; 262 process._env = process.env; 263 process._argv = process.argv; 264 265 process.exit = function (t) { exit = true }; 266 process.env = Hash.merge(process.env, { _ : 'node' }); 267 process.argv = [ './usage' ]; 268 269 var errors = []; 270 var logs = []; 271 272 console._error = console.error; 273 console.error = function (msg) { errors.push(msg) }; 274 console._log = console.log; 275 console.log = function (msg) { logs.push(msg) }; 276 277 var result = f(); 278 279 process.exit = process._exit; 280 process.env = process._env; 281 process.argv = process._argv; 282 283 console.error = console._error; 284 console.log = console._log; 285 286 return { 287 errors : errors, 288 logs : logs, 289 exit : exit, 290 result : result, 291 }; 292 };