Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: test_runner.js

Issue 29517687: Issue 5079, 5516 - Use webpack for browser tests, modules for content scripts (Closed)
Left Patch Set: Created Aug. 16, 2017, 3:40 p.m.
Right Patch Set: Addressed nits Created Aug. 17, 2017, 1:25 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « test/browser/elemHideEmulation.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 24
25 const nodeunit = require("nodeunit"); 25 const nodeunit = require("nodeunit");
26 const webpack = require("webpack");
27 const MemoryFS = require("memory-fs");
26 28
27 const chromiumProcess = require("./chromium_process"); 29 const chromiumProcess = require("./chromium_process");
28 30
29 let unitFiles = []; 31 let unitFiles = [];
30 let browserFiles = []; 32 let browserFiles = [];
31 33
32 function addTestPaths(testPaths, recurse) 34 function addTestPaths(testPaths, recurse)
33 { 35 {
34 for (let testPath of testPaths) 36 for (let testPath of testPaths)
35 { 37 {
(...skipping 12 matching lines...) Expand all
48 if (path.extname(testPath) == ".js") 50 if (path.extname(testPath) == ".js")
49 { 51 {
50 if (testPath.split(path.sep).includes("browser")) 52 if (testPath.split(path.sep).includes("browser"))
51 browserFiles.push(testPath); 53 browserFiles.push(testPath);
52 else 54 else
53 unitFiles.push(testPath); 55 unitFiles.push(testPath);
54 } 56 }
55 } 57 }
56 } 58 }
57 59
60 function webpackInMemory(bundleFilename, options)
61 {
62 return new Promise((resolve, reject) =>
63 {
64 // Based on this example
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 });
92 });
93 }
94
58 function runBrowserTests() 95 function runBrowserTests()
59 { 96 {
60 if (browserFiles.length) 97 if (!browserFiles.length)
61 return chromiumProcess(browserFiles); 98 return;
99
100 let nodeunitPath = path.join(__dirname, "node_modules", "nodeunit",
101 "examples", "browser", "nodeunit.js");
102 let bundleFilename = "bundle.js";
103
104 return webpackInMemory(bundleFilename, {
105 entry: path.join(__dirname, "test", "browser", "_bootstrap.js"),
106 module: {
107 rules: [{
108 resource: nodeunitPath,
109 // I would have rather used exports-loader here, to avoid treating
110 // nodeunit as a global. Unfortunately the nodeunit browser example
111 // script is quite slopily put together, if exports isn't falsey it
112 // breaks! As a workaround we need to use script-loader, which means
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 });
62 } 133 }
63 134
64 if (process.argv.length > 2) 135 if (process.argv.length > 2)
65 addTestPaths(process.argv.slice(2), true); 136 addTestPaths(process.argv.slice(2), true);
66 else 137 else
67 { 138 {
68 addTestPaths( 139 addTestPaths(
69 [path.join(__dirname, "test"), path.join(__dirname, "test", "browser")], 140 [path.join(__dirname, "test"), path.join(__dirname, "test", "browser")],
70 true 141 true
71 ); 142 );
72 } 143 }
73 144
74 Promise.resolve(runBrowserTests()).catch(error => 145 Promise.resolve(runBrowserTests()).catch(error =>
75 { 146 {
76 console.error("Failed running browser tests"); 147 console.error("Failed running browser tests");
77 console.error(error); 148 console.error(error);
78 }).then(() => 149 }).then(() =>
79 { 150 {
80 if (unitFiles.length) 151 if (unitFiles.length)
81 nodeunit.reporters.default.run(unitFiles); 152 nodeunit.reporters.default.run(unitFiles);
82 }); 153 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld