This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
command.test.ts
209 lines (191 loc) · 4.81 KB
/
command.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
import * as Config from '@anycli/config'
import cli from 'cli-ux'
import {expect, fancy} from 'fancy-test'
import Base, {flags, parse} from '../src'
const pjson = require('../package.json')
class Command extends Base {
static description = 'test command'
async run() {
cli.log('foo')
}
}
describe('command', () => {
fancy
.stdout()
.do(() => Command.run([]))
.do(output => expect(output.stdout).to.equal('foo\n'))
.it('logs to stdout')
fancy
.do(() => {
const Command: Config.ICommand = class extends Base {
async run() {
throw new Error('new x error')
}
}
return Command.run([])
})
.catch(/new x error/)
.it('errors out')
fancy
.stdout()
.do(() => {
const Command: Config.ICommand = class extends Base {
async run() {
cli.exit(0)
}
}
return Command.run([])
})
.catch(/EEXIT: 0/)
.it('exits with 0')
describe('convertToCached', () => {
fancy
.do(async () => {
const c = class extends Command {
static title = 'cmd title'
static usage = ['$ usage']
static description = 'test command'
static aliases = ['alias1', 'alias2']
static hidden = true
static flags = {
flaga: flags.boolean(),
flagb: flags.string({
char: 'b',
hidden: true,
required: false,
description: 'flagb desc',
}),
}
static args = [
{
name: 'arg1',
description: 'arg1 desc',
required: true,
hidden: false,
}
]
}.convertToCached({id: 'foo:bar'})
expect(await c.load()).to.have.property('run')
delete c.load
expect(c).to.deep.equal({
_base: `@anycli/command@${pjson.version}`,
id: 'foo:bar',
hidden: true,
pluginName: undefined,
description: 'test command',
aliases: ['alias1', 'alias2'],
title: 'cmd title',
usage: ['$ usage'],
flags: {
flaga: {
char: undefined,
description: undefined,
name: 'flaga',
hidden: undefined,
required: undefined,
type: 'boolean',
},
flagb: {
char: 'b',
description: 'flagb desc',
name: 'flagb',
hidden: true,
required: false,
type: 'option',
helpValue: undefined,
}
},
args: [
{
description: 'arg1 desc',
name: 'arg1',
hidden: false,
required: true,
}
],
})
})
.it('converts to cached with everything set')
fancy
.do(async () => {
const c = class extends Command {
}.convertToCached()
expect(await c.load()).to.have.property('run')
delete c.load
expect(c).to.deep.equal({
_base: `@anycli/command@${pjson.version}`,
id: undefined,
hidden: undefined,
pluginName: undefined,
description: 'test command',
aliases: [],
title: undefined,
usage: undefined,
flags: {},
args: [],
})
})
.it('adds plugin name')
fancy
.do(async () => {
const c = class extends Command {
}.convertToCached({plugin: {name: 'myplugin'} as any})
expect(await c.load()).to.have.property('run')
delete c.load
expect(c).to.deep.equal({
_base: `@anycli/command@${pjson.version}`,
id: undefined,
hidden: undefined,
pluginName: 'myplugin',
description: 'test command',
aliases: [],
title: undefined,
usage: undefined,
flags: {},
args: [],
})
})
.it('converts to cached with nothing set')
})
describe('http', () => {
fancy
.nock('https://api.github.com', nock => nock.get('/me').reply(200, {name: 'jdxcode'}))
.stdout()
.it('makes http call', async ctx => {
const cmd = class extends Base {
async run() {
let {body: user} = await this.http.get('https://api.github.com/me')
cli.log(user.name)
}
}
await cmd.run([])
expect(ctx.stdout).to.equal('jdxcode\n')
})
})
describe('load', () => {
fancy
.it('returns self', async () => {
const cmd = class extends Base {
async run() {}
}
expect(await cmd.load()).to.equal(cmd)
})
})
describe('parse', () => {
fancy
.stdout()
.it('has a flag', async ctx => {
class CMD extends Base {
static flags = {
foo: flags.string()
}
options = parse(this.argv, CMD)
async run() {
cli.log(this.options.flags.foo)
}
}
await CMD.run(['--foo=bar'])
expect(ctx.stdout).to.equal('bar\n')
})
})
})