/
test.js
375 lines (334 loc) Β· 8.02 KB
/
test.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
import test from 'ava';
import indentString from 'indent-string';
import execa from 'execa';
import pkg from './package.json';
import meow from '.';
test('return object', t => {
const cli = meow({
argv: ['foo', '--foo-bar', '-u', 'cat', '--', 'unicorn', 'cake'],
help: `
Usage
foo <input>
`,
flags: {
unicorn: {alias: 'u'},
meow: {default: 'dog'},
'--': true
}
});
t.is(cli.input[0], 'foo');
t.true(cli.flags.fooBar);
t.is(cli.flags.meow, 'dog');
t.is(cli.flags.unicorn, 'cat');
t.deepEqual(cli.flags['--'], ['unicorn', 'cake']);
t.is(cli.pkg.name, 'meow');
t.is(cli.help, indentString('\nCLI app helper\n\nUsage\n foo <input>\n', 2));
});
test('support help shortcut', t => {
const cli = meow(`
unicorn
cat
`);
t.is(cli.help, indentString('\nCLI app helper\n\nunicorn\ncat\n', 2));
});
test('spawn cli and show version', async t => {
const {stdout} = await execa('./fixture.js', ['--version']);
t.is(stdout, pkg.version);
});
test('spawn cli and disabled autoVersion and autoHelp', async t => {
const {stdout} = await execa('./fixture.js', ['--version', '--help']);
t.is(stdout, 'version\nhelp\nmeow\ncamelCaseOption');
});
test('spawn cli and disabled autoVersion', async t => {
const {stdout} = await execa('./fixture.js', ['--version', '--no-auto-version']);
t.is(stdout, 'version\nautoVersion\nmeow\ncamelCaseOption');
});
test('spawn cli and not show version', async t => {
const {stdout} = await execa('./fixture.js', ['--version=beta']);
t.is(stdout, 'version\nmeow\ncamelCaseOption');
});
test('spawn cli and show help screen', async t => {
const {stdout} = await execa('./fixture.js', ['--help']);
t.is(stdout, indentString('\nCustom description\n\nUsage\n foo <input>\n\n', 2));
});
test('spawn cli and disabled autoHelp', async t => {
const {stdout} = await execa('./fixture.js', ['--help', '--no-auto-help']);
t.is(stdout, 'help\nautoHelp\nmeow\ncamelCaseOption');
});
test('spawn cli and not show help', async t => {
const {stdout} = await execa('./fixture.js', ['--help=all']);
t.is(stdout, 'help\nmeow\ncamelCaseOption');
});
test('spawn cli and test input', async t => {
const {stdout} = await execa('./fixture.js', ['-u', 'cat']);
t.is(stdout, 'unicorn\nmeow\ncamelCaseOption');
});
test('spawn cli and test input flag', async t => {
const {stdout} = await execa('./fixture.js', ['--camel-case-option', 'bar']);
t.is(stdout, 'bar');
});
test('spawn cli and test required flag with missing args', async t => {
try {
await execa('./fixture-required.js', []);
} catch (error) {
const {stdout, message} = error;
t.regex(message, /Command failed with exit code 2/);
t.regex(stdout, /Missing required option/);
t.regex(stdout, /--test, -t/);
t.regex(stdout, /--number/);
t.notRegex(stdout, /--notRequired/);
}
});
test('spawn cli and test required flag with all args given', async t => {
const {stdout} = await execa('./fixture-required.js', [
'-t',
'test',
'--number',
'6'
]);
t.is(stdout, 'test,6');
});
test('spawn cli and test required flag with empty string', async t => {
try {
await execa('./fixture-required.js', ['--test', '']);
} catch (error) {
const {stdout, message} = error;
t.regex(message, /Command failed with exit code 2/);
t.regex(stdout, /Missing required option/);
t.notRegex(stdout, /--test, -t/);
}
});
test('spawn cli and test required flag with empty number', async t => {
try {
await execa('./fixture-required.js', ['--number']);
} catch (error) {
const {stdout, message} = error;
t.regex(message, /Command failed with exit code 2/);
t.regex(stdout, /Missing required option/);
t.regex(stdout, /--number/);
}
});
test('spawn cli and test required (specified as function) flag with without arguments', async t => {
const {stdout} = await execa('./fixture-required-function.js', []);
t.is(stdout, 'false,undefined');
});
test('spawn cli and test required (specified as function) flag with trigger only', async t => {
try {
await execa('./fixture-required-function.js', ['--trigger']);
} catch (error) {
const {stdout, message} = error;
t.regex(message, /Command failed with exit code 2/);
t.regex(stdout, /Missing required option/);
t.regex(stdout, /--withTrigger/);
}
});
test('spawn cli and test required (specified as function) flag with trigger and dynamically required option', async t => {
const {stdout} = await execa('./fixture-required-function.js', ['--trigger', '--withTrigger', 'specified']);
t.is(stdout, 'true,specified');
});
test.serial('pkg.bin as a string should work', t => {
meow({
pkg: {
name: 'browser-sync',
bin: 'bin/browser-sync.js'
}
});
t.is(process.title, 'browser-sync');
});
test('single character flag casing should be preserved', t => {
t.deepEqual(meow({argv: ['-F']}).flags, {F: true});
});
test('type inference', t => {
t.is(meow({argv: ['5']}).input[0], '5');
t.is(meow({argv: ['5']}, {input: 'string'}).input[0], '5');
t.is(meow({
argv: ['5'],
inferType: true
}).input[0], 5);
t.is(meow({
argv: ['5'],
inferType: true,
flags: {foo: 'string'}
}).input[0], 5);
t.is(meow({
argv: ['5'],
inferType: true,
flags: {
foo: 'string'
}
}).input[0], 5);
t.is(meow({
argv: ['5'],
input: 'number'
}).input[0], 5);
});
test('booleanDefault: undefined, filter out unset boolean args', t => {
t.deepEqual(meow({
argv: ['--foo'],
booleanDefault: undefined,
flags: {
foo: {
type: 'boolean'
},
bar: {
type: 'boolean'
},
baz: {
type: 'boolean',
default: false
}
}
}).flags, {
foo: true,
baz: false
});
});
test('boolean args are false by default', t => {
t.deepEqual(meow({
argv: ['--foo'],
flags: {
foo: {
type: 'boolean'
},
bar: {
type: 'boolean',
default: true
},
baz: {
type: 'boolean'
}
}
}).flags, {
foo: true,
bar: true,
baz: false
});
});
test('enforces boolean flag type', t => {
const cli = meow({
argv: ['--cursor=false'],
flags: {
cursor: {
type: 'boolean'
}
}
});
t.deepEqual(cli.flags, {cursor: false});
});
test('accept help and options', t => {
t.deepEqual(meow({
argv: ['-f'],
flags: {
foo: {
type: 'boolean',
alias: 'f'
}
}
}).flags, {
foo: true
});
});
test('grouped short-flags work', t => {
const cli = meow({
argv: ['-cl'],
flags: {
coco: {
type: 'boolean',
alias: 'c'
},
loco: {
type: 'boolean',
alias: 'l'
}
}
});
const {unnormalizedFlags} = cli;
t.true(unnormalizedFlags.coco);
t.true(unnormalizedFlags.loco);
t.true(unnormalizedFlags.c);
t.true(unnormalizedFlags.l);
});
test('grouped flags work', t => {
const cli = meow({
argv: ['-cl'],
flags: {
coco: {
type: 'boolean',
alias: 'c'
},
loco: {
type: 'boolean',
alias: 'l'
}
}
});
const {flags} = cli;
t.true(flags.coco);
t.true(flags.loco);
t.is(flags.c, undefined);
t.is(flags.l, undefined);
});
test('disable autoVersion/autoHelp if `cli.input.length > 0`', t => {
t.is(meow({argv: ['bar', '--version']}).input[0], 'bar');
t.is(meow({argv: ['bar', '--help']}).input[0], 'bar');
t.is(meow({argv: ['bar', '--version', '--help']}).input[0], 'bar');
});
test('supports `number` flag type', t => {
const cli = meow({
argv: ['--foo=1.3'],
flags: {
foo: {
type: 'number'
}
}
}).flags.foo;
t.is(cli, 1.3);
});
test('supports `number` flag type - flag but no value', t => {
const cli = meow({
argv: ['--foo'],
flags: {
foo: {
type: 'number'
}
}
}).flags.foo;
t.is(cli, undefined);
});
test('supports `number` flag type - flag but no value but default', t => {
const cli = meow({
argv: ['--foo'],
flags: {
foo: {
type: 'number',
default: 2
}
}
}).flags.foo;
t.is(cli, 2);
});
test('supports `number` flag type - no flag but default', t => {
const cli = meow({
argv: [],
flags: {
foo: {
type: 'number',
default: 2
}
}
}).flags.foo;
t.is(cli, 2);
});
test('supports `number` flag type - throws on incorrect default value', t => {
t.throws(() => {
meow({
argv: [],
flags: {
foo: {
type: 'number',
default: 'x'
}
}
});
});
});