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