Skip to content

Commit

Permalink
chore(allure-js-commons): add default writer factory (via #1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
baev authored Aug 8, 2024
1 parent 199f6c3 commit d3dcdf2
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 88 deletions.
19 changes: 4 additions & 15 deletions packages/allure-codeceptjs/src/reporter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { event } from "codeceptjs";
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,
getEnvironmentLabels,
md5,
} from "allure-js-commons/sdk/reporter";
import { ReporterRuntime, createDefaultWriter, getEnvironmentLabels, md5 } from "allure-js-commons/sdk/reporter";
import type { ReporterConfig } from "allure-js-commons/sdk/reporter";
import { extractMeta } from "./helpers.js";
import type { CodeceptError, CodeceptHook, CodeceptStep, CodeceptTest } from "./model.js";
Expand All @@ -22,16 +15,12 @@ export class AllureCodeceptJsReporter {
currentTest: CodeceptTest | null = null;
config!: ReporterConfig;

constructor(config: ReporterConfig) {
constructor(config: ReporterConfig = {}) {
this.registerEvents();
this.config = config || ({} as ReporterConfig);
this.config = config;
this.allureRuntime = new ReporterRuntime({
...config,
writer: env.ALLURE_TEST_MODE
? new MessageWriter()
: new FileSystemWriter({
resultsDir: config.resultsDir || "./allure-results",
}),
writer: createDefaultWriter(config),
});
}

Expand Down
18 changes: 4 additions & 14 deletions packages/allure-cucumberjs/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import { ContentType, LabelName, Stage, Status } from "allure-js-commons";
import { getMessageAndTraceFromError } from "allure-js-commons/sdk";
import {
ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE,
FileSystemWriter,
MessageWriter,
ReporterRuntime,
applyLinkTemplate,
createDefaultWriter,
createStepResult,
getEnvironmentLabels,
getWorstStepResultStatus,
Expand All @@ -28,7 +27,7 @@ import {
import { AllureCucumberWorld } from "./legacy.js";
import type { AllureCucumberLinkConfig, AllureCucumberReporterConfig, LabelConfig } from "./model.js";

const { ALLURE_THREAD_NAME, ALLURE_TEST_MODE } = process.env;
const { ALLURE_THREAD_NAME } = process.env;

export default class AllureCucumberReporter extends Formatter {
private readonly afterHooks: Record<string, TestCaseHookDefinition> = {};
Expand All @@ -53,19 +52,10 @@ export default class AllureCucumberReporter extends Formatter {
constructor(options: IFormatterOptions) {
super(options);

const {
resultsDir = "./allure-results",
links,
labels,
...rest
} = options.parsedArgvOptions as AllureCucumberReporterConfig;
const { resultsDir, links, labels, ...rest } = options.parsedArgvOptions as AllureCucumberReporterConfig;

this.allureRuntime = new ReporterRuntime({
writer: ALLURE_TEST_MODE
? new MessageWriter()
: new FileSystemWriter({
resultsDir,
}),
writer: createDefaultWriter({ resultsDir }),
links,
...rest,
});
Expand Down
10 changes: 4 additions & 6 deletions packages/allure-cypress/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ 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,
createDefaultWriter,
getEnvironmentLabels,
getSuiteLabels,
parseTestPlan,
Expand All @@ -25,14 +25,12 @@ export class AllureCypress {
globalHooksMessages: CypressMessage[] = [];
videoOnFailOnly: boolean = false;

constructor(config?: AllureCypressConfig) {
const { resultsDir = "./allure-results", videoOnFailOnly = false, ...rest } = config || {};
constructor(config: AllureCypressConfig = {}) {
const { resultsDir, videoOnFailOnly = false, ...rest } = config;

this.videoOnFailOnly = videoOnFailOnly;
this.allureRuntime = new ReporterRuntime({
writer: new FileSystemWriter({
resultsDir,
}),
writer: createDefaultWriter({ resultsDir }),
...rest,
});
}
Expand Down
16 changes: 4 additions & 12 deletions packages/allure-jasmine/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { env } from "node:process";
import * as allure from "allure-js-commons";
import { Stage, Status } from "allure-js-commons";
import type { RuntimeMessage } from "allure-js-commons/sdk";
import { getMessageAndTraceFromError, getStatusFromError, isPromise } from "allure-js-commons/sdk";
import type { FixtureType, ReporterConfig } from "allure-js-commons/sdk/reporter";
import {
FileSystemWriter,
MessageWriter,
ReporterRuntime,
createDefaultWriter,
getEnvironmentLabels,
getSuiteLabels,
hasSkipLabel,
Expand All @@ -28,25 +26,19 @@ class AllureJasmineTestRuntime extends MessageTestRuntime {
}
}

const { ALLURE_TEST_MODE } = env;

export default class AllureJasmineReporter implements jasmine.CustomReporter {
private readonly allureRuntime: ReporterRuntime;
private currentAllureTestUuid?: string;
private currentAllureFixtureUuid?: string;
private jasmineSuitesStack: jasmine.SuiteResult[] = [];
private scopesStack: string[] = [];

constructor(config: ReporterConfig) {
const { resultsDir = "./allure-results", ...restConfig } = config || {};
constructor(config: ReporterConfig = {}) {
const { resultsDir, ...restConfig } = config;

this.allureRuntime = new ReporterRuntime({
...restConfig,
writer: ALLURE_TEST_MODE
? new MessageWriter()
: new FileSystemWriter({
resultsDir,
}),
writer: createDefaultWriter({ resultsDir }),
});

const testRuntime = new AllureJasmineTestRuntime(this);
Expand Down
13 changes: 4 additions & 9 deletions packages/allure-jest/src/environmentFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import type { RuntimeMessage } from "allure-js-commons/sdk";
import { getMessageAndTraceFromError, getStatusFromError } from "allure-js-commons/sdk";
import type { TestPlanV1 } from "allure-js-commons/sdk";
import {
FileSystemWriter,
MessageWriter,
ReporterRuntime,
createDefaultWriter,
getEnvironmentLabels,
getSuiteLabels,
parseTestPlan,
Expand All @@ -21,7 +20,7 @@ import { AllureJestTestRuntime } from "./AllureJestTestRuntime.js";
import type { AllureJestConfig, AllureJestEnvironment, AllureJestProjectConfig, RunContext } from "./model.js";
import { getTestId, getTestPath, isTestPresentInTestPlan, last, shouldHookBeSkipped } from "./utils.js";

const { ALLURE_TEST_MODE, ALLURE_HOST_NAME, ALLURE_THREAD_NAME, JEST_WORKER_ID } = process.env;
const { ALLURE_HOST_NAME, ALLURE_THREAD_NAME, JEST_WORKER_ID } = process.env;
const hostname = os.hostname();

const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T => {
Expand All @@ -43,15 +42,11 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
super(config as JestEnvironmentConfig, context);

const projectConfig = "projectConfig" in config ? config.projectConfig : config;
const { resultsDir = "allure-results", ...restConfig } = projectConfig?.testEnvironmentOptions || {};
const { resultsDir, ...restConfig } = projectConfig?.testEnvironmentOptions || {};

this.runtime = new ReporterRuntime({
...restConfig,
writer: ALLURE_TEST_MODE
? new MessageWriter()
: new FileSystemWriter({
resultsDir,
}),
writer: createDefaultWriter({ resultsDir }),
});
this.testPath = context.testPath.replace(projectConfig.rootDir, "").replace(sep, "");
this.testPlan = parseTestPlan();
Expand Down
11 changes: 11 additions & 0 deletions packages/allure-js-commons/src/sdk/reporter/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { readFile } from "fs/promises";
import { createHash, randomUUID } from "node:crypto";
import type { EventEmitter } from "node:events";
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import properties from "properties";
import type { Label, Link, Status, StepResult, TestResult } from "../../model.js";
import { LabelName, LinkType, StatusByPriority } from "../../model.js";
import type { LinkConfig, LinkTemplate } from "./types.js";
import { FileSystemWriter } from "./writer/FileSystemWriter.js";
import { MessageWriter } from "./writer/MessageWriter.js";

export const randomUuid = () => {
return randomUUID();
Expand Down Expand Up @@ -243,3 +246,11 @@ export const formatLink = (templates: LinkConfig, link: Link) => {

export const formatLinks = (templates: LinkConfig, links: readonly Link[]) =>
links.map((link) => formatLink(templates, link));

export const createDefaultWriter = (config: { resultsDir?: string; emitter?: EventEmitter }) => {
return process.env.ALLURE_TEST_MODE
? new MessageWriter(config.emitter)
: new FileSystemWriter({
resultsDir: config.resultsDir || "./allure-results",
});
};
6 changes: 3 additions & 3 deletions packages/allure-mocha/src/AllureMochaReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type { Category, RuntimeMessage } from "allure-js-commons/sdk";
import { getStatusFromError } from "allure-js-commons/sdk";
import type { ReporterConfig } from "allure-js-commons/sdk/reporter";
import {
FileSystemWriter,
ReporterRuntime,
createDefaultWriter,
ensureSuiteLabels,
getEnvironmentLabels,
getPackageLabelFromPath,
Expand Down Expand Up @@ -55,11 +55,11 @@ export class AllureMochaReporter extends Mocha.reporters.Base {
constructor(runner: Mocha.Runner, opts: Mocha.MochaOptions, isInWorker: boolean = false) {
super(runner, opts);

const { resultsDir = "allure-results", ...restOptions }: ReporterConfig = opts.reporterOptions || {};
const { resultsDir, ...restOptions }: ReporterConfig = opts.reporterOptions || {};

this.isInWorker = isInWorker;
this.runtime = new ReporterRuntime({
writer: new FileSystemWriter({ resultsDir }),
writer: createDefaultWriter({ resultsDir }),
...restOptions,
});
this.testplan = createTestPlanIndices();
Expand Down
11 changes: 2 additions & 9 deletions packages/allure-playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import type { RuntimeMessage, TestPlanV1Test } from "allure-js-commons/sdk";
import { extractMetadataFromString, getMessageAndTraceFromError, hasLabel, stripAnsi } from "allure-js-commons/sdk";
import {
ALLURE_RUNTIME_MESSAGE_CONTENT_TYPE,
FileSystemWriter,
MessageWriter,
ReporterRuntime,
createDefaultWriter,
escapeRegExp,
getEnvironmentLabels,
md5,
Expand Down Expand Up @@ -152,16 +151,10 @@ export class AllureReporter implements ReporterV2 {
onStdOut(): void {}

onBegin(suite: Suite): void {
const writer = process.env.ALLURE_TEST_MODE
? new MessageWriter()
: new FileSystemWriter({
resultsDir: this.options.resultsDir || "./allure-results",
});

this.suite = suite;
this.allureRuntime = new ReporterRuntime({
...this.options,
writer,
writer: createDefaultWriter({ resultsDir: this.options.resultsDir }),
});
}

Expand Down
15 changes: 4 additions & 11 deletions packages/allure-vitest/src/reporter.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { normalize, relative } from "node:path";
import { cwd, env } from "node:process";
import { cwd } from "node:process";
import type { File, Reporter, Task } from "vitest";
import { LabelName, Stage, Status } from "allure-js-commons";
import type { RuntimeMessage } from "allure-js-commons/sdk";
import { extractMetadataFromString } from "allure-js-commons/sdk";
import type { ReporterConfig } from "allure-js-commons/sdk/reporter";
import {
FileSystemWriter,
MessageWriter,
ReporterRuntime,
createDefaultWriter,
getEnvironmentLabels,
getHostLabel,
getSuiteLabels,
Expand All @@ -25,16 +24,10 @@ export default class AllureVitestReporter implements Reporter {
}

onInit() {
const { listeners, ...config } = this.config;
const writer = env.ALLURE_TEST_MODE
? new MessageWriter()
: new FileSystemWriter({
resultsDir: config.resultsDir || "./allure-results",
});

const { listeners, resultsDir, ...config } = this.config;
this.allureReporterRuntime = new ReporterRuntime({
...config,
writer,
writer: createDefaultWriter({ resultsDir }),
listeners,
});
}
Expand Down
12 changes: 3 additions & 9 deletions packages/newman-reporter-allure/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* eslint-disable @typescript-eslint/unbound-method */
import type { EventEmitter } from "events";
import type { ConsoleEvent, Cursor, NewmanRunExecutionAssertion } from "newman";
import { env } from "node:process";
import type { CollectionDefinition, Event, HeaderList, Item, Request, Response } from "postman-collection";
import { ContentType, LabelName, Stage, Status } from "allure-js-commons";
import type { ReporterConfig } from "allure-js-commons/sdk/reporter";
import {
FileSystemWriter,
MessageWriter,
ReporterRuntime,
createDefaultWriter,
getEnvironmentLabels,
getSuiteLabels,
} from "allure-js-commons/sdk/reporter";
Expand All @@ -32,18 +30,14 @@ class AllureReporter {
collection: CollectionDefinition;
},
) {
const { resultsDir = "./allure-results", ...restConfig } = reporterConfig;
const { resultsDir, ...restConfig } = reporterConfig;

this.currentCollection = options.collection;
this.rootCollectionName = options.collection.name;
this.allureConfig = reporterConfig;
this.allureRuntime = new ReporterRuntime({
...restConfig,
writer: env.ALLURE_TEST_MODE
? new MessageWriter(emitter)
: new FileSystemWriter({
resultsDir,
}),
writer: createDefaultWriter({ resultsDir, emitter }),
});
this.registerEvents(emitter);
}
Expand Down

0 comments on commit d3dcdf2

Please sign in to comment.