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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 const url = require("url"); | |
25 | 24 |
26 const nodeunit = require("nodeunit"); | 25 const nodeunit = require("nodeunit"); |
| 26 const webpack = require("webpack"); |
| 27 const MemoryFS = require("memory-fs"); |
27 | 28 |
28 const chromiumProcess = require("./chromium_process"); | 29 const chromiumProcess = require("./chromium_process"); |
29 | 30 |
30 let unitFiles = []; | 31 let unitFiles = []; |
31 let browserFiles = []; | 32 let browserFiles = []; |
32 | 33 |
33 function addTestPaths(testPaths, recurse) | 34 function addTestPaths(testPaths, recurse) |
34 { | 35 { |
35 for (let testPath of testPaths) | 36 for (let testPath of testPaths) |
36 { | 37 { |
(...skipping 12 matching lines...) Expand all Loading... |
49 if (path.extname(testPath) == ".js") | 50 if (path.extname(testPath) == ".js") |
50 { | 51 { |
51 if (testPath.split(path.sep).includes("browser")) | 52 if (testPath.split(path.sep).includes("browser")) |
52 browserFiles.push(testPath); | 53 browserFiles.push(testPath); |
53 else | 54 else |
54 unitFiles.push(testPath); | 55 unitFiles.push(testPath); |
55 } | 56 } |
56 } | 57 } |
57 } | 58 } |
58 | 59 |
59 function getFileURL(filePath) | 60 function webpackInMemory(bundleFilename, options) |
60 { | 61 { |
61 return url.format({ | 62 return new Promise((resolve, reject) => |
62 protocol: "file", | 63 { |
63 slashes: "true", | 64 // Based on this example |
64 pathname: path.resolve(process.cwd(), filePath).split(path.sep).join("/") | 65 // https://webpack.js.org/api/node/#custom-file-systems |
| 66 let memoryFS = new MemoryFS(); |
| 67 |
| 68 options.output = {filename: bundleFilename, path: "/"}; |
| 69 let webpackCompiler = webpack(options); |
| 70 webpackCompiler.outputFileSystem = memoryFS; |
| 71 |
| 72 webpackCompiler.run((err, stats) => |
| 73 { |
| 74 // Error handling is based on this example |
| 75 // https://webpack.js.org/api/node/#error-handling |
| 76 if (err) |
| 77 { |
| 78 let reason = err.stack || err; |
| 79 if (err.details) |
| 80 reason += "\n" + err.details; |
| 81 reject(reason); |
| 82 } |
| 83 else if (stats.hasErrors()) |
| 84 reject(stats.toJson().errors); |
| 85 else |
| 86 { |
| 87 let bundle = memoryFS.readFileSync("/" + bundleFilename, "utf-8"); |
| 88 memoryFS.unlinkSync("/" + bundleFilename); |
| 89 resolve(bundle); |
| 90 } |
| 91 }); |
65 }); | 92 }); |
66 } | 93 } |
67 | 94 |
68 function runBrowserTests() | 95 function runBrowserTests() |
69 { | 96 { |
70 if (!browserFiles.length) | 97 if (!browserFiles.length) |
71 return; | 98 return; |
72 | 99 |
73 // Navigate to this directory because about:blank won't be allowed to load | 100 let nodeunitPath = path.join(__dirname, "node_modules", "nodeunit", |
74 // file:/// URLs. | 101 "examples", "browser", "nodeunit.js"); |
75 let initialPage = getFileURL(__dirname); | 102 let bundleFilename = "bundle.js"; |
76 let bootstrapPath = path.join(__dirname, "test", "browser", | 103 |
77 "_bootstrap.js"); | 104 return webpackInMemory(bundleFilename, { |
78 let nodeunitPath = path.join( | 105 entry: path.join(__dirname, "test", "browser", "_bootstrap.js"), |
79 path.dirname(require.resolve("nodeunit")), | 106 module: { |
80 "examples", "browser", "nodeunit.js" | 107 rules: [{ |
81 ); | 108 resource: nodeunitPath, |
82 let args = [ | 109 // I would have rather used exports-loader here, to avoid treating |
83 getFileURL(nodeunitPath), | 110 // nodeunit as a global. Unfortunately the nodeunit browser example |
84 ...browserFiles.map(getFileURL) | 111 // script is quite slopily put together, if exports isn't falsey it |
85 ]; | 112 // breaks! As a workaround we need to use script-loader, which means |
86 return chromiumProcess(initialPage, bootstrapPath, args); | 113 // that exports is falsey for that script as a side-effect. |
| 114 use: ["script-loader"] |
| 115 }] |
| 116 }, |
| 117 resolve: { |
| 118 alias: { |
| 119 nodeunit$: nodeunitPath |
| 120 }, |
| 121 modules: [path.resolve(__dirname, "lib")] |
| 122 } |
| 123 }).then(bundle => |
| 124 { |
| 125 return chromiumProcess( |
| 126 bundle, bundleFilename, |
| 127 browserFiles.map( |
| 128 file => path.relative(path.join(__dirname, "test", "browser"), |
| 129 file).replace(/\.js$/, "") |
| 130 ) |
| 131 ); |
| 132 }); |
87 } | 133 } |
88 | 134 |
89 if (process.argv.length > 2) | 135 if (process.argv.length > 2) |
90 addTestPaths(process.argv.slice(2), true); | 136 addTestPaths(process.argv.slice(2), true); |
91 else | 137 else |
92 { | 138 { |
93 addTestPaths( | 139 addTestPaths( |
94 [path.join(__dirname, "test"), path.join(__dirname, "test", "browser")], | 140 [path.join(__dirname, "test"), path.join(__dirname, "test", "browser")], |
95 true | 141 true |
96 ); | 142 ); |
97 } | 143 } |
98 | 144 |
99 Promise.resolve(runBrowserTests()).catch(error => | 145 Promise.resolve(runBrowserTests()).catch(error => |
100 { | 146 { |
101 console.error("Failed running browser tests"); | 147 console.error("Failed running browser tests"); |
102 console.error(error); | 148 console.error(error); |
103 }).then(() => | 149 }).then(() => |
104 { | 150 { |
105 if (unitFiles.length) | 151 if (unitFiles.length) |
106 nodeunit.reporters.default.run(unitFiles); | 152 nodeunit.reporters.default.run(unitFiles); |
107 }); | 153 }); |
OLD | NEW |