mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore: expose clear cache to test server (#30268)
This commit is contained in:
parent
5043bd55dc
commit
3cea258a9c
@ -15,23 +15,23 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { removeFolder } from 'playwright/lib/program';
|
|
||||||
import { affectedTestFiles, cacheDir } from 'playwright/lib/transform/compilationCache';
|
import { affectedTestFiles, cacheDir } from 'playwright/lib/transform/compilationCache';
|
||||||
import { buildBundle } from './vitePlugin';
|
import { buildBundle } from './vitePlugin';
|
||||||
import { resolveDirs } from './viteUtils';
|
import { resolveDirs } from './viteUtils';
|
||||||
import type { FullConfig, Suite } from 'playwright/types/testReporter';
|
import type { Suite } from 'playwright/types/testReporter';
|
||||||
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: FullConfig, configDir: string) {
|
export async function clearCacheCommand(config: FullConfigInternal) {
|
||||||
const dirs = await resolveDirs(configDir, config);
|
const dirs = await resolveDirs(config.configDir, config.config);
|
||||||
if (dirs)
|
if (dirs)
|
||||||
await removeFolder(dirs.outDir);
|
await removeFolderAndLogToConsole(dirs.outDir);
|
||||||
await removeFolder(cacheDir);
|
await removeFolderAndLogToConsole(cacheDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function findRelatedTestFilesCommand(files: string[], config: FullConfig, configDir: string, suite: Suite) {
|
export async function findRelatedTestFilesCommand(files: string[], config: FullConfigInternal, suite: Suite) {
|
||||||
await buildBundle(config, configDir, suite);
|
await buildBundle(config.config, config.configDir, suite);
|
||||||
return { testFiles: affectedTestFiles(files) };
|
return { testFiles: affectedTestFiles(files) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -165,6 +165,10 @@ export class TestServerConnection implements TestServerInterface, TestServerInte
|
|||||||
return await this._sendMessage('stopDevServer', params);
|
return await this._sendMessage('stopDevServer', params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async clearCache(params: Parameters<TestServerInterface['clearCache']>[0]): ReturnType<TestServerInterface['clearCache']> {
|
||||||
|
return await this._sendMessage('clearCache', params);
|
||||||
|
}
|
||||||
|
|
||||||
async listFiles(params: Parameters<TestServerInterface['listFiles']>[0]): ReturnType<TestServerInterface['listFiles']> {
|
async listFiles(params: Parameters<TestServerInterface['listFiles']>[0]): ReturnType<TestServerInterface['listFiles']> {
|
||||||
return await this._sendMessage('listFiles', params);
|
return await this._sendMessage('listFiles', params);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -64,6 +64,8 @@ export interface TestServerInterface {
|
|||||||
status: reporterTypes.FullResult['status']
|
status: reporterTypes.FullResult['status']
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
|
clearCache(params: {}): Promise<void>;
|
||||||
|
|
||||||
listFiles(params: {
|
listFiles(params: {
|
||||||
projects?: string[];
|
projects?: string[];
|
||||||
}): Promise<{
|
}): Promise<{
|
||||||
|
|||||||
@ -33,8 +33,8 @@ import { program } from 'playwright-core/lib/cli/program';
|
|||||||
export { program } from 'playwright-core/lib/cli/program';
|
export { program } from 'playwright-core/lib/cli/program';
|
||||||
import type { ReporterDescription } from '../types/test';
|
import type { ReporterDescription } from '../types/test';
|
||||||
import { prepareErrorStack } from './reporters/base';
|
import { prepareErrorStack } from './reporters/base';
|
||||||
import { cacheDir } from './transform/compilationCache';
|
|
||||||
import * as testServer from './runner/testServer';
|
import * as testServer from './runner/testServer';
|
||||||
|
import { clearCacheAndLogToConsole } from './runner/testServer';
|
||||||
|
|
||||||
function addTestCommand(program: Command) {
|
function addTestCommand(program: Command) {
|
||||||
const command = program.command('test [test-filter...]');
|
const command = program.command('test [test-filter...]');
|
||||||
@ -76,26 +76,10 @@ function addClearCacheCommand(program: Command) {
|
|||||||
const configInternal = await loadConfigFromFileRestartIfNeeded(opts.config);
|
const configInternal = await loadConfigFromFileRestartIfNeeded(opts.config);
|
||||||
if (!configInternal)
|
if (!configInternal)
|
||||||
return;
|
return;
|
||||||
const { config, configDir } = configInternal;
|
await clearCacheAndLogToConsole(configInternal);
|
||||||
const override = (config as any)['@playwright/test']?.['cli']?.['clear-cache'];
|
|
||||||
if (override) {
|
|
||||||
await override(config, configDir);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await removeFolder(cacheDir);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removeFolder(folder: string) {
|
|
||||||
try {
|
|
||||||
if (!fs.existsSync(folder))
|
|
||||||
return;
|
|
||||||
console.log(`Removing ${await fs.promises.realpath(folder)}`);
|
|
||||||
await fs.promises.rm(folder, { recursive: true, force: true });
|
|
||||||
} catch {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function addFindRelatedTestFilesCommand(program: Command) {
|
function addFindRelatedTestFilesCommand(program: Command) {
|
||||||
const command = program.command('find-related-test-files [source-files...]', { hidden: true });
|
const command = program.command('find-related-test-files [source-files...]', { hidden: true });
|
||||||
command.description('Returns the list of related tests to the given files');
|
command.description('Returns the list of related tests to the given files');
|
||||||
|
|||||||
@ -150,7 +150,7 @@ export class Runner {
|
|||||||
const resolvedFiles = (files as string[]).map(file => path.resolve(process.cwd(), file));
|
const resolvedFiles = (files as string[]).map(file => path.resolve(process.cwd(), file));
|
||||||
const override = (this._config.config as any)['@playwright/test']?.['cli']?.['find-related-test-files'];
|
const override = (this._config.config as any)['@playwright/test']?.['cli']?.['find-related-test-files'];
|
||||||
if (override)
|
if (override)
|
||||||
return await override(resolvedFiles, this._config.config, this._config.configDir, result.suite);
|
return await override(resolvedFiles, this._config, result.suite);
|
||||||
return { testFiles: affectedTestFiles(resolvedFiles) };
|
return { testFiles: affectedTestFiles(resolvedFiles) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,6 +38,7 @@ import { webServerPluginsForConfig } from '../plugins/webServerPlugin';
|
|||||||
import type { TraceViewerRedirectOptions, TraceViewerServerOptions } from 'playwright-core/lib/server/trace/viewer/traceViewer';
|
import type { TraceViewerRedirectOptions, TraceViewerServerOptions } from 'playwright-core/lib/server/trace/viewer/traceViewer';
|
||||||
import type { TestRunnerPluginRegistration } from '../plugins';
|
import type { TestRunnerPluginRegistration } from '../plugins';
|
||||||
import { serializeError } from '../util';
|
import { serializeError } from '../util';
|
||||||
|
import { cacheDir } from '../transform/compilationCache';
|
||||||
|
|
||||||
const originalStdoutWrite = process.stdout.write;
|
const originalStdoutWrite = process.stdout.write;
|
||||||
const originalStderrWrite = process.stderr.write;
|
const originalStderrWrite = process.stderr.write;
|
||||||
@ -210,6 +211,12 @@ class TestServerDispatcher implements TestServerInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async clearCache(params: Parameters<TestServerInterface['clearCache']>[0]): ReturnType<TestServerInterface['clearCache']> {
|
||||||
|
const { config } = await this._loadConfig(this._configFile);
|
||||||
|
if (config)
|
||||||
|
await clearCacheAndLogToConsole(config);
|
||||||
|
}
|
||||||
|
|
||||||
async listFiles(params: Parameters<TestServerInterface['listFiles']>[0]): ReturnType<TestServerInterface['listFiles']> {
|
async listFiles(params: Parameters<TestServerInterface['listFiles']>[0]): ReturnType<TestServerInterface['listFiles']> {
|
||||||
const { reporter, report } = await this._collectingReporter();
|
const { reporter, report } = await this._collectingReporter();
|
||||||
const { config, error } = await this._loadConfig(this._configFile);
|
const { config, error } = await this._loadConfig(this._configFile);
|
||||||
@ -492,3 +499,23 @@ export async function resolveCtDirs(config: FullConfigInternal) {
|
|||||||
templateDir
|
templateDir
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function clearCacheAndLogToConsole(config: FullConfigInternal) {
|
||||||
|
const override = (config.config as any)['@playwright/test']?.['cli']?.['clear-cache'];
|
||||||
|
if (override) {
|
||||||
|
await override(config);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await removeFolderAndLogToConsole(cacheDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function removeFolderAndLogToConsole(folder: string) {
|
||||||
|
try {
|
||||||
|
if (!fs.existsSync(folder))
|
||||||
|
return;
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(`Removing ${await fs.promises.realpath(folder)}`);
|
||||||
|
await fs.promises.rm(folder, { recursive: true, force: true });
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user