From e21c104f2cdf18d408b91d9a810f3e40d6db4741 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Tue, 8 Aug 2023 16:16:13 -0500 Subject: [PATCH] feat(shorebird_cli): add `useVersion` to `ShorebirdFlutter` (#1069) --- .../lib/src/shorebird_flutter.dart | 14 ++++ .../test/src/shorebird_env_test.dart | 8 +- .../test/src/shorebird_flutter_test.dart | 74 +++++++++++++++++++ 3 files changed, 92 insertions(+), 4 deletions(-) diff --git a/packages/shorebird_cli/lib/src/shorebird_flutter.dart b/packages/shorebird_cli/lib/src/shorebird_flutter.dart index d72381ad..f69042ca 100644 --- a/packages/shorebird_cli/lib/src/shorebird_flutter.dart +++ b/packages/shorebird_cli/lib/src/shorebird_flutter.dart @@ -72,4 +72,18 @@ class ShorebirdFlutter { .map((e) => e.replaceFirst('origin/flutter_release/', '')) .toList(); } + + Future useVersion({required String version}) async { + final revision = await git.revParse( + revision: 'origin/flutter_release/$version', + directory: _workingDirectory(), + ); + + final targetDirectory = Directory(_workingDirectory(revision: revision)); + if (!targetDirectory.existsSync()) { + await installRevision(revision: revision); + } + + shorebirdEnv.flutterRevision = revision; + } } diff --git a/packages/shorebird_cli/test/src/shorebird_env_test.dart b/packages/shorebird_cli/test/src/shorebird_env_test.dart index ed9e4182..81731505 100644 --- a/packages/shorebird_cli/test/src/shorebird_env_test.dart +++ b/packages/shorebird_cli/test/src/shorebird_env_test.dart @@ -399,8 +399,8 @@ test-revision ..createSync(recursive: true) ..writeAsStringSync(revision, flush: true); final snapshot = File( - p.join(shorebirdRoot.path, 'bin', 'cache', 'shorebird.snapshot')) - ..createSync(recursive: true); + p.join(shorebirdRoot.path, 'bin', 'cache', 'shorebird.snapshot'), + )..createSync(recursive: true); expect( runWithOverrides(() => shorebirdEnv.flutterRevision), @@ -429,8 +429,8 @@ test-revision ..createSync(recursive: true) ..writeAsStringSync(revision, flush: true); final snapshot = File( - p.join(shorebirdRoot.path, 'bin', 'cache', 'shorebird.snapshot')) - ..createSync(recursive: true); + p.join(shorebirdRoot.path, 'bin', 'cache', 'shorebird.snapshot'), + )..createSync(recursive: true); expect( runWithOverrides(() => shorebirdEnv.flutterRevision), diff --git a/packages/shorebird_cli/test/src/shorebird_flutter_test.dart b/packages/shorebird_cli/test/src/shorebird_flutter_test.dart index e2213558..703bdbf1 100644 --- a/packages/shorebird_cli/test/src/shorebird_flutter_test.dart +++ b/packages/shorebird_cli/test/src/shorebird_flutter_test.dart @@ -64,6 +64,12 @@ void main() { args: ['--untracked-files=no', '--porcelain'], ), ).thenAnswer((_) async => ''); + when( + () => git.revParse( + revision: any(named: 'revision'), + directory: any(named: 'directory'), + ), + ).thenAnswer((_) async => flutterRevision); when(() => shorebirdEnv.flutterDirectory).thenReturn(flutterDirectory); when(() => shorebirdEnv.flutterRevision).thenReturn(flutterRevision); }); @@ -352,5 +358,73 @@ origin/flutter_release/3.10.6'''; ); }); }); + + group('useVersion', () { + const version = '3.10.0'; + const newRevision = 'new-revision'; + + setUp(() { + when( + () => git.revParse( + revision: any(named: 'revision'), + directory: any(named: 'directory'), + ), + ).thenAnswer((_) async => newRevision); + }); + + test('installs revision if it does not exist', () async { + await expectLater( + runWithOverrides( + () => shorebirdFlutterManager.useVersion(version: version), + ), + completes, + ); + verify( + () => git.revParse( + revision: 'origin/flutter_release/$version', + directory: p.join(flutterDirectory.parent.path, flutterRevision), + ), + ).called(1); + verify( + () => git.clone( + url: ShorebirdFlutter.flutterGitUrl, + outputDirectory: p.join( + flutterDirectory.parent.path, + newRevision, + ), + args: ['--filter=tree:0', '--no-checkout'], + ), + ).called(1); + verify(() => shorebirdEnv.flutterRevision = newRevision).called(1); + }); + + test('skips installation if revision already exists', () async { + Directory(p.join(flutterDirectory.parent.path, newRevision)) + .createSync(recursive: true); + await expectLater( + runWithOverrides( + () => shorebirdFlutterManager.useVersion(version: version), + ), + completes, + ); + verify( + () => git.revParse( + revision: 'origin/flutter_release/$version', + directory: p.join(flutterDirectory.parent.path, flutterRevision), + ), + ).called(1); + verifyNever( + () => git.clone( + url: ShorebirdFlutter.flutterGitUrl, + outputDirectory: p.join( + flutterDirectory.parent.path, + newRevision, + ), + args: ['--filter=tree:0', '--no-checkout'], + ), + ); + verify(() => shorebirdEnv.flutterRevision = newRevision).called(1); + }); + }); }); }