-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathrun.ts
More file actions
142 lines (124 loc) · 3.98 KB
/
run.ts
File metadata and controls
142 lines (124 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { blue, green, red } from 'colorette';
import { type CollectFn } from '@redocly/openapi-core/src/utils';
import { runTestFile } from '../modules/flow-runner';
import {
displayErrors,
displaySummary,
displayFilesSummaryTable,
calculateTotals,
composeJsonLogsFiles,
} from '../modules/cli-output';
import { DefaultLogger } from '../utils/logger/logger';
import { exitWithError } from '../utils/exit-with-error';
import { writeFileSync } from 'node:fs';
import { indent } from '../utils/cli-outputs';
import type { JsonLogs, CommandArgs, RunArgv } from '../types';
export type RespectOptions = {
files: string[];
input?: string;
server?: string;
workflow?: string[];
skip?: string[];
verbose?: boolean;
'har-output'?: string;
'json-output'?: string;
'client-cert'?: string;
'client-key'?: string;
'ca-cert'?: string;
severity?: string;
config?: never;
};
const logger = DefaultLogger.getInstance();
export async function handleRun({ argv, collectSpecData }: CommandArgs<RespectOptions>) {
const harOutputFile = argv['har-output'];
if (harOutputFile && !harOutputFile.endsWith('.har')) {
throw new Error('File for HAR logs should be in .har format');
}
const jsonOutputFile = argv['json-output'];
if (jsonOutputFile && !jsonOutputFile.endsWith('.json')) {
throw new Error('File for JSON logs should be in .json format');
}
const { skip, workflow } = argv;
if (skip && workflow) {
logger.printNewLine();
logger.log(red(`Cannot use both --skip and --workflow flags at the same time.`));
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
throw new Error(
'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 },
performance.now(),
{
harFile: harOutputFile,
},
collectSpecData
);
testsRunProblemsStatus.push(result.hasProblems);
runAllFilesResult.push(result);
}
const hasProblems = runAllFilesResult.some((result) => result.hasProblems);
const hasWarnings = runAllFilesResult.some((result) => result.hasWarnings);
logger.printNewLine();
displayFilesSummaryTable(runAllFilesResult);
logger.printNewLine();
if (jsonOutputFile) {
writeFileSync(
jsonOutputFile,
JSON.stringify(
{
files: composeJsonLogsFiles(runAllFilesResult),
status: hasProblems ? 'error' : hasWarnings ? 'warn' : 'success',
totalTime: performance.now() - startedAt,
} as JsonLogs,
null,
2
),
'utf-8'
);
logger.log(blue(indent(`JSON logs saved in ${green(jsonOutputFile)}`, 2)));
logger.printNewLine();
logger.printNewLine();
}
if (hasProblems) {
throw new Error(' Tests exited with error ');
}
} catch (err) {
exitWithError((err as Error)?.message ?? err);
}
}
async function runFile(
argv: RunArgv,
startedAt: number,
output: { harFile: string | undefined },
collectSpecData?: CollectFn
) {
const { executedWorkflows, ctx } = await runTestFile(argv, output, collectSpecData);
const totals = calculateTotals(executedWorkflows);
const hasProblems = totals.workflows.failed > 0;
const hasWarnings = totals.workflows.warnings > 0;
if (totals.steps.failed > 0 || totals.steps.warnings > 0 || totals.steps.skipped > 0) {
displayErrors(executedWorkflows);
}
displaySummary(startedAt, executedWorkflows, argv);
return {
hasProblems,
hasWarnings,
file: argv.file,
executedWorkflows,
argv,
ctx,
totalTimeMs: performance.now() - startedAt,
totalRequests: totals.totalRequests,
};
}