This repository was archived by the owner on Oct 24, 2019. It is now read-only.
forked from adblockplus/adblockpluscore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner.js
More file actions
212 lines (191 loc) · 5.53 KB
/
test_runner.js
File metadata and controls
212 lines (191 loc) · 5.53 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
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 */
"use strict";
const fs = require("fs");
const path = require("path");
const MemoryFS = require("memory-fs");
const Mocha = require("mocha");
const webpack = require("webpack");
const chromiumRemoteProcess = require("./test/runners/chromium_remote_process");
const chromiumProcess = require("./test/runners/chromium_process");
const edgeProcess = require("./test/runners/edge_process");
const firefoxProcess = require("./test/runners/firefox_process");
let unitFiles = [];
let browserFiles = [];
let runnerDefinitions = {
// Chromium with chrome-remote-interface
chromium_remote: chromiumRemoteProcess,
// Chromium with WebDriver (requires Chromium >= 63.0.3239)
chromium: chromiumProcess,
edge: edgeProcess,
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", "edge", "firefox"];
return ["chromium_remote", "firefox"];
}
return runners.filter(runner => runnerDefinitions.hasOwnProperty(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);
}
}
}
function webpackInMemory(bundleFilename, options)
{
return new Promise((resolve, reject) =>
{
// 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;
webpackCompiler.run((err, stats) =>
{
// Error handling is based on this example
// https://webpack.js.org/api/node/#error-handling
if (err)
{
let reason = err.stack || err;
if (err.details)
reason += "\n" + err.details;
reject(reason);
}
else if (stats.hasErrors())
reject(stats.toJson().errors);
else
{
let bundle = memoryFS.readFileSync("/" + bundleFilename, "utf-8");
memoryFS.unlinkSync("/" + bundleFilename);
resolve(bundle);
}
});
});
}
function runBrowserTests(processes)
{
if (!browserFiles.length)
return Promise.resolve();
let bundleFilename = "bundle.js";
let mochaPath = path.join(__dirname, "node_modules", "mocha",
"mocha.js");
let chaiPath = path.join(__dirname, "node_modules", "chai", "chai.js");
return 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, "lib")]
}
}).then(bundle =>
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(e => e)
)).then(results =>
{
let errors = results.filter(e => typeof e != "undefined");
if (errors.length)
throw `Browser unit test failed: ${errors.join(", ")}`;
})
);
}
if (process.argv.length > 2)
addTestPaths(process.argv.slice(2), true);
else
{
addTestPaths(
[path.join(__dirname, "test"), path.join(__dirname, "test", "browser")],
true
);
}
const mocha = new Mocha();
mocha.checkLeaks();
runBrowserTests(runnerProcesses).then(() =>
{
if (unitFiles.length > 0)
{
mocha.files = unitFiles;
return new Promise((resolve, reject) =>
{
mocha.run(failures =>
{
if (failures)
reject("Tests failed");
else
resolve();
});
});
}
}).catch(error =>
{
console.error(error);
process.exit(1);
});