OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 library command_runner_test; |
| 6 |
| 7 import 'package:args/args.dart'; |
| 8 import 'package:args/command_runner.dart'; |
| 9 import 'package:unittest/unittest.dart'; |
| 10 |
| 11 import 'utils.dart'; |
| 12 |
| 13 const _DEFAULT_USAGE = """ |
| 14 Usage: test <command> [arguments] |
| 15 |
| 16 Global options: |
| 17 -h, --help Print this usage information. |
| 18 |
| 19 Available commands: |
| 20 help Display help information for test. |
| 21 |
| 22 Run "test help <command>" for more information about a command."""; |
| 23 |
| 24 void main() { |
| 25 var runner; |
| 26 setUp(() { |
| 27 runner = new CommandRunner("test", "A test command runner."); |
| 28 }); |
| 29 |
| 30 test(".invocation has a sane default", () { |
| 31 expect(runner.invocation, |
| 32 equals("test <command> [arguments]")); |
| 33 }); |
| 34 |
| 35 group(".usage", () { |
| 36 test("returns the usage string", () { |
| 37 expect(runner.usage, equals(""" |
| 38 A test command runner. |
| 39 |
| 40 $_DEFAULT_USAGE""")); |
| 41 }); |
| 42 |
| 43 test("contains custom commands", () { |
| 44 runner.addCommand(new FooCommand()); |
| 45 |
| 46 expect(runner.usage, equals(""" |
| 47 A test command runner. |
| 48 |
| 49 Usage: test <command> [arguments] |
| 50 |
| 51 Global options: |
| 52 -h, --help Print this usage information. |
| 53 |
| 54 Available commands: |
| 55 foo Set a value. |
| 56 help Display help information for test. |
| 57 |
| 58 Run "test help <command>" for more information about a command.""")); |
| 59 }); |
| 60 |
| 61 test("contains custom options", () { |
| 62 runner.argParser.addFlag("foo", help: "Do something."); |
| 63 |
| 64 expect(runner.usage, equals(""" |
| 65 A test command runner. |
| 66 |
| 67 Usage: test <command> [arguments] |
| 68 |
| 69 Global options: |
| 70 -h, --help Print this usage information. |
| 71 --[no-]foo Do something. |
| 72 |
| 73 Available commands: |
| 74 help Display help information for test. |
| 75 |
| 76 Run "test help <command>" for more information about a command.""")); |
| 77 }); |
| 78 |
| 79 test("doesn't print hidden commands", () { |
| 80 runner.addCommand(new HiddenCommand()); |
| 81 |
| 82 expect(runner.usage, equals(""" |
| 83 A test command runner. |
| 84 |
| 85 $_DEFAULT_USAGE""")); |
| 86 }); |
| 87 |
| 88 test("doesn't print aliases", () { |
| 89 runner.addCommand(new AliasedCommand()); |
| 90 |
| 91 expect(runner.usage, equals(""" |
| 92 A test command runner. |
| 93 |
| 94 Usage: test <command> [arguments] |
| 95 |
| 96 Global options: |
| 97 -h, --help Print this usage information. |
| 98 |
| 99 Available commands: |
| 100 aliased Set a value. |
| 101 help Display help information for test. |
| 102 |
| 103 Run "test help <command>" for more information about a command.""")); |
| 104 }); |
| 105 }); |
| 106 |
| 107 test("usageException splits up the message and usage", () { |
| 108 expect(() => runner.usageException("message"), |
| 109 throwsUsageError("message", _DEFAULT_USAGE)); |
| 110 }); |
| 111 |
| 112 group("run()", () { |
| 113 test("runs a command", () { |
| 114 var command = new FooCommand(); |
| 115 runner.addCommand(command); |
| 116 |
| 117 expect(runner.run(["foo"]).then((_) { |
| 118 expect(command.hasRun, isTrue); |
| 119 }), completes); |
| 120 }); |
| 121 |
| 122 test("runs an asynchronous command", () { |
| 123 var command = new AsyncCommand(); |
| 124 runner.addCommand(command); |
| 125 |
| 126 expect(runner.run(["async"]).then((_) { |
| 127 expect(command.hasRun, isTrue); |
| 128 }), completes); |
| 129 }); |
| 130 |
| 131 test("runs a hidden comand", () { |
| 132 var command = new HiddenCommand(); |
| 133 runner.addCommand(command); |
| 134 |
| 135 expect(runner.run(["hidden"]).then((_) { |
| 136 expect(command.hasRun, isTrue); |
| 137 }), completes); |
| 138 }); |
| 139 |
| 140 test("runs an aliased comand", () { |
| 141 var command = new AliasedCommand(); |
| 142 runner.addCommand(command); |
| 143 |
| 144 expect(runner.run(["als"]).then((_) { |
| 145 expect(command.hasRun, isTrue); |
| 146 }), completes); |
| 147 }); |
| 148 |
| 149 test("runs a subcommand", () { |
| 150 var command = new AsyncCommand(); |
| 151 runner.addCommand(new FooCommand()..addSubcommand(command)); |
| 152 |
| 153 expect(runner.run(["foo", "async"]).then((_) { |
| 154 expect(command.hasRun, isTrue); |
| 155 }), completes); |
| 156 }); |
| 157 |
| 158 group("with --help", () { |
| 159 test("with no command prints the usage", () { |
| 160 expect(() => runner.run(["--help"]), prints(""" |
| 161 A test command runner. |
| 162 |
| 163 $_DEFAULT_USAGE |
| 164 """)); |
| 165 }); |
| 166 |
| 167 test("with a command prints the usage for that command", () { |
| 168 var command = new FooCommand(); |
| 169 runner.addCommand(command); |
| 170 |
| 171 expect(() => runner.run(["foo", "--help"]), prints(""" |
| 172 Set a value. |
| 173 |
| 174 Usage: test foo [arguments] |
| 175 -h, --help Print this usage information. |
| 176 |
| 177 Run "test help" to see global options. |
| 178 """)); |
| 179 }); |
| 180 }); |
| 181 }); |
| 182 |
| 183 group("with a footer", () { |
| 184 setUp(() { |
| 185 runner = new CommandRunnerWithFooter("test", "A test command runner."); |
| 186 }); |
| 187 |
| 188 test("includes the footer in the usage string", () { |
| 189 expect(runner.usage, equals(""" |
| 190 A test command runner. |
| 191 |
| 192 $_DEFAULT_USAGE |
| 193 Also, footer!""")); |
| 194 }); |
| 195 |
| 196 test("includes the footer in usage errors", () { |
| 197 expect(runner.run(["--bad"]), |
| 198 throwsUsageError('Could not find an option named "bad".', |
| 199 "$_DEFAULT_USAGE\nAlso, footer!")); |
| 200 }); |
| 201 }); |
| 202 |
| 203 group("throws a useful error when", () { |
| 204 test("arg parsing fails", () { |
| 205 expect(runner.run(["--bad"]), |
| 206 throwsUsageError('Could not find an option named "bad".', |
| 207 _DEFAULT_USAGE)); |
| 208 }); |
| 209 |
| 210 test("a top-level command doesn't exist", () { |
| 211 expect(runner.run(["bad"]), |
| 212 throwsUsageError('Could not find a command named "bad".', |
| 213 _DEFAULT_USAGE)); |
| 214 }); |
| 215 |
| 216 test("a subcommand doesn't exist", () { |
| 217 runner.addCommand( |
| 218 new FooCommand()..addSubcommand(new AsyncCommand())); |
| 219 |
| 220 expect(runner.run(["foo bad"]), |
| 221 throwsUsageError('Could not find a command named "foo bad".', """ |
| 222 Usage: test <command> [arguments] |
| 223 |
| 224 Global options: |
| 225 -h, --help Print this usage information. |
| 226 |
| 227 Available commands: |
| 228 foo Set a value. |
| 229 help Display help information for test. |
| 230 |
| 231 Run "test help <command>" for more information about a command.""")); |
| 232 }); |
| 233 |
| 234 test("a subcommand wasn't passed", () { |
| 235 runner.addCommand( |
| 236 new FooCommand()..addSubcommand(new AsyncCommand())); |
| 237 |
| 238 expect(runner.run(["foo"]), |
| 239 throwsUsageError('Missing subcommand for "test foo".', """ |
| 240 Usage: test foo <subcommand> [arguments] |
| 241 -h, --help Print this usage information. |
| 242 |
| 243 Available subcommands: |
| 244 async Set a value asynchronously. |
| 245 |
| 246 Run "test help" to see global options.""")); |
| 247 }); |
| 248 |
| 249 test("a command that doesn't take arguments was given them", () { |
| 250 runner.addCommand(new FooCommand()); |
| 251 |
| 252 expect(runner.run(["foo", "bar"]), |
| 253 throwsUsageError('Command "foo" does not take any arguments.', """ |
| 254 Usage: test foo [arguments] |
| 255 -h, --help Print this usage information. |
| 256 |
| 257 Run "test help" to see global options.""")); |
| 258 }); |
| 259 }); |
| 260 } |
OLD | NEW |