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,20 +53,16 @@ export type TraceViewerAppOptions = {
persistentContextOptions?: Parameters<BrowserType['launchPersistentContext']>[2];
};
async function validateTraceUrls(traceUrls: string[]) {
function validateTraceUrls(traceUrls: string[]) {
for (const traceUrl of traceUrls) {
let traceFile = traceUrl;
// If .json is requested, we'll synthesize it.
if (traceUrl.endsWith('.json'))
traceFile = traceUrl.substring(0, traceUrl.length - '.json'.length);
if (!traceUrl.startsWith('http://') && !traceUrl.startsWith('https://') && !fs.existsSync(traceFile) && !fs.existsSync(traceFile + '.trace')) {
// eslint-disable-next-line no-console
console.error(`Trace file ${traceUrl} does not exist!`);
return false;
}
if (!traceUrl.startsWith('http://') && !traceUrl.startsWith('https://') && !fs.existsSync(traceFile) && !fs.existsSync(traceFile + '.trace'))
throw new Error(`Trace file ${traceUrl} does not exist!`);
}
return true;
}
export async function startTraceViewerServer(options?: TraceViewerServerOptions): Promise<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) {
if (!validateTraceUrls(traceUrls))
return;
validateTraceUrls(traceUrls);
const server = await startTraceViewerServer(options);
await installRootRedirect(server, traceUrls, 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) {
if (!validateTraceUrls(traceUrls))
return;
validateTraceUrls(traceUrls);
const server = await startTraceViewerServer(options);
await installRootRedirect(server, traceUrls, options);
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 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')}"`);
});
});