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
10 changes: 8 additions & 2 deletions lib/src/commands/packages/commands/check/check.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
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}) {
addSubcommand(PackagesCheckLicensesCommand(logger: logger));
PackagesCheckCommand({
Logger? logger,
PubLicense? pubLicense,
}) {
addSubcommand(
PackagesCheckLicensesCommand(logger: logger, pubLicense: pubLicense),
);
}

@override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
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}) : _logger = logger ?? Logger();
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.';
Expand Down
5 changes: 3 additions & 2 deletions lib/src/commands/packages/packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ 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));
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
3 changes: 2 additions & 1 deletion test/src/commands/packages/commands/check/check_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ void main() {

test(
'help',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result = await commandRunner.run(
[...commandArguments, '--help'],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ void main() {

test(
'help',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result = await commandRunner.run(
[...commandArguments, '--help'],
);
Expand All @@ -41,7 +42,8 @@ void main() {

test(
'returns exit code 0',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result = await commandRunner.run(commandArguments);
expect(result, equals(ExitCode.success.code));
}),
Expand Down
33 changes: 22 additions & 11 deletions test/src/commands/packages/commands/get_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ void main() {
group('packages get', () {
test(
'help',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result = await commandRunner.run(['packages', 'get', '--help']);
expect(printLogs, equals(_expectedPackagesGetUsage));
expect(result, equals(ExitCode.success.code));
Expand All @@ -38,7 +39,8 @@ void main() {
test(
'throws usage exception '
'when too many arguments are provided',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result = await commandRunner.run(
['packages', 'get', 'arg1', 'arg2'],
);
Expand All @@ -49,7 +51,8 @@ void main() {
test(
'throws pubspec not found exception '
'when no pubspec.yaml exists',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result = await commandRunner.run(['packages', 'get', 'test']);
expect(result, equals(ExitCode.noInput.code));
verify(() {
Expand All @@ -61,7 +64,8 @@ void main() {
test(
'throws pubspec not found exception '
'when no pubspec.yaml exists (recursive)',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result = await commandRunner.run(
['packages', 'get', '-r', 'site'],
);
Expand All @@ -74,7 +78,8 @@ void main() {

test(
'throws when installation fails',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
addTearDown(() => tempDirectory.deleteSync(recursive: true));

Expand All @@ -89,7 +94,8 @@ void main() {

test(
'ignores .fvm directory',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
addTearDown(() => tempDirectory.deleteSync(recursive: true));

Expand Down Expand Up @@ -117,7 +123,8 @@ void main() {
test(
'completes normally '
'when pubspec.yaml exists',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
addTearDown(() => tempDirectory.deleteSync(recursive: true));

Expand Down Expand Up @@ -145,7 +152,8 @@ void main() {
test(
'completes normally '
'when pubspec.yaml exists (recursive)',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
addTearDown(() => tempDirectory.deleteSync(recursive: true));

Expand Down Expand Up @@ -193,7 +201,8 @@ void main() {
test(
'completes normally '
'when pubspec.yaml exists and directory is not ignored (recursive)',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
addTearDown(() => tempDirectory.deleteSync(recursive: true));

Expand Down Expand Up @@ -244,7 +253,8 @@ void main() {
test(
'completes normally '
'''when pubspec.yaml exists and directory is not ignored (recursive) with an empty glob''',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
final directory = Directory(
path.join(tempDirectory.path, 'macos_plugin'),
Expand Down Expand Up @@ -294,7 +304,8 @@ void main() {
test(
'completes normally '
'when pubspec.yaml exists and directory is ignored (recursive)',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final tempDirectory = Directory.systemTemp.createTempSync();
addTearDown(() => tempDirectory.deleteSync(recursive: true));

Expand Down
3 changes: 2 additions & 1 deletion test/src/commands/packages/packages_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ void main() {
group('packages', () {
test(
'help',
withRunner((commandRunner, logger, pubUpdater, printLogs) async {
withRunner(
(commandRunner, logger, pubUpdater, pubLicense, printLogs) async {
final result = await commandRunner.run(['packages', '--help']);
expect(printLogs, equals(_expectedPackagesUsage));
expect(result, equals(ExitCode.success.code));
Expand Down
Loading