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(shorebird_cli): --version includes Flutter version #1075

Merged
merged 1 commit into from
Aug 9, 2023
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
24 changes: 19 additions & 5 deletions packages/shorebird_cli/lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:shorebird_cli/src/commands/commands.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:shorebird_cli/src/process.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';
import 'package:shorebird_cli/src/shorebird_flutter.dart';
import 'package:shorebird_cli/src/version.dart';

const executableName = 'shorebird';
Expand Down Expand Up @@ -142,16 +143,29 @@ ${lightCyan.wrap('shorebird release android -- --no-pub lib/main.dart')}''',
// Run the command or show version
final int? exitCode;
if (topLevelResults['version'] == true) {
logger.info(
'''
Shorebird $packageVersion
Shorebird Engine • revision ${shorebirdEnv.shorebirdEngineRevision}''',
);
final flutterVersion = await _tryGetFlutterVersion();
final shorebirdFlutterPrefix = StringBuffer('Flutter');
if (flutterVersion != null) {
shorebirdFlutterPrefix.write(' $flutterVersion');
}
logger.info('''
Shorebird $packageVersion • git@github.com:shorebirdtech/shorebird.git
$shorebirdFlutterPrefix • revision ${shorebirdEnv.flutterRevision}
Engine • revision ${shorebirdEnv.shorebirdEngineRevision}''');
exitCode = ExitCode.success.code;
} else {
exitCode = await super.runCommand(topLevelResults);
}

return exitCode;
}

Future<String?> _tryGetFlutterVersion() async {
try {
return await shorebirdFlutter.getVersion();
} catch (error) {
logger.detail('Unable to determine Flutter version.\n$error');
return null;
}
}
}
41 changes: 37 additions & 4 deletions packages/shorebird_cli/test/src/command_runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:shorebird_cli/src/command_runner.dart';
import 'package:shorebird_cli/src/logger.dart' hide logger;
import 'package:shorebird_cli/src/process.dart';
import 'package:shorebird_cli/src/shorebird_env.dart';
import 'package:shorebird_cli/src/shorebird_flutter.dart';
import 'package:shorebird_cli/src/version.dart';
import 'package:test/test.dart';

Expand All @@ -15,12 +16,17 @@ class _MockShorebirdEnv extends Mock implements ShorebirdEnv {}

class _MockProcessResult extends Mock implements ShorebirdProcessResult {}

class _MockShorebirdFlutter extends Mock implements ShorebirdFlutter {}

void main() {
group(ShorebirdCliCommandRunner, () {
const shorebirdEngineRevision = 'test-revision';
const shorebirdEngineRevision = 'test-engine-revision';
const flutterRevision = 'test-flutter-revision';
const flutterVersion = '1.2.3';

late Logger logger;
late ShorebirdEnv shorebirdEnv;
late ShorebirdFlutter shorebirdFlutter;
late ShorebirdProcessResult processResult;
late ShorebirdCliCommandRunner commandRunner;

Expand All @@ -30,18 +36,24 @@ void main() {
values: {
loggerRef.overrideWith(() => logger),
shorebirdEnvRef.overrideWith(() => shorebirdEnv),
shorebirdFlutterRef.overrideWith(() => shorebirdFlutter),
},
);
}

setUp(() {
logger = _MockLogger();
shorebirdEnv = _MockShorebirdEnv();
shorebirdFlutter = _MockShorebirdFlutter();
processResult = _MockProcessResult();
when(() => processResult.exitCode).thenReturn(ExitCode.success.code);
when(
() => shorebirdEnv.shorebirdEngineRevision,
).thenReturn(shorebirdEngineRevision);
when(() => shorebirdEnv.flutterRevision).thenReturn(flutterRevision);
when(
() => shorebirdFlutter.getVersion(),
).thenAnswer((_) async => flutterVersion);
commandRunner = runWithOverrides(ShorebirdCliCommandRunner.new);
});

Expand Down Expand Up @@ -110,19 +122,40 @@ ${lightCyan.wrap('shorebird release android -- --no-pub lib/main.dart')}''',
});

group('--version', () {
test('outputs current version and engine revisions', () async {
test('outputs current version info', () async {
final result = await runWithOverrides(
() => commandRunner.run(['--version']),
);
expect(result, equals(ExitCode.success.code));
verify(
() => logger.info(
'''
Shorebird $packageVersion
Shorebird Engine • revision $shorebirdEngineRevision''',
Shorebird $packageVersion • git@github.com:shorebirdtech/shorebird.git
Flutter $flutterVersion • revision $flutterRevision
Engine • revision $shorebirdEngineRevision''',
),
).called(1);
});

test('gracefully handles case when flutter version cannot be determined',
() async {
when(() => shorebirdFlutter.getVersion()).thenThrow('error');
final result = await runWithOverrides(
() => commandRunner.run(['--version']),
);
expect(result, equals(ExitCode.success.code));
verify(
() => logger.info(
'''
Shorebird $packageVersion • git@github.com:shorebirdtech/shorebird.git
Flutter • revision $flutterRevision
Engine • revision $shorebirdEngineRevision''',
),
).called(1);
verify(
() => logger.detail('Unable to determine Flutter version.\nerror'),
).called(1);
});
});

group('--verbose', () {
Expand Down