Skip to content

Commit

Permalink
fix(commons): fix metadata regular expressions (via #1065)
Browse files Browse the repository at this point in the history
  • Loading branch information
delatrie authored Jul 18, 2024
1 parent 8ffa35d commit 7c3780b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/allure-js-commons/src/sdk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export const getMessageAndTraceFromError = (
};
};

export const allureIdRegexp = /@?allure.id[:=](?<id>[^\s]+)/;
export const allureIdRegexp = /(?:^|\s)@?allure\.id[:=](?<id>[^\s]+)/;
export const allureIdRegexpGlobal = new RegExp(allureIdRegexp, "g");
export const allureLabelRegexp = /@?allure.label.(?<name>[^\s]+?)[:=](?<value>[^\s]+)/;
export const allureLabelRegexp = /(?:^|\s)@?allure\.label\.(?<name>[^:=\s]+)[:=](?<value>[^\s]+)/;
export const allureLabelRegexpGlobal = new RegExp(allureLabelRegexp, "g");

export const extractMetadataFromString = (
Expand Down
72 changes: 71 additions & 1 deletion packages/allure-js-commons/test/sdk/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import assert from "node:assert";
import { describe, expect, it } from "vitest";
import { LabelName, Status } from "../../src/model.js";
import type { FixtureResult, StepResult, TestResult } from "../../src/model.js";
import { allureLabelRegexp, getStatusFromError, isAnyStepFailed } from "../../src/sdk/utils.js";
import {
allureLabelRegexp,
extractMetadataFromString,
getStatusFromError,
isAnyStepFailed,
} from "../../src/sdk/utils.js";

type Executable = StepResult | TestResult | FixtureResult;

Expand Down Expand Up @@ -174,3 +179,68 @@ describe("getStatusFromError", () => {
});
});
});

describe("extractMetadataFromString", () => {
it("should preserve the title if no matches", () => {
expect(extractMetadataFromString("")).toEqual({
cleanTitle: "",
labels: [],
});
expect(extractMetadataFromString("foo")).toEqual({
cleanTitle: "foo",
labels: [],
});
});

it("should extract Allure ID", () => {
expect(extractMetadataFromString("foo @allure.id:1004")).toEqual({
cleanTitle: "foo",
labels: [{ name: "ALLURE_ID", value: "1004" }],
});
expect(extractMetadataFromString("foo allure.id:1004")).toEqual({
cleanTitle: "foo",
labels: [{ name: "ALLURE_ID", value: "1004" }],
});
expect(extractMetadataFromString("foo @allure.id=1004")).toEqual({
cleanTitle: "foo",
labels: [{ name: "ALLURE_ID", value: "1004" }],
});
});

it("should extract label", () => {
expect(extractMetadataFromString("foo @allure.label.bar:baz")).toEqual({
cleanTitle: "foo",
labels: [{ name: "bar", value: "baz" }],
});
expect(extractMetadataFromString("foo allure.label.bar:baz")).toEqual({
cleanTitle: "foo",
labels: [{ name: "bar", value: "baz" }],
});
expect(extractMetadataFromString("foo @allure.label.bar=baz")).toEqual({
cleanTitle: "foo",
labels: [{ name: "bar", value: "baz" }],
});
});

it("should extract all matches", () => {
expect(extractMetadataFromString("foo @allure.label.bar:baz @allure.id:1004 @allure.label.qux:quz")).toEqual({
cleanTitle: "foo",
labels: [
{ name: "bar", value: "baz" },
{ name: "ALLURE_ID", value: "1004" },
{ name: "qux", value: "quz" },
],
});
});

it("should support multiline names", () => {
expect(extractMetadataFromString("foo @allure.label.l1=v1\nbar @allure.id:1004 @allure.label.l2:v2 baz")).toEqual({
cleanTitle: "foo\nbar baz",
labels: [
{ name: "l1", value: "v1" },
{ name: "ALLURE_ID", value: "1004" },
{ name: "l2", value: "v2" },
],
});
});
});
2 changes: 1 addition & 1 deletion packages/allure-playwright/test/spec/titleMetadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ something else in name @allure.label.l2=v2 @allure.label.l3=v3 some word\`, asyn
}),
]);

expect(results.tests[0].name).toBe("some strange name to test \nsomething else in name some word");
expect(results.tests[0].name).toBe("some strange name to test\nsomething else in name some word");
});

0 comments on commit 7c3780b

Please sign in to comment.