Skip to content

Commit 991e138

Browse files
authored
refactor(git_cli): stub process.run in unit tests (#441)
1 parent 4725542 commit 991e138

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

test/src/cli/git_cli_test.dart

+55-13
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,75 @@
1+
import 'package:mason/mason.dart';
2+
import 'package:mocktail/mocktail.dart';
13
import 'package:test/test.dart';
4+
import 'package:universal_io/io.dart';
25
import 'package:very_good_cli/src/cli/cli.dart';
36

7+
class _TestProcess {
8+
Future<ProcessResult> run(
9+
String command,
10+
List<String> args, {
11+
bool runInShell = false,
12+
String? workingDirectory,
13+
}) {
14+
throw UnimplementedError();
15+
}
16+
}
17+
18+
class _MockProcess extends Mock implements _TestProcess {}
19+
20+
class _MockProcessResult extends Mock implements ProcessResult {}
21+
422
void main() {
523
group('Git', () {
24+
late ProcessResult processResult;
25+
late _TestProcess process;
26+
27+
setUp(() {
28+
processResult = _MockProcessResult();
29+
process = _MockProcess();
30+
when(() => processResult.exitCode).thenReturn(ExitCode.success.code);
31+
when(
32+
() => process.run(
33+
any(),
34+
any(),
35+
runInShell: any(named: 'runInShell'),
36+
workingDirectory: any(named: 'workingDirectory'),
37+
),
38+
).thenAnswer((_) async => processResult);
39+
});
40+
641
group('reachable', () {
742
test('completes for a reachable remote', () async {
8-
await expectLater(
9-
Git.reachable(
10-
Uri.parse('https://github.com/verygoodopensource/very_good_cli'),
11-
),
12-
completes,
43+
await ProcessOverrides.runZoned(
44+
() async {
45+
await expectLater(
46+
Git.reachable(Uri.parse('https://github.com/org/repo')),
47+
completes,
48+
);
49+
},
50+
runProcess: process.run,
1351
);
1452
});
1553

16-
test('throws UnreachableGitDependency for an unreachable remote',
54+
test(
55+
'throws UnreachableGitDependency '
56+
'for an unreachable remote', () async {
57+
when(() => processResult.exitCode).thenReturn(ExitCode.software.code);
58+
await ProcessOverrides.runZoned(
1759
() async {
18-
await expectLater(
19-
Git.reachable(
20-
Uri.parse('https://github.com/verygoodopensource/_very_good_cli'),
21-
),
22-
throwsA(isA<UnreachableGitDependency>()),
60+
await expectLater(
61+
Git.reachable(Uri.parse('https://github.com/org/repo')),
62+
throwsA(isA<UnreachableGitDependency>()),
63+
);
64+
},
65+
runProcess: process.run,
2366
);
2467
});
2568
});
2669

2770
group('UnreachableGitDependency', () {
2871
test('has correct toString override', () {
29-
final remote =
30-
Uri.parse('https://github.com/verygoodopensource/_very_good_cli');
72+
final remote = Uri.parse('https://github.com/org/repo');
3173
final exception = UnreachableGitDependency(remote: remote);
3274
expect(
3375
exception.toString(),

0 commit comments

Comments
 (0)