fix(tracing): do not throw on missing attachments (#24409)

Fixes #24378.
This commit is contained in:
Dmitry Gozman 2023-07-25 14:32:56 -07:00 committed by GitHub
parent 33d62d9a97
commit 9d0bba9c99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 11 deletions

View File

@ -117,15 +117,16 @@ export async function saveTraceFile(fileName: string, traceEvents: TraceEvent[],
const sha1s = new Set<string>(); const sha1s = new Set<string>();
for (const event of traceEvents.filter(e => e.type === 'after') as AfterActionTraceEvent[]) { for (const event of traceEvents.filter(e => e.type === 'after') as AfterActionTraceEvent[]) {
for (const attachment of (event.attachments || [])) { for (const attachment of (event.attachments || [])) {
let contentPromise: Promise<Buffer> | undefined; let contentPromise: Promise<Buffer | undefined> | undefined;
if (attachment.path) if (attachment.path)
contentPromise = fs.promises.readFile(attachment.path); contentPromise = fs.promises.readFile(attachment.path).catch(() => undefined);
else if (attachment.base64) else if (attachment.base64)
contentPromise = Promise.resolve(Buffer.from(attachment.base64, 'base64')); contentPromise = Promise.resolve(Buffer.from(attachment.base64, 'base64'));
if (!contentPromise)
continue;
const content = await contentPromise; const content = await contentPromise;
if (content === undefined)
continue;
const sha1 = calculateSha1(content); const sha1 = calculateSha1(content);
attachment.sha1 = sha1; attachment.sha1 = sha1;
delete attachment.path; delete attachment.path;

View File

@ -68,13 +68,7 @@ export const AttachmentsSection: React.FunctionComponent<{
</>; </>;
}; };
function attachmentURL(traceUrl: string, attachment: { function attachmentURL(traceUrl: string, attachment: NonNullable<ActionTraceEventInContext['attachments']>[0]) {
name: string;
contentType: string;
path?: string;
sha1?: string;
body?: string;
}) {
if (attachment.sha1) if (attachment.sha1)
return 'sha1/' + attachment.sha1 + '?trace=' + encodeURIComponent(traceUrl); return 'sha1/' + attachment.sha1 + '?trace=' + encodeURIComponent(traceUrl);
return 'file?path=' + encodeURIComponent(attachment.path!); return 'file?path=' + encodeURIComponent(attachment.path!);

View File

@ -686,3 +686,22 @@ test('should show non-expect error in trace', async ({ runInlineTest }, testInfo
' fixture: context', ' fixture: context',
]); ]);
}); });
test('should not throw when attachment is missing', async ({ runInlineTest }, testInfo) => {
const result = await runInlineTest({
'playwright.config.ts': `
module.exports = { use: { trace: 'on' } };
`,
'a.spec.ts': `
import { test, expect } from '@playwright/test';
test('passes', async ({}) => {
test.info().attachments.push({ name: 'screenshot', path: 'nothing-to-see-here', contentType: 'image/png' });
});
`,
}, { workers: 1 });
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
const trace = await parseTrace(testInfo.outputPath('test-results', 'a-passes', 'trace.zip'));
expect(trace.actionTree).toContain('attach "screenshot"');
});