| OLD | NEW |
| 1 // Copyright (c) 2012, 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_test; | 5 library command_test; |
| 6 | 6 |
| 7 import 'package:args/args.dart'; |
| 8 import 'package:args/command_runner.dart'; |
| 7 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
| 8 import 'package:args/args.dart'; | |
| 9 import 'utils.dart'; | 10 import 'utils.dart'; |
| 10 | 11 |
| 11 void main() { | 12 void main() { |
| 12 group('ArgParser.addCommand()', () { | 13 var foo; |
| 13 test('creates a new ArgParser if none is given', () { | 14 setUp(() { |
| 14 var parser = new ArgParser(); | 15 foo = new FooCommand(); |
| 15 var command = parser.addCommand('install'); | 16 |
| 16 expect(parser.commands['install'], equals(command)); | 17 // Make sure [Command.runner] is set up. |
| 17 expect(command is ArgParser, isTrue); | 18 new CommandRunner("test", "A test command runner.").addCommand(foo); |
| 19 }); |
| 20 |
| 21 group(".usageTemplate has a sane default", () { |
| 22 test("without subcommands", () { |
| 23 expect(foo.usageTemplate, |
| 24 equals("test foo <arguments>")); |
| 18 }); | 25 }); |
| 19 | 26 |
| 20 test('uses the command parser if given one', () { | 27 test("with subcommands", () { |
| 21 var parser = new ArgParser(); | 28 foo.addSubcommand(new AsyncCommand()); |
| 22 var command = new ArgParser(); | 29 expect(foo.usageTemplate, |
| 23 var result = parser.addCommand('install', command); | 30 equals("test foo [subcommand] <arguments>")); |
| 24 expect(parser.commands['install'], equals(command)); | |
| 25 expect(result, equals(command)); | |
| 26 }); | 31 }); |
| 27 | 32 |
| 28 test('throws on a duplicate command name', () { | 33 test("for a subcommand", () { |
| 29 var parser = new ArgParser(); | 34 var async = new AsyncCommand(); |
| 30 parser.addCommand('install'); | 35 foo.addSubcommand(async); |
| 31 throwsIllegalArg(() => parser.addCommand('install')); | 36 |
| 37 expect(async.usageTemplate, |
| 38 equals("test foo async <arguments>")); |
| 32 }); | 39 }); |
| 33 }); | 40 }); |
| 34 | 41 |
| 35 group('ArgParser.parse()', () { | 42 group(".usage", () { |
| 36 test('parses a command', () { | 43 test("returns the usage string", () { |
| 37 var parser = new ArgParser(); | 44 expect(foo.usage, equals(""" |
| 38 var command = parser.addCommand('install'); | 45 Set a value. |
| 39 | 46 |
| 40 var args = parser.parse(['install']); | 47 Usage: test foo <arguments> |
| 48 -h, --help Print this usage information. |
| 41 | 49 |
| 42 expect(args.command.name, equals('install')); | 50 Run "test help" to see global options.""")); |
| 43 expect(args.rest, isEmpty); | |
| 44 }); | 51 }); |
| 45 | 52 |
| 46 test('parses a command option', () { | 53 test("contains custom options", () { |
| 47 var parser = new ArgParser(); | 54 foo.argParser.addFlag("flag", help: "Do something."); |
| 48 var command = parser.addCommand('install'); | |
| 49 command.addOption('path'); | |
| 50 | 55 |
| 51 var args = parser.parse(['install', '--path', 'some/path']); | 56 expect(foo.usage, equals(""" |
| 52 expect(args.command['path'], equals('some/path')); | 57 Set a value. |
| 58 |
| 59 Usage: test foo <arguments> |
| 60 -h, --help Print this usage information. |
| 61 --[no-]flag Do something. |
| 62 |
| 63 Run "test help" to see global options.""")); |
| 53 }); | 64 }); |
| 54 | 65 |
| 55 test('parses a parent solo option before the command', () { | 66 test("doesn't print hidden subcommands", () { |
| 56 var parser = new ArgParser(); | 67 foo.addSubcommand(new AsyncCommand()); |
| 57 parser.addOption('mode', abbr: 'm'); | 68 foo.addSubcommand(new HiddenCommand()); |
| 58 var command = parser.addCommand('install'); | |
| 59 | 69 |
| 60 var args = parser.parse(['-m', 'debug', 'install']); | 70 expect(foo.usage, equals(""" |
| 61 expect(args['mode'], equals('debug')); | 71 Set a value. |
| 62 expect(args.command.name, equals('install')); | 72 |
| 73 Usage: test foo [subcommand] <arguments> |
| 74 -h, --help Print this usage information. |
| 75 |
| 76 Available subcommands: |
| 77 async Set a value asynchronously. |
| 78 |
| 79 Run "test help" to see global options.""")); |
| 63 }); | 80 }); |
| 64 | 81 |
| 65 test('parses a parent solo option after the command', () { | 82 test("doesn't print subcommand aliases", () { |
| 66 var parser = new ArgParser(); | 83 foo.addSubcommand(new AliasedCommand()); |
| 67 parser.addOption('mode', abbr: 'm'); | |
| 68 var command = parser.addCommand('install'); | |
| 69 | 84 |
| 70 var args = parser.parse(['install', '-m', 'debug']); | 85 expect(foo.usage, equals(""" |
| 71 expect(args['mode'], equals('debug')); | 86 Set a value. |
| 72 expect(args.command.name, equals('install')); | |
| 73 }); | |
| 74 | 87 |
| 75 test('parses a parent option before the command', () { | 88 Usage: test foo [subcommand] <arguments> |
| 76 var parser = new ArgParser(); | 89 -h, --help Print this usage information. |
| 77 parser.addFlag('verbose'); | |
| 78 var command = parser.addCommand('install'); | |
| 79 | 90 |
| 80 var args = parser.parse(['--verbose', 'install']); | 91 Available subcommands: |
| 81 expect(args['verbose'], isTrue); | 92 aliased Set a value. |
| 82 expect(args.command.name, equals('install')); | |
| 83 }); | |
| 84 | 93 |
| 85 test('parses a parent option after the command', () { | 94 Run "test help" to see global options.""")); |
| 86 var parser = new ArgParser(); | |
| 87 parser.addFlag('verbose'); | |
| 88 var command = parser.addCommand('install'); | |
| 89 | |
| 90 var args = parser.parse(['install', '--verbose']); | |
| 91 expect(args['verbose'], isTrue); | |
| 92 expect(args.command.name, equals('install')); | |
| 93 }); | |
| 94 | |
| 95 test('parses a parent negated option before the command', () { | |
| 96 var parser = new ArgParser(); | |
| 97 parser.addFlag('verbose', defaultsTo: true); | |
| 98 var command = parser.addCommand('install'); | |
| 99 | |
| 100 var args = parser.parse(['--no-verbose', 'install']); | |
| 101 expect(args['verbose'], isFalse); | |
| 102 expect(args.command.name, equals('install')); | |
| 103 }); | |
| 104 | |
| 105 test('parses a parent negated option after the command', () { | |
| 106 var parser = new ArgParser(); | |
| 107 parser.addFlag('verbose', defaultsTo: true); | |
| 108 var command = parser.addCommand('install'); | |
| 109 | |
| 110 var args = parser.parse(['install', '--no-verbose']); | |
| 111 expect(args['verbose'], isFalse); | |
| 112 expect(args.command.name, equals('install')); | |
| 113 }); | |
| 114 | |
| 115 test('parses a parent abbreviation before the command', () { | |
| 116 var parser = new ArgParser(); | |
| 117 parser.addFlag('debug', abbr: 'd'); | |
| 118 parser.addFlag('verbose', abbr: 'v'); | |
| 119 var command = parser.addCommand('install'); | |
| 120 | |
| 121 var args = parser.parse(['-dv', 'install']); | |
| 122 expect(args['debug'], isTrue); | |
| 123 expect(args['verbose'], isTrue); | |
| 124 expect(args.command.name, equals('install')); | |
| 125 }); | |
| 126 | |
| 127 test('parses a parent abbreviation after the command', () { | |
| 128 var parser = new ArgParser(); | |
| 129 parser.addFlag('debug', abbr: 'd'); | |
| 130 parser.addFlag('verbose', abbr: 'v'); | |
| 131 var command = parser.addCommand('install'); | |
| 132 | |
| 133 var args = parser.parse(['install', '-dv']); | |
| 134 expect(args['debug'], isTrue); | |
| 135 expect(args['verbose'], isTrue); | |
| 136 expect(args.command.name, equals('install')); | |
| 137 }); | |
| 138 | |
| 139 test('does not parse a solo command option before the command', () { | |
| 140 var parser = new ArgParser(); | |
| 141 var command = parser.addCommand('install'); | |
| 142 command.addOption('path', abbr: 'p'); | |
| 143 | |
| 144 throwsFormat(parser, ['-p', 'foo', 'install']); | |
| 145 }); | |
| 146 | |
| 147 test('does not parse a command option before the command', () { | |
| 148 var parser = new ArgParser(); | |
| 149 var command = parser.addCommand('install'); | |
| 150 command.addOption('path'); | |
| 151 | |
| 152 throwsFormat(parser, ['--path', 'foo', 'install']); | |
| 153 }); | |
| 154 | |
| 155 test('does not parse a command abbreviation before the command', () { | |
| 156 var parser = new ArgParser(); | |
| 157 var command = parser.addCommand('install'); | |
| 158 command.addFlag('debug', abbr: 'd'); | |
| 159 command.addFlag('verbose', abbr: 'v'); | |
| 160 | |
| 161 throwsFormat(parser, ['-dv', 'install']); | |
| 162 }); | |
| 163 | |
| 164 test('assigns collapsed options to the proper command', () { | |
| 165 var parser = new ArgParser(); | |
| 166 parser.addFlag('apple', abbr: 'a'); | |
| 167 var command = parser.addCommand('cmd'); | |
| 168 command.addFlag('banana', abbr: 'b'); | |
| 169 var subcommand = command.addCommand('subcmd'); | |
| 170 subcommand.addFlag('cherry', abbr: 'c'); | |
| 171 | |
| 172 var args = parser.parse(['cmd', 'subcmd', '-abc']); | |
| 173 expect(args['apple'], isTrue); | |
| 174 expect(args.command.name, equals('cmd')); | |
| 175 expect(args.command['banana'], isTrue); | |
| 176 expect(args.command.command.name, equals('subcmd')); | |
| 177 expect(args.command.command['cherry'], isTrue); | |
| 178 }); | |
| 179 | |
| 180 test('option is given to innermost command that can take it', () { | |
| 181 var parser = new ArgParser(); | |
| 182 parser.addFlag('verbose'); | |
| 183 var command = parser.addCommand('cmd'); | |
| 184 command.addFlag('verbose'); | |
| 185 var subcommand = command.addCommand('subcmd'); | |
| 186 | |
| 187 var args = parser.parse(['cmd', 'subcmd', '--verbose']); | |
| 188 expect(args['verbose'], isFalse); | |
| 189 expect(args.command.name, equals('cmd')); | |
| 190 expect(args.command['verbose'], isTrue); | |
| 191 expect(args.command.command.name, equals('subcmd')); | |
| 192 }); | |
| 193 | |
| 194 test('remaining arguments are given to the innermost command', () { | |
| 195 var parser = new ArgParser(); | |
| 196 var command = parser.addCommand('cmd'); | |
| 197 var subcommand = command.addCommand('subcmd'); | |
| 198 | |
| 199 var args = parser.parse(['cmd', 'subcmd', 'other', 'stuff']); | |
| 200 expect(args.command.name, equals('cmd')); | |
| 201 expect(args.rest, isEmpty); | |
| 202 expect(args.command.command.name, equals('subcmd')); | |
| 203 expect(args.command.rest, isEmpty); | |
| 204 expect(args.command.command.rest, equals(['other', 'stuff'])); | |
| 205 }); | 95 }); |
| 206 }); | 96 }); |
| 97 |
| 98 test("usageError splits up the message and usage", () { |
| 99 expect(() => foo.usageError("message"), throwsUsageError("message", """ |
| 100 Usage: test foo <arguments> |
| 101 -h, --help Print this usage information. |
| 102 |
| 103 Run "test help" to see global options.""")); |
| 104 }); |
| 105 |
| 106 test("considers a command hidden if all its subcommands are hidden", () { |
| 107 foo.addSubcommand(new HiddenCommand()); |
| 108 expect(foo.hidden, isTrue); |
| 109 }); |
| 207 } | 110 } |
| OLD | NEW |