| 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     let 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     }; | 
|  | 63 | 
|  | 64     return loadNext(); | 
| 86   } | 65   } | 
| 87 | 66 | 
| 88   function runTests(modules, callback) | 67   function runTests(modules) | 
| 89   { | 68   { | 
| 90     function bold(str) | 69     function bold(str) | 
| 91     { | 70     { | 
| 92       return "\u001B[1m" + str + "\u001B[22m"; | 71       return "\u001B[1m" + str + "\u001B[22m"; | 
| 93     } | 72     } | 
| 94 | 73 | 
| 95     function ok(str) | 74     function ok(str) | 
| 96     { | 75     { | 
| 97       return "\u001B[32m" + str + "\u001B[39m"; | 76       return "\u001B[32m" + str + "\u001B[39m"; | 
| 98     } | 77     } | 
| 99 | 78 | 
| 100     function error(str) | 79     function error(str) | 
| 101     { | 80     { | 
| 102       return "\u001B[31m" + str + "\u001B[39m"; | 81       return "\u001B[31m" + str + "\u001B[39m"; | 
| 103     } | 82     } | 
| 104 | 83 | 
| 105     nodeunit.runModules(modules, { | 84     return new Promise((resolve, reject) => | 
| 106       moduleStart: function(name) | 85     { | 
| 107       { | 86       nodeunit.runModules(modules, { | 
| 108         console.log(bold(name)); | 87         moduleStart(name) | 
| 109       }, |  | 
| 110       testDone: function(name, assertions) |  | 
| 111       { |  | 
| 112         var errors = assertions.filter(function(assertion) |  | 
| 113         { | 88         { | 
| 114           return assertion.failed(); | 89           console.log(bold(name)); | 
| 115         }).map(function(assertion) | 90         }, | 
|  | 91         testDone(name, assertions) | 
| 116         { | 92         { | 
| 117           return assertion.error; | 93           let errors = assertions.filter(assertion => | 
| 118         }); | 94           { | 
|  | 95             return assertion.failed(); | 
|  | 96           }).map(assertion => | 
|  | 97           { | 
|  | 98             return assertion.error; | 
|  | 99           }); | 
| 119 | 100 | 
| 120         if (errors.length == 0) | 101           if (errors.length == 0) | 
| 121           console.log("\u2714 " + name); | 102             console.log("\u2714 " + name); | 
| 122         else | 103           else | 
|  | 104           { | 
|  | 105             console.log(error("\u2716 " + name) + "\n"); | 
|  | 106             errors.forEach(e => | 
|  | 107             { | 
|  | 108               if (e.stack) | 
|  | 109                 console.log(e.stack); | 
|  | 110               else | 
|  | 111                 console.log(String(e)); | 
|  | 112               console.log(""); | 
|  | 113             }); | 
|  | 114           } | 
|  | 115         }, | 
|  | 116         done(assertions) | 
| 123         { | 117         { | 
| 124           console.log(error("\u2716 " + name) + "\n"); | 118           let failures = assertions.filter(assertion => | 
| 125           errors.forEach(function(error) |  | 
| 126           { | 119           { | 
| 127             console.log(String(error)); | 120             return assertion.failed(); | 
| 128             if (error.stack) |  | 
| 129               console.log(error.stack); |  | 
| 130             console.log(""); |  | 
| 131           }); | 121           }); | 
|  | 122           if (failures.length) | 
|  | 123           { | 
|  | 124             console.log( | 
|  | 125               "\n" + | 
|  | 126               bold(error("FAILURES: ")) + | 
|  | 127               failures.length + "/" + assertions.length + " assertions failed" | 
|  | 128             ); | 
|  | 129           } | 
|  | 130           else | 
|  | 131           { | 
|  | 132             console.log( | 
|  | 133               "\n" + bold(ok("OK: ")) + | 
|  | 134               assertions.length + " assertions" | 
|  | 135             ); | 
|  | 136           } | 
|  | 137 | 
|  | 138           resolve(); | 
| 132         } | 139         } | 
| 133       }, | 140       }); | 
| 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     }); | 141     }); | 
| 159   } | 142   } | 
| 160 | 143 | 
| 161   function run() | 144   return loadScript(document, nodeunitUrl).then(() => | 
| 162   { | 145   { | 
| 163     var system = require("system"); | 146     return loadModules(moduleUrls); | 
| 164     var nodeunitUrl = system.args[1]; | 147   }).then(modules => | 
| 165     var urls = system.args.slice(2); | 148   { | 
| 166 | 149     return runTests(modules); | 
| 167     loadScript(document, nodeunitUrl, safeCall(function() | 150   }); | 
| 168     { | 151 }); | 
| 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 | 
|---|