Skip to content

Commit 9d9e272

Browse files
authored
[tool] Add tests for FakeProcess (#104013)
Because this class has some subtle behaviour with regards to control of exit timing and when and how it streams data to stderr and stdout, it's worth adding unit tests for this class directly, as well as (in a followup patch) for FakeProcessManager. This is additional testing relating to refactoring landed in: flutter/flutter#103947 Issue: flutter/flutter#102451
1 parent fc9e84d commit 9d9e272

File tree

2 files changed

+229
-37
lines changed

2 files changed

+229
-37
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'package:fake_async/fake_async.dart';
7+
8+
import '../src/common.dart';
9+
import '../src/fake_process_manager.dart';
10+
11+
void main() {
12+
group(FakeProcess, () {
13+
testWithoutContext('exits with specified exit code', () async {
14+
final FakeProcess process = FakeProcess(exitCode: 42);
15+
expect(await process.exitCode, 42);
16+
});
17+
18+
testWithoutContext('exits with specified stderr, stdout', () async {
19+
final FakeProcess process = FakeProcess(
20+
stderr: 'stderr\u{FFFD}'.codeUnits,
21+
stdout: 'stdout\u{FFFD}'.codeUnits,
22+
);
23+
await process.exitCode;
24+
25+
// Verify that no encoding changes have been applied to output.
26+
//
27+
// In the past, we had hardcoded UTF-8 encoding for these streams in
28+
// FakeProcess. When a specific encoding is desired, it can be specified
29+
// on FakeCommand or in the encoding parameter of FakeProcessManager.run
30+
// or FakeProcessManager.runAsync.
31+
expect((await process.stderr.toList()).expand((List<int> x) => x), 'stderr\u{FFFD}'.codeUnits);
32+
expect((await process.stdout.toList()).expand((List<int> x) => x), 'stdout\u{FFFD}'.codeUnits);
33+
});
34+
35+
testWithoutContext('exits after specified delay (if no completer specified)', () {
36+
final bool done = FakeAsync().run<bool>((FakeAsync time) {
37+
final FakeProcess process = FakeProcess(
38+
duration: const Duration(seconds: 30),
39+
);
40+
41+
bool hasExited = false;
42+
unawaited(process.exitCode.then((int _) { hasExited = true; }));
43+
44+
// Verify process hasn't exited before specified delay.
45+
time.elapse(const Duration(seconds: 15));
46+
expect(hasExited, isFalse);
47+
48+
// Verify process has exited after specified delay.
49+
time.elapse(const Duration(seconds: 20));
50+
expect(hasExited, isTrue);
51+
52+
return true;
53+
});
54+
expect(done, isTrue);
55+
});
56+
57+
testWithoutContext('exits when completer completes (if no duration specified)', () {
58+
final bool done = FakeAsync().run<bool>((FakeAsync time) {
59+
final Completer<void> completer = Completer<void>();
60+
final FakeProcess process = FakeProcess(
61+
completer: completer,
62+
);
63+
64+
bool hasExited = false;
65+
unawaited(process.exitCode.then((int _) { hasExited = true; }));
66+
67+
// Verify process hasn't exited when all async tasks flushed.
68+
time.elapse(Duration.zero);
69+
expect(hasExited, isFalse);
70+
71+
// Verify process has exited after completer completes.
72+
completer.complete();
73+
time.flushMicrotasks();
74+
expect(hasExited, isTrue);
75+
76+
return true;
77+
});
78+
expect(done, isTrue);
79+
});
80+
81+
testWithoutContext('when completer and duration are specified, does not exit until completer is completed', () {
82+
final bool done = FakeAsync().run<bool>((FakeAsync time) {
83+
final Completer<void> completer = Completer<void>();
84+
final FakeProcess process = FakeProcess(
85+
duration: const Duration(seconds: 30),
86+
completer: completer,
87+
);
88+
89+
bool hasExited = false;
90+
unawaited(process.exitCode.then((int _) { hasExited = true; }));
91+
92+
// Verify process hasn't exited before specified delay.
93+
time.elapse(const Duration(seconds: 15));
94+
expect(hasExited, isFalse);
95+
96+
// Verify process hasn't exited until the completer completes.
97+
time.elapse(const Duration(seconds: 20));
98+
expect(hasExited, isFalse);
99+
100+
// Verify process exits after the completer completes.
101+
completer.complete();
102+
time.flushMicrotasks();
103+
expect(hasExited, isTrue);
104+
105+
return true;
106+
});
107+
expect(done, isTrue);
108+
});
109+
110+
testWithoutContext('when completer and duration are specified, does not exit until duration has elapsed', () {
111+
final bool done = FakeAsync().run<bool>((FakeAsync time) {
112+
final Completer<void> completer = Completer<void>();
113+
final FakeProcess process = FakeProcess(
114+
duration: const Duration(seconds: 30),
115+
completer: completer,
116+
);
117+
118+
bool hasExited = false;
119+
unawaited(process.exitCode.then((int _) { hasExited = true; }));
120+
121+
// Verify process hasn't exited before specified delay.
122+
time.elapse(const Duration(seconds: 15));
123+
expect(hasExited, isFalse);
124+
125+
// Verify process does not exit until duration has elapsed.
126+
completer.complete();
127+
expect(hasExited, isFalse);
128+
129+
// Verify process exits after the duration elapses.
130+
time.elapse(const Duration(seconds: 20));
131+
expect(hasExited, isTrue);
132+
133+
return true;
134+
});
135+
expect(done, isTrue);
136+
});
137+
138+
testWithoutContext('process exit is asynchronous', () async {
139+
final FakeProcess process = FakeProcess();
140+
141+
bool hasExited = false;
142+
unawaited(process.exitCode.then((int _) { hasExited = true; }));
143+
144+
// Verify process hasn't completed.
145+
expect(hasExited, isFalse);
146+
147+
// Flush async tasks. Verify process completes.
148+
await Future<void>.delayed(Duration.zero);
149+
expect(hasExited, isTrue);
150+
});
151+
152+
testWithoutContext('stderr, stdout stream data after exit when outputFollowsExit is true', () async {
153+
final FakeProcess process = FakeProcess(
154+
stderr: 'stderr'.codeUnits,
155+
stdout: 'stdout'.codeUnits,
156+
outputFollowsExit: true,
157+
);
158+
159+
final List<int> stderr = <int>[];
160+
final List<int> stdout = <int>[];
161+
process.stderr.listen(stderr.addAll);
162+
process.stdout.listen(stdout.addAll);
163+
164+
// Ensure that no bytes have been received at process exit.
165+
await process.exitCode;
166+
expect(stderr, isEmpty);
167+
expect(stdout, isEmpty);
168+
169+
// Flush all remaining async work. Ensure stderr, stdout is received.
170+
await Future<void>.delayed(Duration.zero);
171+
expect(stderr, 'stderr'.codeUnits);
172+
expect(stdout, 'stdout'.codeUnits);
173+
});
174+
});
175+
}

packages/flutter_tools/test/src/fake_process_manager.dart

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -123,48 +123,63 @@ class FakeCommand {
123123
}
124124
}
125125

126-
class _FakeProcess implements io.Process {
127-
_FakeProcess(
128-
this._exitCode,
129-
Duration duration,
130-
this.pid,
131-
this._stderr,
126+
/// A fake process for use with [FakeProcessManager].
127+
///
128+
/// The process delays exit until both [duration] (if specified) has elapsed
129+
/// and [completer] (if specified) has completed.
130+
///
131+
/// When [outputFollowsExit] is specified, bytes are streamed to [stderr] and
132+
/// [stdout] after the process exits.
133+
@visibleForTesting
134+
class FakeProcess implements io.Process {
135+
FakeProcess({
136+
int exitCode = 0,
137+
Duration duration = Duration.zero,
138+
this.pid = 1234,
139+
List<int> stderr = const <int>[],
132140
IOSink? stdin,
133-
this._stdout,
134-
this._completer,
135-
bool outputFollowsExit,
136-
) : exitCode = Future<void>.delayed(duration).then((void value) {
137-
if (_completer != null) {
138-
return _completer.future.then((void _) => _exitCode);
139-
}
140-
return _exitCode;
141-
}),
142-
stdin = stdin ?? IOSink(StreamController<List<int>>().sink)
141+
List<int> stdout = const <int>[],
142+
Completer<void>? completer,
143+
bool outputFollowsExit = false,
144+
}) : _exitCode = exitCode,
145+
exitCode = Future<void>.delayed(duration).then((void value) {
146+
if (completer != null) {
147+
return completer.future.then((void _) => exitCode);
148+
}
149+
return exitCode;
150+
}),
151+
_stderr = stderr,
152+
stdin = stdin ?? IOSink(StreamController<List<int>>().sink),
153+
_stdout = stdout,
154+
_completer = completer
143155
{
144156
if (_stderr.isEmpty) {
145-
stderr = const Stream<List<int>>.empty();
157+
this.stderr = const Stream<List<int>>.empty();
146158
} else if (outputFollowsExit) {
147159
// Wait for the process to exit before emitting stderr.
148-
stderr = Stream<List<int>>.fromFuture(exitCode.then((_) {
160+
this.stderr = Stream<List<int>>.fromFuture(this.exitCode.then((_) {
149161
return Future<List<int>>(() => _stderr);
150162
}));
151163
} else {
152-
stderr = Stream<List<int>>.value(_stderr);
164+
this.stderr = Stream<List<int>>.value(_stderr);
153165
}
154166

155167
if (_stdout.isEmpty) {
156-
stdout = const Stream<List<int>>.empty();
168+
this.stdout = const Stream<List<int>>.empty();
157169
} else if (outputFollowsExit) {
158170
// Wait for the process to exit before emitting stdout.
159-
stdout = Stream<List<int>>.fromFuture(exitCode.then((_) {
171+
this.stdout = Stream<List<int>>.fromFuture(this.exitCode.then((_) {
160172
return Future<List<int>>(() => _stdout);
161173
}));
162174
} else {
163-
stdout = Stream<List<int>>.value(_stdout);
175+
this.stdout = Stream<List<int>>.value(_stdout);
164176
}
165177
}
166178

179+
/// The process exit code.
167180
final int _exitCode;
181+
182+
/// When specified, blocks process exit until completed.
168183
final Completer<void>? _completer;
169184

170185
@override
@@ -173,6 +188,7 @@ class _FakeProcess implements io.Process {
173188
@override
174189
final int pid;
175190

191+
/// The raw byte content of stderr.
176192
final List<int> _stderr;
177193

178194
@override
@@ -184,6 +200,7 @@ class _FakeProcess implements io.Process {
184200
@override
185201
late final Stream<List<int>> stdout;
186202

203+
/// The raw byte content of stdout.
187204
final List<int> _stdout;
188205

189206
@override
@@ -231,7 +248,7 @@ abstract class FakeProcessManager implements ProcessManager {
231248
commands.forEach(addCommand);
232249
}
233250

234-
final Map<int, _FakeProcess> _fakeRunningProcesses = <int, _FakeProcess>{};
251+
final Map<int, FakeProcess> _fakeRunningProcesses = <int, FakeProcess>{};
235252

236253
/// Whether this fake has more [FakeCommand]s that are expected to run.
237254
///
@@ -251,7 +268,7 @@ abstract class FakeProcessManager implements ProcessManager {
251268

252269
int _pid = 9999;
253270

254-
_FakeProcess _runCommand(
271+
FakeProcess _runCommand(
255272
List<String> command,
256273
String? workingDirectory,
257274
Map<String, String>? environment,
@@ -266,15 +283,15 @@ abstract class FakeProcessManager implements ProcessManager {
266283
if (fakeCommand.onRun != null) {
267284
fakeCommand.onRun!();
268285
}
269-
return _FakeProcess(
270-
fakeCommand.exitCode,
271-
fakeCommand.duration,
272-
_pid,
273-
encoding?.encode(fakeCommand.stderr) ?? fakeCommand.stderr.codeUnits,
274-
fakeCommand.stdin,
275-
encoding?.encode(fakeCommand.stdout) ?? fakeCommand.stdout.codeUnits,
276-
fakeCommand.completer,
277-
fakeCommand.outputFollowsExit,
286+
return FakeProcess(
287+
duration: fakeCommand.duration,
288+
exitCode: fakeCommand.exitCode,
289+
pid: _pid,
290+
stderr: encoding?.encode(fakeCommand.stderr) ?? fakeCommand.stderr.codeUnits,
291+
stdin: fakeCommand.stdin,
292+
stdout: encoding?.encode(fakeCommand.stdout) ?? fakeCommand.stdout.codeUnits,
293+
completer: fakeCommand.completer,
294+
outputFollowsExit: fakeCommand.outputFollowsExit,
278295
);
279296
}
280297

@@ -287,7 +304,7 @@ abstract class FakeProcessManager implements ProcessManager {
287304
bool runInShell = false, // ignored
288305
io.ProcessStartMode mode = io.ProcessStartMode.normal, // ignored
289306
}) {
290-
final _FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, io.systemEncoding);
307+
final FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, io.systemEncoding);
291308
if (process._completer != null) {
292309
_fakeRunningProcesses[process.pid] = process;
293310
process.exitCode.whenComplete(() {
@@ -307,7 +324,7 @@ abstract class FakeProcessManager implements ProcessManager {
307324
Encoding? stdoutEncoding = io.systemEncoding,
308325
Encoding? stderrEncoding = io.systemEncoding,
309326
}) async {
310-
final _FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, stdoutEncoding);
327+
final FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, stdoutEncoding);
311328
await process.exitCode;
312329
return io.ProcessResult(
313330
process.pid,
@@ -327,7 +344,7 @@ abstract class FakeProcessManager implements ProcessManager {
327344
Encoding? stdoutEncoding = io.systemEncoding,
328345
Encoding? stderrEncoding = io.systemEncoding,
329346
}) {
330-
final _FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, stdoutEncoding);
347+
final FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, stdoutEncoding);
331348
return io.ProcessResult(
332349
process.pid,
333350
process._exitCode,
@@ -345,7 +362,7 @@ abstract class FakeProcessManager implements ProcessManager {
345362
@override
346363
bool killPid(int pid, [io.ProcessSignal signal = io.ProcessSignal.sigterm]) {
347364
// Killing a fake process has no effect unless it has an attached completer.
348-
final _FakeProcess? fakeProcess = _fakeRunningProcesses[pid];
365+
final FakeProcess? fakeProcess = _fakeRunningProcesses[pid];
349366
if (fakeProcess == null) {
350367
return false;
351368
}

0 commit comments

Comments
 (0)