Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/playwright/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import util from 'util';

import debug from 'debug';
import mime from 'mime';
import { minimatch } from 'minimatch';
import { calculateSha1 } from '@utils/crypto';
import { sanitizeForFilePath } from '@utils/fileUtils';
import { isRegExp } from '@isomorphic/rtti';
import { stringifyStackFrames, filteredStackTrace } from '@utils/stackTrace';
Expand Down Expand Up @@ -220,11 +220,14 @@ export async function normalizeAndSaveAttachment(outputPath: string, name: strin
if ((options.path !== undefined ? 1 : 0) + (options.body !== undefined ? 1 : 0) !== 1)
throw new Error(`Exactly one of "path" and "body" must be specified`);
if (options.path !== undefined) {
const hash = calculateSha1(options.path);

if (!isString(name))
throw new Error('"name" should be string.');

// Name the copy after the content, so that attaching the same path again
// with different content keeps each copy, see
// https://github.com/microsoft/playwright/issues/43425.
const hash = await calculateFileSha1(options.path);

const sanitizedNamePrefix = sanitizeForFilePath(name) + '-';
const dest = path.join(outputPath, 'attachments', sanitizedNamePrefix + hash + path.extname(options.path));
await fs.promises.mkdir(path.dirname(dest), { recursive: true });
Expand All @@ -237,6 +240,13 @@ export async function normalizeAndSaveAttachment(outputPath: string, name: strin
}
}

async function calculateFileSha1(filePath: string): Promise<string> {
const hash = crypto.createHash('sha1');
for await (const chunk of fs.createReadStream(filePath))
hash.update(chunk);
return hash.digest('hex');
}

export function fileIsModule(file: string): boolean {
if (file.endsWith('.mjs') || file.endsWith('.mts'))
return true;
Expand Down
17 changes: 13 additions & 4 deletions tests/playwright-test/reporter-attachment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import fs from 'fs';
import path from 'path';
import { test, expect } from './playwright-test-fixtures';

Expand Down Expand Up @@ -97,7 +98,7 @@ test(`testInfo.attach errors`, async ({ runInlineTest }) => {
`,
}, { reporter: 'line', workers: 1 });
const text = result.output.replace(/\\/g, '/');
expect(text).toMatch(/Error: ENOENT: no such file or directory, copyfile '.*foo.txt.*'/);
expect(text).toMatch(/Error: ENOENT: no such file or directory, open '.*foo.txt.*'/);
expect(text).toContain(`Exactly one of "path" and "body" must be specified`);
expect(result.passed).toBe(1);
expect(result.failed).toBe(2);
Expand All @@ -113,7 +114,7 @@ test(`testInfo.attach errors with empty path`, async ({ runInlineTest }) => {
});
`,
}, { reporter: 'line', workers: 1 });
expect(result.output).toMatch(/Error: ENOENT: no such file or directory, copyfile/);
expect(result.output).toMatch(/Error: ENOENT: no such file or directory, open/);
expect(result.exitCode).toBe(1);
});

Expand All @@ -132,21 +133,26 @@ test(`testInfo.attach error in fixture`, async ({ runInlineTest }) => {
`,
}, { reporter: 'line', workers: 1 });
const text = result.output.replace(/\\/g, '/');
expect(text).toMatch(/Error: ENOENT: no such file or directory, copyfile '.*foo.txt.*'/);
expect(text).toMatch(/Error: ENOENT: no such file or directory, open '.*foo.txt.*'/);
expect(result.exitCode).toBe(1);
expect(result.passed).toBe(0);
expect(result.failed).toBe(1);
});

test(`testInfo.attach success in fixture`, async ({ runInlineTest }) => {
test(`testInfo.attach success in fixture`, {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43425' },
}, async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.test.ts': `
import { test as base, expect } from '@playwright/test';
const test = base.extend({
fixture: async ({}, use, testInfo) => {
const filePath = testInfo.outputPath('foo.txt');
require('fs').writeFileSync(filePath, 'hello');
await testInfo.attach('name', { path: filePath });
await use();
// Attaching the same path again with different content keeps both copies.
require('fs').writeFileSync(filePath, 'world');
await testInfo.attach('name', { path: filePath });
},
});
Expand All @@ -158,6 +164,9 @@ test(`testInfo.attach success in fixture`, async ({ runInlineTest }) => {
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
expect(result.output).toContain('attachment #1: name (text/plain)');
expect(result.output).toContain('attachment #2: name (text/plain)');
const attachments = result.report.suites[0].specs[0].tests[0].results[0].attachments.filter(a => a.name === 'name');
expect(attachments.map(a => fs.readFileSync(a.path, 'utf8'))).toEqual(['hello', 'world']);
});

test(`testInfo.attach allow empty string body`, async ({ runInlineTest }) => {
Expand Down