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

Delta Between Two Patch Sets: browsertests.js

Issue 29373596: Issue 4838 - Use nodeunit framework for integration tests running in browser (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Left Patch Set: Simplified phantomjs script Created Jan. 24, 2017, 8:08 p.m.
Right Patch Set: Addressed comments and added additional minor changes Created Feb. 24, 2017, 9:17 a.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 | « README.md ('k') | package.json » ('j') | 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-2016 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 (function() 20 (function()
21 { 21 {
22 // TODO: This should use promises once PhantomJS supports them, this will make 22 // We are currently limited to ECMAScript 5 in this file, because it is being
23 // wrapping all async calls in order to catch errors unnecessary. 23 // used in the browser tests. See https://issues.adblockplus.org/ticket/4796
24 // Once this is resolved we should use promises here.
24 function safeCall(callback) 25 function safeCall(callback)
25 { 26 {
26 return function() 27 return function()
27 { 28 {
28 try 29 try
29 { 30 {
30 callback.apply(this, arguments); 31 callback.apply(this, arguments);
31 } 32 }
32 catch (e) 33 catch (e)
33 { 34 {
34 var message = String(e); 35 var message = String(e);
35 if (e.stack) 36 if (e.stack)
36 message += "\n" + e.stack; 37 message += "\n" + e.stack;
37 console.log(message); 38 console.log(message);
38 phantom.exit(); 39 phantom.exit(1);
39 } 40 }
40 } 41 };
41 } 42 }
42 43
43 function loadScript(url, callback) 44 function loadScript(doc, url, callback)
44 { 45 {
45 var script = document.createElement("script"); 46 var script = doc.createElement("script");
46 script.src = url; 47 script.src = url;
47 script.async = false; 48 script.async = false;
48 document.head.appendChild(script); 49 doc.head.appendChild(script);
49 if (callback) 50 if (callback)
50 window.setTimeout(callback, 0); 51 window.setTimeout(callback, 0);
51 }
52
53 function run()
54 {
55 window.loadScript = loadScript;
56
57 var system = require("system");
58 var nodeunitUrl = system.args[1];
59 var urls = system.args.slice(2);
60
61 loadScript(nodeunitUrl, safeCall(function()
62 {
63 loadModules(urls, safeCall(function(modules)
64 {
65 runTests(modules, function()
66 {
67 phantom.exit();
68 });
69 }));
70 }));
71 } 52 }
72 53
73 function loadModules(urls, callback) 54 function loadModules(urls, callback)
74 { 55 {
75 var modules = {}; 56 var modules = {};
76 57
77 var loadNext = safeCall(function() 58 var loadNext = safeCall(function()
78 { 59 {
79 if (urls.length) 60 if (urls.length)
80 { 61 {
81 window.exports = {}; 62 // Load each module into a new frame so that their scopes don't clash
82 window.module = {exports: window.exports}; 63 var frame = document.createElement("iframe");
64 document.body.appendChild(frame);
65
66 var wnd = frame.contentWindow;
67 wnd.loadScript = loadScript.bind(null, wnd.document);
68 wnd.console = console;
69 wnd.require = require;
70 wnd.exports = {};
71 wnd.module = {exports: wnd.exports};
83 72
84 var url = urls.shift(); 73 var url = urls.shift();
85 var name = url.split("/").pop(); 74 var name = url.split("/").pop();
86 loadScript(url, safeCall(function() 75 wnd.loadScript(url, safeCall(function()
87 { 76 {
88 modules[name] = nodeunit.testCase(window.module.exports); 77 modules[name] = nodeunit.testCase(wnd.module.exports);
89 delete window.exports;
90 delete window.module;
91 loadNext(); 78 loadNext();
92 })); 79 }));
93 } 80 }
94 else 81 else
95 callback(modules); 82 callback(modules);
96 }); 83 });
97 84
98 loadNext(); 85 loadNext();
99 } 86 }
100 87
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 console.log("\u2714 " + name); 121 console.log("\u2714 " + name);
135 else 122 else
136 { 123 {
137 console.log(error("\u2716 " + name) + "\n"); 124 console.log(error("\u2716 " + name) + "\n");
138 errors.forEach(function(error) 125 errors.forEach(function(error)
139 { 126 {
140 console.log(String(error)); 127 console.log(String(error));
141 if (error.stack) 128 if (error.stack)
142 console.log(error.stack); 129 console.log(error.stack);
143 console.log(""); 130 console.log("");
144 }) 131 });
145 } 132 }
146 }, 133 },
147 done: function(assertions) 134 done: function(assertions)
148 { 135 {
149 var failures = assertions.filter(function(assertion) 136 var failures = assertions.filter(function(assertion)
150 { 137 {
151 return assertion.failed(); 138 return assertion.failed();
152 }); 139 });
153 if (failures.length) 140 if (failures.length)
154 { 141 {
155 console.log( 142 console.log(
156 "\n" + 143 "\n" +
157 bold(error("FAILURES: ")) + 144 bold(error("FAILURES: ")) +
158 failures.length + "/" + assertions.length + " assertions failed" 145 failures.length + "/" + assertions.length + " assertions failed"
159 ); 146 );
160 } 147 }
161 else 148 else
162 { 149 {
163 console.log( 150 console.log(
164 "\n" + bold(ok("OK: ")) + 151 "\n" + bold(ok("OK: ")) +
165 assertions.length + " assertions" 152 assertions.length + " assertions"
166 ); 153 );
167 } 154 }
168 155
169 callback(); 156 callback(!failures.length);
170 } 157 }
171 }); 158 });
172 } 159 }
173 160
161 function run()
162 {
163 var system = require("system");
164 var nodeunitUrl = system.args[1];
165 var urls = system.args.slice(2);
166
167 loadScript(document, nodeunitUrl, safeCall(function()
168 {
169 loadModules(urls, safeCall(function(modules)
170 {
171 runTests(modules, function(success)
172 {
173 phantom.exit(success ? 0 : 1);
174 });
175 }));
176 }));
177 }
178
174 safeCall(run)(); 179 safeCall(run)();
175 })(); 180 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld