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

Side by Side Diff: test/browser/_bootstrap.js

Issue 29517687: Issue 5079, 5516 - Use webpack for browser tests, modules for content scripts (Closed)
Patch Set: Created Aug. 16, 2017, 3:40 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 "use strict"; 18 "use strict";
19 19
20 /* globals nodeunit */ 20 /* globals nodeunit */
21 require("nodeunit");
21 22
22 (function(nodeunitUrl, ...moduleUrls) 23 let browserTestModuleContext = require.context("./", true, /^[^_].*\.js$/);
24
25 function runTests(browserTestModules)
Wladimir Palant 2017/08/17 10:05:38 Nit: Frankly, I'm a bit annoyed by the frequent re
kzar 2017/08/17 12:40:22 Done.
23 { 26 {
24 function loadScript(doc, url) 27 function bold(str)
25 { 28 {
26 return new Promise((resolve, reject) => 29 return "\u001B[1m" + str + "\u001B[22m";
27 {
28 let script = doc.createElement("script");
29 script.src = url;
30 script.onload = resolve;
31 doc.head.appendChild(script);
32 });
33 } 30 }
34 31
35 function loadModules(urls) 32 function ok(str)
36 { 33 {
37 let modules = {}; 34 return "\u001B[32m" + str + "\u001B[39m";
38
39 return (function loadNext()
40 {
41 if (urls.length)
42 {
43 // Load each module into a new frame so that their scopes don't clash
44 let frame = document.createElement("iframe");
45 document.body.appendChild(frame);
46
47 let wnd = frame.contentWindow;
48 wnd.loadScript = url => loadScript(wnd.document, url);
49 wnd.exports = {};
50 wnd.module = {exports: wnd.exports};
51
52 let url = urls.shift();
53 let name = url.split("/").pop();
54 return wnd.loadScript(url).then(() =>
55 {
56 modules[name] = nodeunit.testCase(wnd.module.exports);
57 return loadNext();
58 });
59 }
60
61 return Promise.resolve(modules);
62 })();
63 } 35 }
64 36
65 function runTests(modules) 37 function error(str)
66 { 38 {
67 function bold(str) 39 return "\u001B[31m" + str + "\u001B[39m";
68 {
69 return "\u001B[1m" + str + "\u001B[22m";
70 }
71
72 function ok(str)
73 {
74 return "\u001B[32m" + str + "\u001B[39m";
75 }
76
77 function error(str)
78 {
79 return "\u001B[31m" + str + "\u001B[39m";
80 }
81
82 return new Promise((resolve, reject) =>
83 {
84 nodeunit.runModules(modules, {
85 moduleStart(name)
86 {
87 console.log(bold(name));
88 },
89 testDone(name, assertions)
90 {
91 let errors = assertions.filter(assertion => assertion.failed())
92 .map(assertion => assertion.error);
93
94 if (errors.length == 0)
95 console.log("\u2714 " + name);
96 else
97 {
98 console.log(error("\u2716 " + name) + "\n");
99 errors.forEach(e =>
100 {
101 if (e.stack)
102 console.log(e.stack);
103 else
104 console.log(String(e));
105 console.log("");
106 });
107 }
108 },
109 done(assertions)
110 {
111 let failures = assertions.filter(assertion => assertion.failed());
112 if (failures.length)
113 {
114 console.log(
115 "\n" +
116 bold(error("FAILURES: ")) +
117 failures.length + "/" + assertions.length + " assertions failed"
118 );
119 }
120 else
121 {
122 console.log(
123 "\n" + bold(ok("OK: ")) +
124 assertions.length + " assertions"
125 );
126 }
127
128 resolve();
129 }
130 });
131 });
132 } 40 }
133 41
134 return loadScript(document, nodeunitUrl).then(() => 42 if (browserTestModules.length == 0)
43 browserTestModules = browserTestModuleContext.keys();
Wladimir Palant 2017/08/17 10:05:38 Why do we need this? test_runner.js is where we ar
kzar 2017/08/17 12:40:22 Done.
44
45 // We don't know which browser test modules are required at the point we
46 // bundle everything together using webpack, so we bundle them all but
47 // only use the necessary ones here.
Wladimir Palant 2017/08/17 10:05:38 Why is this comment here? That's not where the bun
kzar 2017/08/17 12:40:22 Done.
48 let testModules = {};
Wladimir Palant 2017/08/17 10:05:38 Nit: tests should be unambiguous enough as a name
kzar 2017/08/17 12:40:21 Done.
49 for (let module of browserTestModules)
50 testModules[module] = nodeunit.testCase(browserTestModuleContext(module));
Wladimir Palant 2017/08/17 10:05:38 As mentioned in comment on chrome_process.js, requ
kzar 2017/08/17 12:40:22 Done.
51
52 return new Promise((resolve, reject) =>
135 { 53 {
136 return loadModules(moduleUrls); 54 nodeunit.runModules(testModules, {
137 }).then(modules => 55 moduleStart(name)
138 { 56 {
139 return runTests(modules); 57 console.log(bold(name));
58 },
59 testDone(name, assertions)
60 {
61 let errors = assertions.filter(assertion => assertion.failed())
62 .map(assertion => assertion.error);
63
64 if (errors.length == 0)
65 console.log("\u2714 " + name);
66 else
67 {
68 console.log(error("\u2716 " + name) + "\n");
69 errors.forEach(e =>
70 {
71 if (e.stack)
72 console.log(e.stack);
73 else
74 console.log(String(e));
75 console.log("");
76 });
77 }
78 },
79 done(assertions)
80 {
81 let failures = assertions.filter(assertion => assertion.failed());
82 if (failures.length)
83 {
84 console.log(
85 "\n" +
86 bold(error("FAILURES: ")) +
87 failures.length + "/" + assertions.length + " assertions failed"
88 );
89 }
90 else
91 {
92 console.log(
93 "\n" + bold(ok("OK: ")) +
94 assertions.length + " assertions"
95 );
96 }
97
98 resolve();
99 }
100 });
140 }); 101 });
141 }); 102 }
103
104 module.exports = runTests;
OLDNEW

Powered by Google App Engine
This is Rietveld