Skip to content

Commit

Permalink
feat: Support custom attachment content types
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Nov 13, 2020
1 parent f4e2e4d commit 0ccb389
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 36 deletions.
19 changes: 13 additions & 6 deletions packages/allure-cucumberjs/src/CucumberAllureInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Allure,
AllureStep,
AllureTest,
AttachmentOptions,
ContentType,
ExecutableItemWrapper,
isPromise,
Expand Down Expand Up @@ -61,14 +62,20 @@ export class CucumberAllureInterface extends Allure {
this.step(name, () => {}); // todo status
}

attachment(name: string, content: Buffer | string, type: ContentType) {
const file = this.reporter.writeAttachment(content, type);
this.currentExecutable.addAttachment(name, type, file);
attachment(name: string, content: Buffer | string, options: ContentType | string | AttachmentOptions) {
if (typeof options === "string") {
options = { contentType: options };
}
const file = this.reporter.writeAttachment(content, options);
this.currentExecutable.addAttachment(name, options.contentType, file);
}

testAttachment(name: string, content: Buffer | string, type: ContentType) {
const file = this.reporter.writeAttachment(content, type);
this.currentTest.addAttachment(name, type, file);
testAttachment(name: string, content: Buffer | string, options: ContentType | string | AttachmentOptions) {
if (typeof options === "string") {
options = { contentType: options };
}
const file = this.reporter.writeAttachment(content, options);
this.currentTest.addAttachment(name, options.contentType, file);
}

addParameter(name: string, value: string): void {
Expand Down
13 changes: 8 additions & 5 deletions packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AllureRuntime,
AllureStep,
AllureTest,
AttachmentOptions,
ContentType,
ExecutableItemWrapper,
LabelName
Expand Down Expand Up @@ -239,7 +240,9 @@ export class CucumberJSAllureFormatter extends Formatter {

if (step.argument !== undefined) {
if (step.argument.content !== undefined) {
const file = this.allureRuntime.writeAttachment(step.argument.content, ContentType.TEXT);
const file = this.allureRuntime.writeAttachment(step.argument.content, {
contentType: ContentType.TEXT,
});
allureStep.addAttachment("Text", ContentType.TEXT, file);
}
if (step.argument.rows !== undefined) {
Expand All @@ -249,7 +252,7 @@ export class CucumberJSAllureFormatter extends Formatter {
cell => cell.value.replace(/\t/g, " ")
).join("\t")
).join("\n"),
ContentType.TSV
{ contentType: ContentType.TSV }
);
allureStep.addAttachment("Table", ContentType.TSV, file);
}
Expand All @@ -264,7 +267,7 @@ export class CucumberJSAllureFormatter extends Formatter {
if ([ContentType.JPEG, ContentType.PNG, ContentType.WEBM].indexOf(type) >= 0) {
content = Buffer.from(content, "base64");
}
const file = this.allureRuntime.writeAttachment(content, type);
const file = this.allureRuntime.writeAttachment(content, { contentType: type });
this.currentStep.addAttachment("attached", type, file);
}

Expand Down Expand Up @@ -315,8 +318,8 @@ export class CucumberJSAllureFormatter extends Formatter {
return null;
}

writeAttachment(content: Buffer | string, type: ContentType): string {
return this.allureRuntime.writeAttachment(content, type);
writeAttachment(content: Buffer | string, options: AttachmentOptions): string {
return this.allureRuntime.writeAttachment(content, options);
}
}

Expand Down
14 changes: 9 additions & 5 deletions packages/allure-jasmine/src/JasmineAllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AllureRuntime,
AllureStep,
AllureTest,
AttachmentOptions,
ContentType,
ExecutableItemWrapper,
isPromise,
Expand Down Expand Up @@ -65,8 +66,8 @@ export class JasmineAllureReporter implements jasmine.CustomReporter {
return this.runningExecutable;
}

writeAttachment(content: Buffer | string, type: ContentType): string {
return this.runtime.writeAttachment(content, type);
writeAttachment(content: Buffer | string, options: AttachmentOptions): string {
return this.runtime.writeAttachment(content, options);
}

jasmineStarted(suiteInfo: jasmine.SuiteInfo): void {
Expand Down Expand Up @@ -315,9 +316,12 @@ export class JasmineAllureInterface extends Allure {
this.step(name, () => {}); // todo status
}

attachment(name: string, content: Buffer | string, type: ContentType) {
const file = this.reporter.writeAttachment(content, type);
this.currentExecutable.addAttachment(name, type, file);
attachment(name: string, content: Buffer | string, options: ContentType | string | AttachmentOptions) {
if (typeof options === "string") {
options = { contentType: options };
}
const file = this.reporter.writeAttachment(content, options);
this.currentExecutable.addAttachment(name, options.contentType, file);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/allure-js-commons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { AllureStep } from "./src/ExecutableItemWrapper";
export { isPromise } from "./src/isPromise";
export { Allure, StepInterface } from "./src/Allure";
export {
AttachmentOptions,
Attachment,
Category,
StepResult,
Expand Down
6 changes: 3 additions & 3 deletions packages/allure-js-commons/src/AllureRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Category, ContentType, TestResult, TestResultContainer } from "./model";
import { AttachmentOptions, Category, TestResult, TestResultContainer } from "./model";
import { v4 as randomUUID } from "uuid";
import { IAllureConfig } from "./AllureConfig";
import { AllureGroup } from "./AllureGroup";
Expand Down Expand Up @@ -28,8 +28,8 @@ export class AllureRuntime {
this.writer.writeGroup(result);
}

writeAttachment(content: Buffer | string, contentType: ContentType): string {
const extension = typeToExtension(contentType);
writeAttachment(content: Buffer | string, options: AttachmentOptions) {
const extension = typeToExtension(options);
const fileName = `${randomUUID()}-attachment.${extension}`;
this.writer.writeAttachment(fileName, content);
return fileName;
Expand Down
3 changes: 1 addition & 2 deletions packages/allure-js-commons/src/ExecutableItemWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ export class ExecutableItemWrapper {
this.info.parameters.push({ name, value });
}

public addAttachment(name: string, type: ContentType, fileName: string) {
// eslint-disable-next-line object-shorthand
public addAttachment(name: string, type: string, fileName: string) {
this.info.attachments.push({ name, type, source: fileName });
}

Expand Down
6 changes: 5 additions & 1 deletion packages/allure-js-commons/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export interface Attachment {
source: string
}

export interface AttachmentOptions {
contentType: ContentType | string,
fileExtension?: string;
}

export interface Label {
name: LabelName | string
value: string
Expand Down Expand Up @@ -121,7 +126,6 @@ export enum LabelName {
LANGUAGE = "language",
}

/* eslint-disable no-undef */
export enum Severity {
BLOCKER = "blocker",
CRITICAL = "critical",
Expand Down
11 changes: 7 additions & 4 deletions packages/allure-js-commons/src/writers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ContentType } from "../model";
import { AttachmentOptions, ContentType } from "../model";

export function typeToExtension(type: ContentType): string {
switch (type) {
export function typeToExtension(options: AttachmentOptions): string {
if (options.fileExtension) {
return options.fileExtension;
}
switch (options.contentType) {
case ContentType.TEXT:
return "txt";
case ContentType.XML:
Expand All @@ -25,5 +28,5 @@ export function typeToExtension(type: ContentType): string {
case ContentType.JPEG:
return "jpg";
}
throw new Error(`Unrecognized extension: ${type}`);
throw new Error(`Unrecognized extension: ${options.contentType}`);
}
6 changes: 3 additions & 3 deletions packages/allure-mocha/src/AllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
AllureRuntime,
AllureStep,
AllureTest,
ContentType,
AttachmentOptions,
ExecutableItemWrapper,
LabelName,
Stage,
Expand Down Expand Up @@ -134,8 +134,8 @@ export class AllureReporter {
this.endTest(status, { message: error.message, trace: error.stack });
}

public writeAttachment(content: Buffer | string, type: ContentType): string {
return this.allureRuntime.writeAttachment(content, type);
public writeAttachment(content: Buffer | string, options: AttachmentOptions): string {
return this.allureRuntime.writeAttachment(content, options);
}

public pushStep(step: AllureStep): void {
Expand Down
23 changes: 17 additions & 6 deletions packages/allure-mocha/src/MochaAllure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AllureRuntime,
AllureStep,
AllureTest,
AttachmentOptions,
ContentType,
ExecutableItemWrapper,
isPromise,
Expand Down Expand Up @@ -60,14 +61,24 @@ export class MochaAllure extends Allure {
}); // todo status
}

public attachment(name: string, content: Buffer | string, type: ContentType): void {
const file = this.reporter.writeAttachment(content, type);
this.currentExecutable.addAttachment(name, type, file);
public attachment(name: string, content: Buffer | string, options: ContentType | string | AttachmentOptions): void {
if (typeof options === "string") {
options = { contentType: options };
}
const file = this.reporter.writeAttachment(content, options);
this.currentExecutable.addAttachment(name, options.contentType, file);
}

public testAttachment(name: string, content: Buffer | string, type: ContentType): void {
const file = this.reporter.writeAttachment(content, type);
this.currentTest.addAttachment(name, type, file);
public testAttachment(
name: string,
content: Buffer | string,
options: ContentType | string | AttachmentOptions
): void {
if (typeof options === "string") {
options = { contentType: options };
}
const file = this.reporter.writeAttachment(content, options);
this.currentTest.addAttachment(name, options.contentType, file);
}

public get currentTest(): AllureTest {
Expand Down
4 changes: 4 additions & 0 deletions packages/allure-mocha/test/fixtures/specs/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ class AttachmentSubSuite {
allure.attachment("step 2 attachment 2", "step 2 attachment 2 content", ContentType.TEXT);
});
allure.testAttachment("test attachment 2", "{ \"key\": \"value\" }", ContentType.JSON);
allure.testAttachment("custom-attachment", "{}", {
contentType: "application/vnd.something.custom",
fileExtension: "json",
});
}
}
5 changes: 4 additions & 1 deletion packages/allure-mocha/test/specs/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ class AttachmentsSuite {
expect(test).not.eq(undefined);
expect(test.status).eq(Status.PASSED);

expect(test.attachments).length(2);
expect(test.attachments).length(3);
expect(test.attachments[0].name).eq("test attachment 1");
expect(test.attachments[0].type).eq("text/plain");
expect(test.attachments[1].name).eq("test attachment 2");
expect(test.attachments[1].type).eq("application/json");
expect(test.attachments[2].name).eq("custom-attachment");
expect(test.attachments[2].type).eq("application/vnd.something.custom");
expect(test.attachments[2].source).matches(/\.json$/);

expect(test.steps).length(2);

Expand Down

0 comments on commit 0ccb389

Please sign in to comment.