/
sakuli-runner.class.ts
236 lines (218 loc) · 7.37 KB
/
sakuli-runner.class.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
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
import { Project, TestFile } from "../loader/model";
import { TestExecutionLifecycleHooks } from "./context-provider.interface";
import { TestScriptExecutor } from "./test-script-executor.interface";
import { readFile } from "fs";
import { JsScriptExecutor } from "./js-script-executor.class";
import { join, resolve } from "path";
import { TestExecutionContext } from "./test-execution-context";
import { nodeSignals } from "../node-signals";
import { LifecycleHookRegistry, lifecycleHookRegistry, } from "./lifecycle-hook-registry";
import Signals = NodeJS.Signals;
export class SakuliRunner implements TestExecutionLifecycleHooks {
private hookRegistry: LifecycleHookRegistry;
constructor(
readonly lifecycleHooks: TestExecutionLifecycleHooks[],
readonly testExecutionContext: TestExecutionContext,
readonly testFileExecutor: TestScriptExecutor = new JsScriptExecutor()
) {
this.hookRegistry = lifecycleHookRegistry(lifecycleHooks);
}
/**
* Tears up all lifecycle-hooks, merges their results of getContext and pass this result to the testFile Executor
* Tears down all service providers after execution of each testFile
*
* @param project The Project Structure found by a Project loader
* @returns a merged object from all provided contexts after their execution
*/
async execute(project: Project): Promise<any> {
this.registerSignalHandler(project);
this.registerUnhandledErrorNotifier(project);
this.testExecutionContext.startExecution();
// onProject Phase
await this.onProject(project, this.testExecutionContext);
let result = {};
await this.beforeExecution(project, this.testExecutionContext);
for (const testFile of project.testFiles) {
try {
const testFileContent = await this.readFileContent(
testFile,
project,
this.testExecutionContext
);
await this.beforeRunFile(testFile, project, this.testExecutionContext);
const context = await this.requestContext(
this.testExecutionContext,
project
);
const resultCtx = await this.testFileExecutor.execute(
testFileContent.toString(),
context,
{
filename: resolve(join(project.rootDir, testFile.path)),
breakOnSigint: true,
}
);
await this.afterRunFile(testFile, project, this.testExecutionContext);
result = { ...result, ...resultCtx };
} catch (error: any) {
if (this.testExecutionContext.getCurrentTestSuite()) {
this.testExecutionContext.updateCurrentTestSuite({ error });
}
}
}
await this.afterExecution(project, this.testExecutionContext);
this.testExecutionContext.endExecution();
return result;
}
async onProject(project: Project, tec: TestExecutionContext) {
await Promise.all(
this.hookRegistry
.getOnProjectHooks()
.map((hook) => hook.onProject!(project, tec))
);
}
async beforeExecution(
project: Project,
testExecutionContext: TestExecutionContext
) {
await Promise.all(
this.hookRegistry
.getBeforeExecutionHooks()
.map((hook) => hook.beforeExecution!(project, testExecutionContext))
);
}
async afterExecution(
project: Project,
testExecutionContext: TestExecutionContext
) {
testExecutionContext.logger.trace(
"Executing 'afterExecution' lifecycle hooks..."
);
await Promise.all(
this.hookRegistry
.getAfterExecutionHooks()
.map((hook) => hook.afterExecution!(project, testExecutionContext))
);
testExecutionContext.logger.trace(
"Execution of 'afterExecution' lifecycle hooks completed."
);
}
async afterRunFile(
file: TestFile,
project: Project,
testExecutionContext: TestExecutionContext
) {
await Promise.all(
this.hookRegistry
.getAfterRunFileHooks()
.map((hook) => hook.afterRunFile!(file, project, testExecutionContext))
);
}
async beforeRunFile(
file: TestFile,
project: Project,
testExecutionContext: TestExecutionContext
) {
await Promise.all(
this.hookRegistry
.getBeforeRunFileHooks()
.map((hook) => hook.beforeRunFile!(file, project, testExecutionContext))
);
}
async requestContext(
testExecutionContext: TestExecutionContext,
project: Project
): Promise<any> {
const contexts = await Promise.all(
this.hookRegistry
.getRequestContextHooks()
.map((hook) => hook.requestContext!(testExecutionContext, project))
);
return contexts.reduce((ctx, context) => ({ ...ctx, ...context }), {
...global,
});
}
async readFileContent(
testFile: TestFile,
project: Project,
context: TestExecutionContext
): Promise<string> {
const fileReaders = this.hookRegistry.getReadFileContentHooks();
if (fileReaders.length >= 1) {
const [fileReader] = fileReaders;
return fileReader.readFileContent!(testFile, project, context);
} else {
return new Promise<string>((res, rej) => {
readFile(join(project.rootDir, testFile.path), (err, data) => {
if (err) {
rej(err);
} else {
res(data.toString());
}
});
});
}
}
private registerSignalHandler(project: Project) {
const handleSignal = async (signal: Signals) => {
this.testExecutionContext.logger.trace(
`Forwarding ${signal} to lifecycle hooks...`
);
await Promise.all(
this.hookRegistry
.getOnSignalHooks()
.map((hook) =>
hook.onSignal!(signal, project, this.testExecutionContext)
)
);
this.testExecutionContext.logger.trace(
`Forwarding of Signal ${signal} to lifecycle hooks completed`
);
if (signal === "SIGINT" || signal === "SIGTERM") {
this.testExecutionContext.logger.info(
`Received signal ${signal}, aborting execution`
);
process.exit(130);
}
};
this.testExecutionContext.logger.trace(
"Registering process signal handler"
);
nodeSignals.forEach((signal) => {
process.on(signal, handleSignal);
});
}
private registerUnhandledErrorNotifier(project: Project) {
this.testExecutionContext.logger.trace(
"Registering uncaughtException handler"
);
const notifyOfUnhandledError = async (currentProject: Project, e: any) => {
this.testExecutionContext.logger.trace(
"Forwarding unhandled error to lifecycle hooks..."
);
await Promise.all(
this.hookRegistry
.getOnUnhandledErrorHooks()
.map((hook) =>
hook.onUnhandledError!(e, currentProject, this.testExecutionContext)
)
);
this.testExecutionContext.logger.trace(
"Forwarding of unhandled error to lifecycle hooks completed"
);
};
process.on("uncaughtException", async (e) => {
this.testExecutionContext.logger.warn("Caught uncaughtException.");
this.testExecutionContext.error = e;
await notifyOfUnhandledError(project, e);
});
this.testExecutionContext.logger.trace(
"Registering unhandledRejection handler"
);
process.on("unhandledRejection", async (e) => {
this.testExecutionContext.logger.warn("Caught unhandledRejection.");
this.testExecutionContext.error = e as Error;
await notifyOfUnhandledError(project, e);
});
}
}