-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest.js
More file actions
149 lines (128 loc) · 4.97 KB
/
test.js
File metadata and controls
149 lines (128 loc) · 4.97 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
143
144
145
146
147
148
149
'use strict';
const fs = require('fs');
const path = require('path');
const {describe, it, expect} = require('@jest/globals');
const {run} = require('../bin/cli');
const {parseFile, stringify, writeFile} = require('../openapi-format');
// SELECTIVE TESTING DEBUG
const localTesting = false;
const destroyOutput = false;
// Load tests
const tests = !localTesting
? fs.readdirSync(__dirname).filter(file => {
return fs.statSync(path.join(__dirname, file)).isDirectory() && !file.startsWith('_');
})
: ['yaml-default-newline'];
describe('openapi-format tests', () => {
let consoleLogSpy, consoleWarnSpy;
beforeEach(() => {
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
});
afterEach(() => {
consoleLogSpy.mockRestore();
consoleWarnSpy.mockRestore();
});
tests.forEach(test => {
describe(test, () => {
it('should match expected output', async () => {
const start = process.hrtime();
let options = {};
let configFileOptions = {};
let inputFilename = null;
let configFilename = null;
let input = null;
let snap = null;
// Load input.yaml
configFilename = path.join(__dirname, test, 'options.yaml');
if (!fs.existsSync(configFilename)) {
configFilename = path.join(__dirname, test, 'input.json');
}
if (fs.existsSync(configFilename)) {
// Load options file
configFileOptions = await parseFile(configFilename);
configFileOptions.sort = !configFileOptions['no-sort'];
if (configFileOptions['no-sort'] && configFileOptions['no-sort'] === true) {
configFileOptions.sort = !configFileOptions['no-sort'];
delete configFileOptions['no-sort'];
}
configFileOptions.bundle = !configFileOptions['no-bundle'];
if (configFileOptions['no-sort'] && configFileOptions['no-bundle'] === true) {
configFileOptions.bundle = !configFileOptions['no-bundle'];
delete configFileOptions['no-bundle'];
}
options = Object.assign({}, options, configFileOptions);
}
// Load input.yaml
inputFilename = path.join(__dirname, test, 'input.yaml');
if (!fs.existsSync(inputFilename)) {
inputFilename = path.join(__dirname, test, 'input.json');
}
// DEBUG
// console.log('options', options)
// console.log('inputFilename', inputFilename)
// return done();
const outputFilename = path.join(__dirname, test, options.output);
let readOutput = false;
let output = {};
// Destroy existing output, to force update test
if (destroyOutput) {
try {
fs.unlinkSync(outputFilename);
} catch (e) {
console.error('ERROR delete output.yaml', ex);
}
}
try {
let snapFileOptions = options;
snap = await parseFile(outputFilename, snapFileOptions);
snap = await stringify(snap, snapFileOptions);
readOutput = true;
} catch (ex) {
// console.error('ex', ex);
// No snap found.
}
// Initialize data
let result = input;
// Load proper options for test cases
delete options.output;
if (options.sortFile) options.sortFile = path.join(__dirname, test, options.sortFile);
if (options.sortComponentsFile)
options.sortComponentsFile = path.join(__dirname, test, options.sortComponentsFile);
if (options.filterFile) options.filterFile = path.join(__dirname, test, options.filterFile);
if (options.casingFile) options.casingFile = path.join(__dirname, test, options.casingFile);
if (options.generateFile) options.generateFile = path.join(__dirname, test, options.generateFile);
if (options.overlayFile) options.overlayFile = path.join(__dirname, test, options.overlayFile);
if (outputFilename.indexOf('.json') >= 0 || options.json) {
// Convert OpenAPI object to JSON string
options.format = 'json';
} else {
// Convert OpenAPI object to YAML string
options.format = 'yaml';
}
try {
// Execute OpenAPI-format
result = await run(inputFilename, options);
} catch (e) {
console.error('e', e);
}
try {
if (!readOutput) {
// Write OpenAPI string to file
await writeFile(outputFilename, result, options);
}
} catch (error) {
console.error('error', error);
}
try {
// Assert results with output
expect(result).toStrictEqual(snap);
} finally {
const [sec, nano] = process.hrtime(start);
const ms = (sec * 1e3 + nano / 1e6).toFixed(2);
process.stderr.write(`Test "${test}" took ${ms} ms\n`);
}
});
});
});
});