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: Addressed Wladimir's and Hubert's initial feedback Created Aug. 17, 2017, 12:36 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"); 26 const webpack = require("webpack");
27 const MemoryFS = require("memory-fs"); 27 const MemoryFS = require("memory-fs");
Wladimir Palant 2017/08/17 13:06:10 Nit: sort the imports alphabetically?
kzar 2017/08/17 13:26:42 Well I did but case sensitively, I considered A to
Wladimir Palant 2017/08/18 20:49:56 Actually, people usually sort by module name, not
kzar 2017/08/21 09:07:01 Done.
28 28
29 const chromiumProcess = require("./chromium_process"); 29 const chromiumProcess = require("./chromium_process");
30 30
31 let unitFiles = []; 31 let unitFiles = [];
32 let browserFiles = []; 32 let browserFiles = [];
33 33
34 function addTestPaths(testPaths, recurse) 34 function addTestPaths(testPaths, recurse)
35 { 35 {
36 for (let testPath of testPaths) 36 for (let testPath of testPaths)
37 { 37 {
38 let stat = fs.statSync(testPath); 38 let stat = fs.statSync(testPath);
39 if (stat.isDirectory()) 39 if (stat.isDirectory())
40 { 40 {
41 if (recurse) 41 if (recurse)
42 { 42 {
43 addTestPaths(fs.readdirSync(testPath).map( 43 addTestPaths(fs.readdirSync(testPath).map(
44 file => path.join(testPath, file))); 44 file => path.join(testPath, file)));
45 } 45 }
46 continue; 46 continue;
47 } 47 }
48 if (path.basename(testPath).startsWith("_")) 48 if (path.basename(testPath).startsWith("_"))
49 continue; 49 continue;
50 if (path.extname(testPath) == ".js") 50 if (path.extname(testPath) == ".js")
51 { 51 {
52 if (testPath.split(path.sep).includes("browser")) 52 if (testPath.split(path.sep).includes("browser"))
53 { 53 browserFiles.push(testPath);
54 browserFiles.push(
55 path.relative(path.join(__dirname, "test", "browser"),
56 testPath).replace(/\.js$/, "")
57 );
Wladimir Palant 2017/08/17 13:06:10 I'd prefer to have this "path to module name" conv
kzar 2017/08/17 13:26:42 Done.
58 }
59 else 54 else
60 unitFiles.push(testPath); 55 unitFiles.push(testPath);
61 } 56 }
62 } 57 }
63 } 58 }
64 59
65 function webpackInMemory(bundleFilename, options) 60 function webpackInMemory(bundleFilename, options)
Wladimir Palant 2017/08/17 13:06:10 Do we need the bundleFilename parameter? It seems
kzar 2017/08/17 13:26:42 Well it just means that when things go wrong the e
66 { 61 {
67 return new Promise((resolve, reject) => 62 return new Promise((resolve, reject) =>
68 { 63 {
69 // Based on this example https://webpack.js.org/api/node/#custom-file-system s 64 // Based on this example
Wladimir Palant 2017/08/17 13:06:10 Nit: overlong line, move URL to next line?
kzar 2017/08/17 13:26:42 Done.
65 // https://webpack.js.org/api/node/#custom-file-systems
70 let memoryFS = new MemoryFS(); 66 let memoryFS = new MemoryFS();
71 67
72 options.output = {filename: bundleFilename, path: "/"}; 68 options.output = {filename: bundleFilename, path: "/"};
73 let webpackCompiler = webpack(options); 69 let webpackCompiler = webpack(options);
74 webpackCompiler.outputFileSystem = memoryFS; 70 webpackCompiler.outputFileSystem = memoryFS;
75 71
76 webpackCompiler.run((err, stats) => 72 webpackCompiler.run((err, stats) =>
77 { 73 {
78 // Error handling is based on this example 74 // Error handling is based on this example
79 // https://webpack.js.org/api/node/#error-handling 75 // https://webpack.js.org/api/node/#error-handling
(...skipping 23 matching lines...) Expand all
103 99
104 let nodeunitPath = path.join(__dirname, "node_modules", "nodeunit", 100 let nodeunitPath = path.join(__dirname, "node_modules", "nodeunit",
105 "examples", "browser", "nodeunit.js"); 101 "examples", "browser", "nodeunit.js");
106 let bundleFilename = "bundle.js"; 102 let bundleFilename = "bundle.js";
107 103
108 return webpackInMemory(bundleFilename, { 104 return webpackInMemory(bundleFilename, {
109 entry: path.join(__dirname, "test", "browser", "_bootstrap.js"), 105 entry: path.join(__dirname, "test", "browser", "_bootstrap.js"),
110 module: { 106 module: {
111 rules: [{ 107 rules: [{
112 resource: nodeunitPath, 108 resource: nodeunitPath,
113 // I would have rathered used exports-loader here, to avoid treating 109 // I would have rather used exports-loader here, to avoid treating
Wladimir Palant 2017/08/17 13:06:10 "rathered" => "rather"?
kzar 2017/08/17 13:26:42 Done.
114 // nodeunit as a global. Unfortunately the nodeunit browser example 110 // nodeunit as a global. Unfortunately the nodeunit browser example
115 // script is quite slopily put together, if exports isn't falsey it 111 // script is quite slopily put together, if exports isn't falsey it
116 // breaks! As a workaround we need to use script-loader, which means 112 // breaks! As a workaround we need to use script-loader, which means
117 // that exports is falsey for that script as a side-effect. 113 // that exports is falsey for that script as a side-effect.
118 use: ["script-loader"] 114 use: ["script-loader"]
119 }] 115 }]
120 }, 116 },
121 resolve: { 117 resolve: {
122 alias: { 118 alias: {
123 nodeunit$: nodeunitPath 119 nodeunit$: nodeunitPath
124 }, 120 },
125 modules: [path.resolve(__dirname, "lib")] 121 modules: [path.resolve(__dirname, "lib")]
Wladimir Palant 2017/08/17 13:06:10 We don't need this any more, do we?
kzar 2017/08/17 13:26:43 I think we do, otherwise we'll look for common.js
126 } 122 }
127 }).then(bundle => 123 }).then(bundle =>
128 { 124 {
129 return chromiumProcess(bundle, bundleFilename, browserFiles); 125 return chromiumProcess(
126 bundle, bundleFilename,
127 browserFiles.map(
128 file => path.relative(path.join(__dirname, "test", "browser"),
129 file).replace(/\.js$/, "")
130 )
131 );
130 }); 132 });
131 } 133 }
132 134
133 if (process.argv.length > 2) 135 if (process.argv.length > 2)
134 addTestPaths(process.argv.slice(2), true); 136 addTestPaths(process.argv.slice(2), true);
135 else 137 else
136 { 138 {
137 addTestPaths( 139 addTestPaths(
138 [path.join(__dirname, "test"), path.join(__dirname, "test", "browser")], 140 [path.join(__dirname, "test"), path.join(__dirname, "test", "browser")],
139 true 141 true
140 ); 142 );
141 } 143 }
142 144
143 runBrowserTests().catch(error => 145 Promise.resolve(runBrowserTests()).catch(error =>
144 { 146 {
145 console.error("Failed running browser tests"); 147 console.error("Failed running browser tests");
146 console.error(error); 148 console.error(error);
147 }).then(() => 149 }).then(() =>
148 { 150 {
149 if (unitFiles.length) 151 if (unitFiles.length)
150 nodeunit.reporters.default.run(unitFiles); 152 nodeunit.reporters.default.run(unitFiles);
151 }); 153 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld