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