forked from Mermade/oas-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
testRunner.js
282 lines (257 loc) · 8.66 KB
/
testRunner.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// @ts-check
'use strict';
var fs = require('fs');
var path = require('path');
var util = require('util');
var readfiles = require('node-readfiles');
var yaml = require('js-yaml');
var common = require('./common.js');
var swagger2openapi = require('./index.js');
var validator = require('./validate.js');
var globalExpectFailure = false;
var argv = require('yargs')
.usage('testRunner [options] [{path-to-specs}...]')
.string('encoding')
.alias('e', 'encoding')
.default('encoding', 'utf8')
.describe('encoding', 'encoding for input/output files')
.string('fail')
.describe('fail', 'path to specs expected to fail')
.alias('f', 'fail')
.string('jsonschema')
.alias('j', 'jsonschema')
.describe('jsonschema', 'path to alternative JSON schema')
.boolean('laxurls')
.alias('l', 'laxurls')
.describe('laxurls', 'lax checking of empty urls')
.boolean('lint')
.describe('lint','lint the definition')
.boolean('nopatch')
.alias('n', 'nopatch')
.describe('nopatch', 'do not patch minor errors in the source definition')
.boolean('output')
.alias('o', 'output')
.describe('output', 'output conversion as openapi.yaml')
.boolean('quiet')
.alias('q', 'quiet')
.describe('quiet', 'do not show test passes on console, for CI')
.boolean('resolve')
.alias('r', 'resolve')
.describe('resolve', 'resolve external references')
.boolean('stop')
.alias('s', 'stop')
.describe('stop', 'stop on first error')
.count('verbose')
.alias('v', 'verbose')
.describe('verbose', 'increase verbosity')
.boolean('warnOnly')
.describe('warnOnly','Do not throw on non-patchable errors')
.boolean('whatwg')
.alias('w', 'whatwg')
.describe('whatwg', 'enable WHATWG URL parsing')
.boolean('yaml')
.alias('y', 'yaml')
.describe('yaml', 'skip YAML-safe test')
.help('h')
.alias('h', 'help')
.strict()
.version()
.argv;
var red = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[31m';
var green = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[32m';
var yellow = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[33;1m';
var normal = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[0m';
var pass = 0;
var fail = 0;
var failures = [];
var warnings = [];
var genStack = [];
var options = argv;
options.patch = !argv.nopatch;
function finalise(err, options) {
if (!argv.quiet || err) {
console.log(normal + options.file);
}
if (err) {
console.log(red + options.context.pop() + '\n' + err.message);
if (err.stack && err.name !== 'AssertionError') {
console.log(err.stack);
}
if (options.lintRule && options.lintRule.description !== err.message) {
console.warn(options.lintRule.description);
}
options.valid = !!options.expectFailure;
}
for (var warning of options.warnings) {
warnings.push(options.file + ' ' + warning);
}
var src = options.original;
var result = options.valid;
if (!argv.quiet) {
var colour = ((options.expectFailure ? !result : result) ? green : red);
if (src && src.info) {
console.log(colour + ' %s %s', src.info.title, src.info.version);
console.log(' %s', src.swagger ? (src.host ? src.host : 'relative') : (src.servers && src.servers.length ? src.servers[0].url : 'relative'));
}
}
if (result) {
pass++;
if ((options.file.indexOf('swagger.yaml') >= 0) && argv.output) {
let outFile = options.file.replace('swagger.yaml', 'openapi.yaml');
let resultStr = yaml.safeDump(options.openapi, {lineWidth: -1});
fs.writeFile(outFile, resultStr, argv.encoding);
}
}
else {
fail++;
if (options.file != 'unknown') failures.push(options.file);
if (argv.stop) process.exit(1);
}
genStackNext();
}
function handleResult(err, options) {
var result = false;
if (err) {
options = err.options || { file: 'unknown', src: { info: { version: '', title: '' } } };
options.context = [];
options.warnings = [];
options.expectFailure = globalExpectFailure;
finalise(err,options);
}
else {
result = options.openapi;
}
var resultStr = JSON.stringify(result);
if (typeof result !== 'boolean') try {
if (!options.yaml) {
resultStr = yaml.safeDump(result, { lineWidth: -1 }); // should be representable safely in yaml
let resultStr2 = yaml.safeDump(result, { lineWidth: -1, noRefs: true }); // have no identity ref_s
resultStr.should.not.be.exactly('{}','Result should not be empty');
resultStr.should.equal(resultStr2,'Result should have no object identity ref_s');
}
validator.validate(result, options, finalise);
}
catch (ex) {
console.log(normal + options.file);
console.log(red + options.context.pop() + '\n' + ex.message);
if (ex.stack && ex.name !== 'AssertionError') {
console.log(ex.stack);
}
options.valid = !options.expectFailure;
finalise(ex, options);
}
}
function genStackNext() {
if (!genStack.length) return false;
var gen = genStack.shift();
gen.next();
return true;
}
function* check(file, force, expectFailure) {
var result = false;
options.context = [];
options.expectFailure = expectFailure;
options.file = file;
var components = file.split(path.sep);
var name = components[components.length - 1];
if ((name.indexOf('.yaml') >= 0) || (name.indexOf('.json') >= 0) || force) {
var srcStr = fs.readFileSync(path.resolve(file), options.encoding);
var src;
try {
src = JSON.parse(srcStr);
}
catch (ex) {
try {
src = yaml.safeLoad(srcStr, { schema: yaml.JSON_SCHEMA, json: true });
}
catch (ex) {
var warning = 'Could not parse file ' + file + '\n' + ex.message;
console.log(red + warning);
warnings.push(warning);
}
}
if (!src || ((!src.swagger && !src.openapi))) {
genStackNext();
return true;
}
options.original = src;
options.source = file;
try {
swagger2openapi.convertObj(src, common.clone(options), handleResult);
}
catch (ex) {
console.log(red + 'Converter threw an error: ' + ex.message);
warnings.push('Converter failed ' + options.source);
genStackNext();
result = false;
}
}
else {
genStackNext();
result = true;
}
return result;
}
function processPathSpec(pathspec, expectFailure) {
globalExpectFailure = expectFailure;
if (pathspec.startsWith('@')) {
pathspec = pathspec.substr(1, pathspec.length - 1);
var list = fs.readFileSync(pathspec, 'utf8').split('\r').join('').split('\n');
for (var file of list) {
genStack.push(check(file, false, expectFailure));
}
genStackNext();
}
else if (fs.statSync(path.resolve(pathspec)).isFile()) {
genStack.push(check(pathspec, true, expectFailure));
genStackNext();
}
else {
readfiles(pathspec, { readContents: false, filenameFormat: readfiles.FULL_PATH }, function (err) {
if (err) console.log(util.inspect(err));
})
.then(files => {
files = files.sort();
for (var file of files) {
genStack.push(check(file, false, expectFailure));
}
genStackNext();
})
.catch(err => {
console.log(util.inspect(err));
});
}
}
process.exitCode = 1;
console.log('Gathering...');
if ((!argv._.length) && (!argv.fail)) {
argv._.push('../openapi-directory/APIs/');
}
for (let pathspec of argv._) {
processPathSpec(pathspec, false);
}
if (argv.fail) {
if (!Array.isArray(argv.fail)) argv.fail = [argv.fail];
for (let pathspec of argv.fail) {
processPathSpec(pathspec, true);
}
}
process.on('exit', function () {
if (warnings.length) {
warnings.sort();
console.log(normal + '\nWarnings:' + yellow);
for (var w in warnings) {
console.log(warnings[w]);
}
}
if (failures.length) {
failures.sort();
console.log(normal + '\nFailures:' + red);
for (var f in failures) {
console.log(failures[f]);
}
}
console.log(normal);
console.log('Tests: %s passing, %s failing, %s warnings', pass, fail, warnings.length);
process.exitCode = ((fail === 0) && (pass > 0)) ? 0 : 1;
});