-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest.js
239 lines (208 loc) · 7.89 KB
/
test.js
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
/*
Copyright 2021 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 { runScript } = require('../../lib/app-helper')
const { Flags } = require('@oclif/core')
const BaseCommand = require('../../BaseCommand')
const chalk = require('chalk')
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:test', { provider: 'debug' })
const path = require('path')
class Test extends BaseCommand {
async run () {
const { flags } = await this.parse(Test)
let { all, unit, e2e, action, extension } = flags
// 'all' overrides the setting of either the unit or e2e flag
if (all) {
unit = true
e2e = true
} else if (!unit && !e2e) {
// 'all' not set; we check if neither is set, and default to 'unit'
unit = true
}
const buildConfigs = await this.getAppExtConfigs({ extension })
aioLogger.debug(`run buildConfigs:${JSON.stringify(buildConfigs, null, 2)}`)
const totalResults = []
let exitCode = 0
for (const [extensionName, extensionConfig] of Object.entries(buildConfigs)) {
const results = await this.runExtensionTest(extensionName, extensionConfig, { unit, e2e, action })
totalResults.push(...results)
const failures = results.filter(result => !result.passed)
if (failures.length > 0) {
exitCode = 1
}
}
this.printReport(totalResults)
process.exitCode = exitCode
return exitCode
}
printReport (totalResults) {
if (totalResults.length > 0) {
const greenCheckMark = chalk.green('√')
const redX = chalk.red('×')
this.log('app:test results:')
totalResults.forEach(result => {
const name = chalk.gray(`${result.extensionName}/${result.actionName} (${result.type})`)
this.log(` ${result.passed ? greenCheckMark : redX} ${name}`)
})
} else {
this.log('No test results.')
}
}
normalizedActionList (extensionConfig) {
const actionList = []
const packages = extensionConfig.manifest.full.packages
for (const [packageName, pkg] of Object.entries(packages)) {
const actions = pkg.actions
for (const actionName of Object.keys(actions)) {
actionList.push([packageName, actionName])
}
}
return actionList
}
escapeBackslashes (pathString) {
// for Jest:
// - replace backslashes with forward slashes,
// - OR on Windows you need to escape forward slashes
return pathString.replace(/\\/g, '/')
}
testFolders (extensionConfig) {
return { // add leading period path for jest
unit: './' + this.escapeBackslashes(path.relative(extensionConfig.root, extensionConfig.tests.unit)),
e2e: './' + this.escapeBackslashes(path.relative(extensionConfig.root, extensionConfig.tests.e2e))
}
}
filterActions (actionFilters, extensionConfig, flags) {
const { unit, e2e } = flags
const commandList = []
const { unit: unitTestFolder, e2e: e2eTestFolder } = this.testFolders(extensionConfig)
aioLogger.debug(`filterActions actionFilters:${actionFilters} extensionConfig:${JSON.stringify(extensionConfig, null, 2)} flags:${JSON.stringify(flags)}`)
aioLogger.debug(`filterActions:testFolders unit:${unitTestFolder} e2e:${e2eTestFolder}`)
// filter by action(s)
const actionList = this.normalizedActionList(extensionConfig)
.filter(([packageName, actionName]) => {
const actionFullName = `${packageName}/${actionName}`
aioLogger.debug(`filterActions actionFullName:${actionFullName}`)
return actionFilters
.filter(actionFilter => {
return actionFullName.includes(actionFilter)
})
.length > 0
})
actionList.forEach(([, actionName]) => {
// if the action is foo: matches foo.*.test.js
const pattern = `.*/*${actionName}.*\\.test\\.js`
aioLogger.debug(`filterActions pattern:${pattern}`)
if (unit) {
commandList.push({
actionName,
type: 'unit',
command: 'jest',
args: ['--passWithNoTests', '--testPathPattern', `${unitTestFolder}/${pattern}`]
})
}
if (e2e) {
commandList.push({
actionName,
type: 'e2e',
command: 'jest',
args: ['--passWithNoTests', '--testPathPattern', `${e2eTestFolder}/${pattern}`]
})
}
})
return commandList
}
async runExtensionTest (extensionName, extensionConfig, flags) {
const { unit, e2e, action } = flags
const commandList = []
const { unit: unitTestFolder, e2e: e2eTestFolder } = this.testFolders(extensionConfig)
const actionName = '*'
aioLogger.debug(`runExtensionTest extensionName:${extensionName} extensionConfig:${JSON.stringify(extensionConfig, null, 2)} flags:${JSON.stringify(flags)}`)
aioLogger.debug(`runExtensionTest:testFolders unit:${unitTestFolder} e2e:${e2eTestFolder}`)
aioLogger.debug(`runExtensionTest hooks.test:${extensionConfig.hooks.test}`)
// if hooks.test available, we run that instead
if (extensionConfig.hooks.test) {
commandList.push({
actionName,
type: 'hook',
command: extensionConfig.hooks.test
})
} else if (action) { // filter by ext/action name
const commands = await this.filterActions(action, extensionConfig, flags)
if (commands.length === 0) {
this.log(`No package and action matches action filter(s): ${JSON.stringify(action)}`)
}
commandList.push(...commands)
} else { // run everything
if (unit) {
commandList.push({
actionName,
type: 'unit',
command: 'jest',
args: ['--passWithNoTests', unitTestFolder]
})
}
if (e2e) {
commandList.push({
actionName,
type: 'e2e',
command: 'jest',
args: ['--passWithNoTests', e2eTestFolder]
})
}
}
const results = []
for (const cmd of commandList) {
this.log(chalk.yellow(`${extensionName}/${cmd.actionName} (${cmd.type}) - running tests...`))
aioLogger.debug(`runExtensionTest:runScript cwd:${extensionConfig.root} cmd:${JSON.stringify(cmd)}`)
try {
await runScript(cmd.command, extensionConfig.root, cmd.args)
results.push({ ...cmd, extensionName, passed: true })
} catch (e) {
results.push({ ...cmd, extensionName, passed: false })
aioLogger.debug(`runExtensionTest:runScript exception:${e.toString()}`)
}
this.log()
}
return results
}
}
Test.flags = {
extension: Flags.string({
char: 'e',
description: 'the extension(s) to test',
exclusive: ['action'],
multiple: true
}),
action: Flags.string({
char: 'a',
description: 'the action(s) to test',
exclusive: ['extension'],
multiple: true
}),
all: Flags.boolean({
description: 'run both unit and e2e tests',
default: false
}),
e2e: Flags.boolean({
description: 'run e2e tests',
default: false
}),
unit: Flags.boolean({
description: 'run unit tests',
default: false
})
}
Test.description = `Run tests for an Adobe I/O App
If no flags are specified, by default only unit-tests are run.
For the --action flag, it tries a substring search on the 'package-name/action-name' pair for an action.
For the --extension flag, it tries a substring search on the 'extension-name' only.
If the extension has a hook called 'test' in its 'ext.config.yaml', the script specified will be run instead.
`
module.exports = Test