@@ -123,48 +123,63 @@ class FakeCommand {
123
123
}
124
124
}
125
125
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 > [],
132
140
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
143
155
{
144
156
if (_stderr.isEmpty) {
145
- stderr = const Stream <List <int >>.empty ();
157
+ this . stderr = const Stream <List <int >>.empty ();
146
158
} else if (outputFollowsExit) {
147
159
// 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 ((_) {
149
161
return Future <List <int >>(() => _stderr);
150
162
}));
151
163
} else {
152
- stderr = Stream <List <int >>.value (_stderr);
164
+ this . stderr = Stream <List <int >>.value (_stderr);
153
165
}
154
166
155
167
if (_stdout.isEmpty) {
156
- stdout = const Stream <List <int >>.empty ();
168
+ this . stdout = const Stream <List <int >>.empty ();
157
169
} else if (outputFollowsExit) {
158
170
// 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 ((_) {
160
172
return Future <List <int >>(() => _stdout);
161
173
}));
162
174
} else {
163
- stdout = Stream <List <int >>.value (_stdout);
175
+ this . stdout = Stream <List <int >>.value (_stdout);
164
176
}
165
177
}
166
178
179
+ /// The process exit code.
167
180
final int _exitCode;
181
+
182
+ /// When specified, blocks process exit until completed.
168
183
final Completer <void >? _completer;
169
184
170
185
@override
@@ -173,6 +188,7 @@ class _FakeProcess implements io.Process {
173
188
@override
174
189
final int pid;
175
190
191
+ /// The raw byte content of stderr.
176
192
final List <int > _stderr;
177
193
178
194
@override
@@ -184,6 +200,7 @@ class _FakeProcess implements io.Process {
184
200
@override
185
201
late final Stream <List <int >> stdout;
186
202
203
+ /// The raw byte content of stdout.
187
204
final List <int > _stdout;
188
205
189
206
@override
@@ -231,7 +248,7 @@ abstract class FakeProcessManager implements ProcessManager {
231
248
commands.forEach (addCommand);
232
249
}
233
250
234
- final Map <int , _FakeProcess > _fakeRunningProcesses = < int , _FakeProcess > {};
251
+ final Map <int , FakeProcess > _fakeRunningProcesses = < int , FakeProcess > {};
235
252
236
253
/// Whether this fake has more [FakeCommand] s that are expected to run.
237
254
///
@@ -251,7 +268,7 @@ abstract class FakeProcessManager implements ProcessManager {
251
268
252
269
int _pid = 9999 ;
253
270
254
- _FakeProcess _runCommand (
271
+ FakeProcess _runCommand (
255
272
List <String > command,
256
273
String ? workingDirectory,
257
274
Map <String , String >? environment,
@@ -266,15 +283,15 @@ abstract class FakeProcessManager implements ProcessManager {
266
283
if (fakeCommand.onRun != null ) {
267
284
fakeCommand.onRun !();
268
285
}
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,
278
295
);
279
296
}
280
297
@@ -287,7 +304,7 @@ abstract class FakeProcessManager implements ProcessManager {
287
304
bool runInShell = false , // ignored
288
305
io.ProcessStartMode mode = io.ProcessStartMode .normal, // ignored
289
306
}) {
290
- final _FakeProcess process = _runCommand (command.cast <String >(), workingDirectory, environment, io.systemEncoding);
307
+ final FakeProcess process = _runCommand (command.cast <String >(), workingDirectory, environment, io.systemEncoding);
291
308
if (process._completer != null ) {
292
309
_fakeRunningProcesses[process.pid] = process;
293
310
process.exitCode.whenComplete (() {
@@ -307,7 +324,7 @@ abstract class FakeProcessManager implements ProcessManager {
307
324
Encoding ? stdoutEncoding = io.systemEncoding,
308
325
Encoding ? stderrEncoding = io.systemEncoding,
309
326
}) async {
310
- final _FakeProcess process = _runCommand (command.cast <String >(), workingDirectory, environment, stdoutEncoding);
327
+ final FakeProcess process = _runCommand (command.cast <String >(), workingDirectory, environment, stdoutEncoding);
311
328
await process.exitCode;
312
329
return io.ProcessResult (
313
330
process.pid,
@@ -327,7 +344,7 @@ abstract class FakeProcessManager implements ProcessManager {
327
344
Encoding ? stdoutEncoding = io.systemEncoding,
328
345
Encoding ? stderrEncoding = io.systemEncoding,
329
346
}) {
330
- final _FakeProcess process = _runCommand (command.cast <String >(), workingDirectory, environment, stdoutEncoding);
347
+ final FakeProcess process = _runCommand (command.cast <String >(), workingDirectory, environment, stdoutEncoding);
331
348
return io.ProcessResult (
332
349
process.pid,
333
350
process._exitCode,
@@ -345,7 +362,7 @@ abstract class FakeProcessManager implements ProcessManager {
345
362
@override
346
363
bool killPid (int pid, [io.ProcessSignal signal = io.ProcessSignal .sigterm]) {
347
364
// 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];
349
366
if (fakeProcess == null ) {
350
367
return false ;
351
368
}
0 commit comments