@@ -8,18 +8,45 @@ import {
88} from '../modules/cli-output' ;
99import { 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' ;
1113import 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+
1340const 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
6895async 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} ;
0 commit comments