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

Change link template config type from an array to a record. Support link template functions #1008

Merged
merged 4 commits into from
Jun 20, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ it("sets runtime links", async () => {
require: require.resolve("allure-codeceptjs"),
testMode: true,
enabled: true,
links: [
{
type: "${LinkType.ISSUE}",
links: {
issue: {
urlTemplate: "https://example.org/issues/%s",
},
{
type: "${LinkType.TMS}",
tms: {
urlTemplate: "https://example.org/tasks/%s",
}
]
}
},
},
helpers: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ it("sets runtime links", async () => {
require: require.resolve("allure-codeceptjs"),
testMode: true,
enabled: true,
links: [
{
type: "${LinkType.ISSUE}",
links: {
issue: {
urlTemplate: "https://example.org/issues/%s",
},
{
type: "${LinkType.TMS}",
tms: {
urlTemplate: "https://example.org/tasks/%s",
}
]
}
},
},
helpers: {
Expand Down
12 changes: 4 additions & 8 deletions packages/allure-cucumberjs/src/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { LabelName, LinkType } from "allure-js-commons";
import type { Config } from "allure-js-commons/sdk/reporter";
import type { LabelName } from "allure-js-commons";
import type { Config, LinkConfig, LinkTypeOptions } from "allure-js-commons/sdk/reporter";

export const ALLURE_SETUP_REPORTER_HOOK = "__allure_reporter_setup_hook__";

Expand All @@ -8,14 +8,10 @@ export type LabelConfig = {
name: LabelName | string;
};

export type LinkConfig = {
pattern: RegExp[];
urlTemplate: string;
type: LinkType | string;
};
export type AllureCucumberLinkConfig = LinkConfig<LinkTypeOptions & { pattern: RegExp[] }>;

export interface AllureCucumberReporterConfig extends Omit<Config, "writer" | "links"> {
testMode?: boolean;
links?: LinkConfig[];
links?: AllureCucumberLinkConfig;
labels?: LabelConfig[];
}
22 changes: 11 additions & 11 deletions packages/allure-cucumberjs/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import {
FileSystemWriter,
MessageWriter,
ReporterRuntime,
applyLinkTemplate,
createStepResult,
getWorstStepResultStatus,
md5,
} from "allure-js-commons/sdk/reporter";
import type { Config } from "allure-js-commons/sdk/reporter";
import { AllureCucumberWorld } from "./legacy.js";
import type { AllureCucumberReporterConfig, LabelConfig, LinkConfig } from "./model.js";
import type { AllureCucumberLinkConfig, AllureCucumberReporterConfig, LabelConfig } from "./model.js";
import { ALLURE_SETUP_REPORTER_HOOK } from "./model.js";

const { ALLURE_THREAD_NAME } = process.env;
Expand All @@ -28,7 +28,7 @@ export default class AllureCucumberReporter extends Formatter {
private readonly afterHooks: Record<string, TestCaseHookDefinition> = {};
private readonly beforeHooks: Record<string, TestCaseHookDefinition> = {};

private linksConfigs: LinkConfig[] = [];
private linksConfigs: AllureCucumberLinkConfig = {};
private labelsConfigs: LabelConfig[] = [];
private allureRuntime: ReporterRuntime;

Expand Down Expand Up @@ -59,10 +59,10 @@ export default class AllureCucumberReporter extends Formatter {
: new FileSystemWriter({
resultsDir,
}),
links: links as Config["links"] | undefined,
links,
...rest,
});
this.linksConfigs = links || [];
this.linksConfigs = links || {};
this.labelsConfigs = labels || [];

options.eventBroadcaster.on("envelope", this.parseEnvelope.bind(this));
Expand All @@ -85,8 +85,8 @@ export default class AllureCucumberReporter extends Formatter {

private get tagsIgnorePatterns(): RegExp[] {
const { labelsConfigs, linksConfigs } = this;

return [...labelsConfigs, ...linksConfigs].flatMap(({ pattern }) => pattern);
const linkConfigEntries = Object.entries(linksConfigs).map(([, v]) => v);
return [...labelsConfigs, ...linkConfigEntries].flatMap(({ pattern }) => pattern);
}

private parseEnvelope(envelope: messages.Envelope) {
Expand Down Expand Up @@ -155,18 +155,18 @@ export default class AllureCucumberReporter extends Formatter {
const tagKeyRe = /^@\S+=/;
const links: Link[] = [];

if (this.linksConfigs.length === 0) {
if (Object.keys(this.linksConfigs).length === 0) {
return links;
}

this.linksConfigs.forEach((matcher) => {
Object.entries(this.linksConfigs).forEach(([type, matcher]) => {
const matchedTags = tags.filter((tag) => matcher.pattern.some((pattern) => pattern.test(tag.name)));
const matchedLinks = matchedTags.map((tag) => {
const tagValue = tag.name.replace(tagKeyRe, "");

return {
url: matcher.urlTemplate.replace(/%s$/, tagValue) || tagValue,
type: matcher.type,
url: applyLinkTemplate(matcher.urlTemplate, tagValue) || tagValue,
type,
};
});

Expand Down
10 changes: 4 additions & 6 deletions packages/allure-cucumberjs/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ export const runCucumberInlineTest = async (
name: "severity",
},
],
links: [
{
links: {
issue: {
pattern: [/@issue=(.*)/],
type: "issue",
urlTemplate: "https://example.com/issues/%s",
},
{
tms: {
pattern: [/@tms=(.*)/],
type: "tms",
urlTemplate: "https://example.com/tasks/%s",
},
],
},
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions packages/allure-cypress/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import type Cypress from "cypress";
import { ContentType, LabelName, Stage } from "allure-js-commons";
import { extractMetadataFromString } from "allure-js-commons/sdk";
import { FileSystemWriter, ReporterRuntime, getSuiteLabels } from "allure-js-commons/sdk/reporter";
import type { LinkConfig } from "allure-js-commons/sdk/reporter";
import type { CypressRuntimeMessage, CypressTestEndRuntimeMessage, CypressTestStartRuntimeMessage } from "./model.js";

export type AllureCypressConfig = {
resultsDir?: string;
links?: {
type: string;
urlTemplate: string;
}[];
links?: LinkConfig;
};

export class AllureCypress {
Expand Down
10 changes: 4 additions & 6 deletions packages/allure-cypress/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ export const runCypressInlineTest = async (
viewportWidth: 1240,
setupNodeEvents: (on, config) => {
const reporter = allureCypress(on, {
links: [
{
type: "issue",
links: {
issue: {
urlTemplate: "https://allurereport.org/issues/%s"
},
{
type: "tms",
tms: {
urlTemplate: "https://allurereport.org/tasks/%s"
},
]
}
});

on("after:spec", (spec, result) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ const fixture = `

const reporter = new AllureJasmineReporter({
testMode: true,
links: [
{
type: "issue",
links: {
issue: {
urlTemplate: "https://example.org/issues/%s",
},
{
type: "tms",
tms: {
urlTemplate: "https://example.org/tasks/%s",
}
],
},
categories: [
{
name: "Sad tests",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ const fixture = `

const reporter = new AllureJasmineReporter({
testMode: true,
links: [
{
type: "issue",
links: {
issue: {
urlTemplate: "https://example.org/issues/%s",
},
{
type: "tms",
tms: {
urlTemplate: "https://example.org/tasks/%s",
}
],
},
categories: [
{
name: "Sad tests",
Expand Down
10 changes: 4 additions & 6 deletions packages/allure-jest/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ export const runJestInlineTest = async (test: string): Promise<AllureResults> =>
testEnvironment: require.resolve("allure-jest/node"),
testEnvironmentOptions: {
testMode: true,
links: [
{
type: "issue",
links: {
issue: {
urlTemplate: "https://example.org/issues/%s",
},
{
type: "tms",
tms: {
urlTemplate: "https://example.org/tasks/%s",
}
]
}
},
};

Expand Down
1 change: 1 addition & 0 deletions packages/allure-js-commons/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export enum ContentType {

/* eslint-disable no-shadow */
export enum LinkType {
DEFAULT = "link",
ISSUE = "issue",
TMS = "tms",
}
Expand Down
45 changes: 5 additions & 40 deletions packages/allure-js-commons/src/sdk/reporter/ReporterRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint max-lines: 0 */
import { extname } from "path";
import type { Attachment, AttachmentOptions, FixtureResult, Link, StepResult, TestResult } from "../../model.js";
import type { Attachment, AttachmentOptions, FixtureResult, StepResult, TestResult } from "../../model.js";
import { Stage } from "../../model.js";
import type {
Category,
Expand All @@ -20,7 +20,7 @@ import { MutableAllureContextHolder, StaticContextProvider } from "./context/Sta
import type { AllureContextProvider } from "./context/types.js";
import { createFixtureResult, createStepResult, createTestResult } from "./factory.js";
import type { Config, FixtureType, FixtureWrapper, LinkConfig, TestScope, Writer } from "./types.js";
import { deepClone, randomUuid } from "./utils.js";
import { deepClone, formatLinks, randomUuid } from "./utils.js";
import { getTestResultHistoryId, getTestResultTestCaseId } from "./utils.js";
import { buildAttachmentFileName } from "./utils/attachments.js";
import { resolveWriter } from "./writer/loader.js";
Expand Down Expand Up @@ -144,7 +144,7 @@ type MessageTargets = {
export class ReporterRuntime {
private readonly state = new LifecycleState();
private notifier: Notifier;
private links: LinkConfig[] = [];
private links: LinkConfig;
private contextProvider: AllureContextProvider;
writer: Writer;
categories?: Category[];
Expand All @@ -153,7 +153,7 @@ export class ReporterRuntime {
constructor({
writer,
listeners = [],
links = [],
links = {},
environmentInfo,
categories,
contextProvider = StaticContextProvider.wrap(new MutableAllureContextHolder()),
Expand Down Expand Up @@ -784,7 +784,7 @@ export class ReporterRuntime {

private handleMetadataMessage = (message: RuntimeMetadataMessage, { test, root, step }: MessageTargets) => {
const { links = [], attachments = [], displayName, parameters = [], labels = [], ...rest } = message.data;
const formattedLinks = this.formatLinks(links);
const formattedLinks = formatLinks(this.links, links);

if (displayName) {
root.name = displayName;
Expand Down Expand Up @@ -1047,41 +1047,6 @@ export class ReporterRuntime {
}
};

private formatLinks = (links: Link[]) => {
if (!this.links.length) {
return links;
}

return links.map((link) => {
// TODO:
// @ts-ignore
const matcher = this.links?.find?.(({ type }) => type === link.type);

// TODO:
if (!matcher || link.url.startsWith("http")) {
return link;
}

const url = matcher.urlTemplate.replace("%s", link.url);

// we shouldn't need to reassign already assigned name
if (link.name || !matcher.nameTemplate) {
return {
...link,
url,
};
}

const name = matcher.nameTemplate.replace("%s", link.url);

return {
...link,
name,
url,
};
});
};

private introduceTestIntoScopes = (testUuid: string, scopeUuid: string) => {
const scope = this.state.getScope(scopeUuid);
if (!scope) {
Expand Down
17 changes: 11 additions & 6 deletions packages/allure-js-commons/src/sdk/reporter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
FixtureResult,
Label,
Link,
LinkType,
Parameter,
StepResult,
TestResult,
Expand Down Expand Up @@ -61,11 +62,15 @@ export interface LifecycleListener {
afterStepStop?: (result: StepResult) => void;
}

export interface LinkConfig {
type: string;
urlTemplate: string;
nameTemplate?: string;
}
export type LinkTemplate = string | ((url: string) => string);

export type LinkTypeOptions = {
urlTemplate: LinkTemplate;
nameTemplate?: LinkTemplate;
};

export type LinkConfig<TOpts extends LinkTypeOptions = LinkTypeOptions> = Partial<Record<LinkType, TOpts>> &
Record<string, TOpts>;

export type WriterDescriptor = [cls: string, ...args: readonly unknown[]] | string;

Expand All @@ -74,7 +79,7 @@ export interface Config {
readonly writer: Writer | WriterDescriptor;
// TODO: handle lifecycle hooks here
readonly testMapper?: (test: TestResult) => TestResult | null;
readonly links?: LinkConfig[];
readonly links?: LinkConfig;
readonly listeners?: LifecycleListener[];
readonly environmentInfo?: EnvironmentInfo;
readonly categories?: Category[];
Expand Down
Loading
Loading