/
validation.js
640 lines (589 loc) · 19.4 KB
/
validation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* global describe, it, beforeEach */
var checkUsage = require('./helpers/utils').checkOutput
var expect = require('chai').expect
var yargs = require('../')
require('chai').should()
describe('validation tests', function () {
beforeEach(function () {
yargs.reset()
})
describe('implies', function () {
it("fails if '_' populated, and implied argument not set", function (done) {
yargs(['cat'])
.implies({
1: 'foo' // 1 arg in _ means --foo is required
})
.fail(function (msg) {
msg.should.match(/Implications failed/)
return done()
})
.argv
})
it("fails if key implies values in '_', but '_' is not populated", function (done) {
yargs(['--foo'])
.boolean('foo')
.implies({
'foo': 1 // --foo means 1 arg in _ is required
})
.fail(function (msg) {
msg.should.match(/Implications failed/)
return done()
})
.argv
})
it("fails if --no-foo's implied argument is not set", function (done) {
yargs([])
.implies({
'--no-bar': 'foo' // when --bar is not given, --foo is required
})
.fail(function (msg) {
msg.should.match(/Implications failed/)
return done()
})
.argv
})
it('fails if a key is set, along with a key that it implies should not be set', function (done) {
yargs(['--bar', '--foo'])
.implies({
'bar': '--no-foo' // --bar means --foo cannot be given
})
.fail(function (msg) {
msg.should.match(/Implications failed/)
return done()
})
.argv
})
it('fails if implied key (with "no" in the name) is not set', function () {
var failCalled = false
yargs('--bar')
.implies({
'bar': 'noFoo' // --bar means --noFoo (or --no-foo with boolean-negation disabled) is required
// note that this has nothing to do with --foo
})
.fail(function (msg) {
failCalled = true
msg.should.match(/Implications failed/)
})
.argv
failCalled.should.be.true
})
it('doesn\'t fail if implied key (with "no" in the name) is set', function () {
var failCalled = false
const argv = yargs('--bar --noFoo')
.implies({
'bar': 'noFoo' // --bar means --noFoo (or --no-foo with boolean-negation disabled) is required
// note that this has nothing to do with --foo
})
.fail(function (msg) {
failCalled = true
})
.argv
failCalled.should.be.false
expect(argv.bar).to.be.true
expect(argv.noFoo).to.be.true
expect(argv.foo).to.not.exist
})
it('fails if implied key (with "no" in the name) is given when it should not', function () {
var failCalled = false
yargs('--bar --noFoo')
.implies({
'bar': '--no-noFoo' // --bar means --noFoo (or --no-foo with boolean-negation disabled) cannot be given
// note that this has nothing to do with --foo
})
.fail(function (msg) {
failCalled = true
msg.should.match(/Implications failed/)
})
.argv
failCalled.should.be.true
})
it('doesn\'t fail if implied key (with "no" in the name) that should not be given is not set', function () {
var failCalled = false
const argv = yargs('--bar')
.implies({
'bar': '--no-noFoo' // --bar means --noFoo (or --no-foo with boolean-negation disabled) cannot be given
// note that this has nothing to do with --foo
})
.fail(function (msg) {
failCalled = true
})
.argv
failCalled.should.be.false
expect(argv.bar).to.be.true
expect(argv.noFoo).to.not.exist
expect(argv.foo).to.not.exist
})
it('allows key to be specified with option shorthand', function (done) {
yargs('--bar')
.option('bar', {
implies: 'foo'
})
.fail(function (msg) {
msg.should.match(/Implications failed/)
return done()
})
.argv
})
})
describe('conflicts', function () {
it('fails if both arguments are supplied', function (done) {
yargs(['-f', '-b'])
.conflicts('f', 'b')
.fail(function (msg) {
msg.should.equal('Arguments f and b are mutually exclusive')
return done()
})
.argv
})
it('should not fail if no conflicting arguments are provided', function () {
yargs(['-b', '-c'])
.conflicts('f', 'b')
.fail(function (msg) {
expect.fail()
})
.argv
})
it('allows an object to be provided defining conflicting option pairs', function (done) {
yargs(['-t', '-s'])
.conflicts({
'c': 'a',
's': 't'
})
.fail(function (msg) {
msg.should.equal('Arguments s and t are mutually exclusive')
return done()
})
.argv
})
it('takes into account aliases when applying conflicts logic', function (done) {
yargs(['-t', '-c'])
.conflicts({
'c': 'a',
's': 't'
})
.alias('c', 's')
.fail(function (msg) {
msg.should.equal('Arguments s and t are mutually exclusive')
return done()
})
.argv
})
it('allows key to be specified with option shorthand', function (done) {
yargs(['-f', '-b'])
.option('f', {
conflicts: 'b'
})
.fail(function (msg) {
msg.should.equal('Arguments f and b are mutually exclusive')
return done()
})
.argv
})
})
describe('demand', function () {
it('fails with standard error message if msg is not defined', function (done) {
yargs([])
.demand(1)
.fail(function (msg) {
msg.should.equal('Not enough non-option arguments: got 0, need at least 1')
return done()
})
.argv
})
it('fails in strict mode with invalid command', function (done) {
yargs(['koala'])
.command('wombat', 'wombat burrows')
.command('kangaroo', 'kangaroo handlers')
.demand(1)
.strict()
.fail(function (msg) {
msg.should.equal('Unknown argument: koala')
return done()
})
.argv
})
it('does not fail in strict mode when no commands configured', function () {
var argv = yargs('koala')
.demand(1)
.strict()
.fail(function (msg) {
expect.fail()
})
.argv
argv._[0].should.equal('koala')
})
// addresses: https://github.com/yargs/yargs/issues/791
it('should recognize context variables in strict mode', function (done) {
yargs()
.command('foo <y>')
.strict()
.parse('foo 99', {x: 33}, function (err, argv, output) {
expect(err).to.equal(null)
expect(output).to.equal('')
argv.y.should.equal(99)
argv.x.should.equal(33)
argv._.should.include('foo')
return done()
})
})
// addresses: https://github.com/yargs/yargs/issues/791
it('should recognize context variables in strict mode, when running sub-commands', function (done) {
yargs()
.command('request', 'request command', function (yargs) {
yargs
.command('get', 'sub-command')
.option('y', {
describe: 'y inner option'
})
})
.strict()
.parse('request get --y=22', {x: 33}, function (err, argv, output) {
expect(err).to.equal(null)
expect(output).to.equal('')
argv.y.should.equal(22)
argv.x.should.equal(33)
argv._.should.include('request')
argv._.should.include('get')
return done()
})
})
it('fails when a required argument is missing', function (done) {
yargs('-w 10 marsupial')
.demand(1, ['w', 'b'])
.fail(function (msg) {
msg.should.equal('Missing required argument: b')
return done()
})
.argv
})
it('fails when required arguments are present, but a command is missing', function (done) {
yargs('-w 10 -m wombat')
.demand(1, ['w', 'm'])
.fail(function (msg) {
msg.should.equal('Not enough non-option arguments: got 0, need at least 1')
return done()
})
.argv
})
it('fails without a message if msg is null', function (done) {
yargs([])
.demand(1, null)
.fail(function (msg) {
expect(msg).to.equal(null)
return done()
})
.argv
})
// address regression in: https://github.com/yargs/yargs/pull/740
it('custom failure message should be printed for both min and max constraints', function (done) {
yargs(['foo'])
.demand(0, 0, 'hey! give me a custom exit message')
.fail(function (msg) {
expect(msg).to.equal('hey! give me a custom exit message')
return done()
})
.argv
})
it('interprets min relative to command', function () {
var failureMsg
yargs('lint')
.command('lint', 'Lint a file', function (yargs) {
yargs.demand(1).fail(function (msg) {
failureMsg = msg
})
})
.argv
expect(failureMsg).to.equal('Not enough non-option arguments: got 0, need at least 1')
})
it('interprets max relative to command', function () {
var failureMsg
yargs('lint one.js two.js')
.command('lint', 'Lint a file', function (yargs) {
yargs.demand(0, 1).fail(function (msg) {
failureMsg = msg
})
})
.argv
expect(failureMsg).to.equal('Too many non-option arguments: got 2, maximum of 1')
})
})
describe('choices', function () {
it('fails with one invalid value', function (done) {
yargs(['--state', 'denial'])
.choices('state', ['happy', 'sad', 'hungry'])
.fail(function (msg) {
msg.split('\n').should.deep.equal([
'Invalid values:',
' Argument: state, Given: "denial", Choices: "happy", "sad", "hungry"'
])
return done()
})
.argv
})
it('fails with one valid and one invalid value', function (done) {
yargs(['--characters', 'susie', '--characters', 'linus'])
.choices('characters', ['calvin', 'hobbes', 'susie', 'moe'])
.fail(function (msg) {
msg.split('\n').should.deep.equal([
'Invalid values:',
' Argument: characters, Given: "linus", Choices: "calvin", "hobbes", "susie", "moe"'
])
return done()
})
.argv
})
it('fails with multiple invalid values for same argument', function (done) {
yargs(['--category', 'comedy', '--category', 'drama'])
.choices('category', ['animal', 'vegetable', 'mineral'])
.fail(function (msg) {
msg.split('\n').should.deep.equal([
'Invalid values:',
' Argument: category, Given: "comedy", "drama", Choices: "animal", "vegetable", "mineral"'
])
return done()
})
.argv
})
it('fails with case-insensitive value', function (done) {
yargs(['--env', 'DEV'])
.choices('env', ['dev', 'prd'])
.fail(function (msg) {
msg.split('\n').should.deep.equal([
'Invalid values:',
' Argument: env, Given: "DEV", Choices: "dev", "prd"'
])
return done()
})
.argv
})
it('fails with multiple invalid arguments', function (done) {
yargs(['--system', 'osx', '--arch', '64'])
.choices('system', ['linux', 'mac', 'windows'])
.choices('arch', ['x86', 'x64', 'arm'])
.fail(function (msg) {
msg.split('\n').should.deep.equal([
'Invalid values:',
' Argument: system, Given: "osx", Choices: "linux", "mac", "windows"',
' Argument: arch, Given: 64, Choices: "x86", "x64", "arm"'
])
return done()
})
.argv
})
})
describe('config', function () {
it('should raise an appropriate error if JSON file is not found', function (done) {
yargs(['--settings', 'fake.json', '--foo', 'bar'])
.alias('z', 'zoom')
.config('settings')
.fail(function (msg) {
msg.should.eql('Invalid JSON config file: fake.json')
return done()
})
.argv
})
// see: https://github.com/yargs/yargs/issues/172
it('should not raise an exception if config file is set as default argument value', function () {
var fail = false
yargs([])
.option('config', {
default: 'foo.json'
})
.config('config')
.fail(function () {
fail = true
})
.argv
fail.should.equal(false)
})
it('should be displayed in the help message', function () {
var r = checkUsage(function () {
return yargs(['--help'])
.config('settings')
.help('help')
.wrap(null)
.argv
})
r.should.have.property('logs').with.length(1)
r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --settings Path to JSON config file',
' --help Show help [boolean]',
''
])
})
it('should be displayed in the help message with its default name', function () {
var checkUsage = require('./helpers/utils').checkOutput
var r = checkUsage(function () {
return yargs(['--help'])
.config()
.help('help')
.wrap(null)
.argv
})
r.should.have.property('logs').with.length(1)
r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --config Path to JSON config file',
' --help Show help [boolean]',
''
])
})
it('should allow help message to be overridden', function () {
var checkUsage = require('./helpers/utils').checkOutput
var r = checkUsage(function () {
return yargs(['--help'])
.config('settings', 'pork chop sandwiches')
.help('help')
.wrap(null)
.argv
})
r.should.have.property('logs').with.length(1)
r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --settings pork chop sandwiches',
' --help Show help [boolean]',
''
])
})
it('outputs an error returned by the parsing function', function () {
var checkUsage = require('./helpers/utils').checkOutput
var r = checkUsage(function () {
return yargs(['--settings=./package.json'])
.config('settings', 'path to config file', function (configPath) {
return Error('someone set us up the bomb')
})
.help('help')
.wrap(null)
.argv
})
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --settings path to config file',
' --help Show help [boolean]',
'someone set us up the bomb'
])
})
it('outputs an error if thrown by the parsing function', function () {
var checkUsage = require('./helpers/utils').checkOutput
var r = checkUsage(function () {
return yargs(['--settings=./package.json'])
.config('settings', 'path to config file', function (configPath) {
throw Error('someone set us up the bomb')
})
.help('help')
.wrap(null)
.argv
})
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --settings path to config file',
' --help Show help [boolean]',
'someone set us up the bomb'
])
})
})
describe('defaults', function () {
// See https://github.com/chevex/yargs/issues/31
it('should not fail when demanded options with defaults are missing', function () {
yargs()
.fail(function (msg) {
throw new Error(msg)
})
.option('some-option', {
describe: 'some option',
demand: true,
default: 88
})
.strict()
.parse([])
})
})
describe('strict mode', function () {
it('does not fail when command with subcommands called', function () {
yargs('one')
.command('one', 'level one', function (yargs) {
return yargs
.command('twoA', 'level two A')
.command('twoB', 'level two B')
.strict()
.fail(function (msg) {
expect.fail()
})
}, function (argv) {
argv._[0].should.equal('one')
})
.argv
})
})
describe('demandOption', function () {
it('allows an array of options to be demanded', function (done) {
yargs('-a 10 marsupial')
.demandOption(['a', 'b'])
.fail(function (msg) {
msg.should.equal('Missing required argument: b')
return done()
})
.argv
})
it('allows demandOption in option shorthand', function (done) {
yargs('-a 10 marsupial')
.option('c', {
demandOption: true
})
.fail(function (msg) {
msg.should.equal('Missing required argument: c')
return done()
})
.argv
})
})
describe('demandCommand', function () {
it('should return a custom failure message when too many non-hyphenated arguments are found after a demand count', function () {
var r = checkUsage(function () {
return yargs(['src', 'dest'])
.usage('Usage: $0 [x] [y] [z] {OPTIONS} <src> <dest> [extra_files...]')
.demandCommand(0, 1, 'src and dest files are both required', 'too many arguments are provided')
.wrap(null)
.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', 'dest'])
r.errors.join('\n').split(/\n+/).should.deep.equal([
'Usage: ./usage [x] [y] [z] {OPTIONS} <src> <dest> [extra_files...]',
'too many arguments are provided'
])
})
// see: https://github.com/yargs/yargs/pull/438
it('allows a custom min message to be provided', function (done) {
yargs('-a 10 marsupial')
.demandCommand(2, 'totes got $0 totes expected $1')
.fail(function (msg) {
msg.should.equal('totes got 1 totes expected 2')
return done()
})
.argv
})
// see: https://github.com/yargs/yargs/pull/438
it('allows a custom min and max message to be provided', function (done) {
yargs('-a 10 marsupial mammal bro')
.demandCommand(1, 2, 'totes too few, got $0 totes expected $1', 'totes too many, got $0 totes expected $1')
.fail(function (msg) {
msg.should.equal('totes too many, got 3 totes expected 2')
return done()
})
.argv
})
it('defaults to demanding 1 command', function (done) {
yargs('-a 10')
.demandCommand()
.fail(function (msg) {
msg.should.equal('Not enough non-option arguments: got 0, need at least 1')
return done()
})
.argv
})
})
})