@@ -8,18 +8,43 @@ import {
88} from '../modules/cli-output' ;
99import { DefaultLogger } from '../utils/logger/logger' ;
1010
11+ import type { Config } 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+ } ;
37+
1338const 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
6897async 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} ;
0 commit comments