|
1 |
| -const debug = require('debug')('codeceptjs:heal'); |
2 |
| -const colors = require('chalk'); |
3 |
| -const Container = require('./container'); |
4 |
| -const recorder = require('./recorder'); |
5 |
| -const output = require('./output'); |
6 |
| -const event = require('./event'); |
| 1 | +const debug = require('debug')('codeceptjs:heal') |
| 2 | +const colors = require('chalk') |
| 3 | +const Container = require('./container') |
| 4 | +const recorder = require('./recorder') |
| 5 | +const output = require('./output') |
| 6 | +const event = require('./event') |
7 | 7 |
|
8 | 8 | /**
|
9 | 9 | * @class
|
10 | 10 | */
|
11 | 11 | class Heal {
|
12 | 12 | constructor() {
|
13 |
| - this.recipes = {}; |
14 |
| - this.fixes = []; |
15 |
| - this.prepareFns = []; |
16 |
| - this.contextName = null; |
17 |
| - this.numHealed = 0; |
| 13 | + this.recipes = {} |
| 14 | + this.fixes = [] |
| 15 | + this.prepareFns = [] |
| 16 | + this.contextName = null |
| 17 | + this.numHealed = 0 |
18 | 18 | }
|
19 | 19 |
|
20 | 20 | clear() {
|
21 |
| - this.recipes = {}; |
22 |
| - this.fixes = []; |
23 |
| - this.prepareFns = []; |
24 |
| - this.contextName = null; |
25 |
| - this.numHealed = 0; |
| 21 | + this.recipes = {} |
| 22 | + this.fixes = [] |
| 23 | + this.prepareFns = [] |
| 24 | + this.contextName = null |
| 25 | + this.numHealed = 0 |
26 | 26 | }
|
27 | 27 |
|
28 | 28 | addRecipe(name, opts = {}) {
|
29 |
| - if (!opts.priority) opts.priority = 0; |
| 29 | + if (!opts.priority) opts.priority = 0 |
30 | 30 |
|
31 |
| - if (!opts.fn) throw new Error(`Recipe ${name} should have a function 'fn' to execute`); |
| 31 | + if (!opts.fn) throw new Error(`Recipe ${name} should have a function 'fn' to execute`) |
32 | 32 |
|
33 |
| - this.recipes[name] = opts; |
| 33 | + this.recipes[name] = opts |
34 | 34 | }
|
35 | 35 |
|
36 | 36 | connectToEvents() {
|
37 | 37 | event.dispatcher.on(event.suite.before, suite => {
|
38 |
| - this.contextName = suite.title; |
39 |
| - }); |
| 38 | + this.contextName = suite.title |
| 39 | + }) |
40 | 40 |
|
41 | 41 | event.dispatcher.on(event.test.started, test => {
|
42 |
| - this.contextName = test.fullTitle(); |
43 |
| - }); |
| 42 | + this.contextName = test.fullTitle() |
| 43 | + }) |
44 | 44 |
|
45 | 45 | event.dispatcher.on(event.test.finished, () => {
|
46 |
| - this.contextName = null; |
47 |
| - }); |
| 46 | + this.contextName = null |
| 47 | + }) |
48 | 48 | }
|
49 | 49 |
|
50 | 50 | hasCorrespondingRecipes(step) {
|
51 |
| - return matchRecipes(this.recipes, this.contextName).filter(r => !r.steps || r.steps.includes(step.name)).length > 0; |
| 51 | + return matchRecipes(this.recipes, this.contextName).filter(r => !r.steps || r.steps.includes(step.name)).length > 0 |
52 | 52 | }
|
53 | 53 |
|
54 | 54 | async getCodeSuggestions(context) {
|
55 |
| - const suggestions = []; |
56 |
| - const recipes = matchRecipes(this.recipes, this.contextName); |
| 55 | + const suggestions = [] |
| 56 | + const recipes = matchRecipes(this.recipes, this.contextName) |
57 | 57 |
|
58 |
| - debug('Recipes', recipes); |
| 58 | + debug('Recipes', recipes) |
59 | 59 |
|
60 |
| - const currentOutputLevel = output.level(); |
61 |
| - output.level(0); |
| 60 | + const currentOutputLevel = output.level() |
| 61 | + output.level(0) |
62 | 62 |
|
63 | 63 | for (const [property, prepareFn] of Object.entries(
|
64 | 64 | recipes
|
65 | 65 | .map(r => r.prepare)
|
66 | 66 | .filter(p => !!p)
|
67 | 67 | .reduce((acc, obj) => ({ ...acc, ...obj }), {}),
|
68 | 68 | )) {
|
69 |
| - if (!prepareFn) continue; |
| 69 | + if (!prepareFn) continue |
70 | 70 |
|
71 |
| - if (context[property]) continue; |
72 |
| - context[property] = await prepareFn(Container.support()); |
| 71 | + if (context[property]) continue |
| 72 | + context[property] = await prepareFn(Container.support()) |
73 | 73 | }
|
74 | 74 |
|
75 |
| - output.level(currentOutputLevel); |
| 75 | + output.level(currentOutputLevel) |
76 | 76 |
|
77 | 77 | for (const recipe of recipes) {
|
78 |
| - let snippets = await recipe.fn(context); |
79 |
| - if (!Array.isArray(snippets)) snippets = [snippets]; |
| 78 | + let snippets = await recipe.fn(context) |
| 79 | + if (!Array.isArray(snippets)) snippets = [snippets] |
80 | 80 |
|
81 | 81 | suggestions.push({
|
82 | 82 | name: recipe.name,
|
83 | 83 | snippets,
|
84 |
| - }); |
| 84 | + }) |
85 | 85 | }
|
86 | 86 |
|
87 |
| - return suggestions.filter(s => !isBlank(s.snippets)); |
| 87 | + return suggestions.filter(s => !isBlank(s.snippets)) |
88 | 88 | }
|
89 | 89 |
|
90 | 90 | async healStep(failedStep, error, failureContext = {}) {
|
91 |
| - output.debug(`Trying to heal ${failedStep.toCode()} step`); |
| 91 | + output.debug(`Trying to heal ${failedStep.toCode()} step`) |
92 | 92 |
|
93 | 93 | Object.assign(failureContext, {
|
94 | 94 | error,
|
95 | 95 | step: failedStep,
|
96 | 96 | prevSteps: failureContext?.test?.steps?.slice(0, -1) || [],
|
97 |
| - }); |
| 97 | + }) |
98 | 98 |
|
99 |
| - const suggestions = await this.getCodeSuggestions(failureContext); |
| 99 | + const suggestions = await this.getCodeSuggestions(failureContext) |
100 | 100 |
|
101 | 101 | if (suggestions.length === 0) {
|
102 |
| - debug('No healing suggestions found'); |
103 |
| - throw error; |
| 102 | + debug('No healing suggestions found') |
| 103 | + throw error |
104 | 104 | }
|
105 | 105 |
|
106 |
| - output.debug(`Received ${suggestions.length} suggestion${suggestions.length === 1 ? '' : 's'}`); |
| 106 | + output.debug(`Received ${suggestions.length} suggestion${suggestions.length === 1 ? '' : 's'}`) |
107 | 107 |
|
108 |
| - debug(suggestions); |
| 108 | + debug(suggestions) |
109 | 109 |
|
110 | 110 | for (const suggestion of suggestions) {
|
111 | 111 | for (const codeSnippet of suggestion.snippets) {
|
112 | 112 | try {
|
113 |
| - debug('Executing', codeSnippet); |
| 113 | + debug('Executing', codeSnippet) |
114 | 114 | recorder.catch(e => {
|
115 |
| - debug(e); |
116 |
| - }); |
| 115 | + debug(e) |
| 116 | + }) |
117 | 117 |
|
118 | 118 | if (typeof codeSnippet === 'string') {
|
119 |
| - const I = Container.support('I'); |
120 |
| - await eval(codeSnippet); // eslint-disable-line |
| 119 | + const I = Container.support('I') |
| 120 | + await eval(codeSnippet) |
121 | 121 | } else if (typeof codeSnippet === 'function') {
|
122 |
| - await codeSnippet(Container.support()); |
| 122 | + await codeSnippet(Container.support()) |
123 | 123 | }
|
124 | 124 |
|
125 | 125 | this.fixes.push({
|
126 | 126 | recipe: suggestion.name,
|
127 | 127 | test: failureContext?.test,
|
128 | 128 | step: failedStep,
|
129 | 129 | snippet: codeSnippet,
|
130 |
| - }); |
| 130 | + }) |
131 | 131 |
|
132 |
| - recorder.add('healed', () => output.print(colors.bold.green(` Code healed successfully by ${suggestion.name}`), colors.gray('(no errors thrown)'))); |
133 |
| - this.numHealed++; |
| 132 | + recorder.add('healed', () => output.print(colors.bold.green(` Code healed successfully by ${suggestion.name}`), colors.gray('(no errors thrown)'))) |
| 133 | + this.numHealed++ |
134 | 134 | // recorder.session.restore();
|
135 |
| - return; |
| 135 | + return |
136 | 136 | } catch (err) {
|
137 |
| - debug('Failed to execute code', err); |
138 |
| - recorder.ignoreErr(err); // healing did not help |
139 |
| - recorder.catchWithoutStop(err); |
140 |
| - await recorder.promise(); // wait for all promises to resolve |
| 137 | + debug('Failed to execute code', err) |
| 138 | + recorder.ignoreErr(err) // healing did not help |
| 139 | + recorder.catchWithoutStop(err) |
| 140 | + await recorder.promise() // wait for all promises to resolve |
141 | 141 | }
|
142 | 142 | }
|
143 | 143 | }
|
144 |
| - output.debug(`Couldn't heal the code for ${failedStep.toCode()}`); |
145 |
| - recorder.throw(error); |
| 144 | + output.debug(`Couldn't heal the code for ${failedStep.toCode()}`) |
| 145 | + recorder.throw(error) |
146 | 146 | }
|
147 | 147 |
|
148 | 148 | static setDefaultHealers() {
|
149 |
| - require('./template/heal'); |
| 149 | + require('./template/heal') |
150 | 150 | }
|
151 | 151 | }
|
152 | 152 |
|
153 |
| -const heal = new Heal(); |
| 153 | +const heal = new Heal() |
154 | 154 |
|
155 |
| -module.exports = heal; |
| 155 | +module.exports = heal |
156 | 156 |
|
157 | 157 | function matchRecipes(recipes, contextName) {
|
158 | 158 | return Object.entries(recipes)
|
159 | 159 | .filter(([, recipe]) => !contextName || !recipe.grep || new RegExp(recipe.grep).test(contextName))
|
160 | 160 | .sort(([, a], [, b]) => a.priority - b.priority)
|
161 | 161 | .map(([name, recipe]) => {
|
162 |
| - recipe.name = name; |
163 |
| - return recipe; |
| 162 | + recipe.name = name |
| 163 | + return recipe |
164 | 164 | })
|
165 |
| - .filter(r => !!r.fn); |
| 165 | + .filter(r => !!r.fn) |
166 | 166 | }
|
167 | 167 |
|
168 | 168 | function isBlank(value) {
|
169 |
| - return value == null || (Array.isArray(value) && value.length === 0) || (typeof value === 'object' && Object.keys(value).length === 0) || (typeof value === 'string' && value.trim() === ''); |
| 169 | + return value == null || (Array.isArray(value) && value.length === 0) || (typeof value === 'object' && Object.keys(value).length === 0) || (typeof value === 'string' && value.trim() === '') |
170 | 170 | }
|
0 commit comments