Skip to content

Commit

Permalink
rft config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
baev committed Dec 18, 2024
1 parent 2049534 commit 92a9378
Show file tree
Hide file tree
Showing 19 changed files with 204 additions and 161 deletions.
1 change: 1 addition & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@allurereport/core": "workspace:*",
"@allurereport/core-api": "workspace:*",
"@allurereport/directory-watcher": "workspace:*",
"@allurereport/plugin-api": "workspace:*",
"@allurereport/plugin-awesome": "workspace:*",
"@allurereport/plugin-progress": "workspace:*",
"@allurereport/plugin-server-reload": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AllureReport, readRuntimeConfig } from "@allurereport/core";
import { AllureReport, readConfig } from "@allurereport/core";
import { createCommand } from "../utils/commands.js";

type CommandOptions = {
Expand All @@ -10,7 +10,7 @@ type CommandOptions = {

export const GenerateCommandAction = async (resultsDir: string, options: CommandOptions) => {
const { config: configPath, output, cwd, reportName } = options;
const config = await readRuntimeConfig(configPath, cwd, output, reportName);
const config = await readConfig(cwd, configPath, { name: reportName, output });
const allureReport = new AllureReport(config);

await allureReport.start();
Expand Down
12 changes: 9 additions & 3 deletions packages/cli/src/commands/knownIssue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AllureReport, createConfig, writeKnownIssues } from "@allurereport/core";
import { AllureReport, resolveConfig, writeKnownIssues } from "@allurereport/core";
import console from "node:console";
import { resolve } from "node:path";
import { createCommand } from "../utils/commands.js";
Expand All @@ -9,11 +9,17 @@ type CommandOptions = {

export const KnownIssueCommandAction = async (resultsDir: string, options: CommandOptions) => {
const { output = "known-issues.json" } = options;
const config = await createConfig({});
const config = await resolveConfig({
plugins: {},
});

const allureReport = new AllureReport(config);
const targetPath = resolve(output);

await allureReport.start();
await allureReport.readDirectory(resultsDir);
await allureReport.done();

const targetPath = resolve(output);
await writeKnownIssues(allureReport.store, output);

console.log(`writing known-issues.json to ${targetPath}`);
Expand Down
13 changes: 10 additions & 3 deletions packages/cli/src/commands/open.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { readRuntimeConfig } from "@allurereport/core";
import { readConfig } from "@allurereport/core";
import { serve } from "@allurereport/static-server";
import { createCommand } from "../utils/commands.js";

type CommandOptions = {
config?: string;
cwd?: string;
port?: number;
live: boolean;
};

export const OpenCommandAction = async (reportDir: string | undefined, options: CommandOptions) => {
const { config: configPath, port, live } = options;
const config = await readRuntimeConfig(configPath, undefined, reportDir);
const { config: configPath, port, live, cwd } = options;
const config = await readConfig(cwd, configPath, { output: reportDir });

await serve({
port: port,
Expand Down Expand Up @@ -43,6 +44,12 @@ export const OpenCommand = createCommand({
default: false,
},
],
[
"--cwd <cwd>",
{
description: "The working directory for the command to run (default: current working directory)",
},
],
],
action: OpenCommandAction,
});
15 changes: 11 additions & 4 deletions packages/cli/src/commands/qualityGate.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { AllureReport, readRuntimeConfig } from "@allurereport/core";
import { AllureReport, readConfig } from "@allurereport/core";
import console from "node:console";
import process from "node:process";
import { bold, red } from "yoctocolors";
import { createCommand } from "../utils/commands.js";

type QualityGateCommandOptions = {
config?: string;
cwd?: string;
};

export const QualityGateCommandAction = async (resultsDir: string, options: QualityGateCommandOptions) => {
const cwd = options.cwd ?? process.cwd();
const fullConfig = await readRuntimeConfig(cwd, "./allure-report");
const { cwd, config: configPath } = options;
const fullConfig = await readConfig(cwd, configPath);
const allureReport = new AllureReport(fullConfig);

await allureReport.start();
Expand Down Expand Up @@ -51,10 +52,16 @@ export const QualityGateCommand = createCommand({
name: "quality-gate <resultsDir>",
description: "Returns status code 1 if there any test failure above specified success rate",
options: [
[
"--config, -c <file>",
{
description: "The path Allure config file",
},
],
[
"--cwd <cwd>",
{
description: "The working directory for the command to run (default: current working directory)",
description: "The working directory for the command to run (Default: current working directory)",
},
],
],
Expand Down
7 changes: 3 additions & 4 deletions packages/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AllureReport, isFileNotFoundError, readRuntimeConfig } from "@allurereport/core";
import { AllureReport, isFileNotFoundError, readConfig } from "@allurereport/core";
import { createTestPlan } from "@allurereport/core-api";
import type {
Watcher} from "@allurereport/directory-watcher";
import type { Watcher } from "@allurereport/directory-watcher";
import {
allureResultsDirectoriesWatcher,
delayedFileProcessingWatcher,
Expand Down Expand Up @@ -127,7 +126,7 @@ export const RunCommandAction = async (options: RunCommandOptions) => {
console.log(`${command} ${commandArgs.join(" ")}`);

const { config: configPath, output, reportName, rerun: maxRerun = 0, silent = false } = options;
const config = await readRuntimeConfig(configPath, cwd, output, reportName);
const config = await readConfig(cwd, configPath, { output, name: reportName });

try {
await rm(config.output, { recursive: true });
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/watch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AllureReport, isFileNotFoundError, readRuntimeConfig } from "@allurereport/core";
import { AllureReport, isFileNotFoundError, readConfig } from "@allurereport/core";
import { newFilesInDirectoryWatcher } from "@allurereport/directory-watcher";
import AllureAwesome from "@allurereport/plugin-awesome";
import ProgressPlugin from "@allurereport/plugin-progress";
Expand Down Expand Up @@ -29,7 +29,7 @@ export const WatchCommandAction = async (resultsDir: string, options: WatchComma
});

const { config: configPath, output, cwd, reportName } = options;
const config = await readRuntimeConfig(configPath, cwd, output, reportName);
const config = await readConfig(cwd, configPath, { output, name: reportName });

try {
await rm(config.output, { recursive: true });
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { defineConfig } from "@allurereport/core";
import { cac } from "cac";
import console from "node:console";
import { readFileSync } from "node:fs";
Expand Down Expand Up @@ -53,4 +52,4 @@ console.log(cwd());

cli.parse();

export { defineConfig };
export { defineConfig } from "@allurereport/plugin-api";
5 changes: 3 additions & 2 deletions packages/core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export interface FullConfig {
reportFiles: ReportFiles;
readers?: ResultsReader[];
plugins?: PluginInstance[];
history?: HistoryDataPoint[];
historyPath?: string;
history: HistoryDataPoint[];
historyPath: string;
appendHistory?: boolean;
knownIssuesPath: string;
known?: KnownTestFailure[];
qualityGate?: QualityGateConfig;
// TODO: https://github.com/qameta/allure3/issues/180
Expand Down
126 changes: 56 additions & 70 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,104 +1,90 @@
import type { Config, PluginDescriptor } from "@allurereport/plugin-api";
import type { QualityGateConfig } from "@allurereport/plugin-api";
import { readdir } from "node:fs/promises";
import { join, resolve } from "node:path";
import * as console from "node:console";
import { stat } from "node:fs/promises";
import { resolve } from "node:path";
import * as process from "node:process";
import type { FullConfig, PluginInstance } from "./api.js";
import { readHistory } from "./history.js";
import { readKnownIssues } from "./known.js";
import { FileSystemReportFiles } from "./plugin.js";
import { importWrapper } from "./utils/module.js";

export { defineConfig } from "@allurereport/plugin-api";

export const createConfig = async (opts: {
reportName?: string;
output?: string;
historyPath?: string;
knownIssuesPath?: string;
plugins?: PluginInstance[];
qualityGate?: QualityGateConfig;
cwd?: string;
}): Promise<FullConfig> => {
const {
reportName = "Allure Report",
output = "allure-report",
historyPath,
knownIssuesPath,
qualityGate,
cwd,
} = opts;
const workingDirectory = cwd ?? process.cwd();
const target = resolve(workingDirectory, output);
const history = historyPath ? await readHistory(resolve(workingDirectory, historyPath)) : [];
const known = knownIssuesPath ? await readKnownIssues(resolve(workingDirectory, knownIssuesPath)) : [];

return {
name: reportName,
history,
historyPath,
known,
qualityGate,
plugins: opts.plugins ?? [],
reportFiles: new FileSystemReportFiles(target),
output: target,
};
};

const defaultRuntimeConfig: Config = {};

export const getPluginId = (key: string) => {
return key.replace(/^@.*\//, "").replace(/[/\\]/g, "-");
};

export const findRuntimeConfigPath = async (cwd: string = process.cwd()): Promise<string | undefined> => {
const files = await readdir(cwd);
const configNames = ["allurerc.js", "allurerc.mjs"];
const defaultConfig: Config = {};

if (files.includes("allurerc.js")) {
return join(cwd, "allurerc.js");
export const findConfig = async (cwd: string, configPath?: string) => {
if (configPath) {
const resolved = resolve(cwd, configPath);
try {
const stats = await stat(resolved);
if (stats.isFile()) {
return resolved;
}
} catch (e) {
console.error(e);
}
throw new Error(`invalid config path ${resolved}: not a regular file`);
}

if (files.includes("allurerc.mjs")) {
return join(cwd, "allurerc.mjs");
for (const configName of configNames) {
const resolved = resolve(cwd, configName);
try {
const stats = await stat(resolved);
if (stats.isFile()) {
return resolved;
}
} catch (ignored) {
// ignore
}
}

return undefined;
};

// more advanced override, e.g. default plugins
export const readRuntimeConfig = async (
export interface ConfigOverride {
name?: string;
output?: string;
historyPath?: string;
knownIssuesPath?: string;
}

export const readConfig = async (
cwd: string = process.cwd(),
configPath?: string,
cwd?: string,
output?: string,
name?: string,
override?: ConfigOverride,
): Promise<FullConfig> => {
const runtimeConfigPath = !configPath ? await findRuntimeConfigPath(cwd) : resolve(cwd ?? process.cwd(), configPath);
const runtimeConfig = runtimeConfigPath ? await loadConfig(runtimeConfigPath) : defaultRuntimeConfig;

return await resolveConfig(runtimeConfig, { output, name });
const cfg = await findConfig(cwd, configPath);
const config = cfg ? await loadConfig(cfg) : { ...defaultConfig };
return await resolveConfig(config, override);
};

export const loadConfig = async (configPath: string): Promise<Config> => {
return (await import(configPath)).default;
};

export const resolveConfig = async (
config: Config,
override: { name?: string; output?: string } = {},
): Promise<FullConfig> => {
const { plugins = {}, name, output, historyPath, ...rest } = config;
const pluginInstances = await resolvePlugins(plugins);
const out = resolve(override.output ?? output ?? "./allure-report");
const history = historyPath ? await readHistory(historyPath) : [];
export const resolveConfig = async (config: Config, override: ConfigOverride = {}): Promise<FullConfig> => {
const name = override.name ?? config.name ?? "Allure Report";
const historyPath = resolve(override.historyPath ?? config.historyPath ?? "./.allure/history.jsonl");
const knownIssuesPath = resolve(override.knownIssuesPath ?? config.knownIssuesPath ?? "./allure/known.json");
const output = resolve(override.output ?? config.output ?? "./allure-report");

const history = await readHistory(historyPath);
const known = await readKnownIssues(knownIssuesPath);

const pluginInstances = await resolvePlugins(config.plugins ?? {});

return {
...rest,
name: override.name ?? name ?? "Allure Report",
reportFiles: new FileSystemReportFiles(out),
name,
reportFiles: new FileSystemReportFiles(output),
plugins: pluginInstances,
output: out,
output,
history,
historyPath,
knownIssuesPath,
known,
qualityGate: config.qualityGate,
};
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export * from "./utils/misc.js";
export * from "./utils/crypto.js";
export * from "./history.js";
export * from "./known.js";
export * from "./config.js";
export { resolveConfig, readConfig } from "./config.js";
export * from "./report.js";
export * from "./plugin.js";
export * from "./qualityGate.js";
2 changes: 1 addition & 1 deletion packages/core/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class AllureReport {
this.#events = new Events(this.#eventEmitter);
this.#realTime = realTime;
this.#appendHistory = appendHistory ?? true;
this.#historyPath = historyPath ?? "./.allure/history.jsonl";
this.#historyPath = historyPath;
this.#store = new DefaultAllureStore(history, known, this.#eventEmitter);
this.#readers = [...readers];
this.#plugins = [...plugins];
Expand Down
Loading

0 comments on commit 92a9378

Please sign in to comment.