Skip to content

Commit

Permalink
refactor(git_cli): stub process.run in unit tests (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Jun 24, 2022
1 parent 4725542 commit 991e138
Showing 1 changed file with 55 additions and 13 deletions.
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

0 comments on commit 991e138

Please sign in to comment.