Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shorebird_cli): create Git wrapper #1048

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/shorebird_cli/lib/src/git.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'dart:io';

import 'package:shorebird_cli/src/process.dart';

/// A wrapper around all git related functionality.
class Git {
static const executable = 'git';

/// Clones the git repository located at [url] into the [outputDirectory].
/// `git clone <url> ...<args> <outputDirectory>`
Future<void> clone({
required String url,
required String outputDirectory,
List<String>? args,
}) async {
final arguments = [
'clone',
url,
...?args,
outputDirectory,
];
final result = await process.run(
executable,
arguments,
runInShell: true,
);
if (result.exitCode != 0) {
throw ProcessException(
executable,
arguments,
'${result.stderr}',
result.exitCode,
);
}
}
}
97 changes: 97 additions & 0 deletions packages/shorebird_cli/test/src/git_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'dart:io';

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/process.dart';
import 'package:test/test.dart';

class _MockShorebirdProcess extends Mock implements ShorebirdProcess {}

class _MockShorebirdProcessResult extends Mock
implements ShorebirdProcessResult {}

void main() {
group(Git, () {
late ShorebirdProcessResult processResult;
late ShorebirdProcess process;
late Git git;

R runWithOverrides<R>(R Function() body) {
return runScoped(
() => body(),
values: {
processRef.overrideWith(() => process),
},
);
}

setUp(() {
processResult = _MockShorebirdProcessResult();
process = _MockShorebirdProcess();
git = runWithOverrides(Git.new);

when(
() => process.run(any(), any(), runInShell: any(named: 'runInShell')),
).thenAnswer((_) async => processResult);
when(() => processResult.exitCode).thenReturn(ExitCode.success.code);
});

group('clone', () {
const url = 'https://github.com/shorebirdtech/shorebird';
const outputDirectory = './output';

test('executes correct command (no args)', () async {
await runWithOverrides(
() => git.clone(
url: url,
outputDirectory: outputDirectory,
),
);
verify(
() => process.run(
'git',
['clone', url, outputDirectory],
runInShell: true,
),
).called(1);
});

test('executes correct command (with args)', () async {
const args = <String>['--filter-tree:0', '--no-checkout'];
await runWithOverrides(
() => git.clone(
url: url,
args: ['--filter-tree:0', '--no-checkout'],
outputDirectory: outputDirectory,
),
);
verify(
() => process.run(
'git',
['clone', url, ...args, outputDirectory],
runInShell: true,
),
).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.clone(
url: url,
outputDirectory: outputDirectory,
),
),
throwsA(
isA<ProcessException>().having((e) => e.message, 'message', error),
),
);
});
});
});
}