| 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 chromiumProcess = require("./test/runners/chromium_process"); |
| 30 const firefoxProcess = require("./test/runners/firefox_process"); |
| 30 | 31 |
| 31 let unitFiles = []; | 32 let unitFiles = []; |
| 32 let browserFiles = []; | 33 let browserFiles = []; |
| 33 | 34 |
| 35 let runnerDefinitions = { |
| 36 chromium: chromiumProcess, |
| 37 firefox: firefoxProcess |
| 38 }; |
| 39 |
| 40 function configureRunners() |
| 41 { |
| 42 let runners = "BROWSER_TEST_RUNNERS" in process.env ? |
| 43 process.env.BROWSER_TEST_RUNNERS.split(",") : []; |
| 44 |
| 45 if (runners.length == 0) |
| 46 return ["chromium", "firefox"]; |
| 47 |
| 48 runners.filter(runner => runner in runnerDefinitions); |
| 49 |
| 50 return runners; |
| 51 } |
| 52 |
| 53 let runnerProcesses = configureRunners(); |
| 54 |
| 34 function addTestPaths(testPaths, recurse) | 55 function addTestPaths(testPaths, recurse) |
| 35 { | 56 { |
| 36 for (let testPath of testPaths) | 57 for (let testPath of testPaths) |
| 37 { | 58 { |
| 38 let stat = fs.statSync(testPath); | 59 let stat = fs.statSync(testPath); |
| 39 if (stat.isDirectory()) | 60 if (stat.isDirectory()) |
| 40 { | 61 { |
| 41 if (recurse) | 62 if (recurse) |
| 42 { | 63 { |
| 43 addTestPaths(fs.readdirSync(testPath).map( | 64 addTestPaths(fs.readdirSync(testPath).map( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 else | 107 else |
| 87 { | 108 { |
| 88 let bundle = memoryFS.readFileSync("/" + bundleFilename, "utf-8"); | 109 let bundle = memoryFS.readFileSync("/" + bundleFilename, "utf-8"); |
| 89 memoryFS.unlinkSync("/" + bundleFilename); | 110 memoryFS.unlinkSync("/" + bundleFilename); |
| 90 resolve(bundle); | 111 resolve(bundle); |
| 91 } | 112 } |
| 92 }); | 113 }); |
| 93 }); | 114 }); |
| 94 } | 115 } |
| 95 | 116 |
| 96 function runBrowserTests() | 117 function runBrowserTests(processes) |
| 97 { | 118 { |
| 98 if (!browserFiles.length) | 119 if (!browserFiles.length) |
| 99 return; | 120 return; |
| 100 | 121 |
| 101 let nodeunitPath = path.join(__dirname, "node_modules", "nodeunit", | 122 let nodeunitPath = path.join(__dirname, "node_modules", "nodeunit", |
| 102 "examples", "browser", "nodeunit.js"); | 123 "examples", "browser", "nodeunit.js"); |
| 103 let bundleFilename = "bundle.js"; | 124 let bundleFilename = "bundle.js"; |
| 104 | 125 |
| 105 return webpackInMemory(bundleFilename, { | 126 return webpackInMemory(bundleFilename, { |
| 106 entry: path.join(__dirname, "test", "browser", "_bootstrap.js"), | 127 entry: path.join(__dirname, "test", "browser", "_bootstrap.js"), |
| 107 module: { | 128 module: { |
| 108 rules: [{ | 129 rules: [{ |
| 109 resource: nodeunitPath, | 130 resource: nodeunitPath, |
| 110 // I would have rather used exports-loader here, to avoid treating | 131 // I would have rather used exports-loader here, to avoid treating |
| 111 // nodeunit as a global. Unfortunately the nodeunit browser example | 132 // nodeunit as a global. Unfortunately the nodeunit browser example |
| 112 // script is quite slopily put together, if exports isn't falsey it | 133 // 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 | 134 // breaks! As a workaround we need to use script-loader, which means |
| 114 // that exports is falsey for that script as a side-effect. | 135 // that exports is falsey for that script as a side-effect. |
| 115 use: ["script-loader"] | 136 use: ["script-loader"] |
| 116 }] | 137 }] |
| 117 }, | 138 }, |
| 118 resolve: { | 139 resolve: { |
| 119 alias: { | 140 alias: { |
| 120 nodeunit$: nodeunitPath | 141 nodeunit$: nodeunitPath |
| 121 }, | 142 }, |
| 122 modules: [path.resolve(__dirname, "lib")] | 143 modules: [path.resolve(__dirname, "lib")] |
| 123 } | 144 } |
| 124 }).then(bundle => | 145 }).then(bundle => |
| 125 { | 146 Promise.all( |
| 126 return chromiumProcess( | 147 processes.map(currentProcess => |
| 127 bundle, bundleFilename, | 148 runnerDefinitions[currentProcess]( |
| 128 browserFiles.map( | 149 bundle, bundleFilename, |
| 129 file => path.relative(path.join(__dirname, "test", "browser"), | 150 browserFiles.map( |
| 130 file).replace(/\.js$/, "") | 151 file => path.relative(path.join(__dirname, "test", "browser"), |
| 152 file).replace(/\.js$/, "") |
| 153 ) |
| 154 ) |
| 131 ) | 155 ) |
| 132 ); | 156 ) |
| 133 }); | 157 ); |
| 134 } | 158 } |
| 135 | 159 |
| 136 if (process.argv.length > 2) | 160 if (process.argv.length > 2) |
| 137 addTestPaths(process.argv.slice(2), true); | 161 addTestPaths(process.argv.slice(2), true); |
| 138 else | 162 else |
| 139 { | 163 { |
| 140 addTestPaths( | 164 addTestPaths( |
| 141 [path.join(__dirname, "test"), path.join(__dirname, "test", "browser")], | 165 [path.join(__dirname, "test"), path.join(__dirname, "test", "browser")], |
| 142 true | 166 true |
| 143 ); | 167 ); |
| 144 } | 168 } |
| 145 | 169 |
| 146 Promise.resolve(runBrowserTests()).catch(error => | 170 Promise.resolve(runBrowserTests(runnerProcesses)).catch(error => |
| 147 { | 171 { |
| 148 console.error("Failed running browser tests"); | 172 console.error("Failed running browser tests"); |
| 149 console.error(error); | 173 console.error(error); |
| 150 }).then(() => | 174 }).then(() => |
| 151 { | 175 { |
| 152 if (unitFiles.length) | 176 if (unitFiles.length) |
| 153 nodeunit.reporters.default.run(unitFiles); | 177 nodeunit.reporters.default.run(unitFiles); |
| 154 }); | 178 }); |
| OLD | NEW |