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

refactor(git_cli): stub process.run in unit tests #441

Merged
merged 1 commit into from
Jun 24, 2022
Merged
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
68 changes: 55 additions & 13 deletions test/src/cli/git_cli_test.dart
Original file line number Diff line number Diff line change
@@ -1,33 +1,75 @@
import 'package:mason/mason.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
import 'package:universal_io/io.dart';
import 'package:very_good_cli/src/cli/cli.dart';

class _TestProcess {
Future<ProcessResult> run(
String command,
List<String> args, {
bool runInShell = false,
String? workingDirectory,
}) {
throw UnimplementedError();
}
}

class _MockProcess extends Mock implements _TestProcess {}

class _MockProcessResult extends Mock implements ProcessResult {}

void main() {
group('Git', () {
late ProcessResult processResult;
late _TestProcess process;

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

group('reachable', () {
test('completes for a reachable remote', () async {
await expectLater(
Git.reachable(
Uri.parse('https://github.com/verygoodopensource/very_good_cli'),
),
completes,
await ProcessOverrides.runZoned(
() async {
await expectLater(
Git.reachable(Uri.parse('https://github.com/org/repo')),
completes,
);
},
runProcess: process.run,
);
});

test('throws UnreachableGitDependency for an unreachable remote',
test(
'throws UnreachableGitDependency '
'for an unreachable remote', () async {
when(() => processResult.exitCode).thenReturn(ExitCode.software.code);
await ProcessOverrides.runZoned(
() async {
await expectLater(
Git.reachable(
Uri.parse('https://github.com/verygoodopensource/_very_good_cli'),
),
throwsA(isA<UnreachableGitDependency>()),
await expectLater(
Git.reachable(Uri.parse('https://github.com/org/repo')),
throwsA(isA<UnreachableGitDependency>()),
);
},
runProcess: process.run,
);
});
});

group('UnreachableGitDependency', () {
test('has correct toString override', () {
final remote =
Uri.parse('https://github.com/verygoodopensource/_very_good_cli');
final remote = Uri.parse('https://github.com/org/repo');
final exception = UnreachableGitDependency(remote: remote);
expect(
exception.toString(),
Expand Down