/
help.js
145 lines (116 loc) Β· 2.33 KB
/
help.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
import test from 'ava';
import indentString from 'indent-string';
import meow from '../../source/index.js';
import {_verifyCli, stripIndent, stripIndentTrim} from '../_utils.js';
const importMeta = import.meta;
const verifyCli = _verifyCli();
const verifyHelp = test.macro(async (t, {cli: cliArguments, expected}) => {
const assertions = await t.try(async tt => {
const cli = Array.isArray(cliArguments)
? meow(cliArguments.at(0), {importMeta, ...cliArguments.at(1)})
: meow({importMeta, ...cliArguments});
tt.log('help text:\n', cli.help);
tt.is(cli.help, expected);
});
assertions.commit({retainLogs: !assertions.passed});
});
test('support help shortcut', verifyHelp, {
cli: [`
unicorn
cat
`],
expected: indentString(stripIndent`
CLI app helper
unicorn
cat
`, 2),
});
test('spawn cli and show help screen', verifyCli, {
args: '--help',
expected: indentString(stripIndent`
Custom description
Usage
foo <input>
`, 2),
});
test('spawn cli and disabled autoHelp', verifyCli, {
args: '--help --no-auto-help',
expected: stripIndentTrim`
help
autoHelp
meow
camelCaseOption
`,
});
test('spawn cli and not show help', verifyCli, {
args: '--help=all',
expected: stripIndentTrim`
help
meow
camelCaseOption
`,
});
test('single line help messages are not indented', verifyHelp, {
cli: {
description: false,
help: 'single line',
},
expected: stripIndent`
single line
`,
});
test('descriptions with no help are not indented', verifyHelp, {
cli: {
help: false,
description: 'single line',
},
expected: stripIndent`
single line
`,
});
test('support help shortcut with no indentation', verifyHelp, {
cli: [`
unicorn
cat
`, {
helpIndent: 0,
}],
expected: stripIndent`
CLI app helper
unicorn
cat
`,
});
test('no description and no indentation', verifyHelp, {
cli: [`
unicorn
cat
`, {
helpIndent: 0,
description: false,
}],
expected: stripIndent`
unicorn
cat
`,
});
test('exits with code 0 by default', verifyCli, {
args: '--help',
});
test('showHelp exits with code 2 by default', verifyCli, {
fixture: 'help/fixture.js',
args: '--show-help',
error: {
message: stripIndent`
foo
`,
code: 2,
},
});
test('showHelp exits with given code', verifyCli, {
fixture: 'help/fixture.js',
args: '--show-help --code=0',
expected: stripIndent`
foo
`,
});