From fb5bcac11e02fb7b3a1b448b683b84f7b7dedcd2 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Tue, 8 Aug 2023 13:03:42 -0500 Subject: [PATCH] refactor(shorebird_cli): rename `ShorebirdVersionManager` to `ShorebirdVersion` (#1061) --- packages/shorebird_cli/bin/shorebird.dart | 4 +-- .../lib/src/commands/upgrade_command.dart | 8 ++--- ...on_manager.dart => shorebird_version.dart} | 19 +++++------- .../shorebird_version_validator.dart | 5 ++- .../src/commands/upgrade_command_test.dart | 31 +++++++------------ .../src/shorebird_version_manager_test.dart | 18 +++++------ .../shorebird_version_validator_test.dart | 23 ++++++-------- 7 files changed, 46 insertions(+), 62 deletions(-) rename packages/shorebird_cli/lib/src/{shorebird_version_manager.dart => shorebird_version.dart} (77%) diff --git a/packages/shorebird_cli/bin/shorebird.dart b/packages/shorebird_cli/bin/shorebird.dart index c90ff796..769e7392 100644 --- a/packages/shorebird_cli/bin/shorebird.dart +++ b/packages/shorebird_cli/bin/shorebird.dart @@ -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 main(List args) async { @@ -49,7 +49,7 @@ Future main(List args) async { shorebirdEnvRef, shorebirdFlutterRef, shorebirdValidatorRef, - shorebirdVersionManagerRef, + shorebirdVersionRef, xcodeBuildRef, }, ), diff --git a/packages/shorebird_cli/lib/src/commands/upgrade_command.dart b/packages/shorebird_cli/lib/src/commands/upgrade_command.dart index 7be32b59..2893cb55 100644 --- a/packages/shorebird_cli/lib/src/commands/upgrade_command.dart +++ b/packages/shorebird_cli/lib/src/commands/upgrade_command.dart @@ -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}'); diff --git a/packages/shorebird_cli/lib/src/shorebird_version_manager.dart b/packages/shorebird_cli/lib/src/shorebird_version.dart similarity index 77% rename from packages/shorebird_cli/lib/src/shorebird_version_manager.dart rename to packages/shorebird_cli/lib/src/shorebird_version.dart index 3300f6b1..fea9e054 100644 --- a/packages/shorebird_cli/lib/src/shorebird_version_manager.dart +++ b/packages/shorebird_cli/lib/src/shorebird_version.dart @@ -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 isShorebirdVersionCurrent() async { + Future 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 attemptReset({ - required String newRevision, - }) async { + Future attemptReset({required String revision}) async { return git.reset( - revision: newRevision, + revision: revision, directory: _workingDirectory, args: ['--hard'], ); diff --git a/packages/shorebird_cli/lib/src/validators/shorebird_version_validator.dart b/packages/shorebird_cli/lib/src/validators/shorebird_version_validator.dart index 4a486eb1..3fb60b56 100644 --- a/packages/shorebird_cli/lib/src/validators/shorebird_version_validator.dart +++ b/packages/shorebird_cli/lib/src/validators/shorebird_version_validator.dart @@ -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( diff --git a/packages/shorebird_cli/test/src/commands/upgrade_command_test.dart b/packages/shorebird_cli/test/src/commands/upgrade_command_test.dart index d66cfea7..68421b8f 100644 --- a/packages/shorebird_cli/test/src/commands/upgrade_command_test.dart +++ b/packages/shorebird_cli/test/src/commands/upgrade_command_test.dart @@ -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 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()); diff --git a/packages/shorebird_cli/test/src/shorebird_version_manager_test.dart b/packages/shorebird_cli/test/src/shorebird_version_manager_test.dart index dbe1fa87..7e82191a 100644 --- a/packages/shorebird_cli/test/src/shorebird_version_manager_test.dart +++ b/packages/shorebird_cli/test/src/shorebird_version_manager_test.dart @@ -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 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().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().having( diff --git a/packages/shorebird_cli/test/src/validators/shorebird_version_validator_test.dart b/packages/shorebird_cli/test/src/validators/shorebird_version_validator_test.dart index 005a9c63..959d3c76 100644 --- a/packages/shorebird_cli/test/src/validators/shorebird_version_validator_test.dart +++ b/packages/shorebird_cli/test/src/validators/shorebird_version_validator_test.dart @@ -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 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);