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

History ID and metadata processing improvements #748

Merged
merged 3 commits into from
Aug 16, 2023
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
59 changes: 7 additions & 52 deletions packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as messages from "@cucumber/messages";
import { Tag, TestStepResultStatus } from "@cucumber/messages";
import {
Allure,
AllureCommandStepExecutable,
AllureRuntime,
AllureStep,
AllureTest,
Expand All @@ -18,7 +17,6 @@ import {
md5,
MetadataMessage,
Status,
StepMetadata,
} from "allure-js-commons";
import { ALLURE_METADATA_CONTENT_TYPE } from "allure-js-commons/internal";
import { CucumberAllureWorld } from "./CucumberAllureWorld";
Expand Down Expand Up @@ -300,7 +298,6 @@ export class CucumberJSAllureFormatter extends Formatter {
currentTest.name = pickle.name;
currentTest.fullName = fullName;
currentTest.testCaseId = testCaseId;
currentTest.historyId = testCaseId;

currentTest.addLabel(LabelName.HOST, this.hostname);
currentTest.addLabel(LabelName.LANGUAGE, "javascript");
Expand Down Expand Up @@ -399,56 +396,14 @@ export class CucumberJSAllureFormatter extends Formatter {
step?: AllureStep;
metadata: MetadataMessage;
}) {
const {
labels = [],
links = [],
parameter = [],
steps = [],
description,
descriptionHtml,
} = payload.metadata;

links.forEach((link) => payload.test.addLink(link.url, link.type, link.name));
labels.forEach((label) => payload.test.addLabel(label.name, label.value));
parameter.forEach(({ name, value, excluded, mode }) =>
payload.test.parameter(name, value, {
excluded,
mode,
}),
);
steps.forEach((step) => {
this.handleAllureStep({
test: payload.test,
step: payload.step,
stepMetadata: step,
});
});

if (description) {
payload.test.description = description;
}

if (descriptionHtml) {
payload.test.descriptionHtml = descriptionHtml;
}
}

private handleAllureStep(payload: {
test: AllureTest;
step?: AllureStep;
stepMetadata: StepMetadata;
}) {
const step = AllureCommandStepExecutable.toExecutableItem(
this.allureRuntime,
payload.stepMetadata,
);

if (payload.step) {
payload.step.addStep(step);
return;
}
payload.test.applyMetadata(payload.metadata, (step) => {
if (payload.step) {
payload.step.addStep(step);
return;
}

payload.test.addStep(step);
payload.test.addStep(step);
});
}

private onTestCaseFinished(data: messages.TestCaseFinished): void {
Expand Down
89 changes: 18 additions & 71 deletions packages/allure-hermione/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ import {
addLabel,
addLink,
addParameter,
getFileSrcPath,
getSuitePath,
sendMetadata,
setDescription,
setDescriptionHtml,
setDisplayName,
setHistoryId,
setTestCaseId,
} from "./utils";

export type HermioneAttachmentMessage = {
Expand Down Expand Up @@ -82,19 +85,24 @@ const hermioneAllureReporter = (hermione: Hermione, opts: AllureReportOptions) =
const { ALLURE_HOST_NAME, ALLURE_THREAD_NAME } = process.env;
const thread = ALLURE_THREAD_NAME || test.sessionId;
const hostnameLabel = ALLURE_HOST_NAME || hostname;
const fileSrcPath = getFileSrcPath(test.file!);
const testFullTitle = test.fullTitle();
const currentTest = new AllureTest(runtime, Date.now());
const suites = getSuitePath(test);

currentTest.name = test.title;
currentTest.fullName = test.fullTitle();
currentTest.historyId = md5(test.fullTitle());
currentTest.fullName = testFullTitle;
currentTest.stage = Stage.RUNNING;

currentTest.addLabel(LabelName.HOST, hostnameLabel);
currentTest.addLabel(LabelName.LANGUAGE, "javascript");
currentTest.addLabel(LabelName.FRAMEWORK, "hermione");
currentTest.addParameter("browser", test.browserId);

if (!currentTest.testCaseId) {
currentTest.testCaseId = md5(`${fileSrcPath}#${testFullTitle}`);
}

if (thread) {
currentTest.addLabel(LabelName.THREAD, thread);
}
Expand Down Expand Up @@ -139,75 +147,7 @@ const hermioneAllureReporter = (hermione: Hermione, opts: AllureReportOptions) =
return;
}

const {
attachments = [],
labels = [],
links = [],
parameter = [],
steps = [],
description,
descriptionHtml,
displayName,
} = metadata;

labels.forEach((label) => {
currentTest.addLabel(label.name, label.value);
});
links.forEach((link) => {
currentTest.addLink(link.url, link.name, link.type);
});
parameter.forEach((param) => {
currentTest.parameter(param.name, param.value, {
excluded: param.excluded,
mode: param.mode,
});
});
attachments.forEach((attachment) => {
const attachmentFilename = runtime.writeAttachment(
attachment.content,
attachment.type,
attachment.encoding,
);

currentTest.addAttachment(
"Attachment",
{
contentType: attachment.type,
},
attachmentFilename,
);
});

steps.forEach((step) => {
handleAllureStep(testId, step);
});

if (description) {
currentTest.description = description;
}

if (descriptionHtml) {
currentTest.descriptionHtml = descriptionHtml;
}

if (displayName) {
currentTest.name = displayName;
}
};
const handleAllureStep = (testId: string, stepMetadata: StepMetadata) => {
const currentTest = runningTests.get(testId);

if (!currentTest) {
// eslint-disable-next-line no-console
console.error(
`Can't create "${stepMetadata.name!}" step due test has been finished or hasn't been started`,
);
return;
}

const step = AllureCommandStepExecutable.toExecutableItem(runtime, stepMetadata);

currentTest.addStep(step);
currentTest.applyMetadata(metadata);
};

hermione.on(hermione.events.NEW_BROWSER, (browser, { browserId }) => {
Expand Down Expand Up @@ -280,6 +220,12 @@ const hermioneAllureReporter = (hermione: Hermione, opts: AllureReportOptions) =
browser.addCommand("descriptionHtml", async (id: string, value: string) => {
await setDescriptionHtml(testId(id), value);
});
browser.addCommand("testCaseId", async (id: string, value: string) => {
await setTestCaseId(testId(id), value);
});
browser.addCommand("historyId", async (id: string, value: string) => {
await setHistoryId(testId(id), value);
});
});
hermione.on(hermione.events.NEW_WORKER_PROCESS, (worker) => {
// eslint-disable-next-line
Expand Down Expand Up @@ -341,6 +287,7 @@ const hermioneAllureReporter = (hermione: Hermione, opts: AllureReportOptions) =
handleTestError(test, test.err);
}

currentTest.calculateHistoryId();
currentTest.stage = Stage.FINISHED;
currentTest.endTest(Date.now());
loadedTests.delete(testId());
Expand Down
21 changes: 21 additions & 0 deletions packages/allure-hermione/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { basename } from "path";
import { cwd } from "process";
import { MetadataMessage, ParameterOptions } from "allure-js-commons";
import { ALLURE_METADATA_CONTENT_TYPE } from "allure-js-commons/internal";

export const getFileSrcPath = (filePath: string): string => {
const baseDir = basename(cwd());

return filePath.replace(cwd(), baseDir);
};

export const getSuitePath = (test: Hermione.Test): string[] => {
const path = [];
let currentSuite = test.parent as Hermione.Suite;
Expand All @@ -15,6 +23,7 @@ export const getSuitePath = (test: Hermione.Test): string[] => {

return path;
};

export const sendMetadata = async (testId: string, metadata: MetadataMessage): Promise<void> =>
new Promise((resolve, reject) => {
process.send?.(
Expand Down Expand Up @@ -53,6 +62,18 @@ export const setDescriptionHtml = async (testId: string, descriptionHtml: string
});
};

export const setTestCaseId = async (testId: string, testCaseId: string) => {
await sendMetadata(testId, {
testCaseId,
});
};

export const setHistoryId = async (testId: string, historyId: string) => {
await sendMetadata(testId, {
historyId,
});
};

export const addLabel = async (testId: string, name: string, value: string) => {
await sendMetadata(testId, {
labels: [{ name, value }],
Expand Down
3 changes: 3 additions & 0 deletions packages/allure-hermione/test/fixtures/historyId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it("historyId", async ({ browser, currentTest }) => {
await browser.historyId(currentTest.id, "foo");
});
3 changes: 3 additions & 0 deletions packages/allure-hermione/test/fixtures/testCaseId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it("testCaseId", async ({ browser, currentTest }) => {
await browser.testCaseId(currentTest.id, "foo");
});
24 changes: 24 additions & 0 deletions packages/allure-hermione/test/spec/historyId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { TestResult } from "allure-js-commons";
import { expect } from "chai";
import Hermione from "hermione";
import { before, describe, it } from "mocha";
import { getTestResultByName } from "../runner";
import { HermioneAllure } from "../types";

describe("historyId", () => {
let results: TestResult[];

before(async () => {
const hermione = new Hermione("./test/.hermione.conf.js") as HermioneAllure;

await hermione.run(["./test/fixtures/historyId.js"], {});

results = hermione.allure.writer.results;
});

it("sets custom history id", () => {
const { historyId } = getTestResultByName(results, "historyId");

expect(historyId).eq("foo");
});
});
6 changes: 3 additions & 3 deletions packages/allure-hermione/test/spec/steps.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Status, TestResult } from "allure-js-commons";
import { expect } from "chai";
import Hermione from "hermione";
import { before, beforeEach, describe, it } from "mocha";
import { HermioneAllure } from "../types";
import { getTestResultByName } from "../runner";
import Hermione from "hermione";
import { HermioneAllure } from "../types";

describe("steps", () => {
let results: TestResult[];
Expand Down Expand Up @@ -32,7 +32,7 @@ describe("steps", () => {
});

describe("failed steps", () => {
it("fails the test with original step error", async () => {
it("fails the test with original step error", () => {
const { status, statusDetails, steps, labels } = getTestResultByName(results, "failed");

expect(status).eq(Status.FAILED);
Expand Down
24 changes: 24 additions & 0 deletions packages/allure-hermione/test/spec/testCaseId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { TestResult } from "allure-js-commons";
import { expect } from "chai";
import Hermione from "hermione";
import { before, describe, it } from "mocha";
import { getTestResultByName } from "../runner";
import { HermioneAllure } from "../types";

describe("testCaseId", () => {
let results: TestResult[];

before(async () => {
const hermione = new Hermione("./test/.hermione.conf.js") as HermioneAllure;

await hermione.run(["./test/fixtures/testCaseId.js"], {});

results = hermione.allure.writer.results;
});

it("sets custom test case id", () => {
const { testCaseId } = getTestResultByName(results, "testCaseId");

expect(testCaseId).eq("foo");
});
});
Loading
Loading