| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.drivers; |
| 6 |
|
| 7 |
import java.io.*; |
| 8 |
import java.util.*; |
| 9 |
|
| 10 |
import org.w3c.dom.*; |
| 11 |
|
| 12 |
import org.mozilla.javascript.tools.shell.*; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public class JsDriver { |
| 18 |
private JsDriver() { |
| 19 |
} |
| 20 |
|
| 21 |
private static String join(String[] list) { |
| 22 |
String rv = ""; |
| 23 |
for (int i=0; i<list.length; i++) { |
| 24 |
rv += list[i]; |
| 25 |
if (i+1 != list.length) { |
| 26 |
rv += ","; |
| 27 |
} |
| 28 |
} |
| 29 |
return rv; |
| 30 |
} |
| 31 |
|
| 32 |
private static class Tests { |
| 33 |
private File testDirectory; |
| 34 |
private String[] list; |
| 35 |
private String[] skip; |
| 36 |
|
| 37 |
Tests(File testDirectory, String[] list, String[] skip) throws IOException { |
| 38 |
this.testDirectory = testDirectory; |
| 39 |
this.list = getTestList(list); |
| 40 |
this.skip = getTestList(skip); |
| 41 |
} |
| 42 |
|
| 43 |
private String[] getTestList(String[] tests) throws IOException { |
| 44 |
ArrayList<String> list = new ArrayList<String>(); |
| 45 |
for (int i=0; i < tests.length; i++) { |
| 46 |
if (tests[i].startsWith("@")) |
| 47 |
TestUtils.addTestsFromFile(tests[i].substring(1), list); |
| 48 |
else |
| 49 |
list.add(tests[i]); |
| 50 |
} |
| 51 |
return list.toArray(new String[0]); |
| 52 |
} |
| 53 |
|
| 54 |
private boolean matches(String path) { |
| 55 |
if (list.length == 0) return true; |
| 56 |
return TestUtils.matches(list, path); |
| 57 |
} |
| 58 |
|
| 59 |
private boolean excluded(String path) { |
| 60 |
if (skip.length == 0) return false; |
| 61 |
return TestUtils.matches(skip, path); |
| 62 |
} |
| 63 |
|
| 64 |
private void addFiles(List<Script> rv, String prefix, File directory) { |
| 65 |
File[] files = directory.listFiles(); |
| 66 |
if (files == null) throw new RuntimeException("files null for " + directory); |
| 67 |
for (int i=0; i<files.length; i++) { |
| 68 |
String path = prefix + files[i].getName(); |
| 69 |
if (ShellTest.DIRECTORY_FILTER.accept(files[i])) { |
| 70 |
addFiles(rv, path + "/", files[i]); |
| 71 |
} else { |
| 72 |
boolean isTopLevel = prefix.length() == 0; |
| 73 |
if (ShellTest.TEST_FILTER.accept(files[i]) && matches(path) && !excluded(path) && !isTopLevel) { |
| 74 |
rv.add(new Script(path, files[i])); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
static class Script { |
| 81 |
private String path; |
| 82 |
private File file; |
| 83 |
|
| 84 |
Script(String path, File file) { |
| 85 |
this.path = path; |
| 86 |
this.file = file; |
| 87 |
} |
| 88 |
|
| 89 |
String getPath() { |
| 90 |
return path; |
| 91 |
} |
| 92 |
|
| 93 |
File getFile() { |
| 94 |
return file; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
Script[] getFiles() { |
| 99 |
ArrayList<Script> rv = new ArrayList<Script>(); |
| 100 |
addFiles(rv, "", testDirectory); |
| 101 |
return rv.toArray(new Script[0]); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
private static class ConsoleStatus extends ShellTest.Status { |
| 106 |
private File jsFile; |
| 107 |
|
| 108 |
private Arguments.Console console; |
| 109 |
private boolean trace; |
| 110 |
|
| 111 |
private boolean failed; |
| 112 |
|
| 113 |
ConsoleStatus(Arguments.Console console, boolean trace) { |
| 114 |
this.console = console; |
| 115 |
this.trace = trace; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void running(File jsFile) { |
| 120 |
try { |
| 121 |
console.println("Running: " + jsFile.getCanonicalPath()); |
| 122 |
this.jsFile = jsFile; |
| 123 |
} catch (IOException e) { |
| 124 |
throw new RuntimeException(e); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public void failed(String s) { |
| 130 |
console.println("Failed: " + jsFile + ": " + s); |
| 131 |
failed = true; |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public void threw(Throwable t) { |
| 136 |
console.println("Failed: " + jsFile + " with exception."); |
| 137 |
console.println(ShellTest.getStackTrace(t)); |
| 138 |
failed = true; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void timedOut() { |
| 143 |
console.println("Failed: " + jsFile + ": timed out."); |
| 144 |
failed = true; |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public void exitCodesWere(int expected, int actual) { |
| 149 |
if (expected != actual) { |
| 150 |
console.println("Failed: " + jsFile + " expected " + expected + " actual " + actual); |
| 151 |
failed = true; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void outputWas(String s) { |
| 157 |
if (!failed) { |
| 158 |
console.println("Passed: " + jsFile); |
| 159 |
if (trace) { |
| 160 |
console.println(s); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
private static boolean setContent(Element node, String id, String content) { |
| 168 |
if (node.getAttribute("id").equals(id)) { |
| 169 |
node.setTextContent(node.getTextContent() + "\n" + content); |
| 170 |
return true; |
| 171 |
} else { |
| 172 |
NodeList children = node.getChildNodes(); |
| 173 |
for (int i=0; i<children.getLength(); i++) { |
| 174 |
if (children.item(i) instanceof Element) { |
| 175 |
Element e = (Element)children.item(i); |
| 176 |
boolean rv = setContent( e, id, content ); |
| 177 |
if (rv) { |
| 178 |
return true; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
private static Element getElementById(Element node, String id) { |
| 187 |
if (node.getAttribute("id").equals(id)) { |
| 188 |
return node; |
| 189 |
} else { |
| 190 |
NodeList children = node.getChildNodes(); |
| 191 |
for (int i=0; i<children.getLength(); i++) { |
| 192 |
if (children.item(i) instanceof Element) { |
| 193 |
Element rv = getElementById( (Element)children.item(i), id ); |
| 194 |
if (rv != null) { |
| 195 |
return rv; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
return null; |
| 201 |
} |
| 202 |
|
| 203 |
private static String newlineLineEndings(String s) { |
| 204 |
StringBuilder rv = new StringBuilder(); |
| 205 |
for (int i=0; i<s.length(); i++) { |
| 206 |
if (s.charAt(i) == '\r') { |
| 207 |
if (i+1<s.length() && s.charAt(i+1) == '\n') { |
| 208 |
|
| 209 |
} else { |
| 210 |
|
| 211 |
rv.append('\n'); |
| 212 |
} |
| 213 |
} else { |
| 214 |
rv.append(s.charAt(i)); |
| 215 |
} |
| 216 |
} |
| 217 |
return rv.toString(); |
| 218 |
} |
| 219 |
|
| 220 |
private static class HtmlStatus extends ShellTest.Status { |
| 221 |
private String testPath; |
| 222 |
private String bugUrl; |
| 223 |
private String lxrUrl; |
| 224 |
private Document html; |
| 225 |
private Element failureHtml; |
| 226 |
private boolean failed; |
| 227 |
|
| 228 |
private String output; |
| 229 |
|
| 230 |
HtmlStatus(String lxrUrl, String bugUrl, String testPath, Document html, Element failureHtml) { |
| 231 |
this.testPath = testPath; |
| 232 |
this.bugUrl = bugUrl; |
| 233 |
this.lxrUrl = lxrUrl; |
| 234 |
this.html = html; |
| 235 |
this.failureHtml = failureHtml; |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public void running(File file) { |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public void failed(String s) { |
| 244 |
failed = true; |
| 245 |
setContent(failureHtml, "failureDetails.reason", "Failure reason: \n" + s); |
| 246 |
} |
| 247 |
|
| 248 |
@Override |
| 249 |
public void exitCodesWere(int expected, int actual) { |
| 250 |
if (expected != actual) { |
| 251 |
failed = true; |
| 252 |
setContent(failureHtml, "failureDetails.reason", "expected exit code " + expected + " but got " + actual); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
@Override |
| 257 |
public void threw(Throwable e) { |
| 258 |
failed = true; |
| 259 |
setContent(failureHtml, "failureDetails.reason", "Threw Java exception:\n" + newlineLineEndings(ShellTest.getStackTrace(e))); |
| 260 |
} |
| 261 |
|
| 262 |
@Override |
| 263 |
public void timedOut() { |
| 264 |
failed = true; |
| 265 |
setContent(failureHtml, "failureDetails.reason", "Timed out."); |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public void outputWas(String s) { |
| 270 |
this.output = s; |
| 271 |
} |
| 272 |
|
| 273 |
private String getLinesStartingWith(String prefix) { |
| 274 |
BufferedReader r = new BufferedReader(new StringReader(output)); |
| 275 |
String line = null; |
| 276 |
String rv = ""; |
| 277 |
try { |
| 278 |
while( (line = r.readLine()) != null ) { |
| 279 |
if (line.startsWith(prefix)) { |
| 280 |
if (rv.length() > 0) { |
| 281 |
rv += "\n"; |
| 282 |
} |
| 283 |
rv += line; |
| 284 |
} |
| 285 |
} |
| 286 |
return rv; |
| 287 |
} catch (IOException e) { |
| 288 |
throw new RuntimeException("Can't happen."); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
boolean failed() { |
| 293 |
return failed; |
| 294 |
} |
| 295 |
|
| 296 |
void finish() { |
| 297 |
if (failed) { |
| 298 |
getElementById(failureHtml, "failureDetails.status").setTextContent(getLinesStartingWith("STATUS:")); |
| 299 |
|
| 300 |
String bn = getLinesStartingWith("BUGNUMBER:"); |
| 301 |
Element bnlink = getElementById(failureHtml, "failureDetails.bug.href"); |
| 302 |
if (bn.length() > 0) { |
| 303 |
String number = bn.substring("BUGNUMBER: ".length()); |
| 304 |
if (!number.equals("none")) { |
| 305 |
bnlink.setAttribute("href", bugUrl + number); |
| 306 |
getElementById(bnlink, "failureDetails.bug.number").setTextContent(number); |
| 307 |
} else { |
| 308 |
bnlink.getParentNode().removeChild(bnlink); |
| 309 |
} |
| 310 |
} else { |
| 311 |
bnlink.getParentNode().removeChild(bnlink); |
| 312 |
} |
| 313 |
|
| 314 |
getElementById(failureHtml, "failureDetails.lxr").setAttribute("href", lxrUrl + testPath); |
| 315 |
getElementById(failureHtml, "failureDetails.lxr.text").setTextContent(testPath); |
| 316 |
|
| 317 |
getElementById(html.getDocumentElement(), "retestList.text").setTextContent( |
| 318 |
getElementById(html.getDocumentElement(), "retestList.text").getTextContent() |
| 319 |
+ testPath |
| 320 |
+ "\n" |
| 321 |
); |
| 322 |
|
| 323 |
getElementById(html.getDocumentElement(), "failureDetails").appendChild(failureHtml); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
private static class XmlStatus extends ShellTest.Status { |
| 329 |
private Element target; |
| 330 |
private Date start; |
| 331 |
|
| 332 |
XmlStatus(String path, Element root) { |
| 333 |
this.target = root.getOwnerDocument().createElement("test"); |
| 334 |
this.target.setAttribute("path", path); |
| 335 |
root.appendChild(target); |
| 336 |
} |
| 337 |
|
| 338 |
@Override |
| 339 |
public void running(File file) { |
| 340 |
this.start = new Date(); |
| 341 |
} |
| 342 |
|
| 343 |
private Element createElement(Element parent, String name) { |
| 344 |
Element rv = parent.getOwnerDocument().createElement(name); |
| 345 |
parent.appendChild(rv); |
| 346 |
return rv; |
| 347 |
} |
| 348 |
|
| 349 |
private void finish() { |
| 350 |
Date end = new Date(); |
| 351 |
long elapsed = end.getTime() - start.getTime(); |
| 352 |
this.target.setAttribute("elapsed", String.valueOf(elapsed)); |
| 353 |
} |
| 354 |
|
| 355 |
private void setTextContent(Element e, String content) { |
| 356 |
e.setTextContent( newlineLineEndings(content) ); |
| 357 |
} |
| 358 |
|
| 359 |
@Override |
| 360 |
public void exitCodesWere(int expected, int actual) { |
| 361 |
finish(); |
| 362 |
Element exit = createElement(target, "exit"); |
| 363 |
exit.setAttribute("expected", String.valueOf(expected)); |
| 364 |
exit.setAttribute("actual", String.valueOf(actual)); |
| 365 |
} |
| 366 |
|
| 367 |
@Override |
| 368 |
public void timedOut() { |
| 369 |
finish(); |
| 370 |
createElement(target, "timedOut"); |
| 371 |
} |
| 372 |
|
| 373 |
@Override |
| 374 |
public void failed(String s) { |
| 375 |
finish(); |
| 376 |
Element failed = createElement(target, "failed"); |
| 377 |
setTextContent(failed, s); |
| 378 |
} |
| 379 |
|
| 380 |
@Override |
| 381 |
public void outputWas(String message) { |
| 382 |
finish(); |
| 383 |
Element output = createElement(target, "output"); |
| 384 |
setTextContent(output, message); |
| 385 |
} |
| 386 |
|
| 387 |
@Override |
| 388 |
public void threw(Throwable t) { |
| 389 |
finish(); |
| 390 |
Element threw = createElement(target, "threw"); |
| 391 |
setTextContent(threw, ShellTest.getStackTrace(t)); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
private static class Results { |
| 396 |
private ShellContextFactory factory; |
| 397 |
private Arguments arguments; |
| 398 |
private File output; |
| 399 |
private boolean trace; |
| 400 |
|
| 401 |
private Document html; |
| 402 |
private Element failureHtml; |
| 403 |
|
| 404 |
private Document xml; |
| 405 |
|
| 406 |
private Date start; |
| 407 |
private int tests; |
| 408 |
private int failures; |
| 409 |
|
| 410 |
Results(ShellContextFactory factory, Arguments arguments, boolean trace) { |
| 411 |
this.factory = factory; |
| 412 |
this.arguments = arguments; |
| 413 |
|
| 414 |
File output = arguments.getOutputFile(); |
| 415 |
if (output == null) { |
| 416 |
output = new File("rhino-test-results." + new java.text.SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()) + ".html"); |
| 417 |
} |
| 418 |
this.output = output; |
| 419 |
|
| 420 |
this.trace = trace; |
| 421 |
} |
| 422 |
|
| 423 |
private Document parse(InputStream in) { |
| 424 |
try { |
| 425 |
javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance(); |
| 426 |
factory.setValidating(false); |
| 427 |
javax.xml.parsers.DocumentBuilder dom = factory.newDocumentBuilder(); |
| 428 |
return dom.parse(in); |
| 429 |
} catch (Throwable t) { |
| 430 |
throw new RuntimeException("Parser failure", t); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
private Document getTemplate() { |
| 435 |
return parse(getClass().getResourceAsStream("results.html")); |
| 436 |
} |
| 437 |
|
| 438 |
private void write(Document template, boolean xml) { |
| 439 |
try { |
| 440 |
File output = this.output; |
| 441 |
javax.xml.transform.TransformerFactory factory = javax.xml.transform.TransformerFactory.newInstance(); |
| 442 |
javax.xml.transform.Transformer xform = factory.newTransformer(); |
| 443 |
if (xml) { |
| 444 |
xform.setOutputProperty(javax.xml.transform.OutputKeys.METHOD, "xml"); |
| 445 |
xform.setOutputProperty(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 446 |
output = new File(output.getCanonicalPath() + ".xml"); |
| 447 |
} |
| 448 |
xform.transform( |
| 449 |
new javax.xml.transform.dom.DOMSource(template), |
| 450 |
new javax.xml.transform.stream.StreamResult( new FileOutputStream(output) ) |
| 451 |
); |
| 452 |
} catch (IOException e) { |
| 453 |
arguments.getConsole().println("Could not write results file to " + output + ": "); |
| 454 |
e.printStackTrace(System.err); |
| 455 |
} catch (javax.xml.transform.TransformerConfigurationException e) { |
| 456 |
throw new RuntimeException("Parser failure", e); |
| 457 |
} catch (javax.xml.transform.TransformerException e) { |
| 458 |
throw new RuntimeException("Parser failure", e); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
void start() { |
| 463 |
this.html = getTemplate(); |
| 464 |
this.failureHtml = getElementById(html.getDocumentElement(), "failureDetails.prototype"); |
| 465 |
if (this.failureHtml == null) { |
| 466 |
try { |
| 467 |
javax.xml.transform.TransformerFactory.newInstance().newTransformer().transform( |
| 468 |
new javax.xml.transform.dom.DOMSource(html), |
| 469 |
new javax.xml.transform.stream.StreamResult(System.err) |
| 470 |
); |
| 471 |
} catch (Throwable t) { |
| 472 |
throw new RuntimeException(t); |
| 473 |
} |
| 474 |
throw new RuntimeException("No"); |
| 475 |
} |
| 476 |
this.failureHtml.getParentNode().removeChild(this.failureHtml); |
| 477 |
|
| 478 |
try { |
| 479 |
this.xml = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder() |
| 480 |
.getDOMImplementation().createDocument(null, "results", null) |
| 481 |
; |
| 482 |
xml.getDocumentElement().setAttribute("timestamp", String.valueOf(new Date().getTime())); |
| 483 |
xml.getDocumentElement().setAttribute("optimization", String.valueOf(arguments.getOptimizationLevel())); |
| 484 |
xml.getDocumentElement().setAttribute("strict", String.valueOf(arguments.isStrict())); |
| 485 |
xml.getDocumentElement().setAttribute("timeout", String.valueOf(arguments.getTimeout())); |
| 486 |
} catch (javax.xml.parsers.ParserConfigurationException e) { |
| 487 |
throw new RuntimeException(e); |
| 488 |
} |
| 489 |
|
| 490 |
this.start = new Date(); |
| 491 |
} |
| 492 |
|
| 493 |
void run(Tests.Script script, ShellTest.Parameters parameters) { |
| 494 |
String path = script.getPath(); |
| 495 |
File test = script.getFile(); |
| 496 |
ConsoleStatus cStatus = new ConsoleStatus(arguments.getConsole(), trace); |
| 497 |
HtmlStatus hStatus = new HtmlStatus(arguments.getLxrUrl(), arguments.getBugUrl(), path, html, (Element)failureHtml.cloneNode(true)); |
| 498 |
XmlStatus xStatus = new XmlStatus(path, this.xml.getDocumentElement()); |
| 499 |
ShellTest.Status status = ShellTest.Status.compose(new ShellTest.Status[] { cStatus, hStatus, xStatus }); |
| 500 |
try { |
| 501 |
ShellTest.run(factory, test, parameters, status); |
| 502 |
} catch (Exception e) { |
| 503 |
throw new RuntimeException(e); |
| 504 |
} |
| 505 |
tests++; |
| 506 |
if (hStatus.failed()) { |
| 507 |
failures++; |
| 508 |
} |
| 509 |
hStatus.finish(); |
| 510 |
} |
| 511 |
|
| 512 |
private void set(Document document, String id, String value) { |
| 513 |
getElementById(document.getDocumentElement(), id).setTextContent(value); |
| 514 |
} |
| 515 |
|
| 516 |
void finish() { |
| 517 |
Date end = new Date(); |
| 518 |
long elapsedMs = end.getTime() - start.getTime(); |
| 519 |
set(html, "results.testlist", join(arguments.getTestList())); |
| 520 |
set(html, "results.skiplist", join(arguments.getSkipList())); |
| 521 |
String pct = new java.text.DecimalFormat("##0.00").format( (double)failures / (double)tests * 100.0 ); |
| 522 |
set(html, "results.results", "Tests attempted: " + tests + " Failures: " + failures + " (" + pct + "%)"); |
| 523 |
set(html, "results.platform", "java.home=" + System.getProperty("java.home") |
| 524 |
+ "\n" + "java.version=" + System.getProperty("java.version") |
| 525 |
+ "\n" + "os.name=" + System.getProperty("os.name") |
| 526 |
); |
| 527 |
set(html, "results.classpath", System.getProperty("java.class.path").replace(File.pathSeparatorChar, ' ')); |
| 528 |
int elapsedSeconds = (int)(elapsedMs / 1000); |
| 529 |
int elapsedMinutes = elapsedSeconds / 60; |
| 530 |
elapsedSeconds = elapsedSeconds % 60; |
| 531 |
String elapsed = "" + elapsedMinutes + " minutes, " + elapsedSeconds + " seconds"; |
| 532 |
set(html, "results.elapsed", elapsed); |
| 533 |
set(html, "results.time", new java.text.SimpleDateFormat("MMMM d yyyy h:mm:ss aa").format(new java.util.Date())); |
| 534 |
write(html, false); |
| 535 |
write(xml, true); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
private static class ShellTestParameters extends ShellTest.Parameters { |
| 540 |
private int timeout; |
| 541 |
|
| 542 |
ShellTestParameters(int timeout) { |
| 543 |
this.timeout = timeout; |
| 544 |
} |
| 545 |
|
| 546 |
@Override |
| 547 |
public int getTimeoutMilliseconds() { |
| 548 |
return timeout; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
void run(Arguments arguments) throws Throwable { |
| 553 |
if (arguments.help()) { |
| 554 |
System.out.println("See mozilla/js/tests/README-jsDriver.html; note that some options are not supported."); |
| 555 |
System.out.println("Consult the Java source code at testsrc/org/mozilla/javascript/JsDriver.java for details."); |
| 556 |
System.exit(0); |
| 557 |
} |
| 558 |
|
| 559 |
ShellContextFactory factory = new ShellContextFactory(); |
| 560 |
factory.setOptimizationLevel(arguments.getOptimizationLevel()); |
| 561 |
factory.setStrictMode(arguments.isStrict()); |
| 562 |
|
| 563 |
File path = arguments.getTestsPath(); |
| 564 |
if (path == null) { |
| 565 |
path = new File("../tests"); |
| 566 |
} |
| 567 |
if (!path.exists()) { |
| 568 |
throw new RuntimeException("JavaScript tests not found at " + path.getCanonicalPath()); |
| 569 |
} |
| 570 |
Tests tests = new Tests(path, arguments.getTestList(), arguments.getSkipList()); |
| 571 |
Tests.Script[] all = tests.getFiles(); |
| 572 |
arguments.getConsole().println("Running " + all.length + " tests."); |
| 573 |
|
| 574 |
Results results = new Results(factory, arguments, arguments.trace()); |
| 575 |
|
| 576 |
results.start(); |
| 577 |
for (int i=0; i<all.length; i++) { |
| 578 |
results.run(all[i], new ShellTestParameters(arguments.getTimeout())); |
| 579 |
} |
| 580 |
results.finish(); |
| 581 |
} |
| 582 |
|
| 583 |
public static void main(Arguments arguments) throws Throwable { |
| 584 |
JsDriver driver = new JsDriver(); |
| 585 |
driver.run(arguments); |
| 586 |
} |
| 587 |
|
| 588 |
private static class Arguments { |
| 589 |
private ArrayList<Option> options = new ArrayList<Option>(); |
| 590 |
|
| 591 |
private Option bugUrl = new Option("b", "bugurl", false, false, "http://bugzilla.mozilla.org/show_bug.cgi?id="); |
| 592 |
private Option optimizationLevel = new Option("o", "optimization", false, false, "-1"); |
| 593 |
private Option strict = new Option(null, "strict", false, true, null); |
| 594 |
private Option outputFile = new Option("f", "file", false, false, null); |
| 595 |
private Option help = new Option("h", "help", false, true, null); |
| 596 |
private Option logFailuresToConsole = new Option("k", "confail", false, true, null); |
| 597 |
private Option testList = new Option("l", "list", true, false, null); |
| 598 |
private Option skipList = new Option("L", "neglist", true, false, null); |
| 599 |
private Option testsPath = new Option("p", "testpath", false, false, null); |
| 600 |
private Option trace = new Option("t", "trace", false, true, null); |
| 601 |
private Option lxrUrl = new Option("u", "lxrurl", false, false, "http://lxr.mozilla.org/mozilla/source/js/tests/"); |
| 602 |
private Option timeout = new Option(null, "timeout", false, false, "60000"); |
| 603 |
|
| 604 |
public static class Console { |
| 605 |
public void print(String message) { |
| 606 |
System.out.print(message); |
| 607 |
} |
| 608 |
public void println(String message) { |
| 609 |
System.out.println(message); |
| 610 |
} |
| 611 |
} |
| 612 |
private Console console = new Console(); |
| 613 |
|
| 614 |
private class Option { |
| 615 |
private String letterOption; |
| 616 |
private String wordOption; |
| 617 |
private boolean array; |
| 618 |
private boolean flag; |
| 619 |
private boolean ignored; |
| 620 |
|
| 621 |
private ArrayList<String> values = new ArrayList<String>(); |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
Option(String letterOption, String wordOption, boolean array, |
| 626 |
boolean flag, String unspecified) |
| 627 |
{ |
| 628 |
this.letterOption = letterOption; |
| 629 |
this.wordOption = wordOption; |
| 630 |
this.flag = flag; |
| 631 |
this.array = array; |
| 632 |
if (!flag && !array) { |
| 633 |
this.values.add(unspecified); |
| 634 |
} |
| 635 |
options.add(this); |
| 636 |
} |
| 637 |
|
| 638 |
Option ignored() { |
| 639 |
this.ignored = true; |
| 640 |
return this; |
| 641 |
} |
| 642 |
|
| 643 |
int getInt() { |
| 644 |
return Integer.parseInt( getValue() ); |
| 645 |
} |
| 646 |
|
| 647 |
String getValue() { |
| 648 |
return values.get(0); |
| 649 |
} |
| 650 |
|
| 651 |
boolean getSwitch() { |
| 652 |
return values.size() > 0; |
| 653 |
} |
| 654 |
|
| 655 |
File getFile() { |
| 656 |
if (getValue() == null) return null; |
| 657 |
return new File(getValue()); |
| 658 |
} |
| 659 |
|
| 660 |
String[] getValues() { |
| 661 |
return values.toArray(new String[0]); |
| 662 |
} |
| 663 |
|
| 664 |
void process(List<String> arguments) { |
| 665 |
String option = arguments.get(0); |
| 666 |
String dashLetter = (letterOption == null) ? (String)null : "-" + letterOption; |
| 667 |
if (option.equals(dashLetter) || option.equals("--" + wordOption)) { |
| 668 |
arguments.remove(0); |
| 669 |
if (flag) { |
| 670 |
values.add(0, (String)null ); |
| 671 |
} else if (array) { |
| 672 |
while (arguments.size() > 0 && |
| 673 |
!arguments.get(0).startsWith("-")) |
| 674 |
{ |
| 675 |
values.add(arguments.remove(0)); |
| 676 |
} |
| 677 |
} else { |
| 678 |
values.set(0, arguments.remove(0)); |
| 679 |
} |
| 680 |
if (ignored) { |
| 681 |
System.err.println("WARNING: " + option + " is ignored in the Java version of the test driver."); |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
public String getBugUrl() { |
| 689 |
return bugUrl.getValue(); |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
public int getOptimizationLevel() { |
| 700 |
return optimizationLevel.getInt(); |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
public boolean isStrict() { |
| 705 |
return strict.getSwitch(); |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
public File getOutputFile() { |
| 710 |
return outputFile.getFile(); |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
public boolean help() { |
| 715 |
return help.getSwitch(); |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
public boolean logFailuresToConsole() { |
| 725 |
return logFailuresToConsole.getSwitch(); |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
public String[] getTestList() { |
| 730 |
return testList.getValues(); |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
public String[] getSkipList() { |
| 735 |
return skipList.getValues(); |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
public File getTestsPath() { |
| 740 |
return testsPath.getFile(); |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
public boolean trace() { |
| 748 |
return trace.getSwitch(); |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
public String getLxrUrl() { |
| 753 |
return lxrUrl.getValue(); |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
public int getTimeout() { |
| 763 |
return timeout.getInt(); |
| 764 |
} |
| 765 |
|
| 766 |
public Console getConsole() { |
| 767 |
return console; |
| 768 |
} |
| 769 |
|
| 770 |
void process(List<String> arguments) { |
| 771 |
while(arguments.size() > 0) { |
| 772 |
String option = arguments.get(0); |
| 773 |
if (option.startsWith("--")) { |
| 774 |
|
| 775 |
if (option.indexOf("=") != -1) { |
| 776 |
arguments.set(0, option.substring(option.indexOf("="))); |
| 777 |
arguments.add(1, option.substring(option.indexOf("=") + 1)); |
| 778 |
} |
| 779 |
} else if (option.startsWith("-")) { |
| 780 |
|
| 781 |
if (option.length() > 2) { |
| 782 |
for (int i=2; i<option.length(); i++) { |
| 783 |
arguments.add(1, "-" + option.substring(i,i+1)); |
| 784 |
} |
| 785 |
arguments.set(0, option.substring(0,2)); |
| 786 |
} |
| 787 |
} |
| 788 |
int lengthBefore = arguments.size(); |
| 789 |
for (int i=0; i<options.size(); i++) { |
| 790 |
if (arguments.size() > 0) { |
| 791 |
options.get(i).process(arguments); |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
if (arguments.size() == lengthBefore) { |
| 796 |
System.err.println("WARNING: ignoring unrecognized option " + arguments.remove(0)); |
| 797 |
} |
| 798 |
} |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
public static void main(String[] args) throws Throwable { |
| 803 |
ArrayList<String> arguments = new ArrayList<String>(); |
| 804 |
arguments.addAll(Arrays.asList(args)); |
| 805 |
Arguments clArguments = new Arguments(); |
| 806 |
clArguments.process(arguments); |
| 807 |
main(clArguments); |
| 808 |
} |
| 809 |
} |