-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-runner.ts
More file actions
126 lines (97 loc) · 3.39 KB
/
test-runner.ts
File metadata and controls
126 lines (97 loc) · 3.39 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
import http from "http";
import fs from "fs";
import path from "path";
function template(files: string[]): string {
return /*html*/`
<html>
<head>
<meta charset="utf-8">
<title>ProtoCommander Test</title>
<link href="https://unpkg.com/mocha/mocha.css" rel="stylesheet" />
</head>
<body>
<div id="mocha"></div>
<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>
<script>mocha.setup('bdd')</script>
${files.map(f => `<script type="module" src="${f}"></script>`).join("\n")}
<script type="module">
mocha.checkLeaks();
mocha.run();
</script>
</body>
</html>`;
};
function findFiles(callback: (err: string | null, files: string[] | null) => void) {
function recursiveFileFinder(rootFolder: string): string[] {
let result: string[] = [];
fs.readdirSync(rootFolder).forEach((f) => {
if (fs.lstatSync(rootFolder + "/" + f).isDirectory())
recursiveFileFinder(rootFolder + "/" + f).forEach(ff => result.push(ff));
if (f.endsWith("test.js"))
result.push(rootFolder + "/" + f);
});
return result;
};
try {
callback(null, recursiveFileFinder("./test"));
} catch (err) {
callback(err, null);
}
}
const server = http.createServer((req, res) => {
let filePath = '.' + req.url;
if (filePath == './') {
filePath = './index.html';
}
if (filePath === "./index.html") {
findFiles((err, files) => {
if (err || files === null) {
res.writeHead(500);
res.end("Error: " + err);
return;
}
res.writeHead(200);
res.end(template(files), "utf-8");
});
} else {
let extname = String(path.extname(filePath)).toLowerCase();
let mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp4': 'video/mp4',
'.woff': 'application/font-woff',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'application/font-otf',
'.wasm': 'application/wasm'
};
let contentType = (mimeTypes as any)[extname] || 'application/octet-stream';
fs.readFile(filePath, function (error, content) {
if (error) {
if (error.code == 'ENOENT') {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end("Content not found", 'utf-8');
}
else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
}
}
else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
}
});
server.listen(process.env.PORT || 8080, () => {
console.log(`Test server now running at: http://localhost:${process.env.PORT || 8080}`);
});