move clearCache impl into plugin

This commit is contained in:
Simon Knott 2024-07-25 17:00:39 +02:00
parent 66f8b209fc
commit c4da68d7ce
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
6 changed files with 39 additions and 17 deletions

View file

@ -16,7 +16,7 @@
const { test: baseTest, expect, devices, defineConfig: originalDefineConfig } = require('playwright/test'); const { test: baseTest, expect, devices, defineConfig: originalDefineConfig } = require('playwright/test');
const { fixtures } = require('./lib/mount'); const { fixtures } = require('./lib/mount');
const { clearCacheCommand, runDevServerCommand, findRelatedTestFilesCommand } = require('./lib/cliOverrides'); const { runDevServerCommand } = require('./lib/cliOverrides');
const { createPlugin } = require('./lib/vitePlugin'); const { createPlugin } = require('./lib/vitePlugin');
const defineConfig = (...configs) => { const defineConfig = (...configs) => {
@ -30,7 +30,6 @@ const defineConfig = (...configs) => {
[require.resolve('./lib/tsxTransform')] [require.resolve('./lib/tsxTransform')]
], ],
cli: { cli: {
'clear-cache': clearCacheCommand,
'dev-server': runDevServerCommand, 'dev-server': runDevServerCommand,
}, },
} }

View file

@ -15,18 +15,8 @@
* limitations under the License. * limitations under the License.
*/ */
import { cacheDir } from 'playwright/lib/transform/compilationCache';
import { resolveDirs } from './viteUtils';
import { runDevServer } from './devServer'; import { runDevServer } from './devServer';
import type { FullConfigInternal } from 'playwright/lib/common/config'; import type { FullConfigInternal } from 'playwright/lib/common/config';
import { removeFolderAndLogToConsole } from 'playwright/lib/runner/testServer';
export async function clearCacheCommand(config: FullConfigInternal) {
const dirs = await resolveDirs(config.configDir, config.config);
if (dirs)
await removeFolderAndLogToConsole(dirs.outDir);
await removeFolderAndLogToConsole(cacheDir);
}
export async function runDevServerCommand(config: FullConfigInternal) { export async function runDevServerCommand(config: FullConfigInternal) {
return await runDevServer(config); return await runDevServer(config);

View file

@ -31,6 +31,7 @@ import type { ImportInfo } from './tsxTransform';
import type { ComponentRegistry } from './viteUtils'; import type { ComponentRegistry } from './viteUtils';
import { createConfig, frameworkConfig, hasJSComponents, populateComponentsFromTests, resolveDirs, resolveEndpoint, transformIndexFile } from './viteUtils'; import { createConfig, frameworkConfig, hasJSComponents, populateComponentsFromTests, resolveDirs, resolveEndpoint, transformIndexFile } from './viteUtils';
import { resolveHook } from 'playwright/lib/transform/transform'; import { resolveHook } from 'playwright/lib/transform/transform';
import { removeFolderAndLogToConsole } from 'playwright/lib/runner/testServer';
const log = debug('pw:vite'); const log = debug('pw:vite');
@ -73,6 +74,12 @@ export function createPlugin(): TestRunnerPlugin {
if (stoppableServer) if (stoppableServer)
await new Promise(f => stoppableServer.stop(f)); await new Promise(f => stoppableServer.stop(f));
}, },
clearCache: async () => {
const dirs = await resolveDirs(configDir, config);
if (dirs)
await removeFolderAndLogToConsole(dirs.outDir);
}
}; };
} }

View file

@ -24,6 +24,9 @@ export interface TestRunnerPlugin {
begin?(suite: Suite): Promise<void>; begin?(suite: Suite): Promise<void>;
end?(): Promise<void>; end?(): Promise<void>;
teardown?(): Promise<void>; teardown?(): Promise<void>;
clearCache?(): Promise<void>;
runDevServer?(): Promise<void>;
} }
export type TestRunnerPluginRegistration = { export type TestRunnerPluginRegistration = {

View file

@ -106,6 +106,15 @@ function addRunTasks(taskRunner: TaskRunner<TestRun>, config: FullConfigInternal
return taskRunner; return taskRunner;
} }
export function createTaskRunnerForPluginSetup(config: FullConfigInternal, reporters: ReporterV2[]): TaskRunner<TestRun> {
const taskRunner = TaskRunner.create<TestRun>(reporters, config.config.globalTimeout);
for (const plugin of config.plugins)
taskRunner.addTask('plugin setup', createPluginSetupTask(plugin));
return taskRunner;
}
export function createTaskRunnerForList(config: FullConfigInternal, reporters: ReporterV2[], mode: 'in-process' | 'out-of-process', options: { failOnLoadErrors: boolean, populatePluginDependencies?: boolean }): TaskRunner<TestRun> { export function createTaskRunnerForList(config: FullConfigInternal, reporters: ReporterV2[], mode: 'in-process' | 'out-of-process', options: { failOnLoadErrors: boolean, populatePluginDependencies?: boolean }): TaskRunner<TestRun> {
const taskRunner = TaskRunner.create<TestRun>(reporters, config.config.globalTimeout); const taskRunner = TaskRunner.create<TestRun>(reporters, config.config.globalTimeout);

View file

@ -23,7 +23,7 @@ import type * as reporterTypes from '../../types/testReporter';
import { collectAffectedTestFiles, dependenciesForTestFile } from '../transform/compilationCache'; import { collectAffectedTestFiles, dependenciesForTestFile } from '../transform/compilationCache';
import type { ConfigLocation, FullConfigInternal } from '../common/config'; import type { ConfigLocation, FullConfigInternal } from '../common/config';
import { createReporterForTestServer, createReporters } from './reporters'; import { createReporterForTestServer, createReporters } from './reporters';
import { TestRun, createTaskRunnerForList, createTaskRunnerForTestServer, createTaskRunnerForWatchSetup, createTaskRunnerForListFiles } from './tasks'; import { TestRun, createTaskRunnerForList, createTaskRunnerForTestServer, createTaskRunnerForWatchSetup, createTaskRunnerForListFiles, createTaskRunnerForPluginSetup } from './tasks';
import { open } from 'playwright-core/lib/utilsBundle'; import { open } from 'playwright-core/lib/utilsBundle';
import ListReporter from '../reporters/list'; import ListReporter from '../reporters/list';
import { SigIntWatcher } from './sigIntWatcher'; import { SigIntWatcher } from './sigIntWatcher';
@ -39,6 +39,7 @@ import { serializeError } from '../util';
import { cacheDir } from '../transform/compilationCache'; import { cacheDir } from '../transform/compilationCache';
import { baseFullConfig } from '../isomorphic/teleReceiver'; import { baseFullConfig } from '../isomorphic/teleReceiver';
import { InternalReporter } from '../reporters/internalReporter'; import { InternalReporter } from '../reporters/internalReporter';
import { wrapReporterAsV2 } from '../reporters/reporterV2';
const originalStdoutWrite = process.stdout.write; const originalStdoutWrite = process.stdout.write;
const originalStderrWrite = process.stderr.write; const originalStderrWrite = process.stderr.write;
@ -515,11 +516,24 @@ export async function resolveCtDirs(config: FullConfigInternal) {
} }
export async function clearCacheAndLogToConsole(config: FullConfigInternal) { export async function clearCacheAndLogToConsole(config: FullConfigInternal) {
const override = (config.config as any)['@playwright/test']?.['cli']?.['clear-cache']; const errors: reporterTypes.TestError[] = [];
if (override) { const errorReporter = wrapReporterAsV2({
await override(config); onError(error: reporterTypes.TestError) {
return; errors.push(error);
}
});
const taskRunner = createTaskRunnerForPluginSetup(config, [errorReporter]);
await taskRunner.run(new TestRun(config), 0);
if (errors.length > 0)
throw new Error('Failed to clear cache: ' + errors);
for (const plugin of config.plugins) {
if (!plugin.instance)
throw new Error('Plugin not initialized');
await plugin.instance.clearCache?.();
} }
await removeFolderAndLogToConsole(cacheDir); await removeFolderAndLogToConsole(cacheDir);
} }