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; | 5 library command_runner_test; |
6 | 6 |
7 import 'package:args/args.dart'; | |
8 import 'package:args/command_runner.dart'; | 7 import 'package:args/command_runner.dart'; |
9 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
10 | 9 |
11 import 'utils.dart'; | 10 import 'utils.dart'; |
12 | 11 |
13 const _DEFAULT_USAGE = """ | 12 const _DEFAULT_USAGE = """ |
14 Usage: test <command> [arguments] | 13 Usage: test <command> [arguments] |
15 | 14 |
16 Global options: | 15 Global options: |
17 -h, --help Print this usage information. | 16 -h, --help Print this usage information. |
18 | 17 |
19 Available commands: | 18 Available commands: |
20 help Display help information for test. | 19 help Display help information for test. |
21 | 20 |
22 Run "test help <command>" for more information about a command."""; | 21 Run "test help <command>" for more information about a command."""; |
23 | 22 |
24 void main() { | 23 void main() { |
25 var runner; | 24 var runner; |
26 setUp(() { | 25 setUp(() { |
27 runner = new CommandRunner("test", "A test command runner."); | 26 runner = new CommandRunner("test", "A test command runner."); |
28 }); | 27 }); |
29 | 28 |
30 test(".invocation has a sane default", () { | 29 test(".invocation has a sane default", () { |
31 expect(runner.invocation, | 30 expect(runner.invocation, equals("test <command> [arguments]")); |
32 equals("test <command> [arguments]")); | |
33 }); | 31 }); |
34 | 32 |
35 group(".usage", () { | 33 group(".usage", () { |
36 test("returns the usage string", () { | 34 test("returns the usage string", () { |
37 expect(runner.usage, equals(""" | 35 expect(runner.usage, equals(""" |
38 A test command runner. | 36 A test command runner. |
39 | 37 |
40 $_DEFAULT_USAGE""")); | 38 $_DEFAULT_USAGE""")); |
41 }); | 39 }); |
42 | 40 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 runner.addCommand(command); | 191 runner.addCommand(command); |
194 | 192 |
195 expect(() => runner.run(["help", "foo"]), prints(""" | 193 expect(() => runner.run(["help", "foo"]), prints(""" |
196 Set a value. | 194 Set a value. |
197 | 195 |
198 Usage: test foo [arguments] | 196 Usage: test foo [arguments] |
199 -h, --help Print this usage information. | 197 -h, --help Print this usage information. |
200 | 198 |
201 Run "test help" to see global options. | 199 Run "test help" to see global options. |
202 """)); | 200 """)); |
203 }); | 201 }); |
204 | 202 |
205 test("prints its own usage", () { | 203 test("prints its own usage", () { |
206 expect(() => runner.run(["help", "help"]), prints(""" | 204 expect(() => runner.run(["help", "help"]), prints(""" |
207 Display help information for test. | 205 Display help information for test. |
208 | 206 |
209 Usage: test help [command] | 207 Usage: test help [command] |
210 -h, --help Print this usage information. | 208 -h, --help Print this usage information. |
211 | 209 |
212 Run "test help" to see global options. | 210 Run "test help" to see global options. |
213 """)); | 211 """)); |
214 }); | 212 }); |
215 }); | 213 }); |
216 }); | 214 }); |
217 | 215 |
218 group("with a footer", () { | 216 group("with a footer", () { |
219 setUp(() { | 217 setUp(() { |
220 runner = new CommandRunnerWithFooter("test", "A test command runner."); | 218 runner = new CommandRunnerWithFooter("test", "A test command runner."); |
221 }); | 219 }); |
222 | 220 |
223 test("includes the footer in the usage string", () { | 221 test("includes the footer in the usage string", () { |
224 expect(runner.usage, equals(""" | 222 expect(runner.usage, equals(""" |
225 A test command runner. | 223 A test command runner. |
226 | 224 |
227 $_DEFAULT_USAGE | 225 $_DEFAULT_USAGE |
228 Also, footer!""")); | 226 Also, footer!""")); |
229 }); | 227 }); |
230 | 228 |
231 test("includes the footer in usage errors", () { | 229 test("includes the footer in usage errors", () { |
232 expect(runner.run(["--bad"]), | 230 expect(runner.run(["--bad"]), throwsUsageError( |
233 throwsUsageError('Could not find an option named "bad".', | 231 'Could not find an option named "bad".', |
234 "$_DEFAULT_USAGE\nAlso, footer!")); | 232 "$_DEFAULT_USAGE\nAlso, footer!")); |
235 }); | 233 }); |
236 }); | 234 }); |
237 | 235 |
238 group("throws a useful error when", () { | 236 group("throws a useful error when", () { |
239 test("arg parsing fails", () { | 237 test("arg parsing fails", () { |
240 expect(runner.run(["--bad"]), | 238 expect(runner.run(["--bad"]), throwsUsageError( |
241 throwsUsageError('Could not find an option named "bad".', | 239 'Could not find an option named "bad".', _DEFAULT_USAGE)); |
242 _DEFAULT_USAGE)); | |
243 }); | 240 }); |
244 | 241 |
245 test("a top-level command doesn't exist", () { | 242 test("a top-level command doesn't exist", () { |
246 expect(runner.run(["bad"]), | 243 expect(runner.run(["bad"]), throwsUsageError( |
247 throwsUsageError('Could not find a command named "bad".', | 244 'Could not find a command named "bad".', _DEFAULT_USAGE)); |
248 _DEFAULT_USAGE)); | |
249 }); | 245 }); |
250 | 246 |
251 test("a subcommand doesn't exist", () { | 247 test("a subcommand doesn't exist", () { |
252 runner.addCommand( | 248 runner.addCommand(new FooCommand()..addSubcommand(new AsyncCommand())); |
253 new FooCommand()..addSubcommand(new AsyncCommand())); | |
254 | 249 |
255 expect(runner.run(["foo bad"]), | 250 expect(runner.run(["foo bad"]), throwsUsageError( |
256 throwsUsageError('Could not find a command named "foo bad".', """ | 251 'Could not find a command named "foo bad".', """ |
257 Usage: test <command> [arguments] | 252 Usage: test <command> [arguments] |
258 | 253 |
259 Global options: | 254 Global options: |
260 -h, --help Print this usage information. | 255 -h, --help Print this usage information. |
261 | 256 |
262 Available commands: | 257 Available commands: |
263 foo Set a value. | 258 foo Set a value. |
264 help Display help information for test. | 259 help Display help information for test. |
265 | 260 |
266 Run "test help <command>" for more information about a command.""")); | 261 Run "test help <command>" for more information about a command.""")); |
267 }); | 262 }); |
268 | 263 |
269 test("a subcommand wasn't passed", () { | 264 test("a subcommand wasn't passed", () { |
270 runner.addCommand( | 265 runner.addCommand(new FooCommand()..addSubcommand(new AsyncCommand())); |
271 new FooCommand()..addSubcommand(new AsyncCommand())); | |
272 | 266 |
273 expect(runner.run(["foo"]), | 267 expect(runner.run(["foo"]), throwsUsageError( |
274 throwsUsageError('Missing subcommand for "test foo".', """ | 268 'Missing subcommand for "test foo".', """ |
275 Usage: test foo <subcommand> [arguments] | 269 Usage: test foo <subcommand> [arguments] |
276 -h, --help Print this usage information. | 270 -h, --help Print this usage information. |
277 | 271 |
278 Available subcommands: | 272 Available subcommands: |
279 async Set a value asynchronously. | 273 async Set a value asynchronously. |
280 | 274 |
281 Run "test help" to see global options.""")); | 275 Run "test help" to see global options.""")); |
282 }); | 276 }); |
283 | 277 |
284 test("a command that doesn't take arguments was given them", () { | 278 test("a command that doesn't take arguments was given them", () { |
285 runner.addCommand(new FooCommand()); | 279 runner.addCommand(new FooCommand()); |
286 | 280 |
287 expect(runner.run(["foo", "bar"]), | 281 expect(runner.run(["foo", "bar"]), throwsUsageError( |
288 throwsUsageError('Command "foo" does not take any arguments.', """ | 282 'Command "foo" does not take any arguments.', """ |
289 Usage: test foo [arguments] | 283 Usage: test foo [arguments] |
290 -h, --help Print this usage information. | 284 -h, --help Print this usage information. |
291 | 285 |
292 Run "test help" to see global options.""")); | 286 Run "test help" to see global options.""")); |
293 }); | 287 }); |
294 }); | 288 }); |
295 } | 289 } |
OLD | NEW |