/
command.exitOverride.test.js
209 lines (173 loc) · 6.09 KB
/
command.exitOverride.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
const commander = require('../');
const path = require('path');
// Test details of the exitOverride errors.
// The important checks are the exitCode and code which are intended to be stable for
// semver minor versions. For now, also testing the error.message and that output occured
// to detect accidental changes in behaviour.
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectCommanderError"] }] */
function expectCommanderError(err, exitCode, code, message) {
expect(err).toBeInstanceOf(commander.CommanderError);
expect(err.exitCode).toBe(exitCode);
expect(err.code).toBe(code);
expect(err.message).toBe(message);
}
describe('.exitOverride and error details', () => {
// Use internal knowledge to suppress output to keep test output clean.
let consoleErrorSpy;
let writeSpy; // used for help [sic] and version
beforeAll(() => {
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
});
afterEach(() => {
consoleErrorSpy.mockClear();
writeSpy.mockClear();
});
afterAll(() => {
consoleErrorSpy.mockRestore();
writeSpy.mockRestore();
});
test('when specify unknown program option then throw CommanderError', () => {
const program = new commander.Command();
program
.exitOverride();
let caughtErr;
try {
program.parse(['node', 'test', '-m']);
} catch (err) {
caughtErr = err;
}
expect(consoleErrorSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.unknownOption', "error: unknown option '-m'");
});
test('when specify unknown command then throw CommanderError', () => {
const program = new commander.Command();
program
.name('prog')
.exitOverride()
.command('sub');
let caughtErr;
try {
program.parse(['node', 'test', 'oops']);
} catch (err) {
caughtErr = err;
}
expect(consoleErrorSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.unknownCommand', "error: unknown command 'oops'. See 'prog --help'.");
});
// Same error as above, but with custom handler.
test('when supply custom handler then throw custom error', () => {
const customError = new commander.CommanderError(123, 'custom-code', 'custom-message');
const program = new commander.Command();
program
.exitOverride((_err) => {
throw customError;
});
let caughtErr;
try {
program.parse(['node', 'test', '-m']);
} catch (err) {
caughtErr = err;
}
expectCommanderError(caughtErr, customError.exitCode, customError.code, customError.message);
});
test('when specify option without required value then throw CommanderError', () => {
const optionFlags = '-p, --pepper <type>';
const program = new commander.Command();
program
.exitOverride()
.option(optionFlags, 'add pepper');
let caughtErr;
try {
program.parse(['node', 'test', '--pepper']);
} catch (err) {
caughtErr = err;
}
expect(consoleErrorSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.optionMissingArgument', `error: option '${optionFlags}' argument missing`);
});
test('when specify command without required argument then throw CommanderError', () => {
const program = new commander.Command();
program
.exitOverride()
.command('compress <arg-name>')
.action(() => { });
let caughtErr;
try {
program.parse(['node', 'test', 'compress']);
} catch (err) {
caughtErr = err;
}
expect(consoleErrorSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");
});
test('when specify --help then throw CommanderError', () => {
const program = new commander.Command();
program
.exitOverride();
let caughtErr;
try {
program.parse(['node', 'test', '--help']);
} catch (err) {
caughtErr = err;
}
expect(writeSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 0, 'commander.helpDisplayed', '(outputHelp)');
});
test('when executable subcommand and no command specified then throw CommanderError', () => {
const program = new commander.Command();
program
.exitOverride()
.command('compress', 'compress description');
let caughtErr;
try {
program.parse(['node', 'test']);
} catch (err) {
caughtErr = err;
}
expect(writeSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.help', '(outputHelp)');
});
test('when specify --version then throw CommanderError', () => {
const myVersion = '1.2.3';
const program = new commander.Command();
program
.exitOverride()
.version(myVersion);
let caughtErr;
try {
program.parse(['node', 'test', '--version']);
} catch (err) {
caughtErr = err;
}
expect(writeSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 0, 'commander.version', myVersion);
});
// Have not worked out a cleaner way to do this, so suppress warning.
// eslint-disable-next-line jest/no-test-callback
test('when executableSubcommand succeeds then call exitOverride', (done) => {
const pm = path.join(__dirname, 'fixtures/pm');
const program = new commander.Command();
program
.exitOverride((err) => {
expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');
done();
})
.command('silent', 'description');
program.parse(['node', pm, 'silent']);
});
test('when mandatory program option missing then throw CommanderError', () => {
const optionFlags = '-p, --pepper <type>';
const program = new commander.Command();
program
.exitOverride()
.requiredOption(optionFlags, 'add pepper');
let caughtErr;
try {
program.parse(['node', 'test']);
} catch (err) {
caughtErr = err;
}
expectCommanderError(caughtErr, 1, 'commander.missingMandatoryOptionValue', `error: required option '${optionFlags}' not specified`);
});
});