Skip to content

Don't allow outdated taking arguments #2872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/src/command/outdated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class OutdatedCommand extends PubCommand {
/// when we are outputting machine-readable json.
bool get _shouldShowSpinner => stdout.hasTerminal && !argResults['json'];

@override
bool get takesArguments => false;

OutdatedCommand() {
argParser.addFlag(
'color',
Expand Down
16 changes: 8 additions & 8 deletions lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,13 @@ class PubCommandRunner extends CommandRunner<int> implements PubTopLevel {

@override
Future<int> run(Iterable<String> args) async {
_argResults = parse(args);
return await runCommand(_argResults) ?? exit_codes.SUCCESS;
try {
_argResults = parse(args);
return await runCommand(_argResults) ?? exit_codes.SUCCESS;
} on UsageException catch (error) {
log.exception(error);
return exit_codes.USAGE;
}
}

@override
Expand All @@ -146,12 +151,7 @@ class PubCommandRunner extends CommandRunner<int> implements PubTopLevel {
log.message('Pub ${sdk.version}');
return 0;
}
try {
return await super.runCommand(topLevelResults);
} on UsageException catch (error) {
log.exception(error);
return exit_codes.USAGE;
}
return await super.runCommand(topLevelResults);
}

@override
Expand Down
56 changes: 56 additions & 0 deletions test/outdated/goldens/bad_arguments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
$ pub outdated random_argument
[ERR] Command "outdated" does not take any arguments.
[ERR]
[ERR] Usage: pub outdated [options]
[ERR] -h, --help Print this usage information.
[ERR] --[no-]color Whether to color the output.
[ERR] Defaults to color when connected to a
[ERR] terminal, and no-color otherwise.
[ERR] --[no-]dependency-overrides Show resolutions with `dependency_overrides`.
[ERR] (defaults to on)
[ERR] --[no-]dev-dependencies Take dev dependencies into account.
[ERR] (defaults to on)
[ERR] --json Output the results using a json format.
[ERR] --mode=<PROPERTY> Highlight versions with PROPERTY.
[ERR] Only packages currently missing that PROPERTY
[ERR] will be included unless --show-all.
[ERR] [outdated (default), null-safety]
[ERR] --[no-]prereleases Include prereleases in latest version.
[ERR] (defaults to on in --mode=null-safety).
[ERR] --[no-]show-all Include dependencies that are already
[ERR] fullfilling --mode.
[ERR] --[no-]transitive Show transitive dependencies.
[ERR] (defaults to off in --mode=null-safety).
[ERR]
[ERR] Run "pub help" to see global options.
[ERR] See https://dart.dev/tools/pub/cmd/pub-outdated for detailed documentation.
[Exit code] 64

$ pub outdated --bad_flag
[ERR] Could not find an option named "bad_flag".
[ERR]
[ERR] Usage: pub outdated [options]
[ERR] -h, --help Print this usage information.
[ERR] --[no-]color Whether to color the output.
[ERR] Defaults to color when connected to a
[ERR] terminal, and no-color otherwise.
[ERR] --[no-]dependency-overrides Show resolutions with `dependency_overrides`.
[ERR] (defaults to on)
[ERR] --[no-]dev-dependencies Take dev dependencies into account.
[ERR] (defaults to on)
[ERR] --json Output the results using a json format.
[ERR] --mode=<PROPERTY> Highlight versions with PROPERTY.
[ERR] Only packages currently missing that PROPERTY
[ERR] will be included unless --show-all.
[ERR] [outdated (default), null-safety]
[ERR] --[no-]prereleases Include prereleases in latest version.
[ERR] (defaults to on in --mode=null-safety).
[ERR] --[no-]show-all Include dependencies that are already
[ERR] fullfilling --mode.
[ERR] --[no-]transitive Show transitive dependencies.
[ERR] (defaults to off in --mode=null-safety).
[ERR]
[ERR] Run "pub help" to see global options.
[ERR] See https://dart.dev/tools/pub/cmd/pub-outdated for detailed documentation.
[Exit code] 64

4 changes: 4 additions & 0 deletions test/outdated/goldens/no_pubspec.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$ pub outdated
[ERR] Could not find a file named "pubspec.yaml" in "$SANDBOX/myapp".
[Exit code] 66

37 changes: 28 additions & 9 deletions test/outdated/outdated_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:test/test.dart';
import '../descriptor.dart' as d;
import '../golden_file.dart';
import '../test_pub.dart';

/// Runs `pub outdated [args]` and appends the output to [buffer].
Future<void> runPubOutdated(List<String> args, StringBuffer buffer,
{Map<String, String> environment,
dynamic exitCode = 0,
dynamic stdErr = isEmpty}) async {
{Map<String, String> environment}) async {
final process =
await startPub(args: ['outdated', ...args], environment: environment);
await process.shouldExit(exitCode);
final exitCode = await process.exitCode;

expect(await process.stderr.rest.toList(), stdErr);
buffer.writeln([
'\$ pub outdated ${args.join(' ')}',
...await process.stdout.rest.where((line) {
Expand All @@ -25,6 +24,16 @@ Future<void> runPubOutdated(List<String> args, StringBuffer buffer,
return !line.startsWith('Downloading ');
}).toList(),
].join('\n'));
final stderrLines = await process.stderr.rest.toList();
for (final line in stderrLines) {
final sanitized = line
.replaceAll(d.sandbox, r'$SANDBOX')
.replaceAll(Platform.pathSeparator, '/');
buffer.writeln('[ERR] $sanitized');
}
if (exitCode != 0) {
buffer.writeln('[Exit code] $exitCode');
}
buffer.write('\n');
}

Expand Down Expand Up @@ -65,10 +74,12 @@ Future<void> main() async {
test('no pubspec', () async {
await d.dir(appPath, []).create();
final buffer = StringBuffer();
await runPubOutdated([], buffer,
exitCode: isNot(0),
stdErr: contains(
startsWith('Could not find a file named "pubspec.yaml" in ')));
await runPubOutdated(
[],
buffer,
);
expectMatchesGoldenFile(
buffer.toString(), 'test/outdated/goldens/no_pubspec.txt');
});

test('no lockfile', () async {
Expand Down Expand Up @@ -455,4 +466,12 @@ Future<void> main() async {
'PUB_ENVIRONMENT': 'flutter_cli:get',
});
});

test("doesn't allow arguments. Handles bad flags", () async {
final sb = StringBuffer();
await runPubOutdated(['random_argument'], sb);
await runPubOutdated(['--bad_flag'], sb);
expectMatchesGoldenFile(
sb.toString(), 'test/outdated/goldens/bad_arguments.txt');
});
}