-
Notifications
You must be signed in to change notification settings - Fork 67
/
util.test.ts
179 lines (160 loc) · 6.97 KB
/
util.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
import {resolve} from 'path'
import {Config, Interfaces} from '../../src'
import {test} from '@oclif/test'
import {loadHelpClass, standardizeIDFromArgv} from '../../src/help'
import configuredHelpClass from '../../src/help/_test-help-class'
import {expect} from 'chai'
describe('util', () => {
let config: Interfaces.Config
beforeEach(async () => {
config = await Config.load()
})
describe('#loadHelpClass', () => {
test
.it('defaults to the class exported', async () => {
delete config.pjson.oclif.helpClass
const helpClass = await loadHelpClass(config)
expect(helpClass).not.be.undefined
expect(helpClass.prototype.showHelp)
expect(helpClass.prototype.showCommandHelp)
expect(helpClass.prototype.formatRoot)
})
test
.it('loads help class defined in pjson.oclif.helpClass', async () => {
config.pjson.oclif.helpClass = '../src/help/_test-help-class'
config.root = resolve(__dirname, '..')
expect(configuredHelpClass).to.not.be.undefined
expect(await loadHelpClass(config)).to.deep.equal(configuredHelpClass)
})
describe('error cases', () => {
test
.it('throws an error when failing to load the help class defined in pjson.oclif.helpClass', async () => {
config.pjson.oclif.helpClass = './lib/does-not-exist-help-class'
await expect(loadHelpClass(config)).to.be.rejectedWith('Unable to load configured help class "./lib/does-not-exist-help-class", failed with message:')
})
})
})
describe('#standardizeIDFromArgv', () => {
test
.it('should return standardized id when topic separator is a colon', () => {
config.pjson.oclif.topicSeparator = ':'
const actual = standardizeIDFromArgv(['foo:bar', '--baz'], config)
expect(actual).to.deep.equal(['foo:bar', '--baz'])
})
test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space', () => {
config.topicSeparator = ' '
const actual = standardizeIDFromArgv(['foo', 'bar', '--baz'], config)
expect(actual).to.deep.equal(['foo:bar', '--baz'])
})
test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and command is misspelled', () => {
config.topicSeparator = ' '
const actual = standardizeIDFromArgv(['foo', 'ba', '--baz'], config)
expect(actual).to.deep.equal(['foo:ba', '--baz'])
})
test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has args and command is misspelled', () => {
config.topicSeparator = ' '
// @ts-expect-error private member
config._commands.set('foo:bar', {
id: 'foo:bar',
args: [{name: 'first'}],
})
const actual = standardizeIDFromArgv(['foo', 'ba', 'baz'], config)
expect(actual).to.deep.equal(['foo:ba:baz'])
})
test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has args', () => {
config.topicSeparator = ' '
// @ts-expect-error private member
config._commands.set('foo:bar', {
id: 'foo:bar',
args: [{name: 'first'}],
})
const actual = standardizeIDFromArgv(['foo', 'bar', 'baz'], config)
expect(actual).to.deep.equal(['foo:bar', 'baz'])
})
test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has variable arguments', () => {
config.topicSeparator = ' '
// @ts-expect-error private member
config._commands.set('foo:bar', {
id: 'foo:bar',
strict: false,
})
const actual = standardizeIDFromArgv(['foo', 'bar', 'baz'], config)
expect(actual).to.deep.equal(['foo:bar', 'baz'])
})
test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has variable arguments and flags', () => {
config.topicSeparator = ' '
// @ts-expect-error private member
config._commands.set('foo:bar', {
id: 'foo:bar',
strict: false,
})
const actual = standardizeIDFromArgv(['foo', 'bar', 'baz', '--hello'], config)
expect(actual).to.deep.equal(['foo:bar', 'baz', '--hello'])
})
test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return full id when topic separator is a space and does not have arguments', () => {
config.topicSeparator = ' '
// @ts-expect-error private member
config._commands.set('foo:bar', {
id: 'foo:bar',
args: [],
strict: true,
})
const actual = standardizeIDFromArgv(['foo', 'bar', 'baz'], config)
expect(actual).to.deep.equal(['foo:bar:baz'])
})
test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has arg with value', () => {
config.topicSeparator = ' '
// @ts-expect-error private member
config._commands.set('foo:bar', {id: 'foo:bar'})
const actual = standardizeIDFromArgv(['foo', 'bar', 'hello=world'], config)
expect(actual).to.deep.equal(['foo:bar', 'hello=world'])
})
test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has variable args with value', () => {
config.topicSeparator = ' '
// @ts-expect-error private member
config._commands.set('foo:bar', {id: 'foo:bar', strict: false})
const actual = standardizeIDFromArgv(['foo', 'bar', 'hello=world', 'my-arg=value'], config)
expect(actual).to.deep.equal(['foo:bar', 'hello=world', 'my-arg=value'])
})
test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has flags', () => {
config.topicSeparator = ' '
// @ts-expect-error private member
config._commands.set('foo:bar', {id: 'foo:bar'})
const actual = standardizeIDFromArgv(['foo', 'bar', '--baz'], config)
expect(actual).to.deep.equal(['foo:bar', '--baz'])
})
test
.stub(Config.prototype, 'collectUsableIds', () => ['foo', 'foo:bar'])
.it('should return standardized id when topic separator is a space and has flags, arg, and arg with value', () => {
config.topicSeparator = ' '
// @ts-expect-error private member
config._commands.set('foo:bar', {
id: 'foo:bar',
args: [{name: 'my-arg'}],
strict: true,
})
const actual = standardizeIDFromArgv(['foo', 'bar', 'my-arg', 'hello=world', '--baz'], config)
expect(actual).to.deep.equal(['foo:bar', 'my-arg', 'hello=world', '--baz'])
})
})
})