@@ -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