-
Notifications
You must be signed in to change notification settings - Fork 0
/
suites.ts
129 lines (115 loc) · 3.35 KB
/
suites.ts
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
import { Command, Flags, Args } from '@oclif/core';
import { OASResult } from '../validation';
import { DEFAULT_TEST_NAME, DEFAULT_SUITE_ID } from '../utilities/constants';
import Loast from '../loast';
import JSONStructuredOutputFactory from '../utilities/structured-output';
export default class Suites extends Command {
static description =
'Runs a set of test suites for an API based on the OpenAPI spec';
static flags = {
help: Flags.help({ char: 'h' }),
apiKey: Flags.string({
char: 'a',
description: 'API key to use',
}),
bearerToken: Flags.string({
char: 'b',
description: 'Bearer token to use',
env: 'LOAST_BEARER_TOKEN',
}),
server: Flags.string({
char: 's',
description: 'Server URL to use',
}),
id: Flags.string({
char: 'i',
multiple: true,
description: 'Suite Ids to use',
}),
jsonOutput: Flags.boolean({
char: 'j',
description: 'Format output as JSON',
}),
warning: Flags.boolean({
char: 'w',
description: 'remove warnings',
}),
};
static args = {
path: Args.string({
required: true,
description: 'Url or local file path containing the OpenAPI spec',
}),
};
async run(): Promise<void> {
const { args, flags } = await this.parse(Suites);
const loast = new Loast(DEFAULT_TEST_NAME, {
path: args.path,
server: flags.server,
apiKey: flags.apiKey,
token: flags.bearerToken,
suiteIds: flags.id,
});
let results: OASResult[];
try {
results = await loast.getResults();
} catch (error) {
if (flags.jsonOutput) {
// return the error message as part of the json output
results = [
new OASResult(
DEFAULT_SUITE_ID,
DEFAULT_TEST_NAME,
args.path,
flags.server,
[],
undefined,
(error as Error).message,
),
];
} else {
// otherwise, let the error propagate up
throw error;
}
}
this.logTestResults(results, flags.jsonOutput, flags.warning);
}
private logTestResults(
results: OASResult[],
isJsonOutput: boolean,
hasWarningFlag: boolean,
): void {
results.forEach((result) => {
if (hasWarningFlag) this.noWarningsIncluded(result);
if (isJsonOutput) {
const output = JSONStructuredOutputFactory.buildFromOASResult(result);
this.log(JSON.stringify(output));
} else {
this.log(result.toString());
}
this.determineFailure(result);
});
}
determineFailure(result: OASResult): void {
const failedOperationCount = result.results
? result.results.filter((result) => result.failures.size > 0).length
: 0;
const totalOperationCount = result.results ? result.results.length : 0;
const passedOperationCount = totalOperationCount - failedOperationCount;
if (failedOperationCount > 0) {
this.log(
`${failedOperationCount}/${totalOperationCount} operation${
totalOperationCount > 1 ? 's' : ''
} failed; ${passedOperationCount}/${totalOperationCount} operation${
totalOperationCount > 1 ? 's' : ''
} passed`,
);
}
}
noWarningsIncluded(result: OASResult): OASResult {
result.results?.map((item) => {
delete item.warnings;
});
return result;
}
}