-
Notifications
You must be signed in to change notification settings - Fork 68
/
format-root.test.ts
144 lines (119 loc) · 3.44 KB
/
format-root.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
import {expect, test as base} from '@oclif/test'
import stripAnsi = require('strip-ansi')
import {Help} from '../../src/help'
import {Interfaces} from '../../src'
const g: any = global
g.oclif.columns = 80
const VERSION = require('../../package.json').version
const UA = `@oclif/core/${VERSION} ${process.platform}-${process.arch} node-${process.version}`
// extensions to expose method as public for testing
class TestHelp extends Help {
public formatRoot() {
return super.formatRoot()
}
}
const rootHelp = (ctxOverride?: (config: Interfaces.Config) => Interfaces.Config) => ({
run(ctx: { config: Interfaces.Config; help: Help; commandHelp: string; expectation: string}) {
const config = ctxOverride ? ctxOverride(ctx.config) : ctx.config
const help = new TestHelp(config as any)
const root = help.formatRoot()
if (process.env.TEST_OUTPUT === '1') {
console.log(help)
}
ctx.commandHelp = stripAnsi(root).split('\n').map(s => s.trimEnd()).join('\n')
},
})
const test = base
.loadConfig()
.register('rootHelp', rootHelp)
describe('formatRoot', () => {
test
.rootHelp()
.it('renders the root help', ctx => expect(ctx.commandHelp).to.equal(`base library for oclif CLIs
VERSION
${UA}
USAGE
$ oclif [COMMAND]`))
describe('description', () => {
test
.rootHelp(config => {
return {
...config,
pjson: {
...config.pjson,
description: 'This is the top-level description that appears in the root\nThis appears in the description section after usage',
},
}
})
.it('splits on \\n for the description into the top-level and description sections', ctx => {
expect(ctx.commandHelp).to.equal(`This is the top-level description that appears in the root
VERSION
${UA}
USAGE
$ oclif [COMMAND]
DESCRIPTION
This appears in the description section after usage`)
})
test
.rootHelp(config => {
return {
...config,
pjson: {
...config.pjson,
description: 'This is the top-level description for <%= config.bin %>\nThis <%= config.bin %> appears in the description section after usage',
},
}
})
.it('shows description from a template', ctx => {
expect(ctx.commandHelp).to.equal(`This is the top-level description for oclif
VERSION
${UA}
USAGE
$ oclif [COMMAND]
DESCRIPTION
This oclif appears in the description section after usage`)
})
test
.rootHelp(config => {
return {
...config,
pjson: {
...config.pjson,
description: 'THIS IS THE PJSON DESCRIPTION',
oclif: {
...config.pjson?.oclif,
description: 'THIS IS THE OCLIF DESCRIPTION IN PJSON',
},
},
}
})
.it('prefers the oclif description over the package.json description', ctx => {
expect(ctx.commandHelp).to.equal(`THIS IS THE OCLIF DESCRIPTION IN PJSON
VERSION
${UA}
USAGE
$ oclif [COMMAND]`)
})
test
.rootHelp(config => {
return {
...config,
pjson: {
...config.pjson,
description: 'THIS IS THE PJSON DESCRIPTION',
oclif: {
...config.pjson?.oclif,
description: undefined,
},
},
}
})
.it('uses package.json description when the oclif description is not set', ctx => {
expect(ctx.commandHelp).to.equal(`THIS IS THE PJSON DESCRIPTION
VERSION
${UA}
USAGE
$ oclif [COMMAND]`)
})
})
})