Skip to content

Commit

Permalink
chore: update respect command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tatomyr committed Feb 19, 2025
1 parent b93df0f commit f567723
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 49 deletions.
19 changes: 8 additions & 11 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,7 @@ yargs
.positional('files', {
describe: 'Test files or glob pattern',
type: 'string',
array: true,
default: [],
})
.env('REDOCLY_CLI_RESPECT')
Expand All @@ -892,14 +893,14 @@ yargs
workflow: {
alias: 'w',
describe: 'Workflow name',
type: 'array',
greedy: false,
type: 'string',
array: true,
},
skip: {
alias: 's',
describe: 'Workflow to skip',
type: 'array',
greedy: false,
type: 'string',
array: true,
},
verbose: {
alias: 'v',
Expand Down Expand Up @@ -937,13 +938,9 @@ yargs
},
});
},
async (argv) => {
try {
await handleRun(argv);
} catch (err) {
// logger.error(red(`${err?.message}`));
process.exit(1);
}
(argv) => {
process.env.REDOCLY_CLI_COMMAND = 'respect';
commandWrapper(handleRun)(argv);
}
)
.command(
Expand Down
98 changes: 63 additions & 35 deletions packages/respect-core/src/handlers/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,45 @@ import {
} from '../modules/cli-output';
import { DefaultLogger } from '../utils/logger/logger';

import type { Config, RuleSeverity } from '@redocly/openapi-core';
import type { CollectFn } from '@redocly/openapi-core/src/utils';
import type { RunArgv } from '../types';

export type CommandArgs<T> = {
argv: T;
config: Config;
version: string;
collectSpecData?: CollectFn;
};

export type RespectOptions = {
files: string[];
input?: string;
server?: string;
workflow?: string[];
skip?: string[];
verbose?: boolean;
'har-output'?: string;
'json-output'?: string;
residency?: string;
'client-cert'?: string;
'client-key'?: string;
'ca-cert'?: string;
severity?: string;
config?: string;
'lint-config'?: RuleSeverity;
};

const logger = DefaultLogger.getInstance();
export async function handleRun(argv: any) {
export async function handleRun({ argv, collectSpecData }: CommandArgs<RespectOptions>) {
const harOutputFile = argv['har-output'];
if (harOutputFile && !harOutputFile.endsWith('.har')) {
exitWithErrorMsg('File for HAR logs should be in .har format', 1);
exitWithError('File for HAR logs should be in .har format');
}

const jsonOutputFile = argv['json-output'];
if (jsonOutputFile && !jsonOutputFile.endsWith('.json')) {
exitWithErrorMsg('File for JSON logs should be in .json format', 1);
exitWithError('File for JSON logs should be in .json format');
}

const { skip, workflow } = argv;
Expand All @@ -30,47 +57,48 @@ export async function handleRun(argv: any) {
return;
}

try {
const startedAt = performance.now();
const testsRunProblemsStatus: boolean[] = [];
const { files } = argv;
const runAllFilesResult = [];

if (files.length > 1 && (jsonOutputFile || harOutputFile)) {
// TODO: implement multiple run files logs output
exitWithErrorMsg(
'Currently only a single file can be run with --har-output or --json-output. Please run a single file at a time.',
1
);
}

for (const path of files) {
const result = await runFile({ ...argv, file: path }, startedAt, {
const startedAt = performance.now();
const testsRunProblemsStatus: boolean[] = [];
const { files } = argv;
const runAllFilesResult = [];

if (files.length > 1 && (jsonOutputFile || harOutputFile)) {
// TODO: implement multiple run files logs output
exitWithError(
'Currently only a single file can be run with --har-output or --json-output. Please run a single file at a time.'
);
}

for (const path of files) {
const result = await runFile(
{ ...argv, file: path },
startedAt,
{
harFile: harOutputFile,
jsonFile: jsonOutputFile,
});
testsRunProblemsStatus.push(result.hasProblems);
runAllFilesResult.push(result);
}
},
collectSpecData
);
testsRunProblemsStatus.push(result.hasProblems);
runAllFilesResult.push(result);
}

logger.printNewLine();
displayFilesSummaryTable(runAllFilesResult);
logger.printNewLine();
logger.printNewLine();
displayFilesSummaryTable(runAllFilesResult);
logger.printNewLine();

if (testsRunProblemsStatus.some((problems) => problems)) {
exitWithErrorMsg(' Tests exited with error ', 1);
}
} catch (err) {
exitWithErrorMsg((err as Error)?.message ?? err, 1);
if (testsRunProblemsStatus.some((problems) => problems)) {
exitWithError(' Tests exited with error ');
}
}

async function runFile(
argv: RunArgv,
startedAt: number,
output: { harFile: string | undefined; jsonFile: string | undefined }
output: { harFile: string | undefined; jsonFile: string | undefined },
collectSpecData?: CollectFn
) {
const { workflows } = await runTestFile(argv as RunArgv, output);
const { workflows } = await runTestFile(argv, output, collectSpecData);

const totals = calculateTotals(workflows);
const hasProblems = totals.workflows.failed > 0;
Expand All @@ -84,8 +112,8 @@ async function runFile(
return { hasProblems, file: argv.file, workflows, argv };
}

const exitWithErrorMsg = (message: string, code: 0 | 1 = 1) => {
const exitWithError = (message: string) => {
logger.error(bgRed(message));
logger.printNewLine();
process.exit(code);
throw new Error(message);
};
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('generateTestConfig', () => {
expect(
await generateTestConfig({
descriptionPath: 'description.yaml',
outputFile: '../final-test-location/output.yaml',
outputFile: './final-test-location/output.yaml',
extended: false,
})
).toEqual({
Expand All @@ -103,7 +103,7 @@ describe('generateTestConfig', () => {
{
name: 'description',
type: 'openapi',
url: '../redocly-cli/description.yaml',
url: '../description.yaml',
},
],
workflows: [
Expand Down
8 changes: 7 additions & 1 deletion packages/respect-core/src/modules/flow-runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { calculateTotals, composeJsonLogs, maskSecrets } from '../cli-output';
import { resolveRunningWorkflows } from './resolve-running-workflows';
import { DefaultLogger } from '../../utils/logger/logger';

import type { CollectFn } from '@redocly/openapi-core/src/utils';
import type {
TestDescription,
AppOptions,
Expand All @@ -34,7 +35,11 @@ import type {

const logger = DefaultLogger.getInstance();

export async function runTestFile(argv: RunArgv, output: { harFile?: string; jsonFile?: string }) {
export async function runTestFile(
argv: RunArgv,
output: { harFile?: string; jsonFile?: string },
collectSpecData?: CollectFn
) {
const {
file: filePath,
workflow,
Expand Down Expand Up @@ -66,6 +71,7 @@ export async function runTestFile(argv: RunArgv, output: { harFile?: string; jso
};

const bundledTestDescription = await bundleArazzo(filePath);
collectSpecData?.(bundledTestDescription);
const descriptionCopy = JSON.parse(JSON.stringify(bundledTestDescription));

const { harLogs, jsonLogs, workflows, secretFields } = await runWorkflows(
Expand Down

0 comments on commit f567723

Please sign in to comment.