Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto-detect of attachments extension [playwright] #470

Closed
wants to merge 5 commits into from
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: 16 additions & 0 deletions packages/allure-js-commons/src/AllureRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PathLike } from "fs";
import { extname } from "path";
import { v4 as randomUUID } from "uuid";
import { AllureConfig } from "./AllureConfig";
import { AllureGroup } from "./AllureGroup";
Expand Down Expand Up @@ -52,6 +53,21 @@ export class AllureRuntime {
fromPath: PathLike,
options: ContentType | string | AttachmentOptions,
): string {
if (typeof fromPath === "string") {
const fileExtension = extname(fromPath).substring(1);
if (typeof options === "string") {
options = {
fileExtension: fileExtension,
contentType: options,
};
} else {
options = {
fileExtension: fileExtension,
...options,
};
}
}

const fileName = buildAttachmentFileName(options);
this.writer.writeAttachmentFromPath(fromPath, fileName);
return fileName;
Expand Down
8 changes: 8 additions & 0 deletions packages/allure-playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ class AllureReporter implements Reporter {
trace,
};
}

for (const attachment of result.attachments) {
if (!attachment.body && !attachment.path) {
if (!process.env.PW_ALLURE_POST_PROCESSOR_FOR_TEST) {
console.log(`Attachment ${attachment.name} has no body or path`); // eslint-disable-line no-console
}
continue;
}

Expand Down Expand Up @@ -169,8 +173,12 @@ class AllureReporter implements Reporter {
fileName = runtime.writeAttachment(attachment.body, attachment.contentType);
} else {
if (!fs.existsSync(attachment.path!)) {
if (!process.env.PW_ALLURE_POST_PROCESSOR_FOR_TEST) {
console.log(`File does not exist: ${attachment.path!}`); // eslint-disable-line no-console
}
continue;
}

fileName = runtime.writeAttachmentFromPath(attachment.path!, attachment.contentType);
}

Expand Down
Binary file added packages/allure-playwright/test/assets/empty.mp3
Binary file not shown.
29 changes: 28 additions & 1 deletion packages/allure-playwright/test/attachment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import fs from "fs";
import { expect, test } from "./fixtures";
import * as fs from "fs";

test("should not throw on missing attachment", async ({ runInlineTest }) => {
const result = await runInlineTest(
Expand Down Expand Up @@ -80,3 +80,30 @@ test("should add snapshots correctly and provide a screenshot diff", async ({
]),
);
});

test("should correctly and automatically detect file extension", async ({
runInlineTest,
attachment,
}) => {
const result = await runInlineTest(
{
"a.test.ts": `
import test from '@playwright/test';
test('should add attachment', async ({}, testInfo) => {
testInfo.attachments.push({
name: 'sound',
path: 'a.test.ts-snapshots/file-sound.mp3',
contentType: 'audio/mpeg'
})
});
`,
"a.test.ts-snapshots/file-sound.mp3": fs.readFileSync(attachment("empty.mp3")),
},
(writer) => {
return writer.tests[0].attachments;
},
);

expect(result.length).toBe(1);
expect(result[0].source).toEqual(expect.stringMatching(/.mp3$/));
});