-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathtest-utils.js
117 lines (99 loc) · 3.25 KB
/
test-utils.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
const {spawn} = require("child_process");
const fs = require("fs-extra");
const path = require("path");
const KeySymbol = {
DOWN: "\x1B\x5B\x42",
UP: "\x1B\x5B\x41",
ENTER: "\x0D",
SPACE: "\x20",
};
const MockServerPort = {
SMAPI: 4010,
LWA: 4011,
};
const tempDirectory = path.join(process.cwd(), "test/temp");
const resetTempDirectory = () => {
fs.ensureDirSync(tempDirectory);
fs.emptyDirSync(tempDirectory);
};
const deleteFolderInTempDirectory = (folder) => {
const folderPath = path.join(tempDirectory, folder);
fs.removeSync(folderPath);
};
const getPathInTempDirectory = (folderPath) => path.join(tempDirectory, folderPath);
const makeFolderInTempDirectory = (folderPath) => {
const fullPath = getPathInTempDirectory(folderPath);
fs.ensureDirSync(fullPath);
return fullPath;
};
const run = (cmd, args, options = {}) => {
const inputs = options.inputs || [];
const parse = options.parse || false;
const returnProcessHandle = options.returnProcessHandle || false;
const cwd = options.cwd || tempDirectory;
const env = {...process.env, ...options.env};
fs.ensureDirSync(cwd);
fs.ensureFileSync(cmd);
const childProcess = spawn(cmd, args, {cwd, env, stdio: [null, null, null, null]});
return new Promise((resolve, reject) => {
let output = "";
let errorMessage = "";
const processStream = (data, isError = false) => {
const dataStr = data.toString();
if (isError) {
errorMessage += dataStr;
} else {
output += dataStr;
}
if (process.env.DEBUG) {
console.log(dataStr);
}
return dataStr;
};
const processData = (data) => processStream(data);
const processError = (data) => processStream(data, true);
childProcess.stdout.on("data", (data) => {
const dataStr = processData(data);
const index = inputs.findIndex((i) => dataStr.includes(i.match));
if (index > -1) {
const {input} = inputs[index];
inputs.splice(index, 1);
const value = input ? `${input}${KeySymbol.ENTER}` : KeySymbol.ENTER;
childProcess.stdin.write(value);
}
if (returnProcessHandle && inputs.length === 0) {
resolve(childProcess);
}
});
childProcess.stderr.on("data", (data) => {
errorMessage = processError(data);
});
childProcess.on("close", (code) => {
if (code) {
reject(new Error(`${output}${errorMessage}`));
} else {
output = parse ? JSON.parse(output) : output;
resolve(output);
}
});
});
};
const _startMockServer = async (port, swaggerSpecPath) => {
const inputs = [{match: "Prism is listening on"}];
const args = ["run", "prism", "--", "mock", "-p", port, swaggerSpecPath];
const options = {returnProcessHandle: true, inputs, cwd: process.cwd()};
return run("npm", args, options);
};
const startMockSmapiServer = () => _startMockServer(MockServerPort.SMAPI, "test/integration/fixtures/smapi_spec.json");
const startMockLwaServer = () => _startMockServer(MockServerPort.LWA, "test/integration/fixtures/lwa-swagger.json");
module.exports = {
KeySymbol,
getPathInTempDirectory,
makeFolderInTempDirectory,
resetTempDirectory,
deleteFolderInTempDirectory,
run,
startMockSmapiServer,
startMockLwaServer,
MockServerPort,
};