Skip to content

Commit

Permalink
feat(shorebird_cli): add revParse to Git (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Aug 7, 2023
1 parent 4e9b202 commit 5cb1641
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
22 changes: 22 additions & 0 deletions packages/shorebird_cli/lib/src/git.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,26 @@ class Git {
);
}
}

/// Returns the revision of the git repository located at [directory].
Future<String> 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();
}
}
47 changes: 46 additions & 1 deletion packages/shorebird_cli/test/src/git_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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<ProcessException>().having((e) => e.message, 'message', error),
),
);
});
});
});
}

0 comments on commit 5cb1641

Please sign in to comment.