Skip to content

Commit

Permalink
add configurable links templates
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw committed Feb 21, 2024
1 parent 9ed18c1 commit e020515
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 7 deletions.
28 changes: 26 additions & 2 deletions packages/hermione-allure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
+ },
+ ],
+ }
}
}
Expand Down Expand Up @@ -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");
});
```

Expand Down
4 changes: 4 additions & 0 deletions packages/hermione-allure/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export type AllureReportOptions = {
enabled?: boolean;
resultsDir?: string;
writer?: AllureWriter;
links?: {
type: string;
urlTemplate: string;
}[];
};

export enum HermioneRuntimeMessageType {
Expand Down
32 changes: 29 additions & 3 deletions packages/hermione-allure/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
AllureTest,
ContentType,
LabelName,
Link,
Stage,
Status,
allureReportFolder,
Expand All @@ -30,10 +31,12 @@ const hostname = os.hostname();
export class AllureHermioneReporter {
hermione: Hermione;
runtime: AllureRuntime;
options: AllureReportOptions;
runningTests: Map<string, AllureTest> = new Map();
runningSteps: Map<string, AllureStep[]> = new Map();

constructor(hermione: Hermione, opts?: AllureReportOptions) {
this.options = opts || {};
this.hermione = hermione;
this.runtime = new AllureRuntime({
resultsDir: allureReportFolder(opts?.resultsDir),
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -83,7 +106,10 @@ export class AllureHermioneReporter {
});
});

currentTest.applyMetadata(metadata);
currentTest.applyMetadata({
...metadata,
links: this.processMetadataLinks(links),
});
}

private startAllureTest(test: Test) {
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion packages/hermione-allure/test/.hermione.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ module.exports = {
},
},
},
// saveHistoryMode: "none",
};
12 changes: 11 additions & 1 deletion packages/hermione-allure/test/spec/links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 () => {
Expand All @@ -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 });
});
});
10 changes: 10 additions & 0 deletions packages/hermione-allure/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e020515

Please sign in to comment.