-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathtest_runner.mjs
212 lines (183 loc) · 5.74 KB
/
test_runner.mjs
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
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-present eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
/* eslint-env node */
import {spawn} from "child_process";
import fs from "fs";
import path from "path";
import {fileURLToPath} from "url";
import {promisify} from "util";
import MemoryFS from "memory-fs";
import webpack from "webpack";
import chromiumRemoteProcess from "./test/runners/chromium_remote_process.mjs";
import chromiumProcess from "./test/runners/chromium_process.mjs";
import firefoxProcess from "./test/runners/firefox_process.mjs";
let dirname = path.dirname(fileURLToPath(import.meta.url));
let unitFiles = [];
let browserFiles = [];
let runnerDefinitions = {
// Chromium with chrome-remote-interface
chromium_remote: chromiumRemoteProcess,
// Chromium with WebDriver (requires Chromium >= 63.0.3239)
chromium: chromiumProcess,
firefox: firefoxProcess
};
function configureRunners() {
let runners = "BROWSER_TEST_RUNNERS" in process.env ?
process.env.BROWSER_TEST_RUNNERS.split(",") :
[];
if (runners.length == 0) {
// We default to not using the Chromium remote interface on Windows,
// as it fails.
if (process.platform == "win32")
return ["chromium", "firefox"];
return ["chromium_remote", "firefox"];
}
return runners.filter(
runner => Object.prototype.hasOwnProperty.call(runnerDefinitions, runner)
);
}
let runnerProcesses = configureRunners();
function addTestPaths(testPaths, recurse) {
for (let testPath of testPaths) {
let stat = fs.statSync(testPath);
if (stat.isDirectory()) {
if (recurse) {
addTestPaths(fs.readdirSync(testPath).map(
file => path.join(testPath, file))
);
}
continue;
}
if (path.basename(testPath).startsWith("_"))
continue;
if (path.extname(testPath) == ".js") {
if (testPath.split(path.sep).includes("browser"))
browserFiles.push(testPath);
else
unitFiles.push(testPath);
}
}
}
async function webpackInMemory(bundleFilename, options) {
// Based on this example
// https://webpack.js.org/api/node/#custom-file-systems
let memoryFS = new MemoryFS();
options.output = {filename: bundleFilename, path: "/"};
options.devtool = "cheap-eval-source-map";
let webpackCompiler = webpack(options);
webpackCompiler.outputFileSystem = memoryFS;
let stats = null;
try {
stats = await promisify(webpackCompiler.run).call(webpackCompiler);
}
catch (error) {
// Error handling is based on this example
// https://webpack.js.org/api/node/#error-handling
let reason = error.stack || error;
if (error.details)
reason += "\n" + error.details;
throw reason;
}
if (stats.hasErrors())
throw stats.toJson().errors;
let bundle = memoryFS.readFileSync("/" + bundleFilename, "utf-8");
memoryFS.unlinkSync("/" + bundleFilename);
return bundle;
}
async function runBrowserTests(processes) {
if (browserFiles.length == 0)
return;
let bundleFilename = "bundle.js";
let mochaPath = path.join(dirname, "node_modules", "mocha", "mocha.js");
let chaiPath = path.join(dirname, "node_modules", "chai", "chai.js");
let bundle = await webpackInMemory(bundleFilename, {
entry: path.join(dirname, "test", "browser", "_bootstrap.js"),
module: {
rules: [
{
// we use the browser version of mocha
resource: mochaPath,
use: ["script-loader"]
},
{
resource: chaiPath,
use: ["script-loader"]
}
]
},
resolve: {
alias: {
mocha$: mochaPath,
chai$: chaiPath
},
modules: [path.resolve(dirname, process.env.LIB_FOLDER || "lib")]
},
optimization:
{
minimize: false
}
});
let errors = [];
await Promise.all(
processes.map(
currentProcess => runnerDefinitions[currentProcess](
bundle, bundleFilename, browserFiles.map(
file => path.relative(path.join(dirname, "test", "browser"), file)
.replace(/\.js$/, "")
)
)
// We need to convert rejected promise to a resolved one
// or the test will not let close the webdriver.
.catch(error => {
errors.push(error);
})
)
);
if (errors.length > 0)
throw `Browser tests failed: ${errors.join(", ")}`;
}
function spawnScript(name, ...args) {
return new Promise((resolve, reject) => {
let script = spawn("npm",
["run", name, ...args],
{stdio: ["inherit", "inherit", "inherit"]});
script.on("error", reject);
script.on("close", code => {
if (code == 0)
resolve();
else
reject();
});
});
}
(async function() {
let paths = process.argv.length > 2 ? process.argv.slice(2) :
[path.join(dirname, "test"),
path.join(dirname, "test", "browser"),
path.join(dirname, "test", "scripts")];
addTestPaths(paths, true);
try {
await runBrowserTests(runnerProcesses);
if (unitFiles.length > 0)
await spawnScript("unit-tests", ...unitFiles);
}
catch (error) {
if (error)
console.error(error);
process.exit(1);
}
})();