-
Notifications
You must be signed in to change notification settings - Fork 67
/
config.flexible.test.ts
262 lines (237 loc) · 8.23 KB
/
config.flexible.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
import * as os from 'os'
import * as path from 'path'
import {Config} from '../../src/config/config'
import {Plugin as IPlugin} from '../../src/interfaces'
import {Command as ICommand} from '../../src/interfaces'
import {expect, fancy} from './test'
import {Flags, Interfaces} from '../../src'
interface Options {
pjson?: any;
homedir?: string;
platform?: string;
env?: {[k: string]: string};
commandIds?: string[];
types?: string[];
}
// @ts-expect-error
class MyCommandClass implements ICommand.Class {
_base = ''
aliases: string[] = []
hidden = false
id = 'foo:bar'
flags = {}
new(): ICommand.Instance {
return {
_run(): Promise<any> {
return Promise.resolve()
}}
}
run(): PromiseLike<any> {
return Promise.resolve()
}
}
describe('Config with flexible taxonomy', () => {
const testConfig = ({
homedir = '/my/home',
platform = 'darwin',
env = {},
commandIds = ['foo:bar', 'foo:baz'],
types = [],
}: Options = {}) => {
let test = fancy
.resetConfig()
.env(env, {clear: true})
.stub(os, 'homedir', () => path.join(homedir))
.stub(os, 'platform', () => platform)
const load = async (): Promise<void> => {}
const findCommand = async (): Promise<ICommand.Class> => {
return new MyCommandClass() as unknown as ICommand.Class
}
const commandPluginA: ICommand.Loadable = {
strict: false,
aliases: [],
args: [],
flags: {
flagA: Flags.boolean({char: 'a'}),
},
hidden: false,
id: commandIds[0],
async load(): Promise<ICommand.Class> {
return new MyCommandClass() as unknown as ICommand.Class
},
pluginType: types[0] ?? 'core',
pluginAlias: '@My/plugina',
}
const commandPluginB: ICommand.Loadable = {
strict: false,
aliases: [],
args: [],
flags: {
flagB: Flags.boolean({}),
},
hidden: false,
id: commandIds[1],
async load(): Promise<ICommand.Class> {
return new MyCommandClass() as unknown as ICommand.Class
},
pluginType: types[1] ?? 'core',
pluginAlias: '@My/pluginb',
}
const hooks = {}
const pluginA: IPlugin = {
load,
findCommand,
name: '@My/plugina',
alias: '@My/plugina',
commands: [commandPluginA],
_base: '',
pjson: {} as any,
commandIDs: [commandIds[0]] as string[],
root: '',
version: '0.0.0',
type: types[0] ?? 'core',
hooks,
topics: [],
valid: true,
tag: 'tag',
}
const pluginB: IPlugin = {
load,
findCommand,
name: '@My/pluginb',
alias: '@My/pluginb',
commands: [commandPluginB],
_base: '',
pjson: {} as any,
commandIDs: [commandIds[1]] as string[],
root: '',
version: '0.0.0',
type: types[1] ?? 'core',
hooks,
topics: [],
valid: true,
tag: 'tag',
}
const plugins: IPlugin[] = [pluginA, pluginB]
test = test.add('config', async () => {
const config = await Config.load()
config.flexibleTaxonomy = true
config.plugins = plugins
config.pjson.oclif.plugins = ['@My/pluginb', '@My/plugina']
config.pjson.dependencies = {'@My/pluginb': '0.0.0', '@My/plugina': '0.0.0'}
for (const plugin of config.plugins) {
// @ts-expect-error private method
config.loadCommands(plugin)
// @ts-expect-error private method
config.loadTopics(plugin)
}
return config
})
// @ts-ignore
return {
it(expectation: string, fn: (config: Interfaces.Config) => any) {
test
.do(({config}) => fn(config))
.it(expectation)
return this
},
}
}
testConfig()
.it('has populated topic index', config => {
// @ts-expect-error because private member
const topics = config._topics
expect(topics.has('foo')).to.be.true
expect(topics.has('foo:bar')).to.be.true
expect(topics.has('foo:baz')).to.be.true
})
.it('has populated command permutation index', config => {
// @ts-expect-error because private member
const commandPermutations = config.commandPermutations
expect(commandPermutations.get('foo')).to.deep.equal(new Set(['foo:bar', 'foo:baz']))
expect(commandPermutations.get('foo:bar')).to.deep.equal(new Set(['foo:bar']))
expect(commandPermutations.get('bar')).to.deep.equal(new Set(['foo:bar']))
expect(commandPermutations.get('bar:foo')).to.deep.equal(new Set(['foo:bar']))
expect(commandPermutations.get('foo:baz')).to.deep.equal(new Set(['foo:baz']))
expect(commandPermutations.get('baz')).to.deep.equal(new Set(['foo:baz']))
expect(commandPermutations.get('baz:foo')).to.deep.equal(new Set(['foo:baz']))
})
.it('has populated command index', config => {
// @ts-expect-error because private member
const commands = config._commands
expect(commands.has('foo:bar')).to.be.true
expect(commands.has('foo:baz')).to.be.true
})
.it('has all command id permutations', config => {
expect(config.getAllCommandIDs()).to.deep.equal([
'foo:bar',
'foo:baz',
'bar:foo',
'baz:foo',
])
})
describe('findMatches', () => {
testConfig()
.it('finds command that contains a partial id', config => {
const matches = config.findMatches('foo', [])
expect(matches.length).to.equal(2)
})
.it('finds command that contains a partial id and matching full flag', config => {
const matches = config.findMatches('foo', ['--flagB'])
expect(matches.length).to.equal(1)
expect(matches[0].id).to.equal('foo:baz')
})
.it('finds command that contains a partial id and matching short flag', config => {
const matches = config.findMatches('foo', ['-a'])
expect(matches.length).to.equal(1)
expect(matches[0].id).to.equal('foo:bar')
})
})
describe('findCommand', () => {
testConfig()
.it('find command with no duplicates', config => {
const command = config.findCommand('foo:bar', {must: true})
expect(command).to.have.property('pluginAlias', '@My/plugina')
})
testConfig({commandIds: ['foo:bar', 'foo:bar']})
.it('find command with duplicates and choose the one that appears first in oclif.plugins', config => {
const command = config.findCommand('foo:bar', {must: true})
expect(command).to.have.property('pluginAlias', '@My/pluginb')
})
testConfig({types: ['core', 'user']})
.it('find command with no duplicates core/user', config => {
const command = config.findCommand('foo:bar', {must: true})
expect(command).to.have.property('id', 'foo:bar')
expect(command).to.have.property('pluginType', 'core')
expect(command).to.have.property('pluginAlias', '@My/plugina')
})
testConfig({types: ['user', 'core']})
.it('find command with no duplicates user/core', config => {
const command = config.findCommand('foo:bar', {must: true})
expect(command).to.have.property('id', 'foo:bar')
expect(command).to.have.property('pluginType', 'user')
expect(command).to.have.property('pluginAlias', '@My/plugina')
})
testConfig({commandIds: ['foo:bar', 'foo:bar'], types: ['core', 'user']})
.it('find command with duplicates core/user', config => {
const command = config.findCommand('foo:bar', {must: true})
expect(command).to.have.property('id', 'foo:bar')
expect(command).to.have.property('pluginType', 'core')
expect(command).to.have.property('pluginAlias', '@My/plugina')
})
testConfig({commandIds: ['foo:bar', 'foo:bar'], types: ['user', 'core']})
.it('find command with duplicates user/core', config => {
const command = config.findCommand('foo:bar', {must: true})
expect(command).to.have.property('id', 'foo:bar')
expect(command).to.have.property('pluginType', 'core')
expect(command).to.have.property('pluginAlias', '@My/pluginb')
})
testConfig({commandIds: ['foo:bar', 'foo:bar'], types: ['user', 'user']})
.it('find command with duplicates user/user', config => {
const command = config.findCommand('foo:bar', {must: true})
expect(command).to.have.property('id', 'foo:bar')
expect(command).to.have.property('pluginType', 'user')
expect(command).to.have.property('pluginAlias', '@My/plugina')
})
})
})