playwright/test/harness/fixturePool.js
2020-08-10 20:10:39 -07:00

154 lines
4.1 KiB
JavaScript

/**
* Copyright Microsoft Corporation. All rights reserved.
*
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const debug = require('debug');
const registrations = new Map();
class Fixture {
constructor(pool, name, scope, fn) {
this.pool = pool;
this.name = name;
this.scope = scope;
this.fn = fn;
this.deps = fixtureParameterNames(this.fn);
this.usages = new Set();
this.value = null;
}
async setup() {
for (const name of this.deps) {
await this.pool.setupFixture(name);
this.pool.instances.get(name).usages.add(this.name);
}
const params = {};
for (const n of this.deps)
params[n] = this.pool.instances.get(n).value;
let setupFenceFulfill;
let setupFenceReject;
const setupFence = new Promise((f, r) => { setupFenceFulfill = f; setupFenceReject = r; });
const teardownFence = new Promise(f => this._teardownFenceCallback = f);
debug('pw:test:hook')(`setup "${this.name}"`);
this._tearDownComplete = this.fn(params, async value => {
this.value = value;
setupFenceFulfill();
await teardownFence;
}).catch(e => setupFenceReject(e));
await setupFence;
this._setup = true;
}
async teardown() {
if (this._teardown)
return;
this._teardown = true;
for (const name of this.usages) {
const fixture = this.pool.instances.get(name);
if (!fixture)
continue;
await fixture.teardown();
}
if (this._setup) {
debug('pw:test:hook')(`teardown "${this.name}"`);
this._teardownFenceCallback();
}
await this._tearDownComplete;
this.pool.instances.delete(this.name);
}
}
class FixturePool {
constructor() {
this.instances = new Map();
}
async setupFixture(name) {
let fixture = this.instances.get(name);
if (fixture)
return fixture;
if (!registrations.has(name))
throw new Error('Unknown fixture: ' + name);
const { scope, fn } = registrations.get(name);
fixture = new Fixture(this, name, scope, fn);
this.instances.set(name, fixture);
await fixture.setup();
return fixture;
}
async teardownScope(scope) {
for (const [name, fixture] of this.instances) {
if (fixture.scope === scope)
await fixture.teardown();
}
}
async resolveParametersAndRun(fn) {
const names = fixtureParameterNames(fn);
for (const name of names)
await this.setupFixture(name);
const params = {};
for (const n of names)
params[n] = this.instances.get(n).value;
return fn(params);
}
patchToEnableFixtures(object, name) {
const original = object[name];
object[name] = fn => {
return original(async () => {
return await this.resolveParametersAndRun(fn);
});
}
}
wrapTestCallback(callback) {
if (!callback)
return callback;
return async() => {
try {
return await this.resolveParametersAndRun(callback);
} finally {
await this.teardownScope('test');
}
};
}
}
function fixtureParameterNames(fn) {
const text = fn.toString();
const match = text.match(/async(?:\s+function)?\s*\(\s*{\s*([^}]*)\s*}/);
if (!match || !match[1].trim())
return [];
let signature = match[1];
return signature.split(',').map(t => t.trim());
}
function innerRegisterFixture(name, scope, fn) {
registrations.set(name, { scope, fn });
};
function registerFixture(name, fn) {
innerRegisterFixture(name, 'test', fn);
};
function registerWorkerFixture (name, fn) {
innerRegisterFixture(name, 'worker', fn);
};
module.exports = { FixturePool, registerFixture, registerWorkerFixture };