Skip to content
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

Include bare pub --help command in help_test #4112

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 24 additions & 22 deletions test/help_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,40 @@ import 'golden_file.dart';
/// Result will be an iterable of lists, illustrated as follows:
/// ```
/// [
/// [pub]
/// [pub, get]
/// [pub, --help]
/// [pub, get, --help]
/// ...
/// ]
/// ```
Iterable<List<String>> _extractCommands(
List<String> parents,
Iterable<Command> cmds,
) sync* {
if (parents.isNotEmpty) {
yield parents;
Iterable<List<String>> _extractCommands() sync* {
// dedup aliases.
Set visitedCommands = <Command>{};
final stack = [PubCommandRunner().commands.values.toList()];
final parents = <String>[];
while (true) {
final commands = stack.last;
if (commands.isEmpty) {
stack.removeLast();
yield ['pub', ...parents, '--help'];
if (parents.isEmpty) break;
parents.removeLast();
} else {
final command = commands.removeLast();
if (!visitedCommands.add(command)) continue;
if (command.hidden) continue;
stack.add(command.subcommands.values.toList());
parents.add(command.name);
}
}
// Track that we don't add more than once, we don't want to test aliases
final names = <String>{};
yield* cmds
.where((sub) => !sub.hidden && names.add(sub.name))
.map(
(sub) => _extractCommands(
[...parents, sub.name],
sub.subcommands.values,
),
)
.expand((cmds) => cmds);
}

/// Tests for `pub ... --help`.
Future<void> main() async {
final cmds = _extractCommands([], PubCommandRunner().commands.values);
final cmds = _extractCommands();
for (final c in cmds) {
testWithGolden('pub ${c.join(' ')} --help', (ctx) async {
testWithGolden(c.join(' '), (ctx) async {
await ctx.run(
[...c, '--help'],
c.skip(1).toList(),
environment: {
// Use more columns to avoid unintended line breaking.
'_PUB_TEST_TERMINAL_COLUMNS': '200',
Expand Down
47 changes: 47 additions & 0 deletions test/testdata/goldens/help_test/pub --help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# GENERATED BY: test/help_test.dart

## Section 0
$ pub --help
Pub is a package manager for Dart.

Usage: pub <command> [arguments]

Global options:
-h, --help Print this usage information.
--version Print pub version.
--[no-]trace Print debugging information when an error occurs.
--verbosity Control output verbosity.

[all] Show all output including internal tracing messages.
[error] Show only errors.
[io] Also show IO operations.
[normal] Show errors, warnings, and user messages.
[solver] Show steps during version resolution.
[warning] Show only errors and warnings.

-v, --verbose Shortcut for "--verbosity=all".
--[no-]color Use colors in terminal output.
Defaults to color when connected to a terminal, and no-color otherwise.
-C, --directory=<dir> Run the subcommand in the directory<dir>.
(defaults to ".")

Available commands:
add Add dependencies to `pubspec.yaml`.
cache Work with the system cache.
deps Print package dependencies.
downgrade Downgrade the current package's dependencies to oldest versions.
get Get the current package's dependencies.
global Work with global packages.
login Log into pub.dev.
logout Log out of pub.dev.
outdated Analyze your dependencies to find which ones can be upgraded.
publish Publish the current package to pub.dev.
remove Removes dependencies from `pubspec.yaml`.
run Run an executable from a package.
token Manage authentication tokens for hosted pub repositories.
upgrade Upgrade the current package's dependencies to latest versions.
version Print pub version.

Run "pub help <command>" for more information about a command.
See https://dart.dev/tools/pub/cmd for detailed documentation.