-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
213 lines (171 loc) · 6.32 KB
/
test.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
/** Part of the code (the server part) is from:
* https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework
*
*/
const fs = require('fs');
const http = require('http');
const path = require('path');
const { exit } = require('process');
var exec = require('child_process').exec;
let args = process.argv.slice(2);
const waitFlag = args.includes('--wait')
const verboseFlag = args.includes('--verbose');
const noCleanFlag = args.includes('--no-clean');
if(args.includes('--help') || args.includes('-h')){
console.log("Usage: node test.js [--wait] [--verbose] [--help]");
console.log(" --wait: Keep the server running after the tests are done. (must be killed manually)");
console.log(" --verbose: Print verbose test results");
console.log(" --no-clean: Do not clean the generated feed files")
console.log(" --help: Print this help text");
exit(0);
}
/** START OF PART FROM MDN */
const PORT = 0;
const MIME_TYPES = {
default: 'application/octet-stream',
html: 'text/html; charset=UTF-8',
js: 'application/javascript; charset=UTF-8',
xml: 'application/xml; charset=UTF-8',
css: 'text/css',
png: 'image/png',
jpg: 'image/jpg',
gif: 'image/gif',
ico: 'image/x-icon',
svg: 'image/svg+xml',
};
const STATIC_PATH = path.join(process.cwd(), './tests');
const toBool = [() => true, () => false];
const prepareFile = async (url) => {
const paths = [STATIC_PATH, url];
if (url.endsWith('/')) paths.push('index.html');
const filePath = path.join(...paths);
const pathTraversal = !filePath.startsWith(STATIC_PATH);
const exists = await fs.promises.access(filePath).then(...toBool);
const found = !pathTraversal && exists;
const streamPath = found ? filePath : STATIC_PATH + '/404.html';
const ext = path.extname(streamPath).substring(1).toLowerCase();
const stream = fs.createReadStream(streamPath);
return { found, ext, stream };
};
var srv = http.createServer(async (req, res) => {
const file = await prepareFile(req.url);
const statusCode = file.found ? 200 : 404;
const mimeType = MIME_TYPES[file.ext] || MIME_TYPES.default;
res.writeHead(statusCode, { 'Content-Type': mimeType });
file.stream.pipe(res);
// console.log(`${req.method} ${req.url} ${statusCode}`);
/** END OF PART FROM MDN */
}).listen(PORT, async () => {
console.log(`Server running at http://localhost:${srv.address().port}/`);
let results = await performTests();
printResults(results, verboseFlag);
if(!noCleanFlag)
cleanFeedFiles();
if(!waitFlag)
srv.close();
});
async function performTests() {
return new Promise(async (resolve, reject) => {
const files = fs.readdirSync(STATIC_PATH);
if (!files) {
console.error("Could not list the directory.", err);
reject(err);
}
testFiles = files.filter(file => file.endsWith('.json'));
let results = [];
for(const file of testFiles) {
let fileNoExt = file.replace('.json', '');
let testPath = path.join(STATIC_PATH, file);
let testFile = fs.readFileSync(testPath, 'utf8');
let test = JSON.parse(testFile);
let feedOriginal = test.feedFile;
let feedUrl = test.feedUrl;
// Feed file stuff
if(feedOriginal){
let feedLines = feedOriginal.map( line => `http://localhost:${srv.address().port}/${line}`);
let feed = feedLines.join('\n');
fs.writeFileSync(path.join(STATIC_PATH,`${fileNoExt}.feed`), feed);
}else if(feedUrl){
test.args = test.args.replace('FEEDURL', `http://localhost:${srv.address().port}/${feedUrl}`);
}else{
console.log(`No feed file or url specified. (Skipping test ${fileNoExt}`);
continue;
}
let executionResult = await runTest(test);
let result = new TestResult();
//determine if the test passed or failed
if(test.returnCode == 0 && executionResult.returnCode == 0){
let expectedPath = path.join(STATIC_PATH, `${fileNoExt}.out`);
let expectedOutput;
try{
expectedOutput = fs.readFileSync(expectedPath, 'utf8');
} catch (err) {
console.error(`Could not read the expected output file (${fileNoExt}.out). (skipping test ${fileNoExt})`);
continue;
}
result.passed = executionResult.returnCode == 0 && isOutputCorrect(executionResult.stdout, expectedOutput);
}else{
result.passed = executionResult.returnCode == test.returnCode;
}
result.testName = fileNoExt;
result.executionResult = executionResult;
result.expectedReturnCode = test.returnCode;
results.push(result);
}
resolve(results);
});
}
class TestResult {
constructor() {
this.testName = "";
this.executionResult = null;
this.passed = false;
this.expectedReturnCode = 0;
}
}
class ExecutionResult {
constructor() {
this.returnCode = 0;
this.stdout = '';
this.stderr = '';
}
}
function isOutputCorrect(stdout, expected) {
stdout = stdout.replace(/\s/g, "");
expected = expected.replace(/\s/g, "");
return stdout == expected;
}
async function runTest(test) {
let args = test.args;
return new Promise((resolve, reject) => {
exec("./feedreader "+args, (error, stdout, stderr) => {
let result = new ExecutionResult();
result.returnCode = error ? error.code : 0;
result.stdout = stdout;
result.stderr = stderr;
resolve(result);
});
});
}
function printResults(results, verbose) {
console.log("======= Test results =======");
for(const result of results){
console.log(`Test ${result.testName}: ${result.passed ? "\x1b[32mOK" : "\x1b[1m\x1b[31mFAIL"}\x1b[0m`);
}
console.log("========= Summary =========");
let passed = results.filter(result => result.passed);
console.log(`Passed: ${passed.length}/${results.length}`);
if(verbose)
console.log("Verbose info:", results);
}
function cleanFeedFiles() {
const files = fs.readdirSync(STATIC_PATH);
if (!files) {
console.error("Could not list the directory.", err);
reject(err);
}
testFiles = files.filter(file => file.endsWith('.feed'));
for(const file of testFiles) {
fs.unlinkSync(path.join(STATIC_PATH, file));
}
}