-
Notifications
You must be signed in to change notification settings - Fork 67
/
help-test-utils.ts
92 lines (76 loc) · 2.81 KB
/
help-test-utils.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
import stripAnsi = require('strip-ansi')
import {Interfaces, toCached} from '../../src'
import {Help, CommandHelp} from '../../src/help'
export class TestCommandHelp extends CommandHelp {
protected sections() {
const sections = super.sections()
const flagSection = sections.find(section => section.header === 'FLAGS')
if (flagSection) flagSection.header = 'OPTIONS'
return sections
}
}
// extensions to expose method as public for testing
export class TestHelpWithOptions extends Help {
CommandHelpClass = TestCommandHelp
constructor(config: Interfaces.Config, opts: Partial<Interfaces.HelpOptions> = {}) {
super(config, opts)
this.opts.showFlagNameInTitle = true
this.opts.showFlagOptionsInTitle = true
this.opts.hideCommandSummaryInDescription = true
}
public formatCommand(command: Interfaces.Command) {
return super.formatCommand(command)
}
}
// extensions to expose method as public for testing
export class TestHelp extends Help {
public formatCommand(command: Interfaces.Command) {
return super.formatCommand(command)
}
public formatTopics(topics: Interfaces.Topic[]) {
return super.formatTopics(topics)
}
public formatTopic(topic: Interfaces.Topic) {
return super.formatTopic(topic)
}
}
export const commandHelp = (command?: any) => ({
async run(ctx: {help: TestHelp; commandHelp: string; expectation: string}) {
const cached = await toCached(command!, {} as any)
const help = ctx.help.formatCommand(cached)
if (process.env.TEST_OUTPUT === '1') {
console.log(help)
}
ctx.commandHelp = stripAnsi(help).split('\n').map(s => s.trimEnd()).join('\n')
ctx.expectation = 'has commandHelp'
},
})
export const topicsHelp = (topics: Interfaces.Topic[]) => ({
run(ctx: {help: TestHelp; commandHelp: string; expectation: string}) {
const topicsHelpOutput = ctx.help.formatTopics(topics) || ''
if (process.env.TEST_OUTPUT === '1') {
console.log(topicsHelpOutput)
}
ctx.commandHelp = stripAnsi(topicsHelpOutput).split('\n').map(s => s.trimEnd()).join('\n')
ctx.expectation = 'has topicsHelp'
},
})
export const topicHelp = (topic: Interfaces.Topic) => ({
run(ctx: {help: TestHelp; commandHelp: string; expectation: string}) {
const topicHelpOutput = ctx.help.formatTopic(topic)
if (process.env.TEST_OUTPUT === '1') {
console.log(topicHelpOutput)
}
ctx.commandHelp = stripAnsi(topicHelpOutput).split('\n').map(s => s.trimEnd()).join('\n')
ctx.expectation = 'has topicHelp'
},
})
export function monkeyPatchCommands(config: any, plugins: Array<{commands: Interfaces.Command[], topics: Interfaces.Topic[]}>) {
config.plugins = plugins
config._commands = new Map()
config._topics = new Map()
for (const plugin of config.plugins) {
config.loadCommands(plugin)
config.loadTopics(plugin)
}
}