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 "use strict"; | 18 "use strict"; |
19 | 19 |
20 (function() | 20 /* globals nodeunit */ |
| 21 |
| 22 (function(nodeunitUrl, ...moduleUrls) |
21 { | 23 { |
22 // We are currently limited to ECMAScript 5 in this file, because it is being | 24 function loadScript(doc, url) |
23 // used in the browser tests. See https://issues.adblockplus.org/ticket/4796 | |
24 // Once this is resolved we should use promises here. | |
25 function safeCall(callback) | |
26 { | 25 { |
27 return function() | 26 return new Promise((resolve, reject) => |
28 { | 27 { |
29 try | 28 let script = doc.createElement("script"); |
30 { | 29 script.src = url; |
31 callback.apply(this, arguments); | 30 script.onload = resolve; |
32 } | 31 doc.head.appendChild(script); |
33 catch (e) | 32 }); |
34 { | |
35 var message = String(e); | |
36 if (e.stack) | |
37 message += "\n" + e.stack; | |
38 console.log(message); | |
39 phantom.exit(1); | |
40 } | |
41 }; | |
42 } | 33 } |
43 | 34 |
44 function loadScript(doc, url, callback) | 35 function loadModules(urls) |
45 { | 36 { |
46 var script = doc.createElement("script"); | 37 let modules = {}; |
47 script.src = url; | |
48 script.async = false; | |
49 doc.head.appendChild(script); | |
50 if (callback) | |
51 window.setTimeout(callback, 0); | |
52 } | |
53 | 38 |
54 function loadModules(urls, callback) | 39 return (function loadNext() |
55 { | |
56 var modules = {}; | |
57 | |
58 var loadNext = safeCall(function() | |
59 { | 40 { |
60 if (urls.length) | 41 if (urls.length) |
61 { | 42 { |
62 // Load each module into a new frame so that their scopes don't clash | 43 // Load each module into a new frame so that their scopes don't clash |
63 var frame = document.createElement("iframe"); | 44 let frame = document.createElement("iframe"); |
64 document.body.appendChild(frame); | 45 document.body.appendChild(frame); |
65 | 46 |
66 var wnd = frame.contentWindow; | 47 let wnd = frame.contentWindow; |
67 wnd.loadScript = loadScript.bind(null, wnd.document); | 48 wnd.loadScript = url => loadScript(wnd.document, url); |
68 wnd.console = console; | |
69 wnd.require = require; | |
70 wnd.exports = {}; | 49 wnd.exports = {}; |
71 wnd.module = {exports: wnd.exports}; | 50 wnd.module = {exports: wnd.exports}; |
72 | 51 |
73 var url = urls.shift(); | 52 let url = urls.shift(); |
74 var name = url.split("/").pop(); | 53 let name = url.split("/").pop(); |
75 wnd.loadScript(url, safeCall(function() | 54 return wnd.loadScript(url).then(() => |
76 { | 55 { |
77 modules[name] = nodeunit.testCase(wnd.module.exports); | 56 modules[name] = nodeunit.testCase(wnd.module.exports); |
78 loadNext(); | 57 return loadNext(); |
79 })); | 58 }); |
80 } | 59 } |
81 else | |
82 callback(modules); | |
83 }); | |
84 | 60 |
85 loadNext(); | 61 return Promise.resolve(modules); |
| 62 })(); |
86 } | 63 } |
87 | 64 |
88 function runTests(modules, callback) | 65 function runTests(modules) |
89 { | 66 { |
90 function bold(str) | 67 function bold(str) |
91 { | 68 { |
92 return "\u001B[1m" + str + "\u001B[22m"; | 69 return "\u001B[1m" + str + "\u001B[22m"; |
93 } | 70 } |
94 | 71 |
95 function ok(str) | 72 function ok(str) |
96 { | 73 { |
97 return "\u001B[32m" + str + "\u001B[39m"; | 74 return "\u001B[32m" + str + "\u001B[39m"; |
98 } | 75 } |
99 | 76 |
100 function error(str) | 77 function error(str) |
101 { | 78 { |
102 return "\u001B[31m" + str + "\u001B[39m"; | 79 return "\u001B[31m" + str + "\u001B[39m"; |
103 } | 80 } |
104 | 81 |
105 nodeunit.runModules(modules, { | 82 return new Promise((resolve, reject) => |
106 moduleStart: function(name) | 83 { |
107 { | 84 nodeunit.runModules(modules, { |
108 console.log(bold(name)); | 85 moduleStart(name) |
109 }, | |
110 testDone: function(name, assertions) | |
111 { | |
112 var errors = assertions.filter(function(assertion) | |
113 { | 86 { |
114 return assertion.failed(); | 87 console.log(bold(name)); |
115 }).map(function(assertion) | 88 }, |
| 89 testDone(name, assertions) |
116 { | 90 { |
117 return assertion.error; | 91 let errors = assertions.filter(assertion => assertion.failed()) |
118 }); | 92 .map(assertion => assertion.error); |
119 | 93 |
120 if (errors.length == 0) | 94 if (errors.length == 0) |
121 console.log("\u2714 " + name); | 95 console.log("\u2714 " + name); |
122 else | 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) |
123 { | 110 { |
124 console.log(error("\u2716 " + name) + "\n"); | 111 let failures = assertions.filter(assertion => assertion.failed()); |
125 errors.forEach(function(error) | 112 if (failures.length) |
126 { | 113 { |
127 console.log(String(error)); | 114 console.log( |
128 if (error.stack) | 115 "\n" + |
129 console.log(error.stack); | 116 bold(error("FAILURES: ")) + |
130 console.log(""); | 117 failures.length + "/" + assertions.length + " assertions failed" |
131 }); | 118 ); |
| 119 } |
| 120 else |
| 121 { |
| 122 console.log( |
| 123 "\n" + bold(ok("OK: ")) + |
| 124 assertions.length + " assertions" |
| 125 ); |
| 126 } |
| 127 |
| 128 resolve(); |
132 } | 129 } |
133 }, | 130 }); |
134 done: function(assertions) | |
135 { | |
136 var failures = assertions.filter(function(assertion) | |
137 { | |
138 return assertion.failed(); | |
139 }); | |
140 if (failures.length) | |
141 { | |
142 console.log( | |
143 "\n" + | |
144 bold(error("FAILURES: ")) + | |
145 failures.length + "/" + assertions.length + " assertions failed" | |
146 ); | |
147 } | |
148 else | |
149 { | |
150 console.log( | |
151 "\n" + bold(ok("OK: ")) + | |
152 assertions.length + " assertions" | |
153 ); | |
154 } | |
155 | |
156 callback(!failures.length); | |
157 } | |
158 }); | 131 }); |
159 } | 132 } |
160 | 133 |
161 function run() | 134 return loadScript(document, nodeunitUrl).then(() => |
162 { | 135 { |
163 var system = require("system"); | 136 return loadModules(moduleUrls); |
164 var nodeunitUrl = system.args[1]; | 137 }).then(modules => |
165 var urls = system.args.slice(2); | 138 { |
166 | 139 return runTests(modules); |
167 loadScript(document, nodeunitUrl, safeCall(function() | 140 }); |
168 { | 141 }); |
169 loadModules(urls, safeCall(function(modules) | |
170 { | |
171 runTests(modules, function(success) | |
172 { | |
173 phantom.exit(success ? 0 : 1); | |
174 }); | |
175 })); | |
176 })); | |
177 } | |
178 | |
179 safeCall(run)(); | |
180 })(); | |
OLD | NEW |