2023-01-26 22:20:05 +01: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 path from 'path';
|
2023-01-27 21:44:15 +01:00
|
|
|
|
import type { Reporter, TestError } from '../../types/testReporter';
|
2023-02-01 21:33:42 +01:00
|
|
|
|
import { InProcessLoaderHost, OutOfProcessLoaderHost } from './loaderHost';
|
|
|
|
|
|
import type { LoaderHost } from './loaderHost';
|
2023-02-01 00:59:13 +01:00
|
|
|
|
import { Suite } from '../common/test';
|
|
|
|
|
|
import type { TestCase } from '../common/test';
|
2023-01-30 23:34:48 +01:00
|
|
|
|
import type { FullConfigInternal, FullProjectInternal } from '../common/types';
|
2023-02-03 18:11:02 +01:00
|
|
|
|
import { createTitleMatcher, errorWithFile } from '../util';
|
2023-01-26 22:20:05 +01:00
|
|
|
|
import type { Matcher, TestFileFilter } from '../util';
|
2023-02-02 00:25:26 +01:00
|
|
|
|
import { buildProjectsClosure, collectFilesForProject, filterProjects } from './projectUtils';
|
2023-01-27 21:44:15 +01:00
|
|
|
|
import { requireOrImport } from '../common/transform';
|
2023-02-01 00:59:13 +01:00
|
|
|
|
import { buildFileSuiteForProject, filterByFocusedLine, filterOnly, filterTestsRemoveEmptySuites } from '../common/suiteUtils';
|
|
|
|
|
|
import { filterForShard } from './testGroups';
|
2023-01-26 22:20:05 +01:00
|
|
|
|
|
2023-02-03 18:11:02 +01:00
|
|
|
|
export async function loadAllTests(config: FullConfigInternal, projectsToIgnore: Set<FullProjectInternal>, fileMatcher: Matcher, errors: TestError[]): Promise<Suite> {
|
|
|
|
|
|
const projects = filterProjects(config.projects, config._internal.cliProjectFilter);
|
2023-02-01 00:59:13 +01:00
|
|
|
|
|
|
|
|
|
|
let filesToRunByProject = new Map<FullProjectInternal, string[]>();
|
|
|
|
|
|
let topLevelProjects: FullProjectInternal[];
|
|
|
|
|
|
let dependencyProjects: FullProjectInternal[];
|
2023-02-01 21:33:42 +01:00
|
|
|
|
// Collect files, categorize top level and dependency projects.
|
2023-02-01 00:59:13 +01:00
|
|
|
|
{
|
2023-02-01 21:33:42 +01:00
|
|
|
|
const fsCache = new Map();
|
|
|
|
|
|
|
2023-02-01 00:59:13 +01:00
|
|
|
|
// First collect all files for the projects in the command line, don't apply any file filters.
|
|
|
|
|
|
const allFilesForProject = new Map<FullProjectInternal, string[]>();
|
|
|
|
|
|
for (const project of projects) {
|
2023-02-03 18:11:02 +01:00
|
|
|
|
if (projectsToIgnore.has(project))
|
|
|
|
|
|
continue;
|
2023-02-01 21:33:42 +01:00
|
|
|
|
const files = await collectFilesForProject(project, fsCache);
|
2023-02-01 00:59:13 +01:00
|
|
|
|
allFilesForProject.set(project, files);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Filter files based on the file filters, eliminate the empty projects.
|
|
|
|
|
|
for (const [project, files] of allFilesForProject) {
|
2023-02-03 18:11:02 +01:00
|
|
|
|
const filteredFiles = files.filter(fileMatcher);
|
2023-02-01 00:59:13 +01:00
|
|
|
|
if (filteredFiles.length)
|
|
|
|
|
|
filesToRunByProject.set(project, filteredFiles);
|
|
|
|
|
|
}
|
2023-02-02 00:25:26 +01:00
|
|
|
|
|
|
|
|
|
|
const projectClosure = buildProjectsClosure([...filesToRunByProject.keys()]);
|
|
|
|
|
|
// Remove files for dependency projects, they'll be added back later.
|
|
|
|
|
|
for (const project of projectClosure.filter(p => p._internal.type === 'dependency'))
|
2023-02-01 00:59:13 +01:00
|
|
|
|
filesToRunByProject.delete(project);
|
|
|
|
|
|
|
|
|
|
|
|
// Shard only the top-level projects.
|
|
|
|
|
|
if (config.shard)
|
|
|
|
|
|
filesToRunByProject = filterForShard(config.shard, filesToRunByProject);
|
|
|
|
|
|
|
|
|
|
|
|
// Re-build the closure, project set might have changed.
|
2023-02-02 00:25:26 +01:00
|
|
|
|
const filteredProjectClosure = buildProjectsClosure([...filesToRunByProject.keys()]);
|
|
|
|
|
|
topLevelProjects = filteredProjectClosure.filter(p => p._internal.type === 'top-level');
|
|
|
|
|
|
dependencyProjects = filteredProjectClosure.filter(p => p._internal.type === 'dependency');
|
2023-02-01 00:59:13 +01:00
|
|
|
|
|
2023-02-03 18:11:02 +01:00
|
|
|
|
topLevelProjects = topLevelProjects.filter(p => !projectsToIgnore.has(p));
|
|
|
|
|
|
dependencyProjects = dependencyProjects.filter(p => !projectsToIgnore.has(p));
|
|
|
|
|
|
|
2023-02-01 00:59:13 +01:00
|
|
|
|
// (Re-)add all files for dependent projects, disregard filters.
|
|
|
|
|
|
for (const project of dependencyProjects) {
|
2023-02-01 21:33:42 +01:00
|
|
|
|
const files = allFilesForProject.get(project) || await collectFilesForProject(project, fsCache);
|
2023-02-01 00:59:13 +01:00
|
|
|
|
filesToRunByProject.set(project, files);
|
|
|
|
|
|
}
|
2023-01-30 23:34:48 +01:00
|
|
|
|
}
|
2023-01-26 22:20:05 +01:00
|
|
|
|
|
2023-02-01 00:59:13 +01:00
|
|
|
|
// Load all test files and create a preprocessed root. Child suites are files there.
|
2023-02-01 21:33:42 +01:00
|
|
|
|
const fileSuits: Suite[] = [];
|
|
|
|
|
|
{
|
|
|
|
|
|
const loaderHost: LoaderHost = process.env.PW_TEST_OOP_LOADER ? new OutOfProcessLoaderHost(config) : new InProcessLoaderHost(config);
|
|
|
|
|
|
const allTestFiles = new Set<string>();
|
|
|
|
|
|
for (const files of filesToRunByProject.values())
|
|
|
|
|
|
files.forEach(file => allTestFiles.add(file));
|
|
|
|
|
|
for (const file of allTestFiles) {
|
|
|
|
|
|
const fileSuite = await loaderHost.loadTestFile(file, errors);
|
|
|
|
|
|
fileSuits.push(fileSuite);
|
|
|
|
|
|
}
|
|
|
|
|
|
await loaderHost.stop();
|
|
|
|
|
|
}
|
2023-01-26 22:20:05 +01:00
|
|
|
|
|
|
|
|
|
|
// Complain about duplicate titles.
|
2023-02-01 21:33:42 +01:00
|
|
|
|
errors.push(...createDuplicateTitlesErrors(config, fileSuits));
|
2023-01-26 22:20:05 +01:00
|
|
|
|
|
2023-02-01 00:59:13 +01:00
|
|
|
|
// Create root suites with clones for the projects.
|
|
|
|
|
|
const rootSuite = new Suite('', 'root');
|
|
|
|
|
|
|
|
|
|
|
|
// First iterate leaf projects to focus only, then add all other projects.
|
|
|
|
|
|
for (const project of topLevelProjects) {
|
2023-02-02 00:25:26 +01:00
|
|
|
|
const projectSuite = await createProjectSuite(fileSuits, project, config._internal, filesToRunByProject.get(project)!);
|
2023-02-01 00:59:13 +01:00
|
|
|
|
if (projectSuite)
|
|
|
|
|
|
rootSuite._addSuite(projectSuite);
|
|
|
|
|
|
}
|
2023-01-26 22:20:05 +01:00
|
|
|
|
|
|
|
|
|
|
// Complain about only.
|
|
|
|
|
|
if (config.forbidOnly) {
|
2023-02-01 00:59:13 +01:00
|
|
|
|
const onlyTestsAndSuites = rootSuite._getOnlyItems();
|
2023-01-26 22:20:05 +01:00
|
|
|
|
if (onlyTestsAndSuites.length > 0)
|
|
|
|
|
|
errors.push(...createForbidOnlyErrors(onlyTestsAndSuites));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-01 00:59:13 +01:00
|
|
|
|
// Filter only for leaf projects.
|
|
|
|
|
|
filterOnly(rootSuite);
|
2023-01-26 22:20:05 +01:00
|
|
|
|
|
2023-02-01 00:59:13 +01:00
|
|
|
|
// Prepend the projects that are dependencies.
|
|
|
|
|
|
for (const project of dependencyProjects) {
|
2023-02-03 18:11:02 +01:00
|
|
|
|
const projectSuite = await createProjectSuite(fileSuits, project, { cliFileFilters: [], cliTitleMatcher: undefined }, filesToRunByProject.get(project)!);
|
2023-02-01 00:59:13 +01:00
|
|
|
|
if (projectSuite)
|
|
|
|
|
|
rootSuite._prependSuite(projectSuite);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return rootSuite;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-03 18:11:02 +01:00
|
|
|
|
async function createProjectSuite(fileSuits: Suite[], project: FullProjectInternal, options: { cliFileFilters: TestFileFilter[], cliTitleMatcher?: Matcher }, files: string[]): Promise<Suite | null> {
|
2023-02-01 21:33:42 +01:00
|
|
|
|
const fileSuitesMap = new Map<string, Suite>();
|
|
|
|
|
|
for (const fileSuite of fileSuits)
|
|
|
|
|
|
fileSuitesMap.set(fileSuite._requireFile, fileSuite);
|
2023-02-01 00:59:13 +01:00
|
|
|
|
|
|
|
|
|
|
const projectSuite = new Suite(project.name, 'project');
|
|
|
|
|
|
projectSuite._projectConfig = project;
|
2023-02-02 00:25:26 +01:00
|
|
|
|
if (project._internal.fullyParallel)
|
2023-02-01 00:59:13 +01:00
|
|
|
|
projectSuite._parallelMode = 'parallel';
|
|
|
|
|
|
for (const file of files) {
|
2023-02-01 21:33:42 +01:00
|
|
|
|
const fileSuite = fileSuitesMap.get(file);
|
2023-02-01 00:59:13 +01:00
|
|
|
|
if (!fileSuite)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
for (let repeatEachIndex = 0; repeatEachIndex < project.repeatEach; repeatEachIndex++) {
|
|
|
|
|
|
const builtSuite = buildFileSuiteForProject(project, fileSuite, repeatEachIndex);
|
|
|
|
|
|
projectSuite._addSuite(builtSuite);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Filter tests to respect line/column filter.
|
2023-02-03 18:11:02 +01:00
|
|
|
|
filterByFocusedLine(projectSuite, options.cliFileFilters);
|
2023-02-01 00:59:13 +01:00
|
|
|
|
|
|
|
|
|
|
const grepMatcher = createTitleMatcher(project.grep);
|
|
|
|
|
|
const grepInvertMatcher = project.grepInvert ? createTitleMatcher(project.grepInvert) : null;
|
|
|
|
|
|
|
|
|
|
|
|
const titleMatcher = (test: TestCase) => {
|
|
|
|
|
|
const grepTitle = test.titlePath().join(' ');
|
|
|
|
|
|
if (grepInvertMatcher?.(grepTitle))
|
|
|
|
|
|
return false;
|
2023-02-03 18:11:02 +01:00
|
|
|
|
return grepMatcher(grepTitle) && (!options.cliTitleMatcher || options.cliTitleMatcher(grepTitle));
|
2023-02-01 00:59:13 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (filterTestsRemoveEmptySuites(projectSuite, titleMatcher))
|
|
|
|
|
|
return projectSuite;
|
|
|
|
|
|
return null;
|
2023-01-26 22:20:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function createForbidOnlyErrors(onlyTestsAndSuites: (TestCase | Suite)[]): TestError[] {
|
|
|
|
|
|
const errors: TestError[] = [];
|
|
|
|
|
|
for (const testOrSuite of onlyTestsAndSuites) {
|
|
|
|
|
|
// Skip root and file.
|
|
|
|
|
|
const title = testOrSuite.titlePath().slice(2).join(' ');
|
|
|
|
|
|
const error: TestError = {
|
|
|
|
|
|
message: `Error: focused item found in the --forbid-only mode: "${title}"`,
|
|
|
|
|
|
location: testOrSuite.location!,
|
|
|
|
|
|
};
|
|
|
|
|
|
errors.push(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
return errors;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-01 21:33:42 +01:00
|
|
|
|
function createDuplicateTitlesErrors(config: FullConfigInternal, fileSuites: Suite[]): TestError[] {
|
2023-01-26 22:20:05 +01:00
|
|
|
|
const errors: TestError[] = [];
|
2023-02-01 21:33:42 +01:00
|
|
|
|
for (const fileSuite of fileSuites) {
|
2023-01-26 22:20:05 +01:00
|
|
|
|
const testsByFullTitle = new Map<string, TestCase>();
|
|
|
|
|
|
for (const test of fileSuite.allTests()) {
|
2023-02-01 21:33:42 +01:00
|
|
|
|
const fullTitle = test.titlePath().slice(1).join(' › ');
|
2023-01-26 22:20:05 +01:00
|
|
|
|
const existingTest = testsByFullTitle.get(fullTitle);
|
|
|
|
|
|
if (existingTest) {
|
|
|
|
|
|
const error: TestError = {
|
|
|
|
|
|
message: `Error: duplicate test title "${fullTitle}", first declared in ${buildItemLocation(config.rootDir, existingTest)}`,
|
|
|
|
|
|
location: test.location,
|
|
|
|
|
|
};
|
|
|
|
|
|
errors.push(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
testsByFullTitle.set(fullTitle, test);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return errors;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function buildItemLocation(rootDir: string, testOrSuite: Suite | TestCase) {
|
|
|
|
|
|
if (!testOrSuite.location)
|
|
|
|
|
|
return '';
|
|
|
|
|
|
return `${path.relative(rootDir, testOrSuite.location.file)}:${testOrSuite.location.line}`;
|
|
|
|
|
|
}
|
2023-01-27 21:44:15 +01:00
|
|
|
|
|
|
|
|
|
|
async function requireOrImportDefaultFunction(file: string, expectConstructor: boolean) {
|
|
|
|
|
|
let func = await requireOrImport(file);
|
|
|
|
|
|
if (func && typeof func === 'object' && ('default' in func))
|
|
|
|
|
|
func = func['default'];
|
|
|
|
|
|
if (typeof func !== 'function')
|
|
|
|
|
|
throw errorWithFile(file, `file must export a single ${expectConstructor ? 'class' : 'function'}.`);
|
|
|
|
|
|
return func;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function loadGlobalHook(config: FullConfigInternal, file: string): Promise<(config: FullConfigInternal) => any> {
|
|
|
|
|
|
return requireOrImportDefaultFunction(path.resolve(config.rootDir, file), false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function loadReporter(config: FullConfigInternal, file: string): Promise<new (arg?: any) => Reporter> {
|
|
|
|
|
|
return requireOrImportDefaultFunction(path.resolve(config.rootDir, file), true);
|
|
|
|
|
|
}
|