 Issue 29373596:
  Issue 4838 - Use nodeunit framework for integration tests running in browser  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore
    
  
    Issue 29373596:
  Issue 4838 - Use nodeunit framework for integration tests running in browser  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore| Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | |
| 
Felix Dahlke
2017/01/30 14:37:58
It's 2017 :)
 
Wladimir Palant
2017/02/24 09:18:55
Yes, but the copyright header hasn't been updated
 
Felix Dahlke
2017/02/24 10:43:38
:)
 | |
| 4 * | |
| 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 | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 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/>. | |
| 16 */ | |
| 17 | |
| 18 "use strict"; | |
| 19 | |
| 20 (function() | |
| 21 { | |
| 22 // TODO: This should use promises once PhantomJS supports them, this will make | |
| 
Felix Dahlke
2017/01/30 14:37:58
Could you reference issue 4796 here? In the other
 
Wladimir Palant
2017/02/24 09:18:55
Done.
 | |
| 23 // wrapping all async calls in order to catch errors unnecessary. | |
| 24 function safeCall(callback) | |
| 
Felix Dahlke
2017/01/30 14:37:58
Have you considered using phantom.onError instead?
 
Wladimir Palant
2017/02/24 09:18:55
I was using that originally, it wasn't catching al
 
Felix Dahlke
2017/02/24 10:43:38
Acknowledged.
 | |
| 25 { | |
| 26 return function() | |
| 27 { | |
| 28 try | |
| 29 { | |
| 30 callback.apply(this, arguments); | |
| 31 } | |
| 32 catch (e) | |
| 33 { | |
| 34 var message = String(e); | |
| 35 if (e.stack) | |
| 36 message += "\n" + e.stack; | |
| 37 console.log(message); | |
| 
Felix Dahlke
2017/01/30 14:37:58
Since it's an error message, how about console.err
 
Wladimir Palant
2017/02/24 09:18:55
nodeunit prints all output via console.log(), incl
 
Felix Dahlke
2017/02/24 10:43:39
Acknowledged.
 | |
| 38 phantom.exit(); | |
| 
Felix Dahlke
2017/01/30 14:37:58
phantom.exit(1) in this case?
 
Wladimir Palant
2017/02/24 09:18:55
Done.
 | |
| 39 } | |
| 40 } | |
| 
Felix Dahlke
2017/01/30 14:37:58
Missing semicolon?
 
Wladimir Palant
2017/02/24 09:18:55
Done.
 | |
| 41 } | |
| 42 | |
| 43 function loadScript(doc, url, callback) | |
| 44 { | |
| 45 var script = doc.createElement("script"); | |
| 46 script.src = url; | |
| 47 script.async = false; | |
| 48 doc.head.appendChild(script); | |
| 49 if (callback) | |
| 50 window.setTimeout(callback, 0); | |
| 51 } | |
| 52 | |
| 53 function run() | |
| 
Felix Dahlke
2017/01/30 14:37:58
Nit: Move this below runTests() to have the functi
 
Wladimir Palant
2017/02/24 09:18:55
Done.
 | |
| 54 { | |
| 55 var system = require("system"); | |
| 56 var nodeunitUrl = system.args[1]; | |
| 57 var urls = system.args.slice(2); | |
| 58 | |
| 59 loadScript(document, nodeunitUrl, safeCall(function() | |
| 60 { | |
| 61 loadModules(urls, safeCall(function(modules) | |
| 62 { | |
| 63 runTests(modules, function() | |
| 64 { | |
| 65 phantom.exit(); | |
| 66 }); | |
| 67 })); | |
| 68 })); | |
| 69 } | |
| 70 | |
| 71 function loadModules(urls, callback) | |
| 72 { | |
| 73 var modules = {}; | |
| 74 | |
| 75 var loadNext = safeCall(function() | |
| 76 { | |
| 77 if (urls.length) | |
| 78 { | |
| 79 // Load each module into a new frame so that their scopes don't clash | |
| 80 var frame = document.createElement("iframe"); | |
| 81 document.body.appendChild(frame); | |
| 82 | |
| 83 var wnd = frame.contentWindow; | |
| 84 wnd.loadScript = loadScript.bind(null, wnd.document); | |
| 85 wnd.console = console; | |
| 86 wnd.require = require; | |
| 87 wnd.exports = {}; | |
| 88 wnd.module = {exports: wnd.exports}; | |
| 89 | |
| 90 var url = urls.shift(); | |
| 91 var name = url.split("/").pop(); | |
| 92 wnd.loadScript(url, safeCall(function() | |
| 93 { | |
| 94 modules[name] = nodeunit.testCase(wnd.module.exports); | |
| 95 loadNext(); | |
| 96 })); | |
| 97 } | |
| 98 else | |
| 99 callback(modules); | |
| 100 }); | |
| 101 | |
| 102 loadNext(); | |
| 103 } | |
| 104 | |
| 105 function runTests(modules, callback) | |
| 106 { | |
| 107 function bold(str) | |
| 108 { | |
| 109 return "\u001B[1m" + str + "\u001B[22m"; | |
| 110 } | |
| 111 | |
| 112 function ok(str) | |
| 113 { | |
| 114 return "\u001B[32m" + str + "\u001B[39m"; | |
| 115 } | |
| 116 | |
| 117 function error(str) | |
| 118 { | |
| 119 return "\u001B[31m" + str + "\u001B[39m"; | |
| 120 } | |
| 121 | |
| 122 nodeunit.runModules(modules, { | |
| 123 moduleStart: function(name) | |
| 124 { | |
| 125 console.log(bold(name)); | |
| 126 }, | |
| 127 testDone: function(name, assertions) | |
| 128 { | |
| 129 var errors = assertions.filter(function(assertion) | |
| 130 { | |
| 131 return assertion.failed(); | |
| 132 }).map(function(assertion) | |
| 133 { | |
| 134 return assertion.error; | |
| 135 }); | |
| 136 | |
| 137 if (errors.length == 0) | |
| 138 console.log("\u2714 " + name); | |
| 139 else | |
| 140 { | |
| 141 console.log(error("\u2716 " + name) + "\n"); | |
| 142 errors.forEach(function(error) | |
| 143 { | |
| 144 console.log(String(error)); | |
| 145 if (error.stack) | |
| 146 console.log(error.stack); | |
| 147 console.log(""); | |
| 148 }) | |
| 149 } | |
| 150 }, | |
| 151 done: function(assertions) | |
| 152 { | |
| 153 var failures = assertions.filter(function(assertion) | |
| 154 { | |
| 155 return assertion.failed(); | |
| 156 }); | |
| 157 if (failures.length) | |
| 158 { | |
| 159 console.log( | |
| 160 "\n" + | |
| 161 bold(error("FAILURES: ")) + | |
| 162 failures.length + "/" + assertions.length + " assertions failed" | |
| 163 ); | |
| 164 } | |
| 165 else | |
| 166 { | |
| 167 console.log( | |
| 168 "\n" + bold(ok("OK: ")) + | |
| 169 assertions.length + " assertions" | |
| 170 ); | |
| 171 } | |
| 172 | |
| 173 callback(); | |
| 174 } | |
| 175 }); | |
| 176 } | |
| 177 | |
| 178 safeCall(run)(); | |
| 179 })(); | |
| OLD | NEW |