| 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 group("with help command", () { | |
| 183 test("with no command prints the usage", () { | |
| 184 expect(() => runner.run(["help"]), prints(""" | |
| 185 A test command runner. | |
| 186 | |
| 187 $_DEFAULT_USAGE | |
| 188 """)); | |
| 189 }); | |
| 190 | |
| 191 test("with a command prints the usage for that command", () { | |
| 192 var command = new FooCommand(); | |
| 193 runner.addCommand(command); | |
| 194 | |
| 195 expect(() => runner.run(["help", "foo"]), prints(""" | |
| 196 Set a value. | |
| 197 | |
| 198 Usage: test foo [arguments] | |
| 199 -h, --help Print this usage information. | |
| 200 | |
| 201 Run "test help" to see global options. | |
| 202 """)); | |
| 203 }); | |
| 204 | |
| 205 test("prints its own usage", () { | |
| 206 expect(() => runner.run(["help", "help"]), prints(""" | |
| 207 Display help information for test. | |
| 208 | |
| 209 Usage: test help [command] | |
| 210 -h, --help Print this usage information. | |
| 211 | |
| 212 Run "test help" to see global options. | |
| 213 """)); | |
| 214 }); | |
| 215 }); | |
| 216 }); | |
| 217 | |
| 218 group("with a footer", () { | |
| 219 setUp(() { | |
| 220 runner = new CommandRunnerWithFooter("test", "A test command runner."); | |
| 221 }); | |
| 222 | |
| 223 test("includes the footer in the usage string", () { | |
| 224 expect(runner.usage, equals(""" | |
| 225 A test command runner. | |
| 226 | |
| 227 $_DEFAULT_USAGE | |
| 228 Also, footer!""")); | |
| 229 }); | |
| 230 | |
| 231 test("includes the footer in usage errors", () { | |
| 232 expect(runner.run(["--bad"]), | |
| 233 throwsUsageError('Could not find an option named "bad".', | |
| 234 "$_DEFAULT_USAGE\nAlso, footer!")); | |
| 235 }); | |
| 236 }); | |
| 237 | |
| 238 group("throws a useful error when", () { | |
| 239 test("arg parsing fails", () { | |
| 240 expect(runner.run(["--bad"]), | |
| 241 throwsUsageError('Could not find an option named "bad".', | |
| 242 _DEFAULT_USAGE)); | |
| 243 }); | |
| 244 | |
| 245 test("a top-level command doesn't exist", () { | |
| 246 expect(runner.run(["bad"]), | |
| 247 throwsUsageError('Could not find a command named "bad".', | |
| 248 _DEFAULT_USAGE)); | |
| 249 }); | |
| 250 | |
| 251 test("a subcommand doesn't exist", () { | |
| 252 runner.addCommand( | |
| 253 new FooCommand()..addSubcommand(new AsyncCommand())); | |
| 254 | |
| 255 expect(runner.run(["foo bad"]), | |
| 256 throwsUsageError('Could not find a command named "foo bad".', """ | |
| 257 Usage: test <command> [arguments] | |
| 258 | |
| 259 Global options: | |
| 260 -h, --help Print this usage information. | |
| 261 | |
| 262 Available commands: | |
| 263 foo Set a value. | |
| 264 help Display help information for test. | |
| 265 | |
| 266 Run "test help <command>" for more information about a command.""")); | |
| 267 }); | |
| 268 | |
| 269 test("a subcommand wasn't passed", () { | |
| 270 runner.addCommand( | |
| 271 new FooCommand()..addSubcommand(new AsyncCommand())); | |
| 272 | |
| 273 expect(runner.run(["foo"]), | |
| 274 throwsUsageError('Missing subcommand for "test foo".', """ | |
| 275 Usage: test foo <subcommand> [arguments] | |
| 276 -h, --help Print this usage information. | |
| 277 | |
| 278 Available subcommands: | |
| 279 async Set a value asynchronously. | |
| 280 | |
| 281 Run "test help" to see global options.""")); | |
| 282 }); | |
| 283 | |
| 284 test("a command that doesn't take arguments was given them", () { | |
| 285 runner.addCommand(new FooCommand()); | |
| 286 | |
| 287 expect(runner.run(["foo", "bar"]), | |
| 288 throwsUsageError('Command "foo" does not take any arguments.', """ | |
| 289 Usage: test foo [arguments] | |
| 290 -h, --help Print this usage information. | |
| 291 | |
| 292 Run "test help" to see global options.""")); | |
| 293 }); | |
| 294 }); | |
| 295 } | |
| OLD | NEW |