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';
|
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-03-03 16:49:19 +01:00
|
|
|
|
import { createFileMatcherFromArguments, createFileFiltersFromArguments, createTitleMatcher, errorWithFile, forceRegExp } 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-04-06 20:20:24 +02:00
|
|
|
|
import type { TestRun } from './tasks';
|
2023-01-27 21:44:15 +01:00
|
|
|
|
import { requireOrImport } from '../common/transform';
|
2023-02-07 00:52:14 +01:00
|
|
|
|
import { buildFileSuiteForProject, filterByFocusedLine, filterByTestIds, filterOnly, filterTestsRemoveEmptySuites } from '../common/suiteUtils';
|
2023-03-14 18:41:37 +01:00
|
|
|
|
import { createTestGroups, filterForShard, type TestGroup } from './testGroups';
|
2023-03-03 00:09:50 +01:00
|
|
|
|
import { dependenciesForTestFile } from '../common/compilationCache';
|
2023-04-04 04:49:01 +02:00
|
|
|
|
import { sourceMapSupport } from '../utilsBundle';
|
|
|
|
|
|
import type { RawSourceMap } from 'source-map';
|
2023-01-26 22:20:05 +01:00
|
|
|
|
|
2023-04-06 20:20:24 +02:00
|
|
|
|
export async function collectProjectsAndTestFiles(testRun: TestRun, additionalFileMatcher: Matcher | undefined) {
|
|
|
|
|
|
const config = testRun.config;
|
2023-03-15 21:40:58 +01:00
|
|
|
|
const fsCache = new Map();
|
|
|
|
|
|
const sourceMapCache = new Map();
|
2023-03-03 16:49:19 +01:00
|
|
|
|
const cliFileMatcher = config._internal.cliArgs.length ? createFileMatcherFromArguments(config._internal.cliArgs) : null;
|
|
|
|
|
|
|
2023-03-15 21:40:58 +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 filterProjects(config.projects, config._internal.cliProjectFilter)) {
|
|
|
|
|
|
const files = await collectFilesForProject(project, fsCache);
|
|
|
|
|
|
allFilesForProject.set(project, files);
|
|
|
|
|
|
}
|
2023-02-01 00:59:13 +01:00
|
|
|
|
|
2023-03-15 21:40:58 +01:00
|
|
|
|
// Filter files based on the file filters, eliminate the empty projects.
|
|
|
|
|
|
const filesToRunByProject = new Map<FullProjectInternal, string[]>();
|
|
|
|
|
|
for (const [project, files] of allFilesForProject) {
|
2023-04-04 04:49:01 +02:00
|
|
|
|
const matchedFiles = files.filter(file => {
|
|
|
|
|
|
const hasMatchingSources = sourceMapSources(file, sourceMapCache).some(source => {
|
|
|
|
|
|
if (additionalFileMatcher && !additionalFileMatcher(source))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
if (cliFileMatcher && !cliFileMatcher(source))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
});
|
|
|
|
|
|
return hasMatchingSources;
|
|
|
|
|
|
});
|
2023-03-15 21:40:58 +01:00
|
|
|
|
const filteredFiles = matchedFiles.filter(Boolean) as string[];
|
|
|
|
|
|
if (filteredFiles.length)
|
|
|
|
|
|
filesToRunByProject.set(project, filteredFiles);
|
|
|
|
|
|
}
|
2023-02-03 18:11:02 +01:00
|
|
|
|
|
2023-03-15 21:40:58 +01:00
|
|
|
|
// (Re-)add all files for dependent projects, disregard filters.
|
2023-04-06 20:20:24 +02:00
|
|
|
|
const projectClosure = buildProjectsClosure([...filesToRunByProject.keys()]);
|
|
|
|
|
|
for (const [project, type] of projectClosure) {
|
|
|
|
|
|
if (type === 'dependency') {
|
2023-03-14 18:41:37 +01:00
|
|
|
|
filesToRunByProject.delete(project);
|
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-04-06 20:20:24 +02:00
|
|
|
|
testRun.projects = [...filesToRunByProject.keys()];
|
|
|
|
|
|
testRun.projectFiles = filesToRunByProject;
|
|
|
|
|
|
testRun.projectType = projectClosure;
|
|
|
|
|
|
testRun.projectSuites = new Map();
|
2023-03-15 21:40:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-06 20:20:24 +02:00
|
|
|
|
export async function loadFileSuites(testRun: TestRun, mode: 'out-of-process' | 'in-process', errors: TestError[]) {
|
2023-03-15 21:40:58 +01:00
|
|
|
|
// Determine all files to load.
|
2023-04-06 20:20:24 +02:00
|
|
|
|
const config = testRun.config;
|
2023-03-15 21:40:58 +01:00
|
|
|
|
const allTestFiles = new Set<string>();
|
2023-04-06 20:20:24 +02:00
|
|
|
|
for (const files of testRun.projectFiles.values())
|
2023-03-15 21:40:58 +01:00
|
|
|
|
files.forEach(file => allTestFiles.add(file));
|
|
|
|
|
|
|
|
|
|
|
|
// Load test files.
|
|
|
|
|
|
const fileSuiteByFile = new Map<string, Suite>();
|
|
|
|
|
|
const loaderHost = mode === 'out-of-process' ? new OutOfProcessLoaderHost(config) : new InProcessLoaderHost(config);
|
|
|
|
|
|
for (const file of allTestFiles) {
|
|
|
|
|
|
const fileSuite = await loaderHost.loadTestFile(file, errors);
|
|
|
|
|
|
fileSuiteByFile.set(file, fileSuite);
|
|
|
|
|
|
errors.push(...createDuplicateTitlesErrors(config, fileSuite));
|
|
|
|
|
|
}
|
|
|
|
|
|
await loaderHost.stop();
|
|
|
|
|
|
|
|
|
|
|
|
// Check that no test file imports another test file.
|
|
|
|
|
|
// Loader must be stopped first, since it popuplates the dependency tree.
|
|
|
|
|
|
for (const file of allTestFiles) {
|
|
|
|
|
|
for (const dependency of dependenciesForTestFile(file)) {
|
|
|
|
|
|
if (allTestFiles.has(dependency)) {
|
|
|
|
|
|
const importer = path.relative(config.rootDir, file);
|
|
|
|
|
|
const importee = path.relative(config.rootDir, dependency);
|
|
|
|
|
|
errors.push({
|
|
|
|
|
|
message: `Error: test file "${importer}" should not import test file "${importee}"`,
|
|
|
|
|
|
location: { file, line: 1, column: 1 },
|
|
|
|
|
|
});
|
2023-03-03 00:09:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-02-01 21:33:42 +01:00
|
|
|
|
}
|
2023-01-26 22:20:05 +01:00
|
|
|
|
|
2023-03-15 21:40:58 +01:00
|
|
|
|
// Collect file suites for each project.
|
2023-04-06 20:20:24 +02:00
|
|
|
|
for (const [project, files] of testRun.projectFiles) {
|
2023-03-15 21:40:58 +01:00
|
|
|
|
const suites = files.map(file => fileSuiteByFile.get(file)).filter(Boolean) as Suite[];
|
2023-04-06 20:20:24 +02:00
|
|
|
|
testRun.projectSuites.set(project, suites);
|
2023-03-15 21:40:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-01-26 22:20:05 +01:00
|
|
|
|
|
2023-04-06 20:20:24 +02:00
|
|
|
|
export async function createRootSuite(testRun: TestRun, errors: TestError[], shouldFilterOnly: boolean): Promise<Suite> {
|
|
|
|
|
|
const config = testRun.config;
|
2023-03-15 21:40:58 +01:00
|
|
|
|
// Create root suite, where each child will be a project suite with cloned file suites inside it.
|
2023-02-01 00:59:13 +01:00
|
|
|
|
const rootSuite = new Suite('', 'root');
|
|
|
|
|
|
|
2023-03-15 21:40:58 +01:00
|
|
|
|
// First add top-level projects, so that we can filterOnly and shard just top-level.
|
|
|
|
|
|
{
|
|
|
|
|
|
// Interpret cli parameters.
|
|
|
|
|
|
const cliFileFilters = createFileFiltersFromArguments(config._internal.cliArgs);
|
|
|
|
|
|
const grepMatcher = config._internal.cliGrep ? createTitleMatcher(forceRegExp(config._internal.cliGrep)) : () => true;
|
|
|
|
|
|
const grepInvertMatcher = config._internal.cliGrepInvert ? createTitleMatcher(forceRegExp(config._internal.cliGrepInvert)) : () => false;
|
|
|
|
|
|
const cliTitleMatcher = (title: string) => !grepInvertMatcher(title) && grepMatcher(title);
|
|
|
|
|
|
|
|
|
|
|
|
// Clone file suites for top-level projects.
|
2023-04-06 20:20:24 +02:00
|
|
|
|
for (const [project, fileSuites] of testRun.projectSuites) {
|
|
|
|
|
|
if (testRun.projectType.get(project) === 'top-level')
|
2023-03-15 21:40:58 +01:00
|
|
|
|
rootSuite._addSuite(await createProjectSuite(fileSuites, project, { cliFileFilters, cliTitleMatcher, testIdMatcher: config._internal.testIdMatcher }));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
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-03-14 18:41:37 +01:00
|
|
|
|
// Filter only for top-level projects.
|
2023-02-13 20:13:30 +01:00
|
|
|
|
if (shouldFilterOnly)
|
|
|
|
|
|
filterOnly(rootSuite);
|
2023-01-26 22:20:05 +01:00
|
|
|
|
|
2023-03-14 18:41:37 +01:00
|
|
|
|
// Shard only the top-level projects.
|
|
|
|
|
|
if (config.shard) {
|
2023-03-17 01:11:15 +01:00
|
|
|
|
// Create test groups for top-level projects.
|
|
|
|
|
|
const testGroups: TestGroup[] = [];
|
|
|
|
|
|
for (const projectSuite of rootSuite.suites)
|
|
|
|
|
|
testGroups.push(...createTestGroups(projectSuite, config.workers));
|
|
|
|
|
|
|
|
|
|
|
|
// Shard test groups.
|
|
|
|
|
|
const testGroupsInThisShard = filterForShard(config.shard, testGroups);
|
|
|
|
|
|
const testsInThisShard = new Set<TestCase>();
|
|
|
|
|
|
for (const group of testGroupsInThisShard) {
|
2023-03-14 18:41:37 +01:00
|
|
|
|
for (const test of group.tests)
|
2023-03-17 01:11:15 +01:00
|
|
|
|
testsInThisShard.add(test);
|
2023-03-14 18:41:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-17 01:11:15 +01:00
|
|
|
|
// Update project suites, removing empty ones.
|
|
|
|
|
|
filterTestsRemoveEmptySuites(rootSuite, test => testsInThisShard.has(test));
|
2023-03-14 18:41:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-15 21:40:58 +01:00
|
|
|
|
// Now prepend dependency projects.
|
|
|
|
|
|
{
|
|
|
|
|
|
// Filtering only and sharding might have reduced the number of top-level projects.
|
|
|
|
|
|
// Build the project closure to only include dependencies that are still needed.
|
2023-04-06 20:20:24 +02:00
|
|
|
|
const projectClosure = new Map(buildProjectsClosure(rootSuite.suites.map(suite => suite.project() as FullProjectInternal)));
|
2023-03-15 21:40:58 +01:00
|
|
|
|
|
|
|
|
|
|
// Clone file suites for dependency projects.
|
2023-04-06 20:20:24 +02:00
|
|
|
|
for (const [project, fileSuites] of testRun.projectSuites) {
|
|
|
|
|
|
if (testRun.projectType.get(project) === 'dependency' && projectClosure.has(project))
|
2023-03-17 01:11:15 +01:00
|
|
|
|
rootSuite._prependSuite(await createProjectSuite(fileSuites, project, { cliFileFilters: [], cliTitleMatcher: undefined }));
|
2023-03-15 21:40:58 +01:00
|
|
|
|
}
|
2023-02-01 00:59:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-17 01:11:15 +01:00
|
|
|
|
return rootSuite;
|
2023-02-01 00:59:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-15 21:40:58 +01:00
|
|
|
|
async function createProjectSuite(fileSuites: Suite[], project: FullProjectInternal, options: { cliFileFilters: TestFileFilter[], cliTitleMatcher?: Matcher, testIdMatcher?: Matcher }): Promise<Suite> {
|
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';
|
2023-03-15 21:40:58 +01:00
|
|
|
|
for (const fileSuite of fileSuites) {
|
2023-02-01 00:59:13 +01:00
|
|
|
|
for (let repeatEachIndex = 0; repeatEachIndex < project.repeatEach; repeatEachIndex++) {
|
|
|
|
|
|
const builtSuite = buildFileSuiteForProject(project, fileSuite, repeatEachIndex);
|
|
|
|
|
|
projectSuite._addSuite(builtSuite);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-02-07 00:52:14 +01:00
|
|
|
|
|
2023-02-03 18:11:02 +01:00
|
|
|
|
filterByFocusedLine(projectSuite, options.cliFileFilters);
|
2023-02-07 00:52:14 +01:00
|
|
|
|
filterByTestIds(projectSuite, options.testIdMatcher);
|
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
|
|
|
|
};
|
|
|
|
|
|
|
2023-03-02 22:32:23 +01:00
|
|
|
|
filterTestsRemoveEmptySuites(projectSuite, titleMatcher);
|
|
|
|
|
|
return projectSuite;
|
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-03-15 21:40:58 +01:00
|
|
|
|
function createDuplicateTitlesErrors(config: FullConfigInternal, fileSuite: Suite): TestError[] {
|
2023-01-26 22:20:05 +01:00
|
|
|
|
const errors: TestError[] = [];
|
2023-03-15 21:40:58 +01:00
|
|
|
|
const testsByFullTitle = new Map<string, TestCase>();
|
|
|
|
|
|
for (const test of fileSuite.allTests()) {
|
|
|
|
|
|
const fullTitle = test.titlePath().slice(1).join(' › ');
|
|
|
|
|
|
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);
|
2023-01-26 22:20:05 +01:00
|
|
|
|
}
|
2023-03-15 21:40:58 +01:00
|
|
|
|
testsByFullTitle.set(fullTitle, test);
|
2023-01-26 22:20:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
2023-03-03 16:49:19 +01:00
|
|
|
|
|
2023-04-04 04:49:01 +02:00
|
|
|
|
function sourceMapSources(file: string, cache: Map<string, string[]>): string[] {
|
|
|
|
|
|
let sources = [file];
|
2023-03-03 16:49:19 +01:00
|
|
|
|
if (!file.endsWith('.js'))
|
2023-04-04 04:49:01 +02:00
|
|
|
|
return sources;
|
2023-03-03 16:49:19 +01:00
|
|
|
|
if (cache.has(file))
|
|
|
|
|
|
return cache.get(file)!;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2023-04-04 04:49:01 +02:00
|
|
|
|
const sourceMap = sourceMapSupport.retrieveSourceMap(file);
|
|
|
|
|
|
const sourceMapData: RawSourceMap | undefined = typeof sourceMap?.map === 'string' ? JSON.parse(sourceMap.map) : sourceMap?.map;
|
|
|
|
|
|
if (sourceMapData?.sources)
|
|
|
|
|
|
sources = sourceMapData.sources.map(source => path.resolve(path.dirname(file), source));
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
cache.set(file, sources);
|
|
|
|
|
|
return sources;
|
2023-03-03 16:49:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|