-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
command.helpCommand.test.js
163 lines (145 loc) · 5.48 KB
/
command.helpCommand.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
const commander = require('../');
describe('help command listed in helpInformation', () => {
test('when program has no subcommands then no automatic help command', () => {
const program = new commander.Command();
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch(/help \[command\]/);
});
test('when program has no subcommands and add help command then has help command', () => {
const program = new commander.Command();
program.addHelpCommand(true);
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/help \[command\]/);
});
test('when program has subcommands then has automatic help command', () => {
const program = new commander.Command();
program.command('foo');
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/help \[command\]/);
});
test('when program has subcommands and specify only unknown option then display help', () => {
const program = new commander.Command();
program
.configureHelp({ formatHelp: () => '' })
.exitOverride()
.allowUnknownOption()
.command('foo');
let caughtErr;
try {
program.parse(['--unknown'], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expect(caughtErr.code).toEqual('commander.help');
});
test('when program has subcommands and suppress help command then no help command', () => {
const program = new commander.Command();
program.addHelpCommand(false);
program.command('foo');
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch(/help \[command\]/);
});
test('when add custom help command then custom help command', () => {
const program = new commander.Command();
program.addHelpCommand('myHelp', 'help description');
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/myHelp +help description/);
});
});
describe('help command processed on correct command', () => {
// Use internal knowledge to suppress output to keep test output clean.
let writeErrorSpy;
let writeSpy;
beforeAll(() => {
writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });
writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
});
afterEach(() => {
writeErrorSpy.mockClear();
writeSpy.mockClear();
});
afterAll(() => {
writeErrorSpy.mockRestore();
writeSpy.mockRestore();
});
test('when "program help" then program', () => {
const program = new commander.Command();
program.exitOverride();
program.command('sub1');
program.exitOverride(() => { throw new Error('program'); });
expect(() => {
program.parse('node test.js help'.split(' '));
}).toThrow('program');
});
test('when "program help unknown" then program', () => {
const program = new commander.Command();
program.exitOverride();
program.command('sub1');
program.exitOverride(() => { throw new Error('program'); });
expect(() => {
program.parse('node test.js help unknown'.split(' '));
}).toThrow('program');
});
test('when "program help sub1" then sub1', () => {
const program = new commander.Command();
program.exitOverride();
const sub1 = program.command('sub1');
sub1.exitOverride(() => { throw new Error('sub1'); });
expect(() => {
program.parse('node test.js help sub1'.split(' '));
}).toThrow('sub1');
});
test('when "program sub1 help sub2" then sub2', () => {
const program = new commander.Command();
program.exitOverride();
const sub1 = program.command('sub1');
const sub2 = sub1.command('sub2');
sub2.exitOverride(() => { throw new Error('sub2'); });
expect(() => {
program.parse('node test.js sub1 help sub2'.split(' '));
}).toThrow('sub2');
});
test('when default command and "program help" then program', () => {
const program = new commander.Command();
program.exitOverride();
program.command('sub1', { isDefault: true });
program.exitOverride(() => { throw new Error('program'); });
expect(() => {
program.parse('node test.js help'.split(' '));
}).toThrow('program');
});
test('when no long help flag then "help sub" works', () => {
const program = new commander.Command();
program.exitOverride();
program.helpOption('-H');
const sub = program.command('sub');
// Patch help for easy way to check called.
sub.help = () => { throw new Error('sub help'); };
expect(() => {
program.parse(['help', 'sub'], { from: 'user' });
}).toThrow('sub help');
});
test('when no help options in sub then "help sub" works', () => {
const program = new commander.Command();
program.exitOverride();
const sub = program.command('sub')
.helpOption(false);
// Patch help for easy way to check called.
sub.help = () => { throw new Error('sub help'); };
expect(() => {
program.parse(['help', 'sub'], { from: 'user' });
}).toThrow('sub help');
});
test('when different help options in sub then "help sub" works', () => {
const program = new commander.Command();
program.exitOverride();
const sub = program.command('sub');
program.helpOption('-h, --help');
sub.helpOption('-a, --assist');
// Patch help for easy way to check called.
sub.help = () => { throw new Error('sub help'); };
expect(() => {
program.parse(['help', 'sub'], { from: 'user' });
}).toThrow('sub help');
});
});