Skip to content

Commit e7be640

Browse files
authored
chore: update respect command handler (#1920)
1 parent b93df0f commit e7be640

File tree

4 files changed

+63
-30
lines changed

4 files changed

+63
-30
lines changed

packages/cli/src/index.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,7 @@ yargs
875875
.positional('files', {
876876
describe: 'Test files or glob pattern',
877877
type: 'string',
878+
array: true,
878879
default: [],
879880
})
880881
.env('REDOCLY_CLI_RESPECT')
@@ -892,14 +893,14 @@ yargs
892893
workflow: {
893894
alias: 'w',
894895
describe: 'Workflow name',
895-
type: 'array',
896-
greedy: false,
896+
type: 'string',
897+
array: true,
897898
},
898899
skip: {
899900
alias: 's',
900901
describe: 'Workflow to skip',
901-
type: 'array',
902-
greedy: false,
902+
type: 'string',
903+
array: true,
903904
},
904905
verbose: {
905906
alias: 'v',
@@ -937,13 +938,9 @@ yargs
937938
},
938939
});
939940
},
940-
async (argv) => {
941-
try {
942-
await handleRun(argv);
943-
} catch (err) {
944-
// logger.error(red(`${err?.message}`));
945-
process.exit(1);
946-
}
941+
(argv) => {
942+
process.env.REDOCLY_CLI_COMMAND = 'respect';
943+
commandWrapper(handleRun)(argv);
947944
}
948945
)
949946
.command(

packages/respect-core/src/handlers/run.ts

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,43 @@ import {
88
} from '../modules/cli-output';
99
import { DefaultLogger } from '../utils/logger/logger';
1010

11+
import type { Config } from '@redocly/openapi-core';
12+
import type { CollectFn } from '@redocly/openapi-core/src/utils';
1113
import type { RunArgv } from '../types';
1214

15+
export type CommandArgs<T> = {
16+
argv: T;
17+
config: Config;
18+
version: string;
19+
collectSpecData?: CollectFn;
20+
};
21+
22+
export type RespectOptions = {
23+
files: string[];
24+
input?: string;
25+
server?: string;
26+
workflow?: string[];
27+
skip?: string[];
28+
verbose?: boolean;
29+
'har-output'?: string;
30+
'json-output'?: string;
31+
residency?: string;
32+
'client-cert'?: string;
33+
'client-key'?: string;
34+
'ca-cert'?: string;
35+
severity?: string;
36+
};
37+
1338
const logger = DefaultLogger.getInstance();
14-
export async function handleRun(argv: any) {
39+
export async function handleRun({ argv, collectSpecData }: CommandArgs<RespectOptions>) {
1540
const harOutputFile = argv['har-output'];
1641
if (harOutputFile && !harOutputFile.endsWith('.har')) {
17-
exitWithErrorMsg('File for HAR logs should be in .har format', 1);
42+
throw new Error('File for HAR logs should be in .har format');
1843
}
1944

2045
const jsonOutputFile = argv['json-output'];
2146
if (jsonOutputFile && !jsonOutputFile.endsWith('.json')) {
22-
exitWithErrorMsg('File for JSON logs should be in .json format', 1);
47+
throw new Error('File for JSON logs should be in .json format');
2348
}
2449

2550
const { skip, workflow } = argv;
@@ -38,17 +63,21 @@ export async function handleRun(argv: any) {
3863

3964
if (files.length > 1 && (jsonOutputFile || harOutputFile)) {
4065
// TODO: implement multiple run files logs output
41-
exitWithErrorMsg(
42-
'Currently only a single file can be run with --har-output or --json-output. Please run a single file at a time.',
43-
1
66+
throw new Error(
67+
'Currently only a single file can be run with --har-output or --json-output. Please run a single file at a time.'
4468
);
4569
}
4670

4771
for (const path of files) {
48-
const result = await runFile({ ...argv, file: path }, startedAt, {
49-
harFile: harOutputFile,
50-
jsonFile: jsonOutputFile,
51-
});
72+
const result = await runFile(
73+
{ ...argv, file: path },
74+
startedAt,
75+
{
76+
harFile: harOutputFile,
77+
jsonFile: jsonOutputFile,
78+
},
79+
collectSpecData
80+
);
5281
testsRunProblemsStatus.push(result.hasProblems);
5382
runAllFilesResult.push(result);
5483
}
@@ -58,19 +87,20 @@ export async function handleRun(argv: any) {
5887
logger.printNewLine();
5988

6089
if (testsRunProblemsStatus.some((problems) => problems)) {
61-
exitWithErrorMsg(' Tests exited with error ', 1);
90+
throw new Error(' Tests exited with error ');
6291
}
6392
} catch (err) {
64-
exitWithErrorMsg((err as Error)?.message ?? err, 1);
93+
exitWithError((err as Error)?.message ?? err);
6594
}
6695
}
6796

6897
async function runFile(
6998
argv: RunArgv,
7099
startedAt: number,
71-
output: { harFile: string | undefined; jsonFile: string | undefined }
100+
output: { harFile: string | undefined; jsonFile: string | undefined },
101+
collectSpecData?: CollectFn
72102
) {
73-
const { workflows } = await runTestFile(argv as RunArgv, output);
103+
const { workflows } = await runTestFile(argv, output, collectSpecData);
74104

75105
const totals = calculateTotals(workflows);
76106
const hasProblems = totals.workflows.failed > 0;
@@ -84,8 +114,8 @@ async function runFile(
84114
return { hasProblems, file: argv.file, workflows, argv };
85115
}
86116

87-
const exitWithErrorMsg = (message: string, code: 0 | 1 = 1) => {
117+
const exitWithError = (message: string) => {
88118
logger.error(bgRed(message));
89119
logger.printNewLine();
90-
process.exit(code);
120+
throw new Error(message);
91121
};

packages/respect-core/src/modules/__tests__/test-config-generator/generate-test-config.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('generateTestConfig', () => {
9090
expect(
9191
await generateTestConfig({
9292
descriptionPath: 'description.yaml',
93-
outputFile: '../final-test-location/output.yaml',
93+
outputFile: './final-test-location/output.yaml',
9494
extended: false,
9595
})
9696
).toEqual({
@@ -103,7 +103,7 @@ describe('generateTestConfig', () => {
103103
{
104104
name: 'description',
105105
type: 'openapi',
106-
url: '../redocly-cli/description.yaml',
106+
url: '../description.yaml',
107107
},
108108
],
109109
workflows: [

packages/respect-core/src/modules/flow-runner/runner.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { calculateTotals, composeJsonLogs, maskSecrets } from '../cli-output';
2121
import { resolveRunningWorkflows } from './resolve-running-workflows';
2222
import { DefaultLogger } from '../../utils/logger/logger';
2323

24+
import type { CollectFn } from '@redocly/openapi-core/src/utils';
2425
import type {
2526
TestDescription,
2627
AppOptions,
@@ -34,7 +35,11 @@ import type {
3435

3536
const logger = DefaultLogger.getInstance();
3637

37-
export async function runTestFile(argv: RunArgv, output: { harFile?: string; jsonFile?: string }) {
38+
export async function runTestFile(
39+
argv: RunArgv,
40+
output: { harFile?: string; jsonFile?: string },
41+
collectSpecData?: CollectFn
42+
) {
3843
const {
3944
file: filePath,
4045
workflow,
@@ -66,6 +71,7 @@ export async function runTestFile(argv: RunArgv, output: { harFile?: string; jso
6671
};
6772

6873
const bundledTestDescription = await bundleArazzo(filePath);
74+
collectSpecData?.(bundledTestDescription);
6975
const descriptionCopy = JSON.parse(JSON.stringify(bundledTestDescription));
7076

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

0 commit comments

Comments
 (0)