OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /* eslint-env node */ | 18 /* eslint-env node */ |
19 | 19 |
20 "use strict"; | 20 "use strict"; |
21 | 21 |
22 const fs = require("fs"); | 22 const fs = require("fs"); |
23 const path = require("path"); | 23 const path = require("path"); |
24 | 24 |
25 const MemoryFS = require("memory-fs"); | 25 const MemoryFS = require("memory-fs"); |
26 const nodeunit = require("nodeunit"); | 26 const nodeunit = require("nodeunit"); |
27 const webpack = require("webpack"); | 27 const webpack = require("webpack"); |
28 | 28 |
29 const chromiumProcess = require("./chromium_process"); | 29 const chromiumRemoteProcess = require("./test/runners/chromium_remote_process"); |
| 30 const chromiumProcess = require("./test/runners/chromium_process"); |
| 31 const firefoxProcess = require("./test/runners/firefox_process"); |
30 | 32 |
31 let unitFiles = []; | 33 let unitFiles = []; |
32 let browserFiles = []; | 34 let browserFiles = []; |
33 | 35 |
| 36 let runnerDefinitions = { |
| 37 // Chromium with chrome-remote-interface |
| 38 chromium_remote: chromiumRemoteProcess, |
| 39 // Chromium with WebDriver (requires Chromium >= 63.0.3239) |
| 40 chromium: chromiumProcess, |
| 41 firefox: firefoxProcess |
| 42 }; |
| 43 |
| 44 function configureRunners() |
| 45 { |
| 46 let runners = "BROWSER_TEST_RUNNERS" in process.env ? |
| 47 process.env.BROWSER_TEST_RUNNERS.split(",") : []; |
| 48 |
| 49 if (runners.length == 0) |
| 50 return ["chromium", "firefox"]; |
| 51 |
| 52 return runners.filter(runner => runnerDefinitions.hasOwnProperty(runner)); |
| 53 } |
| 54 |
| 55 let runnerProcesses = configureRunners(); |
| 56 |
34 function addTestPaths(testPaths, recurse) | 57 function addTestPaths(testPaths, recurse) |
35 { | 58 { |
36 for (let testPath of testPaths) | 59 for (let testPath of testPaths) |
37 { | 60 { |
38 let stat = fs.statSync(testPath); | 61 let stat = fs.statSync(testPath); |
39 if (stat.isDirectory()) | 62 if (stat.isDirectory()) |
40 { | 63 { |
41 if (recurse) | 64 if (recurse) |
42 { | 65 { |
43 addTestPaths(fs.readdirSync(testPath).map( | 66 addTestPaths(fs.readdirSync(testPath).map( |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 else | 109 else |
87 { | 110 { |
88 let bundle = memoryFS.readFileSync("/" + bundleFilename, "utf-8"); | 111 let bundle = memoryFS.readFileSync("/" + bundleFilename, "utf-8"); |
89 memoryFS.unlinkSync("/" + bundleFilename); | 112 memoryFS.unlinkSync("/" + bundleFilename); |
90 resolve(bundle); | 113 resolve(bundle); |
91 } | 114 } |
92 }); | 115 }); |
93 }); | 116 }); |
94 } | 117 } |
95 | 118 |
96 function runBrowserTests() | 119 function runBrowserTests(processes) |
97 { | 120 { |
98 if (!browserFiles.length) | 121 if (!browserFiles.length) |
99 return; | 122 return; |
100 | 123 |
101 let nodeunitPath = path.join(__dirname, "node_modules", "nodeunit", | 124 let nodeunitPath = path.join(__dirname, "node_modules", "nodeunit", |
102 "examples", "browser", "nodeunit.js"); | 125 "examples", "browser", "nodeunit.js"); |
103 let bundleFilename = "bundle.js"; | 126 let bundleFilename = "bundle.js"; |
104 | 127 |
105 return webpackInMemory(bundleFilename, { | 128 return webpackInMemory(bundleFilename, { |
106 entry: path.join(__dirname, "test", "browser", "_bootstrap.js"), | 129 entry: path.join(__dirname, "test", "browser", "_bootstrap.js"), |
107 module: { | 130 module: { |
108 rules: [{ | 131 rules: [{ |
109 resource: nodeunitPath, | 132 resource: nodeunitPath, |
110 // I would have rather used exports-loader here, to avoid treating | 133 // I would have rather used exports-loader here, to avoid treating |
111 // nodeunit as a global. Unfortunately the nodeunit browser example | 134 // nodeunit as a global. Unfortunately the nodeunit browser example |
112 // script is quite slopily put together, if exports isn't falsey it | 135 // script is quite slopily put together, if exports isn't falsey it |
113 // breaks! As a workaround we need to use script-loader, which means | 136 // breaks! As a workaround we need to use script-loader, which means |
114 // that exports is falsey for that script as a side-effect. | 137 // that exports is falsey for that script as a side-effect. |
115 use: ["script-loader"] | 138 use: ["script-loader"] |
116 }] | 139 }] |
117 }, | 140 }, |
118 resolve: { | 141 resolve: { |
119 alias: { | 142 alias: { |
120 nodeunit$: nodeunitPath | 143 nodeunit$: nodeunitPath |
121 }, | 144 }, |
122 modules: [path.resolve(__dirname, "lib")] | 145 modules: [path.resolve(__dirname, "lib")] |
123 } | 146 } |
124 }).then(bundle => | 147 }).then(bundle => |
125 { | 148 Promise.all( |
126 return chromiumProcess( | 149 processes.map(currentProcess => |
127 bundle, bundleFilename, | 150 runnerDefinitions[currentProcess]( |
128 browserFiles.map( | 151 bundle, bundleFilename, |
129 file => path.relative(path.join(__dirname, "test", "browser"), | 152 browserFiles.map( |
130 file).replace(/\.js$/, "") | 153 file => path.relative(path.join(__dirname, "test", "browser"), |
| 154 file).replace(/\.js$/, "") |
| 155 ) |
| 156 ) |
131 ) | 157 ) |
132 ); | 158 ) |
133 }); | 159 ); |
134 } | 160 } |
135 | 161 |
136 if (process.argv.length > 2) | 162 if (process.argv.length > 2) |
137 addTestPaths(process.argv.slice(2), true); | 163 addTestPaths(process.argv.slice(2), true); |
138 else | 164 else |
139 { | 165 { |
140 addTestPaths( | 166 addTestPaths( |
141 [path.join(__dirname, "test"), path.join(__dirname, "test", "browser")], | 167 [path.join(__dirname, "test"), path.join(__dirname, "test", "browser")], |
142 true | 168 true |
143 ); | 169 ); |
144 } | 170 } |
145 | 171 |
146 Promise.resolve(runBrowserTests()).catch(error => | 172 Promise.resolve(runBrowserTests(runnerProcesses)).catch(error => |
147 { | 173 { |
148 console.error("Failed running browser tests"); | 174 console.error("Failed running browser tests"); |
149 console.error(error); | 175 console.error(error); |
150 }).then(() => | 176 }).then(() => |
151 { | 177 { |
152 if (unitFiles.length) | 178 if (unitFiles.length) |
153 nodeunit.reporters.default.run(unitFiles); | 179 nodeunit.reporters.default.run(unitFiles); |
154 }); | 180 }); |
OLD | NEW |