2023-03-01 15:27:23 -08: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.
|
|
|
|
*/
|
|
|
|
|
2023-03-04 15:05:41 -08:00
|
|
|
import { showTraceViewer } from 'playwright-core/lib/server';
|
2023-03-01 15:27:23 -08:00
|
|
|
import type { Page } from 'playwright-core/lib/server/page';
|
2023-03-12 15:18:47 -07:00
|
|
|
import { isUnderTest, ManualPromise } from 'playwright-core/lib/utils';
|
2023-03-04 15:05:41 -08:00
|
|
|
import type { FullResult } from '../../reporter';
|
2023-03-19 14:50:09 -07:00
|
|
|
import { clearCompilationCache, collectAffectedTestFiles, dependenciesForTestFile } from '../common/compilationCache';
|
2023-03-01 15:27:23 -08:00
|
|
|
import type { FullConfigInternal } from '../common/types';
|
|
|
|
import { Multiplexer } from '../reporters/multiplexer';
|
|
|
|
import { TeleReporterEmitter } from '../reporters/teleEmitter';
|
|
|
|
import { createReporter } from './reporters';
|
2023-03-04 15:05:41 -08:00
|
|
|
import type { TaskRunnerState } from './tasks';
|
|
|
|
import { createTaskRunnerForList, createTaskRunnerForWatch, createTaskRunnerForWatchSetup } from './tasks';
|
|
|
|
import { chokidar } from '../utilsBundle';
|
|
|
|
import type { FSWatcher } from 'chokidar';
|
2023-03-07 17:20:41 -08:00
|
|
|
import { open } from '../utilsBundle';
|
2023-03-01 15:27:23 -08:00
|
|
|
|
2023-03-04 15:05:41 -08:00
|
|
|
class UIMode {
|
|
|
|
private _config: FullConfigInternal;
|
|
|
|
private _page!: Page;
|
|
|
|
private _testRun: { run: Promise<FullResult['status']>, stop: ManualPromise<void> } | undefined;
|
|
|
|
globalCleanup: (() => Promise<FullResult['status']>) | undefined;
|
2023-03-19 14:50:09 -07:00
|
|
|
private _testWatcher: { watcher: FSWatcher, watchedFiles: string[], collector: Set<string>, timer?: NodeJS.Timeout } | undefined;
|
2023-03-07 14:24:50 -08:00
|
|
|
private _originalStderr: (buffer: string | Uint8Array) => void;
|
2023-03-04 15:05:41 -08:00
|
|
|
|
|
|
|
constructor(config: FullConfigInternal) {
|
|
|
|
this._config = config;
|
2023-03-16 18:17:07 -07:00
|
|
|
process.env.PW_LIVE_TRACE_STACKS = '1';
|
2023-03-09 13:03:01 -08:00
|
|
|
config._internal.configCLIOverrides.forbidOnly = false;
|
|
|
|
config._internal.configCLIOverrides.globalTimeout = 0;
|
|
|
|
config._internal.configCLIOverrides.repeatEach = 0;
|
|
|
|
config._internal.configCLIOverrides.shard = undefined;
|
|
|
|
config._internal.configCLIOverrides.updateSnapshots = undefined;
|
|
|
|
config._internal.listOnly = false;
|
2023-03-04 15:05:41 -08:00
|
|
|
config._internal.passWithNoTests = true;
|
2023-03-13 22:19:31 -07:00
|
|
|
for (const project of config.projects)
|
|
|
|
project._internal.deps = [];
|
2023-03-09 13:03:01 -08:00
|
|
|
|
2023-03-04 15:05:41 -08:00
|
|
|
for (const p of config.projects)
|
|
|
|
p.retries = 0;
|
|
|
|
config._internal.configCLIOverrides.use = config._internal.configCLIOverrides.use || {};
|
2023-03-10 17:01:30 -08:00
|
|
|
config._internal.configCLIOverrides.use.trace = { mode: 'on', sources: false };
|
2023-03-07 20:34:57 -08:00
|
|
|
|
2023-03-07 14:24:50 -08:00
|
|
|
this._originalStderr = process.stderr.write.bind(process.stderr);
|
2023-03-08 19:50:32 -08:00
|
|
|
this._installGlobalWatcher();
|
2023-03-07 20:34:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private _installGlobalWatcher(): FSWatcher {
|
|
|
|
const projectDirs = new Set<string>();
|
|
|
|
for (const p of this._config.projects)
|
|
|
|
projectDirs.add(p.testDir);
|
|
|
|
let coalescingTimer: NodeJS.Timeout | undefined;
|
|
|
|
const watcher = chokidar.watch([...projectDirs], { ignoreInitial: true, persistent: true }).on('all', async event => {
|
2023-03-13 12:14:51 -07:00
|
|
|
if (event !== 'add' && event !== 'change' && event !== 'unlink')
|
2023-03-07 20:34:57 -08:00
|
|
|
return;
|
|
|
|
if (coalescingTimer)
|
|
|
|
clearTimeout(coalescingTimer);
|
|
|
|
coalescingTimer = setTimeout(() => {
|
|
|
|
this._dispatchEvent({ method: 'listChanged' });
|
|
|
|
}, 200);
|
|
|
|
});
|
|
|
|
return watcher;
|
2023-03-04 15:05:41 -08:00
|
|
|
}
|
2023-03-01 15:27:23 -08:00
|
|
|
|
2023-03-04 15:05:41 -08:00
|
|
|
async runGlobalSetup(): Promise<FullResult['status']> {
|
|
|
|
const reporter = await createReporter(this._config, 'watch');
|
|
|
|
const taskRunner = createTaskRunnerForWatchSetup(this._config, reporter);
|
|
|
|
reporter.onConfigure(this._config);
|
2023-03-01 15:27:23 -08:00
|
|
|
const context: TaskRunnerState = {
|
2023-03-04 15:05:41 -08:00
|
|
|
config: this._config,
|
2023-03-01 15:27:23 -08:00
|
|
|
reporter,
|
|
|
|
phases: [],
|
|
|
|
};
|
|
|
|
const { status, cleanup: globalCleanup } = await taskRunner.runDeferCleanup(context, 0);
|
2023-03-04 15:05:41 -08:00
|
|
|
if (status !== 'passed') {
|
|
|
|
await globalCleanup();
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
this.globalCleanup = globalCleanup;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
async showUI() {
|
2023-03-12 15:18:47 -07:00
|
|
|
this._page = await showTraceViewer([], 'chromium', { app: 'watch.html', headless: isUnderTest() && process.env.PWTEST_HEADED_FOR_TEST !== '1' });
|
2023-03-20 13:45:35 -07:00
|
|
|
if (!process.env.PWTEST_DEBUG) {
|
|
|
|
process.stdout.write = (chunk: string | Buffer) => {
|
|
|
|
this._dispatchEvent({ method: 'stdio', params: chunkToPayload('stdout', chunk) });
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
process.stderr.write = (chunk: string | Buffer) => {
|
|
|
|
this._dispatchEvent({ method: 'stdio', params: chunkToPayload('stderr', chunk) });
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
2023-03-04 15:05:41 -08:00
|
|
|
const exitPromise = new ManualPromise();
|
|
|
|
this._page.on('close', () => exitPromise.resolve());
|
2023-03-07 20:34:57 -08:00
|
|
|
let queue = Promise.resolve();
|
2023-03-04 15:05:41 -08:00
|
|
|
this._page.exposeBinding('sendMessage', false, async (source, data) => {
|
|
|
|
const { method, params }: { method: string, params: any } = data;
|
2023-03-07 20:34:57 -08:00
|
|
|
if (method === 'exit') {
|
|
|
|
exitPromise.resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (method === 'watch') {
|
2023-03-12 10:50:21 -07:00
|
|
|
this._watchFiles(params.fileNames);
|
2023-03-07 20:34:57 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (method === 'open' && params.location) {
|
2023-03-09 08:04:02 -08:00
|
|
|
open('vscode://file/' + params.location).catch(e => this._originalStderr(String(e)));
|
2023-03-07 20:34:57 -08:00
|
|
|
return;
|
|
|
|
}
|
2023-03-07 14:24:50 -08:00
|
|
|
if (method === 'resizeTerminal') {
|
|
|
|
process.stdout.columns = params.cols;
|
|
|
|
process.stdout.rows = params.rows;
|
|
|
|
process.stderr.columns = params.cols;
|
|
|
|
process.stderr.columns = params.rows;
|
2023-03-07 20:34:57 -08:00
|
|
|
return;
|
2023-03-07 14:24:50 -08:00
|
|
|
}
|
2023-03-07 20:34:57 -08:00
|
|
|
if (method === 'stop') {
|
|
|
|
this._stopTests();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
queue = queue.then(() => this._queueListOrRun(method, params));
|
|
|
|
await queue;
|
2023-03-04 15:05:41 -08:00
|
|
|
});
|
|
|
|
await exitPromise;
|
|
|
|
}
|
|
|
|
|
2023-03-07 20:34:57 -08:00
|
|
|
private async _queueListOrRun(method: string, params: any) {
|
|
|
|
if (method === 'list')
|
|
|
|
await this._listTests();
|
|
|
|
if (method === 'run')
|
|
|
|
await this._runTests(params.testIds);
|
|
|
|
}
|
|
|
|
|
2023-03-04 15:05:41 -08:00
|
|
|
private _dispatchEvent(message: any) {
|
|
|
|
// eslint-disable-next-line no-console
|
2023-03-07 14:24:50 -08:00
|
|
|
this._page.mainFrame().evaluateExpression(dispatchFuncSource, true, message).catch(e => this._originalStderr(String(e)));
|
2023-03-01 15:27:23 -08:00
|
|
|
}
|
|
|
|
|
2023-03-04 15:05:41 -08:00
|
|
|
private async _listTests() {
|
|
|
|
const listReporter = new TeleReporterEmitter(e => this._dispatchEvent(e));
|
2023-03-01 15:27:23 -08:00
|
|
|
const reporter = new Multiplexer([listReporter]);
|
2023-03-07 20:34:57 -08:00
|
|
|
this._config._internal.listOnly = true;
|
|
|
|
this._config._internal.testIdMatcher = undefined;
|
|
|
|
const taskRunner = createTaskRunnerForList(this._config, reporter, 'out-of-process');
|
2023-03-04 15:05:41 -08:00
|
|
|
const context: TaskRunnerState = { config: this._config, reporter, phases: [] };
|
2023-03-07 20:34:57 -08:00
|
|
|
clearCompilationCache();
|
2023-03-04 15:05:41 -08:00
|
|
|
reporter.onConfigure(this._config);
|
2023-03-21 12:03:26 -07:00
|
|
|
const status = await taskRunner.run(context, 0);
|
|
|
|
reporter.onExit({ status });
|
2023-03-01 15:27:23 -08:00
|
|
|
}
|
|
|
|
|
2023-03-04 15:05:41 -08:00
|
|
|
private async _runTests(testIds: string[]) {
|
|
|
|
await this._stopTests();
|
2023-03-01 15:27:23 -08:00
|
|
|
|
2023-03-04 15:05:41 -08:00
|
|
|
const testIdSet = testIds ? new Set<string>(testIds) : null;
|
2023-03-07 20:34:57 -08:00
|
|
|
this._config._internal.listOnly = false;
|
2023-03-04 15:05:41 -08:00
|
|
|
this._config._internal.testIdMatcher = id => !testIdSet || testIdSet.has(id);
|
2023-03-01 15:27:23 -08:00
|
|
|
|
2023-03-04 15:05:41 -08:00
|
|
|
const runReporter = new TeleReporterEmitter(e => this._dispatchEvent(e));
|
2023-03-09 08:04:02 -08:00
|
|
|
const reporter = await createReporter(this._config, 'ui', [runReporter]);
|
2023-03-04 15:05:41 -08:00
|
|
|
const taskRunner = createTaskRunnerForWatch(this._config, reporter);
|
|
|
|
const context: TaskRunnerState = { config: this._config, reporter, phases: [] };
|
|
|
|
clearCompilationCache();
|
|
|
|
reporter.onConfigure(this._config);
|
|
|
|
const stop = new ManualPromise();
|
|
|
|
const run = taskRunner.run(context, 0, stop).then(async status => {
|
|
|
|
await reporter.onExit({ status });
|
2023-03-04 16:28:30 -08:00
|
|
|
this._testRun = undefined;
|
2023-03-05 13:46:21 -08:00
|
|
|
this._config._internal.testIdMatcher = undefined;
|
2023-03-04 15:05:41 -08:00
|
|
|
return status;
|
|
|
|
});
|
|
|
|
this._testRun = { run, stop };
|
|
|
|
await run;
|
|
|
|
}
|
|
|
|
|
2023-03-12 10:50:21 -07:00
|
|
|
private async _watchFiles(fileNames: string[]) {
|
|
|
|
const files = new Set<string>();
|
|
|
|
for (const fileName of fileNames) {
|
|
|
|
files.add(fileName);
|
|
|
|
dependenciesForTestFile(fileName).forEach(file => files.add(file));
|
|
|
|
}
|
2023-03-19 14:50:09 -07:00
|
|
|
const watchedFiles = [...files].sort();
|
2023-03-20 13:45:35 -07:00
|
|
|
if (this._testWatcher && JSON.stringify(this._testWatcher.watchedFiles) === JSON.stringify(watchedFiles))
|
2023-03-19 14:50:09 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (this._testWatcher) {
|
|
|
|
if (this._testWatcher.collector.size)
|
|
|
|
this._dispatchEvent({ method: 'filesChanged', params: { fileNames: [...this._testWatcher.collector] } });
|
|
|
|
clearTimeout(this._testWatcher.timer);
|
|
|
|
this._testWatcher.watcher.close().then(() => {});
|
|
|
|
this._testWatcher = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!watchedFiles.length)
|
|
|
|
return;
|
2023-03-12 10:50:21 -07:00
|
|
|
|
2023-03-19 14:50:09 -07:00
|
|
|
const collector = new Set<string>();
|
|
|
|
const watcher = chokidar.watch(watchedFiles, { ignoreInitial: true }).on('all', async (event, file) => {
|
2023-03-04 15:05:41 -08:00
|
|
|
if (event !== 'add' && event !== 'change')
|
|
|
|
return;
|
2023-03-19 14:50:09 -07:00
|
|
|
collectAffectedTestFiles(file, collector);
|
|
|
|
if (this._testWatcher!.timer)
|
|
|
|
clearTimeout(this._testWatcher!.timer);
|
|
|
|
this._testWatcher!.timer = setTimeout(() => {
|
|
|
|
const fileNames = [...collector];
|
|
|
|
collector.clear();
|
|
|
|
this._dispatchEvent({ method: 'filesChanged', params: { fileNames } });
|
|
|
|
}, 250);
|
2023-03-01 15:27:23 -08:00
|
|
|
});
|
2023-03-19 14:50:09 -07:00
|
|
|
this._testWatcher = { watcher, watchedFiles, collector };
|
2023-03-01 15:27:23 -08:00
|
|
|
}
|
|
|
|
|
2023-03-04 15:05:41 -08:00
|
|
|
private async _stopTests() {
|
|
|
|
this._testRun?.stop?.resolve();
|
|
|
|
await this._testRun?.run;
|
2023-03-01 15:27:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-04 15:05:41 -08:00
|
|
|
const dispatchFuncSource = String((message: any) => {
|
|
|
|
(window as any).dispatch(message);
|
|
|
|
});
|
2023-03-01 15:27:23 -08:00
|
|
|
|
2023-03-04 15:05:41 -08:00
|
|
|
export async function runUIMode(config: FullConfigInternal): Promise<FullResult['status']> {
|
|
|
|
const uiMode = new UIMode(config);
|
|
|
|
const status = await uiMode.runGlobalSetup();
|
|
|
|
if (status !== 'passed')
|
|
|
|
return status;
|
|
|
|
await uiMode.showUI();
|
|
|
|
return await uiMode.globalCleanup?.() || 'passed';
|
2023-03-01 15:27:23 -08:00
|
|
|
}
|
2023-03-07 14:24:50 -08:00
|
|
|
|
|
|
|
type StdioPayload = {
|
|
|
|
type: 'stdout' | 'stderr';
|
|
|
|
text?: string;
|
|
|
|
buffer?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
function chunkToPayload(type: 'stdout' | 'stderr', chunk: Buffer | string): StdioPayload {
|
|
|
|
if (chunk instanceof Buffer)
|
|
|
|
return { type, buffer: chunk.toString('base64') };
|
|
|
|
return { type, text: chunk };
|
|
|
|
}
|