refactor(shorebird_cli): rename ShorebirdVersionManager to ShorebirdVersion (#1061)

This commit is contained in:
Felix Angelov
2023-08-08 13:03:42 -05:00
committed by GitHub
parent 00da93e0de
commit fb5bcac11e
7 changed files with 46 additions and 62 deletions
+2 -2
View File
@@ -21,7 +21,7 @@ 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/shorebird_validator.dart';
import 'package:shorebird_cli/src/shorebird_version_manager.dart';
import 'package:shorebird_cli/src/shorebird_version.dart';
import 'package:shorebird_cli/src/xcodebuild.dart';
Future<void> main(List<String> args) async {
@@ -49,7 +49,7 @@ Future<void> main(List<String> args) async {
shorebirdEnvRef,
shorebirdFlutterRef,
shorebirdValidatorRef,
shorebirdVersionManagerRef,
shorebirdVersionRef,
xcodeBuildRef,
},
),
@@ -4,7 +4,7 @@ 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';
import 'package:shorebird_cli/src/shorebird_version_manager.dart';
import 'package:shorebird_cli/src/shorebird_version.dart';
/// {@template upgrade_command}
/// `shorebird upgrade`
@@ -28,7 +28,7 @@ class UpgradeCommand extends ShorebirdCommand {
late final String currentVersion;
try {
currentVersion = await shorebirdVersionManager.fetchCurrentGitHash();
currentVersion = await shorebirdVersion.fetchCurrentGitHash();
} on ProcessException catch (error) {
updateCheckProgress.fail();
logger.err('Fetching current version failed: ${error.message}');
@@ -37,7 +37,7 @@ class UpgradeCommand extends ShorebirdCommand {
late final String latestVersion;
try {
latestVersion = await shorebirdVersionManager.fetchLatestGitHash();
latestVersion = await shorebirdVersion.fetchLatestGitHash();
} on ProcessException catch (error) {
updateCheckProgress.fail();
logger.err('Checking for updates failed: ${error.message}');
@@ -55,7 +55,7 @@ class UpgradeCommand extends ShorebirdCommand {
final updateProgress = logger.progress('Updating');
try {
await shorebirdVersionManager.attemptReset(newRevision: latestVersion);
await shorebirdVersion.attemptReset(revision: latestVersion);
} on ProcessException catch (error) {
updateProgress.fail();
logger.err('Updating failed: ${error.message}');
@@ -4,21 +4,20 @@ import 'package:path/path.dart' as p;
import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/git.dart';
/// A reference to a [ShorebirdVersionManager] instance.
final shorebirdVersionManagerRef = create(ShorebirdVersionManager.new);
/// A reference to a [ShorebirdVersion] instance.
final shorebirdVersionRef = create(ShorebirdVersion.new);
/// The [ShorebirdVersionManager] instance available in the current zone.
ShorebirdVersionManager get shorebirdVersionManager =>
read(shorebirdVersionManagerRef);
/// The [ShorebirdVersion] instance available in the current zone.
ShorebirdVersion get shorebirdVersion => read(shorebirdVersionRef);
/// {@template shorebird_version_manager}
/// Provides information about installed and available versions of Shorebird.
/// {@endtemplate}
class ShorebirdVersionManager {
class ShorebirdVersion {
String get _workingDirectory => p.dirname(Platform.script.toFilePath());
/// Whether the current version of Shorebird is the latest available.
Future<bool> isShorebirdVersionCurrent() async {
Future<bool> isLatest() async {
final currentVersion = await fetchCurrentGitHash();
final latestVersion = await fetchLatestGitHash();
@@ -48,11 +47,9 @@ class ShorebirdVersionManager {
/// This is a reset instead of fast forward because if we are on a release
/// branch with cherry picks, there may not be a direct fast-forward route
/// to the next release.
Future<void> attemptReset({
required String newRevision,
}) async {
Future<void> attemptReset({required String revision}) async {
return git.reset(
revision: newRevision,
revision: revision,
directory: _workingDirectory,
args: ['--hard'],
);
@@ -1,6 +1,6 @@
import 'dart:io';
import 'package:shorebird_cli/src/shorebird_version_manager.dart';
import 'package:shorebird_cli/src/shorebird_version.dart';
import 'package:shorebird_cli/src/validators/validators.dart';
/// Verifies that the currently installed version of Shorebird is the latest.
@@ -18,8 +18,7 @@ class ShorebirdVersionValidator extends Validator {
final bool isShorebirdUpToDate;
try {
isShorebirdUpToDate =
await shorebirdVersionManager.isShorebirdVersionCurrent();
isShorebirdUpToDate = await shorebirdVersion.isLatest();
} on ProcessException catch (e) {
return [
ValidationIssue(
@@ -6,7 +6,7 @@ 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:shorebird_cli/src/shorebird_version_manager.dart';
import 'package:shorebird_cli/src/shorebird_version.dart';
import 'package:test/test.dart';
class _MockLogger extends Mock implements Logger {}
@@ -15,8 +15,7 @@ class _MockProgress extends Mock implements Progress {}
class _MockShorebirdFlutter extends Mock implements ShorebirdFlutter {}
class _MockShorebirdVersionManager extends Mock
implements ShorebirdVersionManager {}
class _MockShorebirdVersion extends Mock implements ShorebirdVersion {}
void main() {
const currentShorebirdRevision = 'revision-1';
@@ -25,7 +24,7 @@ void main() {
group('upgrade', () {
late Logger logger;
late ShorebirdFlutter shorebirdFlutter;
late ShorebirdVersionManager shorebirdVersionManager;
late ShorebirdVersion shorebirdVersion;
late UpgradeCommand command;
R runWithOverrides<R>(R Function() body) {
@@ -34,9 +33,7 @@ void main() {
values: {
loggerRef.overrideWith(() => logger),
shorebirdFlutterRef.overrideWith(() => shorebirdFlutter),
shorebirdVersionManagerRef.overrideWith(
() => shorebirdVersionManager,
),
shorebirdVersionRef.overrideWith(() => shorebirdVersion),
},
);
}
@@ -47,7 +44,7 @@ void main() {
logger = _MockLogger();
shorebirdFlutter = _MockShorebirdFlutter();
shorebirdVersionManager = _MockShorebirdVersionManager();
shorebirdVersion = _MockShorebirdVersion();
command = runWithOverrides(UpgradeCommand.new);
when(
@@ -56,15 +53,13 @@ void main() {
),
).thenAnswer((_) async {});
when(
shorebirdVersionManager.fetchCurrentGitHash,
shorebirdVersion.fetchCurrentGitHash,
).thenAnswer((_) async => currentShorebirdRevision);
when(
shorebirdVersionManager.fetchLatestGitHash,
shorebirdVersion.fetchLatestGitHash,
).thenAnswer((_) async => newerShorebirdRevision);
when(
() => shorebirdVersionManager.attemptReset(
newRevision: any(named: 'newRevision'),
),
() => shorebirdVersion.attemptReset(revision: any(named: 'revision')),
).thenAnswer((_) async => {});
when(() => progress.complete(any())).thenAnswer((_) {
@@ -83,7 +78,7 @@ void main() {
'handles errors when determining the current version',
() async {
const errorMessage = 'oops';
when(shorebirdVersionManager.fetchCurrentGitHash).thenThrow(
when(shorebirdVersion.fetchCurrentGitHash).thenThrow(
const ProcessException(
'git',
['rev-parse'],
@@ -105,7 +100,7 @@ void main() {
'handles errors when determining the latest version',
() async {
const errorMessage = 'oops';
when(shorebirdVersionManager.fetchLatestGitHash).thenThrow(
when(shorebirdVersion.fetchLatestGitHash).thenThrow(
const ProcessException(
'git',
['rev-parse'],
@@ -124,9 +119,7 @@ void main() {
test('handles errors when updating', () async {
const errorMessage = 'oops';
when(
() => shorebirdVersionManager.attemptReset(
newRevision: any(named: 'newRevision'),
),
() => shorebirdVersion.attemptReset(revision: any(named: 'revision')),
).thenThrow(const ProcessException('git', ['reset'], errorMessage));
final result = await runWithOverrides(command.run);
@@ -171,7 +164,7 @@ void main() {
test(
'does not update when already on latest version',
() async {
when(shorebirdVersionManager.fetchLatestGitHash)
when(shorebirdVersion.fetchLatestGitHash)
.thenAnswer((_) async => currentShorebirdRevision);
when(() => logger.progress(any())).thenReturn(_MockProgress());
@@ -4,18 +4,18 @@ import 'package:mason_logger/mason_logger.dart';
import 'package:mocktail/mocktail.dart';
import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/git.dart';
import 'package:shorebird_cli/src/shorebird_version_manager.dart';
import 'package:shorebird_cli/src/shorebird_version.dart';
import 'package:test/test.dart';
class _MockGit extends Mock implements Git {}
void main() {
group(ShorebirdVersionManager, () {
group(ShorebirdVersion, () {
const currentShorebirdRevision = 'revision-1';
const newerShorebirdRevision = 'revision-2';
late Git git;
late ShorebirdVersionManager shorebirdVersionManager;
late ShorebirdVersion shorebirdVersionManager;
R runWithOverrides<R>(R Function() body) {
return runScoped(
@@ -28,7 +28,7 @@ void main() {
setUp(() {
git = _MockGit();
shorebirdVersionManager = ShorebirdVersionManager();
shorebirdVersionManager = ShorebirdVersion();
when(
() => git.fetch(
@@ -61,7 +61,7 @@ void main() {
test('returns true if current and latest git hashes match', () async {
expect(
await runWithOverrides(
shorebirdVersionManager.isShorebirdVersionCurrent,
shorebirdVersionManager.isLatest,
),
isTrue,
);
@@ -100,7 +100,7 @@ void main() {
expect(
await runWithOverrides(
shorebirdVersionManager.isShorebirdVersionCurrent,
shorebirdVersionManager.isLatest,
),
isFalse,
);
@@ -141,7 +141,7 @@ void main() {
expect(
runWithOverrides(
shorebirdVersionManager.isShorebirdVersionCurrent,
shorebirdVersionManager.isLatest,
),
throwsA(
isA<ProcessException>().having(
@@ -158,7 +158,7 @@ void main() {
test('completes when git command exits with code 0', () async {
expect(
runWithOverrides(
() => shorebirdVersionManager.attemptReset(newRevision: 'HEAD'),
() => shorebirdVersionManager.attemptReset(revision: 'HEAD'),
),
completes,
);
@@ -184,7 +184,7 @@ void main() {
expect(
runWithOverrides(
() => shorebirdVersionManager.attemptReset(newRevision: 'HEAD'),
() => shorebirdVersionManager.attemptReset(revision: 'HEAD'),
),
throwsA(
isA<ProcessException>().having(
@@ -2,35 +2,32 @@ import 'dart:io';
import 'package:mocktail/mocktail.dart';
import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/shorebird_version_manager.dart';
import 'package:shorebird_cli/src/shorebird_version.dart';
import 'package:shorebird_cli/src/validators/validators.dart';
import 'package:test/test.dart';
class _MockShorebirdVersionManager extends Mock
implements ShorebirdVersionManager {}
class _MockShorebirdVersion extends Mock implements ShorebirdVersion {}
void main() {
group('ShorebirdVersionValidator', () {
late ShorebirdVersionManager shorebirdVersionManager;
late ShorebirdVersion shorebirdVersion;
late ShorebirdVersionValidator validator;
R runWithOverrides<R>(R Function() body) {
return runScoped(
body,
values: {
shorebirdVersionManagerRef
.overrideWith(() => shorebirdVersionManager),
shorebirdVersionRef.overrideWith(() => shorebirdVersion),
},
);
}
setUp(() {
shorebirdVersionManager = _MockShorebirdVersionManager();
shorebirdVersion = _MockShorebirdVersion();
validator = ShorebirdVersionValidator();
when(
shorebirdVersionManager.isShorebirdVersionCurrent,
shorebirdVersion.isLatest,
).thenAnswer((_) async => false);
});
@@ -43,8 +40,7 @@ void main() {
});
test('returns no issues when shorebird is up-to-date', () async {
when(shorebirdVersionManager.isShorebirdVersionCurrent)
.thenAnswer((_) async => true);
when(shorebirdVersion.isLatest).thenAnswer((_) async => true);
final results = await runWithOverrides(validator.validate);
@@ -53,7 +49,7 @@ void main() {
test('retursn an error when shorebird version cannot be determined',
() async {
when(shorebirdVersionManager.isShorebirdVersionCurrent).thenThrow(
when(shorebirdVersion.isLatest).thenThrow(
const ProcessException('git', ['rev-parse', 'HEAD']),
);
@@ -68,8 +64,7 @@ void main() {
});
test('returns a warning when a newer shorebird is available', () async {
when(shorebirdVersionManager.isShorebirdVersionCurrent)
.thenAnswer((_) async => false);
when(shorebirdVersion.isLatest).thenAnswer((_) async => false);
final results = await runWithOverrides(validator.validate);