-
Notifications
You must be signed in to change notification settings - Fork 67
/
show-help.test.ts
374 lines (307 loc) · 9.69 KB
/
show-help.test.ts
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import {test as base} from '@oclif/test'
import {stub, SinonStub} from 'sinon'
import * as path from 'path'
import {Help} from '../../src/help'
import {AppsIndex, AppsDestroy, AppsCreate, AppsTopic, AppsAdminTopic, AppsAdminAdd, AppsAdminIndex, DbCreate, DbTopic} from './fixtures/fixtures'
import {Interfaces, Config} from '../../src'
import {monkeyPatchCommands} from './help-test-utils'
import {expect} from 'chai'
const g: any = global
g.oclif.columns = 80
// extension makes previously protected methods public
class TestHelp extends Help {
public config: any;
public async showRootHelp() {
return super.showRootHelp()
}
public async showTopicHelp(topic: Interfaces.Topic) {
return super.showTopicHelp(topic)
}
}
const test = base
.register('setupHelp', () => ({
async run(ctx: { help: TestHelp; stubs: { [k: string]: SinonStub }}) {
ctx.stubs = {
showRootHelp: stub(TestHelp.prototype, 'showRootHelp').resolves(),
showTopicHelp: stub(TestHelp.prototype, 'showTopicHelp').resolves(),
showCommandHelp: stub(TestHelp.prototype, 'showCommandHelp').resolves(),
}
// use devPlugins: true to bring in plugins-plugin with topic commands for testing
const config = await Config.load({devPlugins: true, root: path.resolve(__dirname, '..')})
ctx.help = new TestHelp(config)
},
finally(ctx) {
for (const stub of Object.values(ctx.stubs)) stub.restore()
},
}))
.register('makeTopicsWithoutCommand', () => ({
async run(ctx: {help: TestHelp; makeTopicOnlyStub: SinonStub}) {
// by returning no matching command for a subject, it becomes a topic only
// with no corresponding command (in which case the showCommandHelp is shown)
// eslint-disable-next-line unicorn/no-useless-undefined
ctx.makeTopicOnlyStub = stub(ctx.help.config, 'findCommand').returns(undefined)
},
finally(ctx) {
ctx.makeTopicOnlyStub.restore()
},
}))
describe('showHelp for root', () => {
test
.loadConfig()
.stdout()
.do(async () => {
const config = await Config.load({root: path.resolve(__dirname, '..')});
(config as any).plugins = [{
commands: [AppsIndex, AppsCreate, AppsDestroy],
topics: [],
}]
for (const plugin of config.plugins) {
// @ts-expect-error private method
config.loadCommands(plugin)
// @ts-expect-error private method
config.loadTopics(plugin)
}
const help = new TestHelp(config as any)
await help.showHelp([])
})
.it('shows a command and topic when the index has siblings', ({stdout, config}) => {
expect(stdout.trim()).to.equal(`base library for oclif CLIs
VERSION
${config.userAgent}
USAGE
$ oclif [COMMAND]
TOPICS
apps List all apps (app index command)
plugins List installed plugins.
COMMANDS
apps List all apps (app index command)
help Display help for oclif.
plugins List installed plugins.`)
})
test
.loadConfig()
.stdout()
.do(async ctx => {
const config = ctx.config
monkeyPatchCommands(config, [{
commands: [AppsIndex],
topics: [],
}])
const help = new TestHelp(config as any)
await help.showHelp([])
})
.it('shows a command only when the topic only contains an index', ({stdout, config}) => {
expect(stdout.trim()).to.equal(`base library for oclif CLIs
VERSION
${config.userAgent}
USAGE
$ oclif [COMMAND]
COMMANDS
apps List all apps (app index command)`)
})
})
describe('showHelp for a topic', () => {
test
.loadConfig()
.stdout()
.do(async ctx => {
const config = ctx.config
monkeyPatchCommands(config, [{
commands: [AppsCreate, AppsDestroy],
topics: [AppsTopic],
}])
const help = new TestHelp(config as any)
await help.showHelp(['apps'])
})
.it('shows topic help with commands', ({stdout}) => {
expect(stdout.trim()).to.equal(`This topic is for the apps topic
USAGE
$ oclif apps:COMMAND
COMMANDS
apps:create Create an app
apps:destroy Destroy an app`)
})
test
.loadConfig()
.stdout()
.do(async ctx => {
const config = ctx.config
monkeyPatchCommands(config, [{
commands: [AppsCreate, AppsDestroy, AppsAdminAdd],
topics: [AppsTopic, AppsAdminTopic],
}])
const help = new TestHelp(config as any)
await help.showHelp(['apps'])
})
.it('shows topic help with topic and commands', ({stdout}) => {
expect(stdout.trim()).to.equal(`This topic is for the apps topic
USAGE
$ oclif apps:COMMAND
TOPICS
apps:admin This topic is for the apps topic
COMMANDS
apps:create Create an app
apps:destroy Destroy an app`)
})
test
.loadConfig()
.stdout()
.do(async ctx => {
const config = ctx.config
monkeyPatchCommands(config, [{
commands: [AppsCreate, AppsDestroy, AppsAdminIndex, AppsAdminAdd],
topics: [AppsTopic, AppsAdminTopic],
}])
const help = new TestHelp(config as any)
await help.showHelp(['apps'])
})
.it('shows topic help with topic and commands and topic command', ({stdout}) => {
expect(stdout.trim()).to.equal(`This topic is for the apps topic
USAGE
$ oclif apps:COMMAND
TOPICS
apps:admin This topic is for the apps topic
COMMANDS
apps:admin List of admins for an app
apps:create Create an app
apps:destroy Destroy an app`)
})
test
.loadConfig()
.stdout()
.do(async ctx => {
const config = ctx.config
monkeyPatchCommands(config, [{
commands: [AppsCreate, AppsDestroy, AppsAdminAdd, DbCreate],
topics: [AppsTopic, AppsAdminTopic, DbTopic],
}])
const help = new TestHelp(config as any)
await help.showHelp(['apps'])
})
.it('ignores other topics and commands', ({stdout}) => {
expect(stdout.trim()).to.equal(`This topic is for the apps topic
USAGE
$ oclif apps:COMMAND
TOPICS
apps:admin This topic is for the apps topic
COMMANDS
apps:create Create an app
apps:destroy Destroy an app`)
})
})
describe('showHelp for a command', () => {
test
.loadConfig()
.stdout()
.do(async ctx => {
const config = ctx.config
monkeyPatchCommands(config, [{
commands: [AppsCreate],
topics: [AppsTopic],
}])
const help = new TestHelp(config as any)
await help.showHelp(['apps:create'])
})
.it('shows help for a leaf (or childless) command', ({stdout}) => {
expect(stdout.trim()).to.equal(`Create an app
USAGE
$ oclif apps:create
DESCRIPTION
Create an app
this only shows up in command help under DESCRIPTION`)
})
test
.loadConfig()
.stdout()
.do(async ctx => {
const config = ctx.config
monkeyPatchCommands(config, [{
commands: [AppsIndex, AppsCreate, AppsAdminAdd],
topics: [AppsTopic, AppsAdminTopic],
}])
const help = new TestHelp(config as any)
await help.showHelp(['apps'])
})
.it('shows help for a command that has children topics and commands', ({stdout}) => {
expect(stdout.trim()).to.equal(`List all apps (app index command)
USAGE
$ oclif apps
TOPICS
apps:admin This topic is for the apps topic
COMMANDS
apps:create Create an app`)
})
})
describe('showHelp routing', () => {
describe('shows root help', () => {
test
.setupHelp()
.it('shows root help when no subject is provided', async ({help, stubs}) => {
await help.showHelp([])
expect(stubs.showRootHelp.called).to.be.true
expect(stubs.showCommandHelp.called).to.be.false
expect(stubs.showTopicHelp.called).to.be.false
})
test
.setupHelp()
.it('shows root help when help is the only arg', async ({help, stubs}) => {
await help.showHelp(['help'])
expect(stubs.showRootHelp.called).to.be.true
expect(stubs.showCommandHelp.called).to.be.false
expect(stubs.showTopicHelp.called).to.be.false
})
})
describe('shows topic help', () => {
test
.setupHelp()
.makeTopicsWithoutCommand()
.it('shows the topic help when a topic has no matching command', async ({help, stubs}) => {
await help.showHelp(['plugins'])
expect(stubs.showTopicHelp.called).to.be.true
expect(stubs.showRootHelp.called).to.be.false
expect(stubs.showCommandHelp.called).to.be.false
})
test
.setupHelp()
.makeTopicsWithoutCommand()
.it('shows the topic help when a topic has no matching command and is preceded by help', async ({help, stubs}) => {
await help.showHelp(['help', 'plugins'])
expect(stubs.showTopicHelp.called).to.be.true
expect(stubs.showRootHelp.called).to.be.false
expect(stubs.showCommandHelp.called).to.be.false
})
})
describe('shows command help', () => {
test
.setupHelp()
.it('calls showCommandHelp when a topic that is also a command is called', async ({help, stubs}) => {
await help.showHelp(['plugins'])
expect(stubs.showCommandHelp.called).to.be.true
expect(stubs.showRootHelp.called).to.be.false
expect(stubs.showTopicHelp.called).to.be.false
})
test
.setupHelp()
.it('calls showCommandHelp when a command is called', async ({help, stubs}) => {
await help.showHelp(['plugins:install'])
expect(stubs.showCommandHelp.called).to.be.true
expect(stubs.showRootHelp.called).to.be.false
expect(stubs.showTopicHelp.called).to.be.false
})
test
.setupHelp()
.it('calls showCommandHelp when a command is preceded by the help arg', async ({help, stubs}) => {
await help.showHelp(['help', 'plugins:install'])
expect(stubs.showCommandHelp.called).to.be.true
expect(stubs.showRootHelp.called).to.be.false
expect(stubs.showTopicHelp.called).to.be.false
})
})
describe('errors', () => {
test
.setupHelp()
.it('shows an error when there is a subject but it does not match a topic or command', async ({help}) => {
await expect(help.showHelp(['meow'])).to.be.rejectedWith('Command meow not found')
})
})
})