forked from sindresorhus/meow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
122 lines (108 loc) · 2.83 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
import test from 'ava';
import indentString from 'indent-string';
import execa from 'execa';
import pkg from './package';
import m from '.';
test('return object', t => {
const cli = m({
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 = m(`
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 not show version', async t => {
const {stdout} = await execa('./fixture.js', ['--version', '--no-auto-version']);
t.is(stdout, 'version\nautoVersion\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 not show help screen', async t => {
const {stdout} = await execa('./fixture.js', ['--help', '--no-auto-help']);
t.is(stdout, 'help\nautoHelp\nmeow\ncamelCaseOption');
});
test('spawn cli and test input', async t => {
const {stdout} = await execa('./fixture.js', ['-u', 'cat']);
t.is(stdout, 'u\nunicorn\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');
});
// TODO: This fails in Node.js 7.10.0, but not 6 or 4
test.serial.skip('pkg.bin as a string should work', t => { // eslint-disable-line ava/no-skip-test
m({
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(m({argv: ['-F']}).flags, {F: true});
});
test('type inference', t => {
t.is(m({argv: ['5']}).input[0], '5');
t.is(m({argv: ['5']}, {input: 'string'}).input[0], '5');
t.is(m({
argv: ['5'],
inferType: true
}).input[0], 5);
t.is(m({
argv: ['5'],
inferType: true,
flags: {foo: 'string'}
}).input[0], 5);
t.is(m({
argv: ['5'],
inferType: true,
flags: {
foo: 'string'
}
}).input[0], 5);
t.is(m({
argv: ['5'],
input: 'number'
}).input[0], 5);
});
test('accept help and options', t => {
t.deepEqual(m('help', {
argv: ['-f'],
flags: {
foo: {
type: 'boolean',
alias: 'f'
}
}
}).flags, {
foo: true,
f: true
});
});