forked from tj/commander.js
/
command.help.test.js
299 lines (264 loc) · 10.8 KB
/
command.help.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
const commander = require('../');
// Testing various help incantations.
//
// Note there are also specific help tests included in many of the feature tests,
// such as the alias, version, usage, name, helpOption, and commandHelp tests.
// Avoid doing many full format tests as will be broken by any layout changes!
test('when call helpInformation for program then help format is as expected (usage, options, commands)', () => {
const program = new commander.Command();
program
.command('my-command <file>');
const expectedHelpInformation =
`Usage: test [options] [command]
Options:
-h, --help display help for command
Commands:
my-command <file>
help [command] display help for command
`;
program.name('test');
const helpInformation = program.helpInformation();
expect(helpInformation).toBe(expectedHelpInformation);
});
test('when use .description for command then help includes description', () => {
const program = new commander.Command();
program
.command('simple-command')
.description('custom-description');
program._help = 'test';
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/simple-command +custom-description/);
});
test('when call .help then exit', () => {
// Optional. Suppress normal output to keep test output clean.
const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
const program = new commander.Command();
program
.exitOverride();
expect(() => {
program.help();
}).toThrow('(outputHelp)');
writeSpy.mockClear();
});
test('when specify --help then exit', () => {
// Optional. Suppress normal output to keep test output clean.
const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
const program = new commander.Command();
program
.exitOverride();
expect(() => {
program.parse(['node', 'test', '--help']);
}).toThrow('(outputHelp)');
writeSpy.mockClear();
});
test('when call help(cb) then display cb output and exit', () => {
// Using spy to detect custom output
const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
const helpReplacement = 'reformatted help';
const program = new commander.Command();
program
.exitOverride();
expect(() => {
program.help((helpInformation) => {
return helpReplacement;
});
}).toThrow('(outputHelp)');
expect(writeSpy).toHaveBeenCalledWith(helpReplacement);
writeSpy.mockClear();
});
test('when call outputHelp(cb) then display cb output', () => {
// Using spy to detect custom output
const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
const helpReplacement = 'reformatted help';
const program = new commander.Command();
program.outputHelp((helpInformation) => {
return helpReplacement;
});
expect(writeSpy).toHaveBeenCalledWith(helpReplacement);
writeSpy.mockClear();
});
test('when call deprecated outputHelp(cb) with wrong callback return type then throw', () => {
const program = new commander.Command();
expect(() => {
program.outputHelp((helpInformation) => 3);
}).toThrow();
});
test('when command sets deprecated noHelp then not displayed in helpInformation', () => {
const program = new commander.Command();
program
.command('secret', 'secret description', { noHelp: true });
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch('secret');
});
test('when command sets hidden then not displayed in helpInformation', () => {
const program = new commander.Command();
program
.command('secret', 'secret description', { hidden: true });
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch('secret');
});
test('when addCommand with hidden:true then not displayed in helpInformation', () => {
const secretCmd = new commander.Command('secret');
const program = new commander.Command();
program
.addCommand(secretCmd, { hidden: true });
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch('secret');
});
test('when help short flag masked then not displayed in helpInformation', () => {
const program = new commander.Command();
program
.option('-h, --host', 'select host');
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch(/\W-h\W.*display help/);
});
test('when both help flags masked then not displayed in helpInformation', () => {
const program = new commander.Command();
program
.option('-h, --help', 'custom');
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch('display help');
});
test('when call .help then output on stdout', () => {
const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
const program = new commander.Command();
program
.exitOverride();
expect(() => {
program.help();
}).toThrow('(outputHelp)');
expect(writeSpy).toHaveBeenCalledWith(program.helpInformation());
writeSpy.mockClear();
});
test('when call .help with { error: true } then output on stderr', () => {
const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });
const program = new commander.Command();
program
.exitOverride();
expect(() => {
program.help({ error: true });
}).toThrow('(outputHelp)');
expect(writeSpy).toHaveBeenCalledWith(program.helpInformation());
writeSpy.mockClear();
});
test('when no options then Options not included in helpInformation', () => {
const program = new commander.Command();
// No custom options, no version option, no help option
program
.helpOption(false);
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch('Options');
});
test('when negated option then option included in helpInformation', () => {
const program = new commander.Command();
program
.option('-C, --no-colour', 'colourless');
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch('--no-colour');
expect(helpInformation).toMatch('colourless');
});
test('when option.hideHelp() then option not included in helpInformation', () => {
const program = new commander.Command();
program
.addOption(new commander.Option('-s,--secret', 'secret option').hideHelp());
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch('secret');
});
test('when option.hideHelp(true) then option not included in helpInformation', () => {
const program = new commander.Command();
program
.addOption(new commander.Option('-s,--secret', 'secret option').hideHelp(true));
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch('secret');
});
test('when option.hideHelp(false) then option included in helpInformation', () => {
const program = new commander.Command();
program
.addOption(new commander.Option('-s,--secret', 'secret option').hideHelp(false));
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch('secret');
});
test('when option has default value then default included in helpInformation', () => {
const program = new commander.Command();
program
.option('-p, --port <portNumber>', 'port number', 80);
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch('(default: 80)');
});
test('when option has default value description then default description included in helpInformation', () => {
const program = new commander.Command();
program
.addOption(new commander.Option('-a, --address <dotted>', 'ip address').default('127.0.0.1', 'home'));
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch('(default: home)');
});
test('when option has choices then choices included in helpInformation', () => {
const program = new commander.Command();
program
.addOption(new commander.Option('-c, --colour <colour>').choices(['red', 'blue']));
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch('(choices: "red", "blue")');
});
test('when option has choices and default then both included in helpInformation', () => {
const program = new commander.Command();
program
.addOption(new commander.Option('-c, --colour <colour>').choices(['red', 'blue']).default('red'));
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch('(choices: "red", "blue", default: "red")');
});
test('when argument then included in helpInformation', () => {
const program = new commander.Command();
program
.name('foo')
.argument('<file>');
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch('Usage: foo [options] <file>');
});
test('when argument described then included in helpInformation', () => {
const program = new commander.Command();
program
.argument('<file>', 'input source')
.helpOption(false);
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/Arguments:\n +file +input source/);
});
test('when argument described with default then included in helpInformation', () => {
const program = new commander.Command();
program
.argument('[file]', 'input source', 'test.txt')
.helpOption(false);
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/Arguments:\n +file +input source \(default: "test.txt"\)/);
});
test('when arguments described in deprecated way then included in helpInformation', () => {
const program = new commander.Command();
program
.arguments('<file>')
.helpOption(false)
.description('description', { file: 'input source' });
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/Arguments:\n +file +input source/);
});
test('when arguments described in deprecated way and empty description then arguments still included in helpInformation', () => {
const program = new commander.Command();
program
.arguments('<file>')
.helpOption(false)
.description('', { file: 'input source' });
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/Arguments:\n +file +input source/);
});
test('when argument has choices then choices included in helpInformation', () => {
const program = new commander.Command();
program
.addArgument(new commander.Argument('<colour>', 'preferred colour').choices(['red', 'blue']));
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch('(choices: "red", "blue")');
});
test('when argument has choices and default then both included in helpInformation', () => {
const program = new commander.Command();
program
.addArgument(new commander.Argument('<colour>', 'preferred colour').choices(['red', 'blue']).default('red'));
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch('(choices: "red", "blue", default: "red")');
});