| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 package com.google.dart.runner; | |
| 6 | |
| 7 import com.google.dart.compiler.CommandLineOptions; | |
| 8 import com.google.dart.compiler.CommandLineOptions.TestRunnerOptions; | |
| 9 import com.google.dart.compiler.DartCompiler; | |
| 10 import com.google.dart.compiler.LibrarySource; | |
| 11 import com.google.dart.compiler.UnitTestBatchRunner; | |
| 12 import com.google.dart.compiler.UnitTestBatchRunner.Invocation; | |
| 13 | |
| 14 import org.kohsuke.args4j.CmdLineException; | |
| 15 import org.kohsuke.args4j.CmdLineParser; | |
| 16 | |
| 17 import java.io.ByteArrayOutputStream; | |
| 18 import java.io.OutputStream; | |
| 19 import java.io.PrintStream; | |
| 20 import java.util.ArrayList; | |
| 21 import java.util.List; | |
| 22 | |
| 23 /** | |
| 24 * Runs dart programs.<br/> | |
| 25 * The command-line interface is similar to the VM's command line interface. | |
| 26 * </br> | |
| 27 */ | |
| 28 public class TestRunner { | |
| 29 | |
| 30 public static void main(String[] args) { | |
| 31 | |
| 32 try { | |
| 33 boolean runBatch = false; | |
| 34 TestRunnerOptions options = processCommandLineOptions(args); | |
| 35 if (options.shouldBatch()) { | |
| 36 runBatch = true; | |
| 37 if (args.length > 1) { | |
| 38 System.err.println("(Extra arguments specified with -batch ignored.)")
; | |
| 39 } | |
| 40 } | |
| 41 if (runBatch) { | |
| 42 UnitTestBatchRunner.runAsBatch(args, new Invocation() { | |
| 43 @Override | |
| 44 public boolean invoke(String[] args) throws Throwable { | |
| 45 try { | |
| 46 throwingMain(args, System.out, System.err); | |
| 47 } catch (RunnerError e) { | |
| 48 System.out.println(e.getLocalizedMessage()); | |
| 49 return false; | |
| 50 } | |
| 51 return true; | |
| 52 } | |
| 53 }); | |
| 54 } else { | |
| 55 throwingMain(args, System.out, System.err); | |
| 56 } | |
| 57 } catch (RunnerError e) { | |
| 58 System.err.println(e.getLocalizedMessage()); | |
| 59 System.exit(1); | |
| 60 } catch (Throwable e) { | |
| 61 e.printStackTrace(); | |
| 62 DartCompiler.crash(); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 private static void printUsageAndThrow(CmdLineParser cmdLineParser, String rea
son) throws RunnerError { | |
| 67 StringBuilder usage = new StringBuilder(); | |
| 68 usage.append(reason); | |
| 69 usage.append("\n"); | |
| 70 usage.append("Usage: "); | |
| 71 usage.append(System.getProperty("com.google.dart.runner.progname", | |
| 72 TestRunner.class.getSimpleName())); | |
| 73 usage.append(" [<options>] <dart-script-file> [<script-arguments>]\n"); | |
| 74 usage.append("\n"); | |
| 75 | |
| 76 OutputStream s = new ByteArrayOutputStream(); | |
| 77 if (cmdLineParser == null) { | |
| 78 cmdLineParser = new CmdLineParser(new TestRunnerOptions()); | |
| 79 } | |
| 80 cmdLineParser.printUsage(s); | |
| 81 usage.append(s); | |
| 82 throw new RunnerError(usage.toString()); | |
| 83 } | |
| 84 | |
| 85 private static TestRunnerOptions processCommandLineOptions(String[] args) thro
ws RunnerError { | |
| 86 CmdLineParser cmdLineParser = null; | |
| 87 TestRunnerOptions parsedOptions = null; | |
| 88 try { | |
| 89 parsedOptions = new TestRunnerOptions(); | |
| 90 cmdLineParser = CommandLineOptions.parse(args, parsedOptions); | |
| 91 if (args.length == 0 || parsedOptions.showHelp()) { | |
| 92 printUsageAndThrow(cmdLineParser, ""); | |
| 93 System.exit(1); | |
| 94 } | |
| 95 } catch (CmdLineException e) { | |
| 96 printUsageAndThrow(cmdLineParser, e.getLocalizedMessage()); | |
| 97 System.exit(1); | |
| 98 } | |
| 99 | |
| 100 assert parsedOptions != null; | |
| 101 return parsedOptions; | |
| 102 } | |
| 103 public static void throwingMain(String[] args, PrintStream stdout, PrintStream
stderr) | |
| 104 throws RunnerError { | |
| 105 TestRunnerOptions options = processCommandLineOptions(args); | |
| 106 List<LibrarySource> imports = new ArrayList<LibrarySource>(); | |
| 107 DartRunner.throwingMain(options, args, imports, stdout, stderr); | |
| 108 } | |
| 109 } | |
| OLD | NEW |