Skip to content

Commit f567723

Browse files
committed
chore: update respect command handler
1 parent b93df0f commit f567723

File tree

4 files changed

+80
-49
lines changed

4 files changed

+80
-49
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: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,45 @@ import {
88
} from '../modules/cli-output';
99
import { DefaultLogger } from '../utils/logger/logger';
1010

11+
import type { Config, RuleSeverity } 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+
config?: string;
37+
'lint-config'?: RuleSeverity;
38+
};
39+
1340
const logger = DefaultLogger.getInstance();
14-
export async function handleRun(argv: any) {
41+
export async function handleRun({ argv, collectSpecData }: CommandArgs<RespectOptions>) {
1542
const harOutputFile = argv['har-output'];
1643
if (harOutputFile && !harOutputFile.endsWith('.har')) {
17-
exitWithErrorMsg('File for HAR logs should be in .har format', 1);
44+
exitWithError('File for HAR logs should be in .har format');
1845
}
1946

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

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

33-
try {
34-
const startedAt = performance.now();
35-
const testsRunProblemsStatus: boolean[] = [];
36-
const { files } = argv;
37-
const runAllFilesResult = [];
38-
39-
if (files.length > 1 && (jsonOutputFile || harOutputFile)) {
40-
// 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
44-
);
45-
}
46-
47-
for (const path of files) {
48-
const result = await runFile({ ...argv, file: path }, startedAt, {
60+
const startedAt = performance.now();
61+
const testsRunProblemsStatus: boolean[] = [];
62+
const { files } = argv;
63+
const runAllFilesResult = [];
64+
65+
if (files.length > 1 && (jsonOutputFile || harOutputFile)) {
66+
// TODO: implement multiple run files logs output
67+
exitWithError(
68+
'Currently only a single file can be run with --har-output or --json-output. Please run a single file at a time.'
69+
);
70+
}
71+
72+
for (const path of files) {
73+
const result = await runFile(
74+
{ ...argv, file: path },
75+
startedAt,
76+
{
4977
harFile: harOutputFile,
5078
jsonFile: jsonOutputFile,
51-
});
52-
testsRunProblemsStatus.push(result.hasProblems);
53-
runAllFilesResult.push(result);
54-
}
79+
},
80+
collectSpecData
81+
);
82+
testsRunProblemsStatus.push(result.hasProblems);
83+
runAllFilesResult.push(result);
84+
}
5585

56-
logger.printNewLine();
57-
displayFilesSummaryTable(runAllFilesResult);
58-
logger.printNewLine();
86+
logger.printNewLine();
87+
displayFilesSummaryTable(runAllFilesResult);
88+
logger.printNewLine();
5989

60-
if (testsRunProblemsStatus.some((problems) => problems)) {
61-
exitWithErrorMsg(' Tests exited with error ', 1);
62-
}
63-
} catch (err) {
64-
exitWithErrorMsg((err as Error)?.message ?? err, 1);
90+
if (testsRunProblemsStatus.some((problems) => problems)) {
91+
exitWithError(' Tests exited with error ');
6592
}
6693
}
6794

6895
async function runFile(
6996
argv: RunArgv,
7097
startedAt: number,
71-
output: { harFile: string | undefined; jsonFile: string | undefined }
98+
output: { harFile: string | undefined; jsonFile: string | undefined },
99+
collectSpecData?: CollectFn
72100
) {
73-
const { workflows } = await runTestFile(argv as RunArgv, output);
101+
const { workflows } = await runTestFile(argv, output, collectSpecData);
74102

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

87-
const exitWithErrorMsg = (message: string, code: 0 | 1 = 1) => {
115+
const exitWithError = (message: string) => {
88116
logger.error(bgRed(message));
89117
logger.printNewLine();
90-
process.exit(code);
118+
throw new Error(message);
91119
};

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)