fix(trace-viewer): exit if given trace.zip does not exist (#30222)

This commit is contained in:
Max Schmitt 2024-04-03 18:47:03 +02:00 committed by GitHub
parent 010bc29a3c
commit 43745210a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 11 deletions

View File

@ -53,21 +53,17 @@ export type TraceViewerAppOptions = {
persistentContextOptions?: Parameters<BrowserType['launchPersistentContext']>[2]; persistentContextOptions?: Parameters<BrowserType['launchPersistentContext']>[2];
}; };
async function validateTraceUrls(traceUrls: string[]) { function validateTraceUrls(traceUrls: string[]) {
for (const traceUrl of traceUrls) { for (const traceUrl of traceUrls) {
let traceFile = traceUrl; let traceFile = traceUrl;
// If .json is requested, we'll synthesize it. // If .json is requested, we'll synthesize it.
if (traceUrl.endsWith('.json')) if (traceUrl.endsWith('.json'))
traceFile = traceUrl.substring(0, traceUrl.length - '.json'.length); traceFile = traceUrl.substring(0, traceUrl.length - '.json'.length);
if (!traceUrl.startsWith('http://') && !traceUrl.startsWith('https://') && !fs.existsSync(traceFile) && !fs.existsSync(traceFile + '.trace')) { if (!traceUrl.startsWith('http://') && !traceUrl.startsWith('https://') && !fs.existsSync(traceFile) && !fs.existsSync(traceFile + '.trace'))
// eslint-disable-next-line no-console throw new Error(`Trace file ${traceUrl} does not exist!`);
console.error(`Trace file ${traceUrl} does not exist!`);
return false;
} }
} }
return true;
}
export async function startTraceViewerServer(options?: TraceViewerServerOptions): Promise<HttpServer> { export async function startTraceViewerServer(options?: TraceViewerServerOptions): Promise<HttpServer> {
const server = new HttpServer(); const server = new HttpServer();
@ -146,8 +142,7 @@ export async function installRootRedirect(server: HttpServer, traceUrls: string[
} }
export async function runTraceViewerApp(traceUrls: string[], browserName: string, options: TraceViewerServerOptions & { headless?: boolean }, exitOnClose?: boolean) { export async function runTraceViewerApp(traceUrls: string[], browserName: string, options: TraceViewerServerOptions & { headless?: boolean }, exitOnClose?: boolean) {
if (!validateTraceUrls(traceUrls)) validateTraceUrls(traceUrls);
return;
const server = await startTraceViewerServer(options); const server = await startTraceViewerServer(options);
await installRootRedirect(server, traceUrls, options); await installRootRedirect(server, traceUrls, options);
const page = await openTraceViewerApp(server.urlPrefix(), browserName, options); const page = await openTraceViewerApp(server.urlPrefix(), browserName, options);
@ -157,8 +152,7 @@ export async function runTraceViewerApp(traceUrls: string[], browserName: string
} }
export async function runTraceInBrowser(traceUrls: string[], options: TraceViewerServerOptions) { export async function runTraceInBrowser(traceUrls: string[], options: TraceViewerServerOptions) {
if (!validateTraceUrls(traceUrls)) validateTraceUrls(traceUrls);
return;
const server = await startTraceViewerServer(options); const server = await startTraceViewerServer(options);
await installRootRedirect(server, traceUrls, options); await installRootRedirect(server, traceUrls, options);
await openTraceInBrowser(server.urlPrefix()); await openTraceInBrowser(server.urlPrefix());

View File

@ -58,4 +58,14 @@ test('cli should work', async ({ exec, tmpWorkspace }) => {
await exec('npx playwright screenshot about:blank two.png'); await exec('npx playwright screenshot about:blank two.png');
await fs.promises.stat(path.join(tmpWorkspace, 'two.png')); await fs.promises.stat(path.join(tmpWorkspace, 'two.png'));
}); });
await test.step('show-trace', async () => {
const result = await exec('npx playwright show-trace i-do-not-exist.zip', { expectToExitWithError: true });
expect(result).toContain(`Trace file i-do-not-exist.zip does not exist`);
});
await test.step('show-report', async () => {
const result = await exec('npx playwright show-report', { expectToExitWithError: true });
expect(result).toContain(`No report found at "${path.join(fs.realpathSync(tmpWorkspace), 'playwright-report')}"`);
});
}); });