| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library command_runner_test; | |
| 6 | |
| 7 import 'package:args/command_runner.dart'; | 5 import 'package:args/command_runner.dart'; |
| 8 import 'package:test/test.dart'; | 6 import 'package:test/test.dart'; |
| 9 | 7 |
| 10 import 'utils.dart'; | 8 import 'utils.dart'; |
| 11 | 9 |
| 12 const _DEFAULT_USAGE = """ | 10 const _DEFAULT_USAGE = """ |
| 13 Usage: test <command> [arguments] | 11 Usage: test <command> [arguments] |
| 14 | 12 |
| 15 Global options: | 13 Global options: |
| 16 -h, --help Print this usage information. | 14 -h, --help Print this usage information. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 Global options: | 47 Global options: |
| 50 -h, --help Print this usage information. | 48 -h, --help Print this usage information. |
| 51 | 49 |
| 52 Available commands: | 50 Available commands: |
| 53 foo Set a value. | 51 foo Set a value. |
| 54 help Display help information for test. | 52 help Display help information for test. |
| 55 | 53 |
| 56 Run "test help <command>" for more information about a command.""")); | 54 Run "test help <command>" for more information about a command.""")); |
| 57 }); | 55 }); |
| 58 | 56 |
| 57 test("truncates newlines in command descriptions by default", () { |
| 58 runner.addCommand(new MultilineCommand()); |
| 59 |
| 60 expect(runner.usage, equals(""" |
| 61 A test command runner. |
| 62 |
| 63 Usage: test <command> [arguments] |
| 64 |
| 65 Global options: |
| 66 -h, --help Print this usage information. |
| 67 |
| 68 Available commands: |
| 69 help Display help information for test. |
| 70 multiline Multi |
| 71 |
| 72 Run "test help <command>" for more information about a command.""")); |
| 73 }); |
| 74 |
| 75 test("supports newlines in command summaries", () { |
| 76 runner.addCommand(new MultilineSummaryCommand()); |
| 77 |
| 78 expect(runner.usage, equals(""" |
| 79 A test command runner. |
| 80 |
| 81 Usage: test <command> [arguments] |
| 82 |
| 83 Global options: |
| 84 -h, --help Print this usage information. |
| 85 |
| 86 Available commands: |
| 87 help Display help information for test. |
| 88 multiline Multi |
| 89 line. |
| 90 |
| 91 Run "test help <command>" for more information about a command.""")); |
| 92 }); |
| 93 |
| 59 test("contains custom options", () { | 94 test("contains custom options", () { |
| 60 runner.argParser.addFlag("foo", help: "Do something."); | 95 runner.argParser.addFlag("foo", help: "Do something."); |
| 61 | 96 |
| 62 expect(runner.usage, equals(""" | 97 expect(runner.usage, equals(""" |
| 63 A test command runner. | 98 A test command runner. |
| 64 | 99 |
| 65 Usage: test <command> [arguments] | 100 Usage: test <command> [arguments] |
| 66 | 101 |
| 67 Global options: | 102 Global options: |
| 68 -h, --help Print this usage information. | 103 -h, --help Print this usage information. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 97 Available commands: | 132 Available commands: |
| 98 aliased Set a value. | 133 aliased Set a value. |
| 99 help Display help information for test. | 134 help Display help information for test. |
| 100 | 135 |
| 101 Run "test help <command>" for more information about a command.""")); | 136 Run "test help <command>" for more information about a command.""")); |
| 102 }); | 137 }); |
| 103 }); | 138 }); |
| 104 | 139 |
| 105 test("usageException splits up the message and usage", () { | 140 test("usageException splits up the message and usage", () { |
| 106 expect(() => runner.usageException("message"), | 141 expect(() => runner.usageException("message"), |
| 107 throwsUsageError("message", _DEFAULT_USAGE)); | 142 throwsUsageException("message", _DEFAULT_USAGE)); |
| 108 }); | 143 }); |
| 109 | 144 |
| 110 group("run()", () { | 145 group("run()", () { |
| 111 test("runs a command", () { | 146 test("runs a command", () { |
| 112 var command = new FooCommand(); | 147 var command = new FooCommand(); |
| 113 runner.addCommand(command); | 148 runner.addCommand(command); |
| 114 | 149 |
| 115 expect(runner.run(["foo"]).then((_) { | 150 expect(runner.run(["foo"]).then((_) { |
| 116 expect(command.hasRun, isTrue); | 151 expect(command.hasRun, isTrue); |
| 117 }), completes); | 152 }), completes); |
| 118 }); | 153 }); |
| 119 | 154 |
| 120 test("runs an asynchronous command", () { | 155 test("runs an asynchronous command", () { |
| 121 var command = new AsyncCommand(); | 156 var command = new AsyncCommand(); |
| 122 runner.addCommand(command); | 157 runner.addCommand(command); |
| 123 | 158 |
| 124 expect(runner.run(["async"]).then((_) { | 159 expect(runner.run(["async"]).then((_) { |
| 125 expect(command.hasRun, isTrue); | 160 expect(command.hasRun, isTrue); |
| 126 }), completes); | 161 }), completes); |
| 127 }); | 162 }); |
| 128 | 163 |
| 164 test("runs a command with a return value", () { |
| 165 var runner = new CommandRunner<int>("test", ""); |
| 166 var command = new ValueCommand(); |
| 167 runner.addCommand(command); |
| 168 |
| 169 expect(runner.run(["foo"]), completion(equals(12))); |
| 170 }); |
| 171 |
| 172 test("runs a command with an asynchronous return value", () { |
| 173 var runner = new CommandRunner<String>("test", ""); |
| 174 var command = new AsyncValueCommand(); |
| 175 runner.addCommand(command); |
| 176 |
| 177 expect(runner.run(["foo"]), completion(equals("hi"))); |
| 178 }); |
| 179 |
| 129 test("runs a hidden comand", () { | 180 test("runs a hidden comand", () { |
| 130 var command = new HiddenCommand(); | 181 var command = new HiddenCommand(); |
| 131 runner.addCommand(command); | 182 runner.addCommand(command); |
| 132 | 183 |
| 133 expect(runner.run(["hidden"]).then((_) { | 184 expect(runner.run(["hidden"]).then((_) { |
| 134 expect(command.hasRun, isTrue); | 185 expect(command.hasRun, isTrue); |
| 135 }), completes); | 186 }), completes); |
| 136 }); | 187 }); |
| 137 | 188 |
| 138 test("runs an aliased comand", () { | 189 test("runs an aliased comand", () { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 155 | 206 |
| 156 group("with --help", () { | 207 group("with --help", () { |
| 157 test("with no command prints the usage", () { | 208 test("with no command prints the usage", () { |
| 158 expect(() => runner.run(["--help"]), prints(""" | 209 expect(() => runner.run(["--help"]), prints(""" |
| 159 A test command runner. | 210 A test command runner. |
| 160 | 211 |
| 161 $_DEFAULT_USAGE | 212 $_DEFAULT_USAGE |
| 162 """)); | 213 """)); |
| 163 }); | 214 }); |
| 164 | 215 |
| 165 test("with a command prints the usage for that command", () { | 216 test("with a preceding command prints the usage for that command", () { |
| 166 var command = new FooCommand(); | 217 var command = new FooCommand(); |
| 167 runner.addCommand(command); | 218 runner.addCommand(command); |
| 168 | 219 |
| 169 expect(() => runner.run(["foo", "--help"]), prints(""" | 220 expect(() => runner.run(["foo", "--help"]), prints(""" |
| 170 Set a value. | 221 Set a value. |
| 171 | 222 |
| 172 Usage: test foo [arguments] | 223 Usage: test foo [arguments] |
| 173 -h, --help Print this usage information. | 224 -h, --help Print this usage information. |
| 174 | 225 |
| 175 Run "test help" to see global options. | 226 Run "test help" to see global options. |
| 176 """)); | 227 """)); |
| 177 }); | 228 }); |
| 229 |
| 230 test("with a following command prints the usage for that command", () { |
| 231 var command = new FooCommand(); |
| 232 runner.addCommand(command); |
| 233 |
| 234 expect(() => runner.run(["--help", "foo"]), prints(""" |
| 235 Set a value. |
| 236 |
| 237 Usage: test foo [arguments] |
| 238 -h, --help Print this usage information. |
| 239 |
| 240 Run "test help" to see global options. |
| 241 """)); |
| 242 }); |
| 178 }); | 243 }); |
| 179 | 244 |
| 180 group("with help command", () { | 245 group("with help command", () { |
| 181 test("with no command prints the usage", () { | 246 test("with no command prints the usage", () { |
| 182 expect(() => runner.run(["help"]), prints(""" | 247 expect(() => runner.run(["help"]), prints(""" |
| 183 A test command runner. | 248 A test command runner. |
| 184 | 249 |
| 185 $_DEFAULT_USAGE | 250 $_DEFAULT_USAGE |
| 186 """)); | 251 """)); |
| 187 }); | 252 }); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 204 expect(() => runner.run(["help", "help"]), prints(""" | 269 expect(() => runner.run(["help", "help"]), prints(""" |
| 205 Display help information for test. | 270 Display help information for test. |
| 206 | 271 |
| 207 Usage: test help [command] | 272 Usage: test help [command] |
| 208 -h, --help Print this usage information. | 273 -h, --help Print this usage information. |
| 209 | 274 |
| 210 Run "test help" to see global options. | 275 Run "test help" to see global options. |
| 211 """)); | 276 """)); |
| 212 }); | 277 }); |
| 213 }); | 278 }); |
| 279 |
| 280 group("with an invalid argument", () { |
| 281 test("at the root throws the root usage", () { |
| 282 expect(runner.run(["--asdf"]), throwsUsageException( |
| 283 'Could not find an option named "asdf".', |
| 284 '$_DEFAULT_USAGE')); |
| 285 }); |
| 286 |
| 287 test("for a command throws the command usage", () { |
| 288 var command = new FooCommand(); |
| 289 runner.addCommand(command); |
| 290 |
| 291 expect(runner.run(["foo", "--asdf"]), throwsUsageException( |
| 292 'Could not find an option named "asdf".', |
| 293 """ |
| 294 Usage: test foo [arguments] |
| 295 -h, --help Print this usage information. |
| 296 |
| 297 Run "test help" to see global options.""")); |
| 298 }); |
| 299 }); |
| 214 }); | 300 }); |
| 215 | 301 |
| 216 group("with a footer", () { | 302 group("with a footer", () { |
| 217 setUp(() { | 303 setUp(() { |
| 218 runner = new CommandRunnerWithFooter("test", "A test command runner."); | 304 runner = new CommandRunnerWithFooter("test", "A test command runner."); |
| 219 }); | 305 }); |
| 220 | 306 |
| 221 test("includes the footer in the usage string", () { | 307 test("includes the footer in the usage string", () { |
| 222 expect(runner.usage, equals(""" | 308 expect(runner.usage, equals(""" |
| 223 A test command runner. | 309 A test command runner. |
| 224 | 310 |
| 225 $_DEFAULT_USAGE | 311 $_DEFAULT_USAGE |
| 226 Also, footer!""")); | 312 Also, footer!""")); |
| 227 }); | 313 }); |
| 228 | 314 |
| 229 test("includes the footer in usage errors", () { | 315 test("includes the footer in usage errors", () { |
| 230 expect(runner.run(["--bad"]), throwsUsageError( | 316 expect(runner.run(["--bad"]), throwsUsageException( |
| 231 'Could not find an option named "bad".', | 317 'Could not find an option named "bad".', |
| 232 "$_DEFAULT_USAGE\nAlso, footer!")); | 318 "$_DEFAULT_USAGE\nAlso, footer!")); |
| 233 }); | 319 }); |
| 234 }); | 320 }); |
| 235 | 321 |
| 236 group("throws a useful error when", () { | 322 group("throws a useful error when", () { |
| 237 test("arg parsing fails", () { | 323 test("arg parsing fails", () { |
| 238 expect(runner.run(["--bad"]), throwsUsageError( | 324 expect(runner.run(["--bad"]), throwsUsageException( |
| 239 'Could not find an option named "bad".', _DEFAULT_USAGE)); | 325 'Could not find an option named "bad".', _DEFAULT_USAGE)); |
| 240 }); | 326 }); |
| 241 | 327 |
| 242 test("a top-level command doesn't exist", () { | 328 test("a top-level command doesn't exist", () { |
| 243 expect(runner.run(["bad"]), throwsUsageError( | 329 expect(runner.run(["bad"]), throwsUsageException( |
| 244 'Could not find a command named "bad".', _DEFAULT_USAGE)); | 330 'Could not find a command named "bad".', _DEFAULT_USAGE)); |
| 245 }); | 331 }); |
| 246 | 332 |
| 247 test("a subcommand doesn't exist", () { | 333 test("a subcommand doesn't exist", () { |
| 248 runner.addCommand(new FooCommand()..addSubcommand(new AsyncCommand())); | 334 runner.addCommand(new FooCommand()..addSubcommand(new AsyncCommand())); |
| 249 | 335 |
| 250 expect(runner.run(["foo bad"]), throwsUsageError( | 336 expect(runner.run(["foo bad"]), throwsUsageException( |
| 251 'Could not find a command named "foo bad".', """ | 337 'Could not find a command named "foo bad".', """ |
| 252 Usage: test <command> [arguments] | 338 Usage: test <command> [arguments] |
| 253 | 339 |
| 254 Global options: | 340 Global options: |
| 255 -h, --help Print this usage information. | 341 -h, --help Print this usage information. |
| 256 | 342 |
| 257 Available commands: | 343 Available commands: |
| 258 foo Set a value. | 344 foo Set a value. |
| 259 help Display help information for test. | 345 help Display help information for test. |
| 260 | 346 |
| 261 Run "test help <command>" for more information about a command.""")); | 347 Run "test help <command>" for more information about a command.""")); |
| 262 }); | 348 }); |
| 263 | 349 |
| 264 test("a subcommand wasn't passed", () { | 350 test("a subcommand wasn't passed", () { |
| 265 runner.addCommand(new FooCommand()..addSubcommand(new AsyncCommand())); | 351 runner.addCommand(new FooCommand()..addSubcommand(new AsyncCommand())); |
| 266 | 352 |
| 267 expect(runner.run(["foo"]), throwsUsageError( | 353 expect(runner.run(["foo"]), throwsUsageException( |
| 268 'Missing subcommand for "test foo".', """ | 354 'Missing subcommand for "test foo".', """ |
| 269 Usage: test foo <subcommand> [arguments] | 355 Usage: test foo <subcommand> [arguments] |
| 270 -h, --help Print this usage information. | 356 -h, --help Print this usage information. |
| 271 | 357 |
| 272 Available subcommands: | 358 Available subcommands: |
| 273 async Set a value asynchronously. | 359 async Set a value asynchronously. |
| 274 | 360 |
| 275 Run "test help" to see global options.""")); | 361 Run "test help" to see global options.""")); |
| 276 }); | 362 }); |
| 277 | 363 |
| 278 test("a command that doesn't take arguments was given them", () { | 364 test("a command that doesn't take arguments was given them", () { |
| 279 runner.addCommand(new FooCommand()); | 365 runner.addCommand(new FooCommand()); |
| 280 | 366 |
| 281 expect(runner.run(["foo", "bar"]), throwsUsageError( | 367 expect(runner.run(["foo", "bar"]), throwsUsageException( |
| 282 'Command "foo" does not take any arguments.', """ | 368 'Command "foo" does not take any arguments.', """ |
| 283 Usage: test foo [arguments] | 369 Usage: test foo [arguments] |
| 284 -h, --help Print this usage information. | 370 -h, --help Print this usage information. |
| 285 | 371 |
| 286 Run "test help" to see global options.""")); | 372 Run "test help" to see global options.""")); |
| 287 }); | 373 }); |
| 288 }); | 374 }); |
| 289 } | 375 } |
| OLD | NEW |