forked from thim81/openapi-format
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-utils.js
More file actions
67 lines (58 loc) · 1.67 KB
/
test-utils.js
File metadata and controls
67 lines (58 loc) · 1.67 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
const fs = require('fs');
const os = require('os');
const {exec} = require('child_process');
const path = require('path');
const {parseFile, stringify} = require('../../utils/file');
async function loadTest(folder, inputType = 'yaml', outType = 'yaml') {
let input,
outputBefore,
outputAfter = {};
const testPath = `./test/${folder}`;
const inputFile = `input.${inputType}`;
const inputPath = `./test/${folder}/${inputFile}`;
const outputFile = `output.${outType}`;
const outputPath = `./test/${folder}/${outputFile}`;
const tmpOutput = path.join(os.tmpdir(), `openapi-format-${folder}-output.${outType}`);
try {
input = await parseFile(inputPath);
} catch (err) {
// File not found = {} will be used
}
try {
outputBefore = await parseFile(outputPath);
} catch (err) {
// File not found = {} will be used
}
let result = await cli([`${inputFile}`, `--configFile options.yaml`, `--output ${tmpOutput}`], testPath);
try {
outputAfter = await parseFile(tmpOutput);
} catch (err) {
//
}
return {
result: result,
input: input,
outputBefore: await stringify(outputBefore),
outputAfter: await stringify(outputAfter)
};
}
function cli(args, cwd, envOverrides = {}) {
return new Promise(resolve => {
const env = {...process.env, ...envOverrides};
if (env.FORCE_COLOR && env.NO_COLOR) {
delete env.NO_COLOR;
}
exec(`node ${path.resolve('./bin/cli')} ${args.join(' ')}`, {cwd, env}, (error, stderr, stdout) => {
resolve({
code: error && error.code ? error.code : 0,
error,
stdout,
stderr
});
});
});
}
module.exports = {
loadTest: loadTest,
cli: cli
};