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

feat: Support custom attachment content types #221

Merged
merged 1 commit into from
Dec 2, 2020
Merged
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
13 changes: 7 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,14 @@ 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) {
const file = this.reporter.writeAttachment(content, options);
this.currentExecutable.addAttachment(name, options, 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) {
const file = this.reporter.writeAttachment(content, options);
this.currentTest.addAttachment(name, options, 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: ContentType | string | AttachmentOptions): string {
return this.allureRuntime.writeAttachment(content, options);
}
}

Expand Down
11 changes: 6 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: ContentType | string | AttachmentOptions): string {
return this.runtime.writeAttachment(content, options);
}

jasmineStarted(suiteInfo: jasmine.SuiteInfo): void {
Expand Down Expand Up @@ -315,9 +316,9 @@ 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) {
const file = this.reporter.writeAttachment(content, options);
this.currentExecutable.addAttachment(name, options, 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
8 changes: 6 additions & 2 deletions packages/allure-js-commons/src/Allure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Category, LinkType, Status } from "./model";
import { AttachmentOptions, Category, LinkType, Status } from "./model";
import { ContentType } from "./model";
import { LabelName } from "./model";
import { AllureTest } from "./AllureTest";
Expand Down Expand Up @@ -63,7 +63,11 @@ export abstract class Allure {
this.currentExecutable.descriptionHtml = html;
}

public abstract attachment(name: string, content: Buffer | string, type: ContentType): void;
public abstract attachment(
name: string,
content: Buffer | string,
options: ContentType | string | AttachmentOptions
): void;

public owner(owner: string): void {
this.label(LabelName.OWNER, owner);
Expand Down
9 changes: 6 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, ContentType, 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,11 @@ export class AllureRuntime {
this.writer.writeGroup(result);
}

writeAttachment(content: Buffer | string, contentType: ContentType): string {
const extension = typeToExtension(contentType);
writeAttachment(content: Buffer | string, options: ContentType | string | AttachmentOptions) {
if (typeof options === "string") {
options = { contentType: options };
}
const extension = typeToExtension(options);
const fileName = `${randomUUID()}-attachment.${extension}`;
this.writer.writeAttachment(fileName, content);
return fileName;
Expand Down
9 changes: 6 additions & 3 deletions packages/allure-js-commons/src/ExecutableItemWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AttachmentOptions,
StatusDetails,
StepResult,
FixtureResult,
Expand Down Expand Up @@ -58,9 +59,11 @@ export class ExecutableItemWrapper {
this.info.parameters.push({ name, value });
}

public addAttachment(name: string, type: ContentType, fileName: string) {
// eslint-disable-next-line object-shorthand
this.info.attachments.push({ name, type, source: fileName });
public addAttachment(name: string, options: ContentType | string | AttachmentOptions, fileName: string) {
if (typeof options === "string") {
options = { contentType: options };
}
this.info.attachments.push({ name, type: options.contentType, source: fileName });
}

public startStep(name: string, start?: number): AllureStep {
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}`);
}
5 changes: 3 additions & 2 deletions packages/allure-mocha/src/AllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AllureRuntime,
AllureStep,
AllureTest,
AttachmentOptions,
ContentType,
ExecutableItemWrapper,
LabelName,
Expand Down Expand Up @@ -134,8 +135,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: ContentType | string | AttachmentOptions): string {
return this.allureRuntime.writeAttachment(content, options);
}

public pushStep(step: AllureStep): void {
Expand Down
17 changes: 11 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,18 @@ 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 {
const file = this.reporter.writeAttachment(content, options);
this.currentExecutable.addAttachment(name, options, 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 {
const file = this.reporter.writeAttachment(content, options);
this.currentTest.addAttachment(name, options, 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