-
Notifications
You must be signed in to change notification settings - Fork 67
/
show-customized-help.test.ts
210 lines (169 loc) · 5.67 KB
/
show-customized-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
import {expect, test as base} from '@oclif/test'
import {stub, SinonStub} from 'sinon'
import * as path from 'path'
import {CommandHelp, Help} from '../../src/help'
import {AppsIndexWithDesc, AppsDestroy, AppsCreate, AppsTopic, AppsAdminTopic, AppsAdminAdd} from './fixtures/fixtures'
import {Interfaces, Config} from '../../src'
import {monkeyPatchCommands} from './help-test-utils'
const g: any = global
g.oclif.columns = 80
// Allow overriding section headers
class TestCommandHelp extends CommandHelp {
protected sections() {
const sectionsToRemove = new Set(['DESCRIPTION'])
let sections = super.sections()
sections = sections.filter(section => !sectionsToRemove.has(section.header))
sections.push({
header: 'CUSTOM',
generate: () => `my custom section
${this.indent(this.wrap('force it '.repeat(29)))}`,
})
return sections
}
}
class TestHelp extends Help {
CommandHelpClass = TestCommandHelp
public config: any;
constructor(config: Interfaces.Config, opts: Partial<Interfaces.HelpOptions> = {}) {
super(config, opts)
this.opts.usageHeader = 'SYNOPSIS'
}
summary(c: Interfaces.Command): string {
// This will essentially ignore the summary
return this.wrap(c.description || '')
}
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 ctx => {
const config = ctx.config
monkeyPatchCommands(config, [{
commands: [AppsIndexWithDesc, AppsCreate, AppsDestroy],
topics: [],
}])
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}
SYNOPSIS
$ oclif [COMMAND]
TOPICS
apps List all apps (app index command)
COMMANDS
apps List all apps (app index command)
this only shows up in command help under DESCRIPTION`)
})
test
.loadConfig()
.stdout()
.do(async ctx => {
const config = ctx.config
monkeyPatchCommands(config, [{
commands: [AppsIndexWithDesc],
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}
SYNOPSIS
$ oclif [COMMAND]
COMMANDS
apps List all apps (app index command)
this only shows up in command help under DESCRIPTION`)
})
})
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(`this only shows up in command help under DESCRIPTION
SYNOPSIS
$ oclif apps:create
CUSTOM
my custom section
force it force it force it force it force it force it force it force
it force it force it force it force it force it force it force it force
it force it force it force it force it force it force it force it force
it force it force it force it force it force it`)
})
test
.loadConfig()
.stdout()
.do(async ctx => {
const config = ctx.config
monkeyPatchCommands(config, [{
commands: [AppsIndexWithDesc, 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)
this only shows up in command help under DESCRIPTION
SYNOPSIS
$ oclif apps
CUSTOM
my custom section
force it force it force it force it force it force it force it force
it force it force it force it force it force it force it force it force
it force it force it force it force it force it force it force it force
it force it force it force it force it force it
TOPICS
apps:admin This topic is for the apps topic
COMMANDS
apps:create this only shows up in command help under DESCRIPTION`)
})
})