| 29 | export type Task<Context> = { title: string, setup?: TaskPhase<Context>, teardown?: TaskPhase<Context> }; |
| 30 | |
| 31 | export class TaskRunner<Context> { |
| 32 | private _tasks: Task<Context>[] = []; |
| 33 | private _reporter: InternalReporter; |
| 34 | private _hasErrors = false; |
| 35 | private _interrupted = false; |
| 36 | private _isTearDown = false; |
| 37 | private _globalTimeoutForError: number; |
| 38 | |
| 39 | constructor(reporter: InternalReporter, globalTimeoutForError: number) { |
| 40 | this._reporter = reporter; |
| 41 | this._globalTimeoutForError = globalTimeoutForError; |
| 42 | } |
| 43 | |
| 44 | addTask(task: Task<Context>) { |
| 45 | this._tasks.push(task); |
| 46 | } |
| 47 | |
| 48 | async run(context: Context, deadline: number, cancelPromise?: ManualPromise<void>): Promise<FullResult['status']> { |
| 49 | const { status, cleanup } = await this.runDeferCleanup(context, deadline, cancelPromise); |
| 50 | const teardownStatus = await cleanup(); |
| 51 | return status === 'passed' ? teardownStatus : status; |
| 52 | } |
| 53 | |
| 54 | async runDeferCleanup(context: Context, deadline: number, cancelPromise = new ManualPromise<void>()): Promise<{ status: FullResult['status'], cleanup: () => Promise<FullResult['status']> }> { |
| 55 | const sigintWatcher = new SigIntWatcher(); |
| 56 | const timeoutWatcher = new TimeoutWatcher(deadline); |
| 57 | const teardownRunner = new TaskRunner<Context>(this._reporter, this._globalTimeoutForError); |
| 58 | teardownRunner._isTearDown = true; |
| 59 | |
| 60 | let currentTaskName: string | undefined; |
| 61 | |
| 62 | const taskLoop = async () => { |
| 63 | for (const task of this._tasks) { |
| 64 | currentTaskName = task.title; |
| 65 | if (this._interrupted) |
| 66 | break; |
| 67 | debug('pw:test:task')(`"${task.title}" started`); |
| 68 | const errors: TestError[] = []; |
| 69 | const softErrors: TestError[] = []; |
| 70 | try { |
| 71 | teardownRunner._tasks.unshift({ title: `teardown for ${task.title}`, setup: task.teardown }); |
| 72 | await task.setup?.(context, errors, softErrors); |
| 73 | } catch (e) { |
| 74 | debug('pw:test:task')(`error in "${task.title}": `, e); |
| 75 | errors.push(serializeError(e)); |
| 76 | } finally { |
| 77 | for (const error of [...softErrors, ...errors]) |
| 78 | this._reporter.onError?.(error); |
| 79 | if (errors.length) { |
| 80 | if (!this._isTearDown) |
| 81 | this._interrupted = true; |
| 82 | this._hasErrors = true; |
| 83 | } |
| 84 | } |
| 85 | debug('pw:test:task')(`"${task.title}" finished`); |
| 86 | } |
| 87 | }; |
| 88 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…