From 0cb686db6de35ad35d2c4279e1c3d4546fd0d026 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Mon, 7 Aug 2023 15:33:52 -0500 Subject: [PATCH] feat(shorebird_cli): add `fetch` to `Git` --- packages/shorebird_cli/lib/src/git.dart | 18 +++++++ packages/shorebird_cli/test/src/git_test.dart | 48 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/packages/shorebird_cli/lib/src/git.dart b/packages/shorebird_cli/lib/src/git.dart index 598b7a728..3704f871c 100644 --- a/packages/shorebird_cli/lib/src/git.dart +++ b/packages/shorebird_cli/lib/src/git.dart @@ -69,6 +69,24 @@ class Git { } } + /// Fetch branches/tags from the repository at [directory]. + Future fetch({required String directory, List? args}) async { + final arguments = ['fetch', ...?args]; + final result = await process.run( + executable, + arguments, + workingDirectory: directory, + ); + if (result.exitCode != 0) { + throw ProcessException( + executable, + arguments, + '${result.stderr}', + result.exitCode, + ); + } + } + /// Returns the revision of the git repository located at [directory]. Future revParse({ required String revision, diff --git a/packages/shorebird_cli/test/src/git_test.dart b/packages/shorebird_cli/test/src/git_test.dart index 89b8a8482..c3b8d152a 100644 --- a/packages/shorebird_cli/test/src/git_test.dart +++ b/packages/shorebird_cli/test/src/git_test.dart @@ -141,6 +141,54 @@ void main() { }); }); + group('fetch', () { + const directory = 'repository'; + test('executes correct command', () async { + await expectLater( + runWithOverrides( + () => git.fetch(directory: directory), + ), + completes, + ); + verify( + () => process.run( + 'git', + ['fetch'], + workingDirectory: directory, + ), + ).called(1); + }); + + test('executes correct command w/args', () async { + final args = ['--tags']; + await expectLater( + runWithOverrides( + () => git.fetch(directory: directory, args: args), + ), + completes, + ); + verify( + () => process.run( + 'git', + ['fetch', ...args], + workingDirectory: directory, + ), + ).called(1); + }); + + test('throws ProcessException if process exits with error', () async { + const error = 'oops'; + when(() => processResult.exitCode).thenReturn(ExitCode.software.code); + when(() => processResult.stderr).thenReturn(error); + expect( + () => runWithOverrides(() => git.fetch(directory: directory)), + throwsA( + isA().having((e) => e.message, 'message', error), + ), + ); + }); + }); + group('revParse', () { const directory = './output'; const revision = 'revision';