-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shorebird_cli): add
shorebird flutter version use
(#1070)
- Loading branch information
Showing
4 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/shorebird_cli/lib/src/commands/flutter/versions/flutter_versions_use_command.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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_use_command} | ||
/// `shorebird flutter versions use` | ||
/// Use a different Flutter version. | ||
/// {@endtemplate} | ||
class FlutterVersionsUseCommand extends ShorebirdCommand { | ||
/// {@macro flutter_versions_use_command} | ||
FlutterVersionsUseCommand(); | ||
|
||
@override | ||
String get description => 'Use a different Flutter version.'; | ||
|
||
@override | ||
String get name => 'use'; | ||
|
||
@override | ||
Future<int> run() async { | ||
if (results.rest.isEmpty) { | ||
logger.err( | ||
''' | ||
No version specified. | ||
Usage: shorebird flutter versions use <version> | ||
Use `shorebird flutter versions list` to list available versions.''', | ||
); | ||
return ExitCode.usage.code; | ||
} | ||
|
||
if (results.rest.length > 1) { | ||
logger.err(''' | ||
Too many arguments. | ||
Usage: shorebird flutter versions use <version>'''); | ||
return ExitCode.usage.code; | ||
} | ||
|
||
final version = results.rest.first; | ||
final fetchFlutterVersionsProgress = logger.progress( | ||
'Fetching Flutter versions', | ||
); | ||
final List<String> versions; | ||
try { | ||
versions = await shorebirdFlutter.getVersions(); | ||
fetchFlutterVersionsProgress.complete(); | ||
} catch (error) { | ||
fetchFlutterVersionsProgress.fail('Failed to fetch Flutter versions.'); | ||
logger.err('$error'); | ||
return ExitCode.software.code; | ||
} | ||
|
||
if (!versions.contains(version)) { | ||
logger.err(''' | ||
Version $version not found. | ||
Use `shorebird flutter versions list` to list available versions.'''); | ||
return ExitCode.software.code; | ||
} | ||
|
||
final installRevisionProgress = logger.progress( | ||
'Installing Flutter $version', | ||
); | ||
try { | ||
await shorebirdFlutter.useVersion(version: version); | ||
installRevisionProgress.complete(); | ||
} catch (error) { | ||
installRevisionProgress.fail('Failed to install Flutter $version.'); | ||
logger.err('$error'); | ||
return ExitCode.software.code; | ||
} | ||
|
||
return ExitCode.success.code; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/shorebird_cli/lib/src/commands/flutter/versions/versions.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export 'flutter_versions_command.dart'; | ||
export 'flutter_versions_list_command.dart'; | ||
export 'flutter_versions_use_command.dart'; |
150 changes: 150 additions & 0 deletions
150
...s/shorebird_cli/test/src/commands/flutter/versions/flutter_versions_use_command_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import 'package:args/args.dart'; | ||
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 _MockArgResults extends Mock implements ArgResults {} | ||
|
||
class _MockLogger extends Mock implements Logger {} | ||
|
||
class _MockProgress extends Mock implements Progress {} | ||
|
||
class _MockShorebirdFlutter extends Mock implements ShorebirdFlutter {} | ||
|
||
void main() { | ||
group(FlutterVersionsUseCommand, () { | ||
const version = '1.2.3'; | ||
|
||
late ArgResults argResults; | ||
late Progress progress; | ||
late Logger logger; | ||
late ShorebirdFlutter shorebirdFlutter; | ||
late FlutterVersionsUseCommand command; | ||
|
||
R runWithOverrides<R>(R Function() body) { | ||
return runScoped( | ||
body, | ||
values: { | ||
loggerRef.overrideWith(() => logger), | ||
shorebirdFlutterRef.overrideWith(() => shorebirdFlutter), | ||
}, | ||
); | ||
} | ||
|
||
setUp(() { | ||
argResults = _MockArgResults(); | ||
when(() => argResults.rest).thenReturn([version]); | ||
progress = _MockProgress(); | ||
logger = _MockLogger(); | ||
shorebirdFlutter = _MockShorebirdFlutter(); | ||
command = runWithOverrides(FlutterVersionsUseCommand.new) | ||
..testArgResults = argResults; | ||
|
||
when(() => logger.progress(any())).thenReturn(progress); | ||
when( | ||
() => shorebirdFlutter.getVersions(), | ||
).thenAnswer((_) async => [version]); | ||
when( | ||
() => shorebirdFlutter.useVersion(version: any(named: 'version')), | ||
).thenAnswer((_) async {}); | ||
}); | ||
|
||
test('has correct name and description', () { | ||
expect(command.name, equals('use')); | ||
expect(command.description, equals('Use a different Flutter version.')); | ||
}); | ||
|
||
test('exits with code 64 when no version is specified', () async { | ||
when(() => argResults.rest).thenReturn([]); | ||
await expectLater( | ||
runWithOverrides(command.run), | ||
completion(equals(ExitCode.usage.code)), | ||
); | ||
verify( | ||
() => logger.err(''' | ||
No version specified. | ||
Usage: shorebird flutter versions use <version> | ||
Use `shorebird flutter versions list` to list available versions.'''), | ||
).called(1); | ||
}); | ||
|
||
test('exits with code 64 when too many args are provided', () async { | ||
when(() => argResults.rest).thenReturn([version, 'foo']); | ||
await expectLater( | ||
runWithOverrides(command.run), | ||
completion(equals(ExitCode.usage.code)), | ||
); | ||
verify( | ||
() => logger.err(''' | ||
Too many arguments. | ||
Usage: shorebird flutter versions use <version>'''), | ||
).called(1); | ||
}); | ||
|
||
test('exits with code 70 when unable to fetch 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 70 when version is not found', () async { | ||
when(() => shorebirdFlutter.getVersions()).thenAnswer( | ||
(_) async => ['other-version'], | ||
); | ||
await expectLater( | ||
runWithOverrides(command.run), | ||
completion(equals(ExitCode.software.code)), | ||
); | ||
verifyInOrder([ | ||
() => logger.progress('Fetching Flutter versions'), | ||
() => progress.complete(), | ||
() => logger.err(''' | ||
Version $version not found. | ||
Use `shorebird flutter versions list` to list available versions.'''), | ||
]); | ||
}); | ||
|
||
test('exits with code 70 when unable to install version', () async { | ||
when( | ||
() => shorebirdFlutter.useVersion(version: any(named: 'version')), | ||
).thenThrow('error'); | ||
await expectLater( | ||
runWithOverrides(command.run), | ||
completion(equals(ExitCode.software.code)), | ||
); | ||
verifyInOrder([ | ||
() => logger.progress('Fetching Flutter versions'), | ||
() => progress.complete(), | ||
() => logger.progress('Installing Flutter $version'), | ||
() => progress.fail('Failed to install Flutter $version.'), | ||
() => logger.err('error'), | ||
]); | ||
}); | ||
|
||
test('exits with code 0 when install succeeds', () async { | ||
await expectLater( | ||
runWithOverrides(command.run), | ||
completion(equals(ExitCode.success.code)), | ||
); | ||
verifyInOrder([ | ||
() => logger.progress('Fetching Flutter versions'), | ||
() => shorebirdFlutter.getVersions(), | ||
() => progress.complete(), | ||
() => logger.progress('Installing Flutter $version'), | ||
() => shorebirdFlutter.useVersion(version: version), | ||
() => progress.complete(), | ||
]); | ||
}); | ||
}); | ||
} |