-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: extract pool builder, simplify project suite cloning (#20235)
- Loading branch information
1 parent
d950f5b
commit fdd62f3
Showing
10 changed files
with
187 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* 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 { FixturePool, isFixtureOption } from './fixtures'; | ||
import type { Suite, TestCase } from './test'; | ||
import type { TestTypeImpl } from './testType'; | ||
import type { Fixtures, FixturesWithLocation, FullProjectInternal } from './types'; | ||
|
||
export class PoolBuilder { | ||
private _project: FullProjectInternal; | ||
private _testTypePools = new Map<TestTypeImpl, FixturePool>(); | ||
|
||
constructor(project: FullProjectInternal) { | ||
this._project = project; | ||
} | ||
|
||
buildPools(suite: Suite, repeatEachIndex: number) { | ||
suite.forEachTest(test => { | ||
const pool = this._buildPoolForTest(test); | ||
test._workerHash = `run${this._project._id}-${pool.digest}-repeat${repeatEachIndex}`; | ||
test._pool = pool; | ||
}); | ||
} | ||
|
||
private _buildPoolForTest(test: TestCase): FixturePool { | ||
let pool = this._buildTestTypePool(test._testType); | ||
|
||
const parents: Suite[] = []; | ||
for (let parent: Suite | undefined = test.parent; parent; parent = parent.parent) | ||
parents.push(parent); | ||
parents.reverse(); | ||
|
||
for (const parent of parents) { | ||
if (parent._use.length) | ||
pool = new FixturePool(parent._use, pool, parent._type === 'describe'); | ||
for (const hook of parent._hooks) | ||
pool.validateFunction(hook.fn, hook.type + ' hook', hook.location); | ||
for (const modifier of parent._modifiers) | ||
pool.validateFunction(modifier.fn, modifier.type + ' modifier', modifier.location); | ||
} | ||
|
||
pool.validateFunction(test.fn, 'Test', test.location); | ||
return pool; | ||
} | ||
|
||
private _buildTestTypePool(testType: TestTypeImpl): FixturePool { | ||
if (!this._testTypePools.has(testType)) { | ||
const fixtures = this._applyConfigUseOptions(testType, this._project.use || {}); | ||
const pool = new FixturePool(fixtures); | ||
this._testTypePools.set(testType, pool); | ||
} | ||
return this._testTypePools.get(testType)!; | ||
} | ||
|
||
private _applyConfigUseOptions(testType: TestTypeImpl, configUse: Fixtures): FixturesWithLocation[] { | ||
const configKeys = new Set(Object.keys(configUse)); | ||
if (!configKeys.size) | ||
return testType.fixtures; | ||
const result: FixturesWithLocation[] = []; | ||
for (const f of testType.fixtures) { | ||
result.push(f); | ||
const optionsFromConfig: Fixtures = {}; | ||
for (const [key, value] of Object.entries(f.fixtures)) { | ||
if (isFixtureOption(value) && configKeys.has(key)) | ||
(optionsFromConfig as any)[key] = [(configUse as any)[key], value[1]]; | ||
} | ||
if (Object.entries(optionsFromConfig).length) { | ||
// Add config options immediately after original option definition, | ||
// so that any test.use() override it. | ||
result.push({ fixtures: optionsFromConfig, location: { file: `project#${this._project._id}`, line: 1, column: 1 }, fromConfig: true }); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* 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 path from 'path'; | ||
import { calculateSha1 } from 'playwright-core/lib/utils'; | ||
import type { Suite, TestCase } from './test'; | ||
import type { FullProjectInternal } from './types'; | ||
|
||
export function filterTests(suite: Suite, filter: (test: TestCase) => boolean): boolean { | ||
suite.suites = suite.suites.filter(child => filterTests(child, filter)); | ||
suite.tests = suite.tests.filter(filter); | ||
const entries = new Set([...suite.suites, ...suite.tests]); | ||
suite._entries = suite._entries.filter(e => entries.has(e)); // Preserve the order. | ||
return !!suite._entries.length; | ||
} | ||
|
||
export function buildFileSuiteForProject(project: FullProjectInternal, suite: Suite, repeatEachIndex: number): Suite { | ||
const relativeFile = path.relative(project.testDir, suite.location!.file).split(path.sep).join('/'); | ||
const fileId = calculateSha1(relativeFile).slice(0, 20); | ||
|
||
// Clone suite. | ||
const result = suite._deepClone(); | ||
result._fileId = fileId; | ||
|
||
// Assign test properties with project-specific values. | ||
result.forEachTest((test, suite) => { | ||
suite._fileId = fileId; | ||
const repeatEachIndexSuffix = repeatEachIndex ? ` (repeat:${repeatEachIndex})` : ''; | ||
// At the point of the query, suite is not yet attached to the project, so we only get file, describe and test titles. | ||
const testIdExpression = `[project=${project._id}]${test.titlePath().join('\x1e')}${repeatEachIndexSuffix}`; | ||
const testId = fileId + '-' + calculateSha1(testIdExpression).slice(0, 20); | ||
test.id = testId; | ||
test.repeatEachIndex = repeatEachIndex; | ||
test._projectId = project._id; | ||
test.retries = project.retries; | ||
for (let parentSuite: Suite | undefined = suite; parentSuite; parentSuite = parentSuite.parent) { | ||
if (parentSuite._retries !== undefined) { | ||
test.retries = parentSuite._retries; | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.