From e0205159de78182fbf838930579b4ae2e9642c2e Mon Sep 17 00:00:00 2001 From: epszaw Date: Wed, 21 Feb 2024 15:23:55 +0100 Subject: [PATCH] add configurable links templates --- packages/hermione-allure/README.md | 28 ++++++++++++++-- packages/hermione-allure/src/model.ts | 4 +++ packages/hermione-allure/src/reporter.ts | 32 +++++++++++++++++-- .../hermione-allure/test/.hermione.conf.js | 1 - .../hermione-allure/test/spec/links.test.ts | 12 ++++++- packages/hermione-allure/test/utils.ts | 10 ++++++ 6 files changed, 80 insertions(+), 7 deletions(-) diff --git a/packages/hermione-allure/README.md b/packages/hermione-allure/README.md index bed6d2d87..62c35fdd6 100644 --- a/packages/hermione-allure/README.md +++ b/packages/hermione-allure/README.md @@ -28,7 +28,17 @@ Add `hermione-allure` field to `plugins` in your `.hermione.conf.js` file: module.exports = { plugins: { + "hermione-allure": { -+ resultsDir: "./allure-results" ++ resultsDir: "./allure-results", ++ links: [ ++ { ++ type: "issue", ++ urlTemplate: "https://example.org/issue/%s", ++ }, ++ { ++ type: "tms", ++ urlTemplate: "https://example.org/task/%s", ++ }, ++ ], + } } } @@ -130,7 +140,21 @@ Or using aliases: `issue`, `tms`: import { allure } from "hermione-allure/runtime"; it("my test", async ({ currentTest }) => { - await allure(currentTest).issue("my_link_name", "http://example.org"); + await allure(currentTest).issue("http://example.org", "my_link_name"); + await allure(currentTest).tms("http://example.org", "my_link_name"); +}); +``` + +If you configured links templates you can use shorter syntax of links as well: + +```js +import { allure } from "hermione-allure/runtime"; + +it("my test", async ({ currentTest }) => { + await allure(currentTest).issue("1"); + await allure(currentTest).issue("2", "Issue name"); + await allure(currentTest).tms("1"); + await allure(currentTest).tms("2", "Task name"); }); ``` diff --git a/packages/hermione-allure/src/model.ts b/packages/hermione-allure/src/model.ts index 0d10a827f..e5ec793c8 100644 --- a/packages/hermione-allure/src/model.ts +++ b/packages/hermione-allure/src/model.ts @@ -5,6 +5,10 @@ export type AllureReportOptions = { enabled?: boolean; resultsDir?: string; writer?: AllureWriter; + links?: { + type: string; + urlTemplate: string; + }[]; }; export enum HermioneRuntimeMessageType { diff --git a/packages/hermione-allure/src/reporter.ts b/packages/hermione-allure/src/reporter.ts index 8b9828ca6..b4b57a1b0 100644 --- a/packages/hermione-allure/src/reporter.ts +++ b/packages/hermione-allure/src/reporter.ts @@ -8,6 +8,7 @@ import { AllureTest, ContentType, LabelName, + Link, Stage, Status, allureReportFolder, @@ -30,10 +31,12 @@ const hostname = os.hostname(); export class AllureHermioneReporter { hermione: Hermione; runtime: AllureRuntime; + options: AllureReportOptions; runningTests: Map = new Map(); runningSteps: Map = new Map(); constructor(hermione: Hermione, opts?: AllureReportOptions) { + this.options = opts || {}; this.hermione = hermione; this.runtime = new AllureRuntime({ resultsDir: allureReportFolder(opts?.resultsDir), @@ -51,6 +54,26 @@ export class AllureHermioneReporter { this.hermione.on(this.hermione.events.TEST_END, this.onTestEnd.bind(this)); } + private processMetadataLinks(links: Link[]): Link[] { + return links.map((link) => { + // TODO: + // @ts-ignore + const matcher = this.options.links?.find?.(({ type }) => type === link.type); + + // TODO: + if (!matcher || link.url.startsWith("http")) { + return link; + } + + const url = matcher.urlTemplate.replace("%s", link.url); + + return { + ...link, + url, + }; + }); + } + private applyMetadata(message: HermioneMetadataMessage) { const currentTest = this.runningTests.get(message.testId); @@ -63,7 +86,7 @@ export class AllureHermioneReporter { const currentSteps = this.runningSteps.get(message.testId) || []; const currentStep = currentSteps[currentSteps.length - 1]; const currentExecutable = (currentStep || currentTest) as AllureTest | AllureStep; - const { attachments = [], parameter = [], ...metadata } = message.metadata; + const { links = [], attachments = [], parameter = [], ...metadata } = message.metadata; attachments.forEach((attachment) => { const attachmentFilename = this.runtime.writeAttachment(attachment.content, attachment.type, attachment.encoding); @@ -83,7 +106,10 @@ export class AllureHermioneReporter { }); }); - currentTest.applyMetadata(metadata); + currentTest.applyMetadata({ + ...metadata, + links: this.processMetadataLinks(links), + }); } private startAllureTest(test: Test) { @@ -128,7 +154,7 @@ export class AllureHermioneReporter { if (!currentExecutable) { // eslint-disable-next-line no-console console.error("Can't start step because there isn't any running test or step!"); - return + return; } const currentStep = currentExecutable.startStep(message.name); diff --git a/packages/hermione-allure/test/.hermione.conf.js b/packages/hermione-allure/test/.hermione.conf.js index e79cc63b7..ed0c86805 100644 --- a/packages/hermione-allure/test/.hermione.conf.js +++ b/packages/hermione-allure/test/.hermione.conf.js @@ -11,5 +11,4 @@ module.exports = { }, }, }, - // saveHistoryMode: "none", }; diff --git a/packages/hermione-allure/test/spec/links.test.ts b/packages/hermione-allure/test/spec/links.test.ts index 5dc7de4a7..10d7b0501 100644 --- a/packages/hermione-allure/test/spec/links.test.ts +++ b/packages/hermione-allure/test/spec/links.test.ts @@ -2,7 +2,7 @@ import { expect } from "chai"; import { LinkType } from "allure-js-commons"; import { runHermioneInlineTest } from "../utils"; -describe("labels", () => { +describe("links", () => { it("adds `bar` custom link", async () => { const { tests } = await runHermioneInlineTest(` const { allure } = require("hermione-allure/dist/runtime.js"); @@ -22,11 +22,16 @@ describe("labels", () => { it("tms", async ({ currentTest }) => { await allure(currentTest).tms("https://example.org", "foo"); + await allure(currentTest).tms("1", "bar"); + await allure(currentTest).tms("2", "baz"); }); `); expect(tests).length(1); + expect(tests[0].links).length(3); tests[0].links.should.contain.something.like({ name: "foo", url: "https://example.org", type: LinkType.TMS }); + tests[0].links.should.contain.something.like({ name: "bar", url: "https://example.org/task/1", type: LinkType.TMS }); + tests[0].links.should.contain.something.like({ name: "baz", url: "https://example.org/task/2", type: LinkType.TMS }); }); it("adds `foo` issue link", async () => { @@ -35,10 +40,15 @@ describe("labels", () => { it("issue", async ({ currentTest }) => { await allure(currentTest).issue("https://example.org", "foo"); + await allure(currentTest).issue("1", "bar"); + await allure(currentTest).issue("2", "baz"); }); `); expect(tests).length(1); + expect(tests[0].links).length(3); tests[0].links.should.contain.something.like({ name: "foo", url: "https://example.org", type: LinkType.ISSUE }); + tests[0].links.should.contain.something.like({ name: "bar", url: "https://example.org/issue/1", type: LinkType.ISSUE }); + tests[0].links.should.contain.something.like({ name: "baz", url: "https://example.org/issue/2", type: LinkType.ISSUE }); }); }); diff --git a/packages/hermione-allure/test/utils.ts b/packages/hermione-allure/test/utils.ts index d030378a7..30f8e0da6 100644 --- a/packages/hermione-allure/test/utils.ts +++ b/packages/hermione-allure/test/utils.ts @@ -18,6 +18,16 @@ export const runHermioneInlineTest = async (test: string) => { hermioneAllure(hermione, { writer, + links: [ + { + type: "issue", + urlTemplate: "https://example.org/issue/%s", + }, + { + type: "tms", + urlTemplate: "https://example.org/task/%s", + }, + ], }); try {