From f25b2fb319d4181532f55e142171c5f2d5ab8e2d Mon Sep 17 00:00:00 2001 From: Tushar Ojha Date: Fri, 19 Mar 2021 21:32:20 +0530 Subject: [PATCH] add commands to fail if extra arguments are passed --- lib/src/command/add.dart | 2 ++ test/add/common/add_test.dart | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/src/command/add.dart b/lib/src/command/add.dart index fda22503d..d74dd6c10 100644 --- a/lib/src/command/add.dart +++ b/lib/src/command/add.dart @@ -82,6 +82,8 @@ class AddCommand extends PubCommand { Future runProtected() async { if (argResults.rest.isEmpty) { usageException('Must specify a package to be added.'); + } else if (argResults.rest.length > 1) { + usageException('Takes only a single argument.'); } final packageInformation = _parsePackage(argResults.rest.first); diff --git a/test/add/common/add_test.dart b/test/add/common/add_test.dart index 5252978d0..c3c77391a 100644 --- a/test/add/common/add_test.dart +++ b/test/add/common/add_test.dart @@ -37,6 +37,30 @@ void main() { }); group('normally', () { + test('fails if extra arguments are passed', () async { + await servePackages((builder) { + builder.serve('foo', '1.2.2'); + }); + + await d.dir(appPath, [ + d.pubspec({'name': 'myapp'}) + ]).create(); + + await pubAdd( + args: ['foo', '^1.2.2'], + exitCode: exit_codes.USAGE, + error: contains('Takes only a single argument.')); + + await d.dir(appPath, [ + d.pubspec({ + 'name': 'myapp', + }), + d.nothing('.dart_tool/package_config.json'), + d.nothing('pubspec.lock'), + d.nothing('.packages'), + ]).validate(); + }); + test('adds a package from a pub server', () async { await servePackages((builder) => builder.serve('foo', '1.2.3'));