Skip to content

Commit

Permalink
feat(shorebird_cli): add shorebird flutter versions list (#1064)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Aug 8, 2023
1 parent a6fb9fc commit 65f15f4
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'flutter_command.dart';
export 'versions/versions.dart';
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/commands/commands.dart';

/// {@template flutter_command}
/// `shorebird flutter`
/// Manage your Shorebird Flutter installation.
/// {@endtemplate}
class FlutterCommand extends ShorebirdCommand {
/// {@macro flutter_command}
FlutterCommand();
FlutterCommand() {
addSubcommand(FlutterVersionsCommand());
}

@override
String get description => 'Manage your Shorebird Flutter installation.';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/commands/commands.dart';

/// {@template flutter_versions_command}
/// `shorebird flutter versions`
/// Manage your Shorebird Flutter versions.
/// {@endtemplate}
class FlutterVersionsCommand extends ShorebirdCommand {
/// {@macro flutter_versions_command}
FlutterVersionsCommand() {
addSubcommand(FlutterVersionsListCommand());
}

@override
String get description => 'Manage your Shorebird Flutter versions.';

@override
String get name => 'versions';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:mason_logger/mason_logger.dart';
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/shorebird_flutter.dart';

/// {@template flutter_versions_list_command}
/// `shorebird flutter versions list`
/// List available Flutter versions.
/// {@endtemplate}
class FlutterVersionsListCommand extends ShorebirdCommand {
/// {@macro flutter_versions_list_command}
FlutterVersionsListCommand();

@override
String get description => 'List available Flutter versions.';

@override
String get name => 'list';

@override
Future<int> run() async {
final progress = logger.progress('Fetching Flutter versions');
final List<String> versions;
try {
versions = await shorebirdFlutter.getVersions();
progress.complete();
} catch (error) {
progress.fail('Failed to fetch Flutter versions.');
logger.err('$error');
return ExitCode.software.code;
}

logger.info('📦 Flutter Versions');
for (final version in versions.reversed) {
logger.info(version);
}
return ExitCode.success.code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'flutter_versions_command.dart';
export 'flutter_versions_list_command.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:shorebird_cli/src/commands/commands.dart';
import 'package:test/test.dart';

void main() {
group(FlutterVersionsCommand, () {
test('has correct name and description', () {
final command = FlutterVersionsCommand();
expect(command.name, equals('versions'));
expect(
command.description,
equals('Manage your Shorebird Flutter versions.'),
);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'package:mason_logger/mason_logger.dart';
import 'package:mocktail/mocktail.dart';
import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/commands/commands.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/shorebird_flutter.dart';
import 'package:test/test.dart';

class _MockLogger extends Mock implements Logger {}

class _MockProgress extends Mock implements Progress {}

class _MockShorebirdFlutter extends Mock implements ShorebirdFlutter {}

void main() {
group(FlutterVersionsListCommand, () {
late Progress progress;
late Logger logger;
late ShorebirdFlutter shorebirdFlutter;
late FlutterVersionsListCommand command;

R runWithOverrides<R>(R Function() body) {
return runScoped(
body,
values: {
loggerRef.overrideWith(() => logger),
shorebirdFlutterRef.overrideWith(() => shorebirdFlutter),
},
);
}

setUp(() {
progress = _MockProgress();
logger = _MockLogger();
shorebirdFlutter = _MockShorebirdFlutter();
command = runWithOverrides(FlutterVersionsListCommand.new);

when(() => logger.progress(any())).thenReturn(progress);
});

test('has correct name and description', () {
expect(command.name, equals('list'));
expect(command.description, equals('List available Flutter versions.'));
});

test('exits with code 70 when unable to determine Flutter versions',
() async {
when(() => shorebirdFlutter.getVersions()).thenThrow('error');
await expectLater(
runWithOverrides(command.run),
completion(equals(ExitCode.software.code)),
);
verifyInOrder([
() => logger.progress('Fetching Flutter versions'),
() => progress.fail('Failed to fetch Flutter versions.'),
() => logger.err('error'),
]);
});

test('exits with code 0 when able to determine Flutter versions', () async {
const versions = ['1.0.0', '1.0.1'];
when(
() => shorebirdFlutter.getVersions(),
).thenAnswer((_) async => versions);
await expectLater(
runWithOverrides(command.run),
completion(equals(ExitCode.success.code)),
);
verifyInOrder([
() => logger.progress('Fetching Flutter versions'),
() => progress.complete(),
() => logger.info('📦 Flutter Versions'),
() => logger.info('1.0.1'),
() => logger.info('1.0.0'),
]);
});
});
}

0 comments on commit 65f15f4

Please sign in to comment.