From 5cb164107f2860a721f15946760e7a3958ba75be Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Mon, 7 Aug 2023 15:29:50 -0500 Subject: [PATCH] feat(shorebird_cli): add `revParse` to `Git` (#1051) --- packages/shorebird_cli/lib/src/git.dart | 22 +++++++++ packages/shorebird_cli/test/src/git_test.dart | 47 ++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/packages/shorebird_cli/lib/src/git.dart b/packages/shorebird_cli/lib/src/git.dart index d334c76b0..598b7a728 100644 --- a/packages/shorebird_cli/lib/src/git.dart +++ b/packages/shorebird_cli/lib/src/git.dart @@ -68,4 +68,26 @@ class Git { ); } } + + /// Returns the revision of the git repository located at [directory]. + Future revParse({ + required String revision, + required String directory, + }) async { + final arguments = ['rev-parse', '--verify', revision]; + final result = await process.run( + executable, + arguments, + workingDirectory: directory, + ); + if (result.exitCode != 0) { + throw ProcessException( + executable, + arguments, + '${result.stderr}', + result.exitCode, + ); + } + return '${result.stdout}'.trim(); + } } diff --git a/packages/shorebird_cli/test/src/git_test.dart b/packages/shorebird_cli/test/src/git_test.dart index 2c1be3cf6..89b8a8482 100644 --- a/packages/shorebird_cli/test/src/git_test.dart +++ b/packages/shorebird_cli/test/src/git_test.dart @@ -33,7 +33,12 @@ void main() { git = runWithOverrides(Git.new); when( - () => process.run(any(), any(), runInShell: any(named: 'runInShell')), + () => process.run( + any(), + any(), + runInShell: any(named: 'runInShell'), + workingDirectory: any(named: 'workingDirectory'), + ), ).thenAnswer((_) async => processResult); when(() => processResult.exitCode).thenReturn(ExitCode.success.code); }); @@ -135,5 +140,45 @@ void main() { ); }); }); + + group('revParse', () { + const directory = './output'; + const revision = 'revision'; + + test('executes correct command', () async { + const output = 'revision'; + when(() => processResult.stdout).thenReturn(output); + await expectLater( + runWithOverrides( + () => git.revParse(directory: directory, revision: revision), + ), + completion(equals(output)), + ); + verify( + () => process.run( + 'git', + ['rev-parse', '--verify', revision], + 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.revParse( + directory: directory, + revision: revision, + ), + ), + throwsA( + isA().having((e) => e.message, 'message', error), + ), + ); + }); + }); }); }