Skip to content

Commit

Permalink
global labels support (fixes #1040, via #1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw authored Jul 5, 2024
1 parent a66bd07 commit 7ea9ab3
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 11 deletions.
9 changes: 8 additions & 1 deletion packages/allure-codeceptjs/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import path from "node:path";
import { env } from "node:process";
import { LabelName, Stage, Status, type StepResult } from "allure-js-commons";
import { type RuntimeMessage, extractMetadataFromString, getMessageAndTraceFromError } from "allure-js-commons/sdk";
import { FileSystemWriter, MessageWriter, ReporterRuntime, md5 } from "allure-js-commons/sdk/reporter";
import {
FileSystemWriter,
MessageWriter,
ReporterRuntime,
getEnvironmentLabels,
md5,
} from "allure-js-commons/sdk/reporter";
import type { Config } from "allure-js-commons/sdk/reporter";
import { extractMeta } from "./helpers.js";
import type { CodeceptError, CodeceptHook, CodeceptStep, CodeceptTest } from "./model.js";
Expand Down Expand Up @@ -72,6 +78,7 @@ export class AllureCodeceptJsReporter {
result.labels.push(...titleMetadata.labels);
result.labels.push({ name: LabelName.LANGUAGE, value: "javascript" });
result.labels.push({ name: LabelName.FRAMEWORK, value: "codeceptjs" });
result.labels.push(...getEnvironmentLabels());

if (test?.parent?.title) {
result.labels.push({
Expand Down
25 changes: 25 additions & 0 deletions packages/allure-codeceptjs/test/spec/globalLabels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect, it } from "vitest";
import { runCodeceptJsInlineTest } from "../utils.js";

it("sets allure labels from env variables", async () => {
const { tests } = await runCodeceptJsInlineTest(
{
"sample.test.js": `
Feature("sample-feature");
Scenario("sample-scenario", async () => {});
`,
},
{
ALLURE_LABEL_A: "a",
ALLURE_LABEL_B: "b",
},
);

expect(tests).toHaveLength(1);
expect(tests[0].labels).toEqual(
expect.arrayContaining([
{ name: "A", value: "a" },
{ name: "B", value: "b" },
]),
);
});
2 changes: 2 additions & 0 deletions packages/allure-cucumberjs/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ReporterRuntime,
applyLinkTemplate,
createStepResult,
getEnvironmentLabels,
getWorstStepResultStatus,
md5,
} from "allure-js-commons/sdk/reporter";
Expand Down Expand Up @@ -255,6 +256,7 @@ export default class AllureCucumberReporter extends Formatter {
fullName,
};

result.labels!.push(...getEnvironmentLabels());
result.labels!.push(
{
name: LabelName.HOST,
Expand Down
92 changes: 92 additions & 0 deletions packages/allure-cucumberjs/test/spec/globalLabels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { expect, it } from "vitest";
import { Stage, Status } from "allure-js-commons";
import { runCucumberInlineTest } from "../utils.js";

it("sets label from env variables", async () => {
const { tests } = await runCucumberInlineTest(["simple"], ["simple"], undefined, undefined, {
ALLURE_LABEL_A: "a",
ALLURE_LABEL_B: "b",
});

expect(tests).toHaveLength(3);
expect(tests).toContainEqual(
expect.objectContaining({
name: "passed",
status: Status.PASSED,
stage: Stage.FINISHED,
steps: [
expect.objectContaining({
name: "Given a passed step",
status: Status.PASSED,
stage: Stage.FINISHED,
}),
],
labels: expect.arrayContaining([
{
name: "A",
value: "a",
},
{
name: "B",
value: "b",
},
]),
}),
);
expect(tests).toContainEqual(
expect.objectContaining({
name: "failed",
status: Status.FAILED,
stage: Stage.FINISHED,
steps: [
expect.objectContaining({
name: "Given a failed step",
status: Status.FAILED,
stage: Stage.FINISHED,
statusDetails: expect.objectContaining({
message: expect.stringContaining("AssertionError"),
trace: expect.any(String),
}),
}),
],
labels: expect.arrayContaining([
{
name: "A",
value: "a",
},
{
name: "B",
value: "b",
},
]),
}),
);
expect(tests).toContainEqual(
expect.objectContaining({
name: "broken",
status: Status.BROKEN,
stage: Stage.FINISHED,
steps: [
expect.objectContaining({
name: "Given a broken step",
status: Status.BROKEN,
stage: Stage.FINISHED,
statusDetails: expect.objectContaining({
message: expect.stringContaining("Error"),
trace: expect.any(String),
}),
}),
],
labels: expect.arrayContaining([
{
name: "A",
value: "a",
},
{
name: "B",
value: "b",
},
]),
}),
);
});
15 changes: 11 additions & 4 deletions packages/allure-cucumberjs/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const runCucumberInlineTest = async (
stepsDefs: string[],
parallel: boolean = true,
testPlan?: TestPlanV1,
env?: Record<string, string>,
): Promise<AllureResults> => {
const fixturesPath = join(__dirname, "fixtures");
const testDir = join(__dirname, "fixtures/temp", randomUUID());
Expand Down Expand Up @@ -119,14 +120,20 @@ export const runCucumberInlineTest = async (
});
}

const env: Record<string, string> = {};
const finalEnv: Record<string, string> = {
...env,
};

if (testPlan) {
await step("testplan.json", async () => {
const data = JSON.stringify(testPlan);
const testPlanPath = join(testDir, "testplan.json");
await writeFile(testPlanPath, data, "utf8");
env.ALLURE_TESTPLAN_PATH = testPlanPath;
await attachment("testplan.json", data, { contentType: "application/json", fileExtension: ".json" });
finalEnv.ALLURE_TESTPLAN_PATH = testPlanPath;
await attachment("testplan.json", data, {
contentType: "application/json",
fileExtension: ".json",
});
});
}

Expand All @@ -138,7 +145,7 @@ export const runCucumberInlineTest = async (
return fork(modulePath, args, {
env: {
...process.env,
...env,
...finalEnv,
ALLURE_TEST_MODE: "1",
},
cwd: testDir,
Expand Down
9 changes: 8 additions & 1 deletion packages/allure-cypress/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import type Cypress from "cypress";
import { ContentType, LabelName, Stage, Status } from "allure-js-commons";
import type { RuntimeMessage } from "allure-js-commons/sdk";
import { extractMetadataFromString } from "allure-js-commons/sdk";
import { FileSystemWriter, ReporterRuntime, getSuiteLabels, parseTestPlan } from "allure-js-commons/sdk/reporter";
import {
FileSystemWriter,
ReporterRuntime,
getEnvironmentLabels,
getSuiteLabels,
parseTestPlan,
} from "allure-js-commons/sdk/reporter";
import type { Config } from "allure-js-commons/sdk/reporter";
import type {
CypressHookEndMessage,
Expand Down Expand Up @@ -154,6 +160,7 @@ export class AllureCypress {
},
...suiteLabels,
...titleMetadata.labels,
...getEnvironmentLabels(),
],
},
runContext.scopes,
Expand Down
29 changes: 29 additions & 0 deletions packages/allure-cypress/test/spec/globalLabels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect, it } from "vitest";
import { Stage, Status } from "allure-js-commons";
import { runCypressInlineTest } from "../utils.js";

it("sets allure labels from env variables", async () => {
const { tests } = await runCypressInlineTest(
{
"cypress/e2e/sample.cy.js": () => `
it("passed", () => {
cy.wrap(1).should("eq", 1);
});
`,
},
() => ({
ALLURE_LABEL_A: "a",
ALLURE_LABEL_B: "b",
}),
);

expect(tests).toHaveLength(1);
expect(tests[0].status).toBe(Status.PASSED);
expect(tests[0].stage).toBe(Stage.FINISHED);
expect(tests[0].labels).toEqual(
expect.arrayContaining([
{ name: "A", value: "a" },
{ name: "B", value: "b" },
]),
);
});
9 changes: 8 additions & 1 deletion packages/allure-jasmine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { Stage, Status } from "allure-js-commons";
import type { RuntimeMessage } from "allure-js-commons/sdk";
import { isPromise } from "allure-js-commons/sdk";
import type { Config, FixtureType } from "allure-js-commons/sdk/reporter";
import { FileSystemWriter, MessageWriter, ReporterRuntime, getSuiteLabels } from "allure-js-commons/sdk/reporter";
import {
FileSystemWriter,
MessageWriter,
ReporterRuntime,
getEnvironmentLabels,
getSuiteLabels,
} from "allure-js-commons/sdk/reporter";
import { MessageTestRuntime, setGlobalTestRuntime } from "allure-js-commons/sdk/runtime";
import type { JasmineBeforeAfterFn } from "./model.js";
import { findAnyError, findMessageAboutThrow } from "./utils.js";
Expand Down Expand Up @@ -143,6 +149,7 @@ export default class AllureJasmineReporter implements jasmine.CustomReporter {
const suitesLabels = getSuiteLabels(specPath);

result.labels.push(...suitesLabels);
result.labels.push(...getEnvironmentLabels());

if (spec.status === "pending" || spec.status === "disabled" || spec.status === "excluded") {
result.status = Status.SKIPPED;
Expand Down
26 changes: 26 additions & 0 deletions packages/allure-jasmine/test/spec/globalLabels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect, it } from "vitest";
import { runJasmineInlineTest } from "../utils.js";

it("sets allure labels from env variables", async () => {
const { tests } = await runJasmineInlineTest(
{
"spec/test/sample1.spec.js": `
it("should pass", () => {
expect(true).toBe(true);
});
`,
},
{
ALLURE_LABEL_A: "a",
ALLURE_LABEL_B: "b",
},
);

expect(tests).toHaveLength(1);
expect(tests[0].labels).toEqual(
expect.arrayContaining([
{ name: "A", value: "a" },
{ name: "B", value: "b" },
]),
);
});
6 changes: 5 additions & 1 deletion packages/allure-jasmine/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { attachment, step } from "allure-js-commons";
import type { AllureResults } from "allure-js-commons/sdk";
import { MessageReader } from "allure-js-commons/sdk/reporter";

export const runJasmineInlineTest = async (files: Record<string, string>): Promise<AllureResults> => {
export const runJasmineInlineTest = async (
files: Record<string, string>,
env?: Record<string, string>,
): Promise<AllureResults> => {
const testDir = join(__dirname, "temp", randomUUID());
const testFiles = {
"spec/support/jasmine.json": await readFile(join(__dirname, "./fixtures/spec/support/jasmine.json"), "utf8"),
Expand Down Expand Up @@ -34,6 +37,7 @@ export const runJasmineInlineTest = async (files: Record<string, string>): Promi
return fork(modulePath, args, {
env: {
...process.env,
...env,
ALLURE_TEST_MODE: "1",
},
cwd: testDir,
Expand Down
2 changes: 2 additions & 0 deletions packages/allure-jest/src/environmentFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
FileSystemWriter,
MessageWriter,
ReporterRuntime,
getEnvironmentLabels,
getSuiteLabels,
parseTestPlan,
} from "allure-js-commons/sdk/reporter";
Expand Down Expand Up @@ -211,6 +212,7 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
name: LabelName.PACKAGE,
value: packageLabel,
},
...getEnvironmentLabels(),
],
},
[scopeUuid],
Expand Down
26 changes: 26 additions & 0 deletions packages/allure-jest/test/spec/globalLabels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect, it } from "vitest";
import { runJestInlineTest } from "../utils.js";

it("sets allure labels from env variables", async () => {
const { tests } = await runJestInlineTest(
{
"sample.spec.js": `
it("should pass", () => {
expect(true).toBe(true);
});
`,
},
() => ({
ALLURE_LABEL_A: "a",
ALLURE_LABEL_B: "b",
}),
);

expect(tests).toHaveLength(1);
expect(tests[0].labels).toEqual(
expect.arrayContaining([
{ name: "A", value: "a" },
{ name: "B", value: "b" },
]),
);
});
3 changes: 2 additions & 1 deletion packages/allure-playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
MessageWriter,
ReporterRuntime,
escapeRegExp,
getEnvironmentLabels,
md5,
parseTestPlan,
readImageAsBase64,
Expand Down Expand Up @@ -178,7 +179,7 @@ export class AllureReporter implements ReporterV2 {
const testCaseIdBase = `${relativeFile}#${nameSuites}${test.title}`;
const result: Partial<TestResult> = {
name: titleMetadata.cleanTitle,
labels: [...titleMetadata.labels],
labels: [...titleMetadata.labels, ...getEnvironmentLabels()],
links: [],
parameters: [],
testCaseId: md5(testCaseIdBase),
Expand Down
Loading

0 comments on commit 7ea9ab3

Please sign in to comment.