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

feat: inject PubLicense in VeryGoodCommandRunner #843

Merged
merged 15 commits into from
Oct 6, 2023
5 changes: 4 additions & 1 deletion lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import 'package:args/args.dart';
import 'package:args/command_runner.dart';
import 'package:cli_completion/cli_completion.dart';
import 'package:mason/mason.dart' hide packageVersion;
import 'package:meta/meta.dart';
import 'package:pub_updater/pub_updater.dart';
import 'package:very_good_cli/src/commands/commands.dart';
import 'package:very_good_cli/src/pub_license/pub_license.dart';
import 'package:very_good_cli/src/version.dart';

/// The package name.
Expand All @@ -17,6 +19,7 @@ class VeryGoodCommandRunner extends CompletionCommandRunner<int> {
VeryGoodCommandRunner({
Logger? logger,
PubUpdater? pubUpdater,
@visibleForTesting PubLicense? pubLicense,
}) : _logger = logger ?? Logger(),
_pubUpdater = pubUpdater ?? PubUpdater(),
super('very_good', '🦄 A Very Good Command-Line Interface') {
Expand All @@ -31,7 +34,7 @@ class VeryGoodCommandRunner extends CompletionCommandRunner<int> {
help: 'Noisy logging, including all shell commands executed.',
);
addCommand(CreateCommand(logger: _logger));
addCommand(PackagesCommand(logger: _logger));
addCommand(PackagesCommand(logger: _logger, pubLicense: pubLicense));
addCommand(TestCommand(logger: _logger));
addCommand(UpdateCommand(logger: _logger, pubUpdater: pubUpdater));
}
Expand Down
29 changes: 29 additions & 0 deletions lib/src/commands/packages/commands/check/check.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:args/command_runner.dart';
import 'package:mason/mason.dart';
import 'package:very_good_cli/src/commands/packages/commands/check/commands/commands.dart';
import 'package:very_good_cli/src/pub_license/pub_license.dart';

/// {@template packages_check_command}
/// `very_good packages check` command for performing checks in a Dart or
/// Flutter project.
/// {@endtemplate}
class PackagesCheckCommand extends Command<int> {
/// {@macro packages_check_command}
PackagesCheckCommand({
Logger? logger,
PubLicense? pubLicense,
}) {
addSubcommand(
PackagesCheckLicensesCommand(logger: logger, pubLicense: pubLicense),
);
}

@override
String get description => 'Perform checks in a Dart or Flutter project.';

@override
String get name => 'check';

@override
bool get hidden => true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'licenses.dart';
36 changes: 36 additions & 0 deletions lib/src/commands/packages/commands/check/commands/licenses.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:args/command_runner.dart';
import 'package:mason/mason.dart';
import 'package:very_good_cli/src/pub_license/pub_license.dart';

/// {@template packages_check_licenses_command}
/// `very_good packages check licenses` command for checking packages licenses.
/// {@endtemplate}
class PackagesCheckLicensesCommand extends Command<int> {
/// {@macro packages_check_licenses_command}
PackagesCheckLicensesCommand({
Logger? logger,
PubLicense? pubLicense,
}) : _logger = logger ?? Logger(),
_pubLicense = pubLicense ?? PubLicense();

// ignore: unused_field
final Logger _logger;

// ignore: unused_field
final PubLicense _pubLicense;

@override
String get description =>
'Check packages licenses in a Dart or Flutter project.';

@override
String get name => 'licenses';

@override
bool get hidden => true;

@override
Future<int> run() async {
return ExitCode.success.code;
}
}
5 changes: 4 additions & 1 deletion lib/src/commands/packages/packages.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import 'package:args/command_runner.dart';
import 'package:mason/mason.dart';
import 'package:very_good_cli/src/commands/packages/commands/check/check.dart';
import 'package:very_good_cli/src/commands/packages/commands/commands.dart';
import 'package:very_good_cli/src/pub_license/pub_license.dart';

/// {@template packages_command}
/// `very_good packages` command for managing packages.
/// {@endtemplate}
class PackagesCommand extends Command<int> {
/// {@macro packages_command}
PackagesCommand({Logger? logger}) {
PackagesCommand({Logger? logger, PubLicense? pubLicense}) {
addSubcommand(PackagesGetCommand(logger: logger));
addSubcommand(PackagesCheckCommand(logger: logger, pubLicense: pubLicense));
}

@override
Expand Down
9 changes: 8 additions & 1 deletion test/helpers/command_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import 'package:mason/mason.dart';
import 'package:mocktail/mocktail.dart';
import 'package:pub_updater/pub_updater.dart';
import 'package:very_good_cli/src/command_runner.dart';
import 'package:very_good_cli/src/pub_license/pub_license.dart';

class MockLogger extends Mock implements Logger {}

class MockProgress extends Mock implements Progress {}

class MockPubUpdater extends Mock implements PubUpdater {}

class _MockPubLicense extends Mock implements PubLicense {}

void Function() _overridePrint(void Function(List<String>) fn) {
return () {
final printLogs = <String>[];
Expand All @@ -31,13 +34,15 @@ void Function() withRunner(
VeryGoodCommandRunner commandRunner,
Logger logger,
PubUpdater pubUpdater,
PubLicense pubLicense,
List<String> printLogs,
) runnerFn,
) {
return _overridePrint((printLogs) async {
final logger = MockLogger();
final progress = MockProgress();
final pubUpdater = MockPubUpdater();
final pubLicense = _MockPubLicense();
final progressLogs = <String>[];
final commandRunner = VeryGoodCommandRunner(
logger: logger,
Expand All @@ -55,7 +60,9 @@ void Function() withRunner(
currentVersion: any(named: 'currentVersion'),
),
).thenAnswer((_) => Future.value(true));
when(() => pubLicense.getLicense(any()))
.thenAnswer((_) => Future.value({'MIT'}));

await runnerFn(commandRunner, logger, pubUpdater, printLogs);
await runnerFn(commandRunner, logger, pubUpdater, pubLicense, printLogs);
});
}
3 changes: 2 additions & 1 deletion test/src/commands/create/commands/dart_cli_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ void main() {
group('create dart_cli', () {
test(
'help',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result =
await commandRunner.run(['create', 'dart_cli', '--help']);
expect(printLogs, equals(expectedUsage));
Expand Down
3 changes: 2 additions & 1 deletion test/src/commands/create/commands/dart_package_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ void main() {
group('create dart_package', () {
test(
'help',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result =
await commandRunner.run(['create', 'dart_package', '--help']);
expect(printLogs, equals(expectedUsage));
Expand Down
3 changes: 2 additions & 1 deletion test/src/commands/create/commands/docs_site_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ void main() {
group('create docs_site', () {
test(
'help',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result =
await commandRunner.run(['create', 'docs_site', '--help']);
expect(printLogs, equals(expectedUsage));
Expand Down
3 changes: 2 additions & 1 deletion test/src/commands/create/commands/flame_game_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ void main() {
group('create flame_game', () {
test(
'help',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result =
await commandRunner.run(['create', 'flame_game', '--help']);
expect(printLogs, equals(expectedUsage));
Expand Down
3 changes: 2 additions & 1 deletion test/src/commands/create/commands/flutter_app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ void main() {
group('create flutter_app', () {
test(
'help',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result =
await commandRunner.run(['create', 'flutter_app', '--help']);
expect(printLogs, equals(expectedUsage));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ void main() {
group('create flutter_package', () {
test(
'help',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result =
await commandRunner.run(['create', 'flutter_package', '--help']);
expect(printLogs, equals(expectedUsage));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ void main() {
group('create flutter_plugin', () {
test(
'help',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result =
await commandRunner.run(['create', 'flutter_plugin', '--help']);
expect(printLogs, equals(expectedUsage));
Expand Down
3 changes: 2 additions & 1 deletion test/src/commands/create/create_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ void main() {
group('create', () {
test(
'help',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result = await commandRunner.run(['create', '--help']);
expect(printLogs, equals(expectedUsage));
expect(result, equals(ExitCode.success.code));
Expand Down
51 changes: 51 additions & 0 deletions test/src/commands/packages/commands/check/check_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'dart:collection';

import 'package:mason_logger/mason_logger.dart';
import 'package:test/test.dart';
import 'package:very_good_cli/src/commands/packages/commands/check/check.dart';

import '../../../../../helpers/helpers.dart';

const _expectedPackagesCheckUsage = [
// ignore: no_adjacent_strings_in_list
'Perform checks in a Dart or Flutter project.\n'
'\n'
'Usage: very_good packages check <subcommand> [arguments]\n'
'-h, --help Print this usage information.\n'
'\n'
'Available subcommands:\n'
' licenses Check packages licenses in a Dart or Flutter project.\n'
'\n'
'Run "very_good help" to see global options.'
];

void main() {
group('packages check licenses', () {
final commandArguments = UnmodifiableListView(
['packages', 'check'],
);

test(
'help',
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result = await commandRunner.run(
[...commandArguments, '--help'],
);
expect(printLogs, equals(_expectedPackagesCheckUsage));
expect(result, equals(ExitCode.success.code));

printLogs.clear();

final resultAbbr = await commandRunner.run([...commandArguments, '-h']);
expect(printLogs, equals(_expectedPackagesCheckUsage));
expect(resultAbbr, equals(ExitCode.success.code));
}),
);

test('is hidden', () {
final command = PackagesCheckCommand();
expect(command.hidden, isTrue);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'dart:collection';

import 'package:mason_logger/mason_logger.dart';
import 'package:test/test.dart';
import 'package:very_good_cli/src/commands/packages/commands/check/commands/commands.dart';

import '../../../../../../helpers/helpers.dart';

const _expectedPackagesCheckLicensesUsage = [
// ignore: no_adjacent_strings_in_list
'Check packages licenses in a Dart or Flutter project.\n'
'\n'
'Usage: very_good packages check licenses [arguments]\n'
'-h, --help Print this usage information.\n'
'\n'
'Run "very_good help" to see global options.'
];

void main() {
group('packages check licenses', () {
final commandArguments = UnmodifiableListView(
['packages', 'check', 'licenses'],
);

test(
'help',
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result = await commandRunner.run(
[...commandArguments, '--help'],
);
expect(printLogs, equals(_expectedPackagesCheckLicensesUsage));
expect(result, equals(ExitCode.success.code));

printLogs.clear();

final resultAbbr = await commandRunner.run([...commandArguments, '-h']);
expect(printLogs, equals(_expectedPackagesCheckLicensesUsage));
expect(resultAbbr, equals(ExitCode.success.code));
}),
);

test(
'returns exit code 0',
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result = await commandRunner.run(commandArguments);
expect(result, equals(ExitCode.success.code));
}),
);

test('is hidden', () {
final command = PackagesCheckLicensesCommand();
expect(command.hidden, isTrue);
});
});
}
Loading