Skip to content

Commit

Permalink
preserve original attachment file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
baev committed May 21, 2024
1 parent f4821eb commit b6165c3
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 10 deletions.
5 changes: 4 additions & 1 deletion packages/allure-js-commons/src/sdk/ReporterRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,10 @@ export class ReporterRuntime {

buildAttachmentFileName = (options: AttachmentOptions): string => {
const attachmentUuid = this.crypto.uuid();
const attachmentExtension = options.fileExtension || typeToExtension({ contentType: options.contentType });
const attachmentExtension = typeToExtension({
fileExtension: options.fileExtension,
contentType: options.contentType,
});

return `${attachmentUuid}-attachment${attachmentExtension}`;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/allure-js-commons/src/sdk/Writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface Writer {

writeGroup(result: TestResultContainer): void;

writeAttachment(name: string, content: Buffer | string, encoding?: BufferEncoding): void;
writeAttachment(distFileName: string, content: Buffer | string, encoding?: BufferEncoding): void;

writeAttachmentFromPath(from: string, distFileName: string): void;

Expand Down
6 changes: 5 additions & 1 deletion packages/allure-js-commons/src/sdk/node/ReporterRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { extname } from "path";
import { AttachmentOptions, TestResult } from "../../model.js";
import { Config } from "../Config.js";
import { ReporterRuntime } from "../ReporterRuntime.js";
Expand Down Expand Up @@ -34,7 +35,10 @@ export class AllureNodeReporterRuntime extends ReporterRuntime {
return;
}

const attachmentFilename = this.buildAttachmentFileName(options);
const attachmentFilename = this.buildAttachmentFileName({
...options,
fileExtension: options.fileExtension ?? extname(attachmentPath),
});

this.writer.writeAttachmentFromPath(attachmentPath, attachmentFilename);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export class FileSystemAllureWriter implements Writer {
}
}

writeAttachment(name: string, content: Buffer | string, encoding: BufferEncoding = "utf-8"): void {
const path = this.buildPath(name);
writeAttachment(distFileName: string, content: Buffer | string, encoding: BufferEncoding = "utf-8"): void {
const path = this.buildPath(distFileName);

writeFileSync(path, content, encoding);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class MessageAllureWriter implements Writer {
this.sendData(path, type, Buffer.from(JSON.stringify(data)));
}

writeAttachment(name: string, content: Buffer | string, encoding: BufferEncoding = "utf-8"): void {
this.sendData(name, "attachment", typeof content === "string" ? Buffer.from(content, encoding) : content);
writeAttachment(distFileName: string, content: Buffer | string, encoding: BufferEncoding = "utf-8"): void {
this.sendData(distFileName, "attachment", typeof content === "string" ? Buffer.from(content, encoding) : content);
}

writeAttachmentFromPath(from: PathLike, distFileName: string): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export class AllureInMemoryAllureWriter implements Writer, AllureResults {
this.tests.push(result);
}

public writeAttachment(name: string, content: Buffer | string): void {
this.attachments[name] = content;
public writeAttachment(distFileName: string, content: Buffer | string): void {
this.attachments[distFileName] = content;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public writeAttachmentFromPath(from: PathLike, toFileName: string): void {
public writeAttachmentFromPath(from: PathLike, distFileName: string): void {
throw new Error(
"Can't write attachment from path because generic writer doesn't implement this logic! Use AllureInMemoryWriter for node.js.",
);
Expand Down
70 changes: 70 additions & 0 deletions packages/allure-js-commons/test/sdk/node/ReporterRuntime.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { describe, expect, it } from "vitest";
import { AllureNodeReporterRuntime } from "../../../src/sdk/node";
import { mockWriter } from "../../utils/writer";

describe("AllureNodeReporterRuntime", () => {
describe("writeAttachmentFromPath", () => {
it("should use extension from fileExtension option if specified", async () => {
const writer = mockWriter();
const runtime = new AllureNodeReporterRuntime({ writer });

runtime.startTest({});

runtime.writeAttachmentFromPath("some attachment", "some/path/to/file", {
fileExtension: ".mst",
contentType: "*/*",
});

const attachment = runtime.getCurrentTest()!.attachments[0];

expect(attachment.name).to.be.eq("some attachment");
expect(attachment.source).to.match(/.+\.mst/);
const writeAttachmentFromPathCall = writer.writeAttachmentFromPath.mock.calls[0];

expect(writeAttachmentFromPathCall[0]).to.be.eq("some/path/to/file");
expect(writeAttachmentFromPathCall[1]).to.be.eq(attachment.source);
});

it("should use extension from original file if fileExtension option is not specified", async () => {
const writer = mockWriter();
const runtime = new AllureNodeReporterRuntime({ writer });

runtime.startTest({});

runtime.writeAttachmentFromPath("some attachment", "some/path/to/file.abc", {
contentType: "*/*",
});

const attachment = runtime.getCurrentTest()!.attachments[0];

expect(attachment.name).to.be.eq("some attachment");
expect(attachment.source).to.match(/.+\.abc/);
const writeAttachmentFromPathCall = writer.writeAttachmentFromPath.mock.calls[0];

expect(writeAttachmentFromPathCall[0]).to.be.eq("some/path/to/file.abc");
expect(writeAttachmentFromPathCall[1]).to.be.eq(attachment.source);
});

it("should detect extension by content type if no option or path specified", async () => {
const writer = mockWriter();
const runtime = new AllureNodeReporterRuntime({ writer });

runtime.startTest({});

runtime.writeAttachment({
contentType: "text/csv",
name: "some other attachment",
content: "attachment content",
});

const attachment = runtime.getCurrentTest()!.attachments[0];

expect(attachment.name).to.be.eq("some other attachment");
expect(attachment.source).to.match(/.+\.csv/);
const writeAttachmentFromPathCall = writer.writeAttachment.mock.calls[0];

expect(writeAttachmentFromPathCall[0]).to.be.eq(attachment.source);
expect(writeAttachmentFromPathCall[1]).to.be.eq("attachment content");
});
});
});
11 changes: 11 additions & 0 deletions packages/allure-js-commons/test/utils/writer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { vi } from "vitest";
import { Category, TestResult, TestResultContainer } from "../../src";

export const mockWriter = () => ({
writeResult: vi.fn<[TestResult], void>(),
writeGroup: vi.fn<[TestResultContainer], void>(),
writeEnvironmentInfo: vi.fn<[Record<string, string | undefined>], void>(),
writeCategoriesDefinitions: vi.fn<[Category[]], void>(),
writeAttachment: vi.fn<[string, Buffer | string, BufferEncoding | undefined], void>(),
writeAttachmentFromPath: vi.fn<[string, string], void>(),
});

0 comments on commit b6165c3

Please sign in to comment.