-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathBaseCommand.test.js
More file actions
357 lines (311 loc) · 14.6 KB
/
BaseCommand.test.js
File metadata and controls
357 lines (311 loc) · 14.6 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
Copyright 2019 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
const { stdout } = require('stdout-stderr')
const { Command } = require('@oclif/core')
jest.mock('@adobe/aio-lib-core-config')
const mockAioConfig = require('@adobe/aio-lib-core-config')
const mockConfigLoader = require('@adobe/aio-cli-lib-app-config')
jest.mock('@adobe/aio-cli-lib-app-config')
const getMockConfig = require('./data-mocks/config-loader')
const libEnv = require('@adobe/aio-lib-env')
jest.mock('@adobe/aio-lib-env')
jest.mock('@adobe/aio-lib-ims')
const { getToken } = require('@adobe/aio-lib-ims')
jest.mock('@adobe/aio-cli-lib-console')
const LibConsoleCLI = require('@adobe/aio-cli-lib-console')
LibConsoleCLI.init.mockResolvedValue({})
const TheCommand = require('../src/BaseCommand')
jest.mock('inquirer')
const inquirer = require('inquirer')
const mockExtensionPrompt = jest.fn()
inquirer.createPromptModule = jest.fn().mockReturnValue(mockExtensionPrompt)
beforeEach(() => {
libEnv.getCliEnv.mockReturnValue('prod')
mockConfigLoader.load.mockReset()
LibConsoleCLI.init.mockClear()
LibConsoleCLI.cleanStdOut.mockClear()
inquirer.createPromptModule.mockClear()
mockExtensionPrompt.mockReset()
})
test('exports', async () => {
expect(typeof TheCommand).toEqual('function')
expect(TheCommand.prototype instanceof Command).toBeTruthy()
})
test('flags', async () => {
expect(typeof TheCommand.flags.version).toBe('object')
expect(typeof TheCommand.flags.version.description).toBe('string')
expect(typeof TheCommand.flags.verbose).toBe('object')
expect(TheCommand.flags.verbose.char).toBe('v')
expect(typeof TheCommand.flags.verbose.description).toBe('string')
})
test('args', async () => {
expect(TheCommand.args).toEqual({})
})
test('basecommand defines method', async () => {
const cmd = new TheCommand()
expect(cmd.getLaunchUrlPrefix).toBeDefined()
expect(typeof cmd.getLaunchUrlPrefix).toBe('function')
expect(cmd.preRelease).toBeDefined()
expect(typeof cmd.preRelease).toBe('function')
})
test('preRelease() outputs to log', async () => {
const cmd = new TheCommand()
cmd.log = jest.fn()
cmd.preRelease()
expect(cmd.log).toHaveBeenCalledWith(expect.stringMatching('Pre-release warning: This command is in pre-release, and not suitable for production.'))
})
test('getLaunchUrlPrefix() warns on older url', async () => {
const cmd = new TheCommand()
mockAioConfig.get.mockReturnValue('some-url/apps/some-param')
expect(cmd.getLaunchUrlPrefix()).toBe('some-url/custom-apps/some-param')
expect(stdout.output).toMatch('Warning: your environment variables contains an older version of AIO_LAUNCH_URL_PREFIX')
mockAioConfig.get.mockReturnValue('some-url/myapps/some-param')
expect(cmd.getLaunchUrlPrefix()).toBe('some-url/custom-apps/some-param')
expect(stdout.output).toMatch('Warning: your environment variables contains an older version of AIO_LAUNCH_URL_PREFIX')
mockAioConfig.get.mockReturnValue('some-url/custom-apps/some-param')
expect(cmd.getLaunchUrlPrefix()).toBe('some-url/custom-apps/some-param')
expect(stdout.output).toMatch('')
mockAioConfig.get.mockReturnValue(null)
expect(cmd.getLaunchUrlPrefix()).toEqual(expect.stringContaining('https://'))
})
test('getLaunchUrlPrefix() uses stage launch prefix', async () => {
const cmd = new TheCommand()
libEnv.getCliEnv.mockReturnValue('stage')
mockAioConfig.get.mockReturnValue(0)
expect(cmd.getLaunchUrlPrefix()).toBe('https://experience-stage.adobe.com/?devMode=true#/custom-apps/?localDevUrl=')
})
describe('getFullConfig', () => {
test('keeps cache', async () => {
const cmd = new TheCommand()
mockConfigLoader.load.mockResolvedValue({ a: 'hello' })
const config = await cmd.getFullConfig()
const config2 = await cmd.getFullConfig()
expect(config).toEqual({ a: 'hello' })
expect(config).toEqual(config2)
expect(mockConfigLoader.load).toHaveBeenCalledTimes(1)
})
test('with options', async () => {
const cmd = new TheCommand()
mockConfigLoader.load.mockResolvedValue({ a: 'hello' })
const config = await cmd.getFullConfig({ someOptions: {} })
expect(config).toEqual({ a: 'hello' })
expect(mockConfigLoader.load).toHaveBeenCalledWith({ someOptions: {}, validateAppConfig: true })
})
test('with config-validation=true', async () => {
const cmd = new TheCommand()
mockConfigLoader.load.mockResolvedValue({ a: 'hello' })
const config = await cmd.getFullConfig({ someOptions: {} }, { 'config-validation': true })
expect(config).toEqual({ a: 'hello' })
expect(mockConfigLoader.load).toHaveBeenCalledWith({ someOptions: {}, validateAppConfig: true })
})
test('with config-validation=false', async () => {
const cmd = new TheCommand()
mockConfigLoader.load.mockResolvedValue({ a: 'hello' })
const config = await cmd.getFullConfig({ someOptions: {} }, { 'config-validation': false })
expect(config).toEqual({ a: 'hello' })
expect(mockConfigLoader.load).toHaveBeenCalledWith({ someOptions: {}, validateAppConfig: false })
})
})
describe('getConfigFileForKey', () => {
test('returns empty object if not found', async () => {
mockConfigLoader.load.mockResolvedValue(getMockConfig('exc', {}))
const cmd = new TheCommand()
expect(await cmd.getConfigFileForKey('notexist.key.abc')).toEqual({})
expect(mockConfigLoader.load).toHaveBeenCalledWith({ validateAppConfig: true })
})
test('returns file and key if found', async () => {
const config = getMockConfig('exc', {})
const cmd = new TheCommand()
mockConfigLoader.load.mockResolvedValue(config)
expect(await cmd.getConfigFileForKey('extensions')).toEqual(config.includeIndex.extensions)
})
})
describe('getRuntimeManifestConfigFile', () => {
test('no actions', async () => {
mockConfigLoader.load.mockResolvedValue(getMockConfig('app-no-actions', {}))
const cmd = new TheCommand()
expect(await cmd.getRuntimeManifestConfigFile('application')).toEqual({ file: 'app.config.yaml', key: 'application.runtimeManifest' })
expect(mockConfigLoader.load).toHaveBeenCalledWith({ validateAppConfig: true })
})
test('multiple implementations', async () => {
const config = getMockConfig('app-exc-nui', {})
mockConfigLoader.load.mockResolvedValue(config)
const cmd = new TheCommand()
expect(await cmd.getRuntimeManifestConfigFile('application')).toEqual({ file: 'app.config.yaml', key: 'application.runtimeManifest' })
expect(await cmd.getRuntimeManifestConfigFile('dx/asset-compute/worker/1')).toEqual({ file: 'src/dx-asset-compute-worker-1/ext.config.yaml', key: 'runtimeManifest' })
expect(await cmd.getRuntimeManifestConfigFile('dx/excshell/1')).toEqual({ file: 'src/dx-excshell-1/ext.config.yaml', key: 'runtimeManifest' })
})
})
describe('getEventsConfigFile', () => {
test('no events', async () => {
mockConfigLoader.load.mockResolvedValue(getMockConfig('app-no-actions', {}))
const cmd = new TheCommand()
expect(await cmd.getEventsConfigFile('application')).toEqual({ file: 'app.config.yaml', key: 'application.events' })
expect(mockConfigLoader.load).toHaveBeenCalledWith({ validateAppConfig: true })
})
test('multiple implementations', async () => {
const config = getMockConfig('app-exc-nui', {})
mockConfigLoader.load.mockResolvedValue(config)
const cmd = new TheCommand()
expect(await cmd.getEventsConfigFile('application')).toEqual({ file: 'app.config.yaml', key: 'application.events' })
expect(await cmd.getEventsConfigFile('dx/asset-compute/worker/1')).toEqual({ file: 'src/dx-asset-compute-worker-1/ext.config.yaml', key: 'events' })
expect(await cmd.getEventsConfigFile('dx/excshell/1')).toEqual({ file: 'src/dx-excshell-1/ext.config.yaml', key: 'events' })
})
})
describe('getAppExtConfigs', () => {
test('no extension flags', async () => {
const config = getMockConfig('app-exc-nui', {})
mockConfigLoader.load.mockResolvedValue(config)
const cmd = new TheCommand()
expect(await cmd.getAppExtConfigs({})).toEqual(config.all)
expect(mockConfigLoader.load).toHaveBeenCalledWith({ validateAppConfig: true })
})
test('with options', async () => {
const config = getMockConfig('app-exc-nui', {})
mockConfigLoader.load.mockResolvedValue(config)
const cmd = new TheCommand()
expect(await cmd.getAppExtConfigs({}, { some: 'options' })).toEqual(config.all)
expect(mockConfigLoader.load).toHaveBeenCalledWith({ some: 'options', validateAppConfig: true })
})
test('with flag validateAppConfig=false', async () => {
const config = getMockConfig('app-exc-nui', {})
mockConfigLoader.load.mockResolvedValue(config)
const cmd = new TheCommand()
expect(await cmd.getAppExtConfigs({ 'config-validation': false }, { some: 'options' })).toEqual(config.all)
expect(mockConfigLoader.load).toHaveBeenCalledWith({ some: 'options', validateAppConfig: false })
})
test('-e exc -e asset', async () => {
const config = getMockConfig('app-exc-nui', {})
mockConfigLoader.load.mockResolvedValue(config)
const cmd = new TheCommand()
expect(await cmd.getAppExtConfigs({ extension: ['exc', 'asset'] }))
.toEqual({
'dx/excshell/1': config.all['dx/excshell/1'],
'dx/asset-compute/worker/1': config.all['dx/asset-compute/worker/1']
})
})
test('-e application', async () => {
const config = getMockConfig('app-exc-nui', {})
mockConfigLoader.load.mockResolvedValue(config)
const cmd = new TheCommand()
expect(await cmd.getAppExtConfigs({ extension: ['application'] }))
.toEqual({ application: config.all.application })
})
test('-e application, { validateAppConfig: true }', async () => {
const config = getMockConfig('app-exc-nui', {})
mockConfigLoader.load.mockResolvedValue(config)
const cmd = new TheCommand()
expect(await cmd.getAppExtConfigs({ extension: ['application'] }, { validateAppConfig: true }))
.toEqual({ application: config.all.application })
expect(mockConfigLoader.load).toHaveBeenCalledWith({ validateAppConfig: true })
})
test('-e exc -e notexists', async () => {
const config = getMockConfig('app-exc-nui', {})
mockConfigLoader.load.mockResolvedValue(config)
const cmd = new TheCommand()
await expect(async () => await cmd.getAppExtConfigs({ extension: ['exc', 'notexists'] }))
.rejects.toThrow('No matching extension implementation found for flag \'-e notexists\'')
})
test('-e dx (matches more than one)', async () => {
const config = getMockConfig('app-exc-nui', {})
mockConfigLoader.load.mockResolvedValue(config)
const cmd = new TheCommand()
await expect(async () => await cmd.getAppExtConfigs({ extension: ['dx'] }))
.rejects.toThrow('Flag \'-e dx\' matches multiple extension implementation')
})
})
describe('getLibConsoleCLI', () => {
test('cache', async () => {
const cmd = new TheCommand()
const a = await cmd.getLibConsoleCLI()
const b = await cmd.getLibConsoleCLI()
expect(a).toBe(b)
expect(LibConsoleCLI.init).toHaveBeenCalledTimes(1)
})
test('prod env', async () => {
getToken.mockReturnValue('hola')
libEnv.getCliEnv.mockReturnValue('prod')
const cmd = new TheCommand()
await cmd.getLibConsoleCLI()
expect(LibConsoleCLI.init).toHaveBeenCalledWith({ env: 'prod', accessToken: 'hola', apiKey: expect.any(String) })
})
test('stage env', async () => {
getToken.mockReturnValue('hola')
libEnv.getCliEnv.mockReturnValue('stage')
const cmd = new TheCommand()
await cmd.getLibConsoleCLI()
expect(LibConsoleCLI.init).toHaveBeenCalledWith({ env: 'stage', accessToken: 'hola', apiKey: expect.any(String) })
})
})
test('init', async () => {
const cmd = new TheCommand([])
cmd.config = global.createOclifMockConfig()
await cmd.init()
expect(cmd.prompt).toBe(mockExtensionPrompt)
expect(inquirer.createPromptModule).toHaveBeenCalledWith({ output: process.stderr })
})
test('catch', async () => {
const cmd = new TheCommand([])
cmd.config = global.createOclifMockConfig()
cmd.error = jest.fn()
await cmd.catch(new Error('fake error'))
expect(cmd.error).toHaveBeenCalledWith('fake error')
})
test('will change error message when aio app outside of the application root', async () => {
const cmd = new TheCommand([])
cmd.config = global.createOclifMockConfig()
cmd.error = jest.fn()
await cmd.catch(new Error('ENOENT: no such file or directory, open \'package.json\''))
const errorList = [
'ENOENT: no such file or directory, open \'package.json\''
]
expect(cmd.error).toHaveBeenCalledWith(errorList.join('\n'))
})
test('will change error message when aio app outside of the application root (--verbose)', async () => {
const cmd = new TheCommand(['--verbose'])
cmd.config = global.createOclifMockConfig()
cmd.error = jest.fn()
await cmd.catch(new Error('ENOENT: no such file or directory, open \'package.json\''))
const errorList = [
'Error: ENOENT: no such file or directory, open \'package.json\''
]
expect(cmd.error).toHaveBeenCalledWith(expect.stringContaining(errorList.join('\n')))
})
test('will handle errors without stack traces when using --verbose flag', async () => {
const cmd = new TheCommand(['--verbose'])
cmd.config = global.createOclifMockConfig()
cmd.error = jest.fn()
const errorWithoutStack = new Error('fake error')
delete errorWithoutStack.stack
await cmd.catch(errorWithoutStack)
expect(cmd.error).toHaveBeenCalledWith(expect.stringContaining('fake error'))
})
test('will handle errors without stack traces when not using --verbose flag', async () => {
const cmd = new TheCommand([])
cmd.config = global.createOclifMockConfig()
cmd.error = jest.fn()
const errorWithoutStack = new Error('fake error')
delete errorWithoutStack.stack
await cmd.catch(errorWithoutStack)
expect(cmd.error).toHaveBeenCalledWith(expect.stringContaining('fake error'))
})
test('pjson', async () => {
const cmd = new TheCommand([])
cmd.config = { pjson: { name: 'fake', version: '0' } }
expect(cmd.pjson).toEqual({ name: 'fake', version: '0' })
expect(cmd.appName).toEqual('fake')
expect(cmd.appVersion).toEqual('0')
})
test('cleanConsoleCLIOutput', async () => {
const cmd = new TheCommand([])
await cmd.cleanConsoleCLIOutput()
expect(LibConsoleCLI.cleanStdOut).toHaveBeenCalled()
})