2021-06-06 17:09:53 -07:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import type { TestType, FullProject, Fixtures, FixturesWithLocation } from './types';
|
2021-07-19 14:54:18 -07:00
|
|
|
import { Suite, TestCase } from './test';
|
2021-06-06 17:09:53 -07:00
|
|
|
import { FixturePool } from './fixtures';
|
|
|
|
import { DeclaredFixtures, TestTypeImpl } from './testType';
|
|
|
|
|
|
|
|
export class ProjectImpl {
|
|
|
|
config: FullProject;
|
|
|
|
private index: number;
|
|
|
|
private defines = new Map<TestType<any, any>, Fixtures>();
|
|
|
|
private testTypePools = new Map<TestTypeImpl, FixturePool>();
|
2021-07-19 14:54:18 -07:00
|
|
|
private testPools = new Map<TestCase, FixturePool>();
|
2021-06-06 17:09:53 -07:00
|
|
|
|
|
|
|
constructor(project: FullProject, index: number) {
|
|
|
|
this.config = project;
|
|
|
|
this.index = index;
|
|
|
|
this.defines = new Map();
|
|
|
|
for (const { test, fixtures } of Array.isArray(project.define) ? project.define : [project.define])
|
|
|
|
this.defines.set(test, fixtures);
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildTestTypePool(testType: TestTypeImpl): FixturePool {
|
|
|
|
if (!this.testTypePools.has(testType)) {
|
|
|
|
const fixtures = this.resolveFixtures(testType);
|
|
|
|
const overrides: Fixtures = this.config.use;
|
|
|
|
const overridesWithLocation = {
|
|
|
|
fixtures: overrides,
|
|
|
|
location: {
|
|
|
|
file: `<configuration file>`,
|
|
|
|
line: 1,
|
|
|
|
column: 1,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const pool = new FixturePool([...fixtures, overridesWithLocation]);
|
|
|
|
this.testTypePools.set(testType, pool);
|
|
|
|
}
|
|
|
|
return this.testTypePools.get(testType)!;
|
|
|
|
}
|
|
|
|
|
2021-07-15 22:02:10 -07:00
|
|
|
// TODO: we can optimize this function by building the pool inline in cloneSuite
|
2021-07-19 14:54:18 -07:00
|
|
|
private buildPool(test: TestCase): FixturePool {
|
2021-07-15 22:02:10 -07:00
|
|
|
if (!this.testPools.has(test)) {
|
|
|
|
let pool = this.buildTestTypePool(test._testType);
|
2021-06-06 17:09:53 -07:00
|
|
|
|
2021-08-10 16:32:32 -07:00
|
|
|
const parents: Suite[] = [];
|
2021-10-19 08:38:04 -07:00
|
|
|
for (let parent: Suite | undefined = test.parent; parent; parent = parent.parent)
|
2021-08-10 16:32:32 -07:00
|
|
|
parents.push(parent);
|
|
|
|
parents.reverse();
|
|
|
|
|
|
|
|
for (const parent of parents) {
|
|
|
|
if (parent._use.length)
|
|
|
|
pool = new FixturePool(parent._use, pool, parent._isDescribe);
|
2021-08-09 13:26:33 -07:00
|
|
|
for (const hook of parent._eachHooks)
|
|
|
|
pool.validateFunction(hook.fn, hook.type + ' hook', hook.location);
|
|
|
|
for (const hook of parent._allHooks)
|
|
|
|
pool.validateFunction(hook.fn, hook._type + ' hook', hook.location);
|
2021-07-02 15:49:05 -07:00
|
|
|
for (const modifier of parent._modifiers)
|
2021-08-09 13:26:33 -07:00
|
|
|
pool.validateFunction(modifier.fn, modifier.type + ' modifier', modifier.location);
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
2021-08-10 16:32:32 -07:00
|
|
|
|
|
|
|
pool.validateFunction(test.fn, 'Test', test.location);
|
|
|
|
this.testPools.set(test, pool);
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
2021-07-15 22:02:10 -07:00
|
|
|
return this.testPools.get(test)!;
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 14:54:18 -07:00
|
|
|
private _cloneEntries(from: Suite, to: Suite, repeatEachIndex: number, filter: (test: TestCase) => boolean): boolean {
|
2021-08-09 13:26:33 -07:00
|
|
|
for (const hook of from._allHooks) {
|
|
|
|
const clone = hook._clone();
|
|
|
|
clone._pool = this.buildPool(hook);
|
|
|
|
clone._projectIndex = this.index;
|
|
|
|
to._addAllHook(clone);
|
|
|
|
}
|
2021-07-16 15:23:50 -07:00
|
|
|
for (const entry of from._entries) {
|
2021-07-15 22:02:10 -07:00
|
|
|
if (entry instanceof Suite) {
|
2021-07-16 15:23:50 -07:00
|
|
|
const suite = entry._clone();
|
|
|
|
to._addSuite(suite);
|
|
|
|
if (!this._cloneEntries(entry, suite, repeatEachIndex, filter)) {
|
|
|
|
to._entries.pop();
|
|
|
|
to.suites.pop();
|
|
|
|
}
|
2021-07-15 22:02:10 -07:00
|
|
|
} else {
|
|
|
|
const pool = this.buildPool(entry);
|
|
|
|
const test = entry._clone();
|
|
|
|
test.retries = this.config.retries;
|
2021-08-09 12:33:16 -07:00
|
|
|
test._workerHash = `run${this.index}-${pool.digest}-repeat${repeatEachIndex}`;
|
2021-07-15 22:02:10 -07:00
|
|
|
test._id = `${entry._ordinalInFile}@${entry._requireFile}#run${this.index}-repeat${repeatEachIndex}`;
|
|
|
|
test._pool = pool;
|
2021-07-16 15:23:50 -07:00
|
|
|
test._repeatEachIndex = repeatEachIndex;
|
|
|
|
test._projectIndex = this.index;
|
|
|
|
to._addTest(test);
|
|
|
|
if (!filter(test)) {
|
|
|
|
to._entries.pop();
|
|
|
|
to.suites.pop();
|
|
|
|
}
|
2021-07-15 22:02:10 -07:00
|
|
|
}
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
2021-07-16 15:23:50 -07:00
|
|
|
return to._entries.length > 0;
|
|
|
|
}
|
|
|
|
|
2021-07-19 14:54:18 -07:00
|
|
|
cloneFileSuite(suite: Suite, repeatEachIndex: number, filter: (test: TestCase) => boolean): Suite | undefined {
|
2021-07-16 15:23:50 -07:00
|
|
|
const result = suite._clone();
|
|
|
|
return this._cloneEntries(suite, result, repeatEachIndex, filter) ? result : undefined;
|
2021-06-06 17:09:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private resolveFixtures(testType: TestTypeImpl): FixturesWithLocation[] {
|
|
|
|
return testType.fixtures.map(f => {
|
|
|
|
if (f instanceof DeclaredFixtures) {
|
|
|
|
const fixtures = this.defines.get(f.testType.test) || {};
|
|
|
|
return { fixtures, location: f.location };
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|