Unverified Commit 9d9e272e authored by Chris Bracken's avatar Chris Bracken Committed by GitHub

[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:
https://github.com/flutter/flutter/pull/103947

Issue: https://github.com/flutter/flutter/issues/102451
parent fc9e84da
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:fake_async/fake_async.dart';
import '../src/common.dart';
import '../src/fake_process_manager.dart';
void main() {
group(FakeProcess, () {
testWithoutContext('exits with specified exit code', () async {
final FakeProcess process = FakeProcess(exitCode: 42);
expect(await process.exitCode, 42);
});
testWithoutContext('exits with specified stderr, stdout', () async {
final FakeProcess process = FakeProcess(
stderr: 'stderr\u{FFFD}'.codeUnits,
stdout: 'stdout\u{FFFD}'.codeUnits,
);
await process.exitCode;
// Verify that no encoding changes have been applied to output.
//
// In the past, we had hardcoded UTF-8 encoding for these streams in
// FakeProcess. When a specific encoding is desired, it can be specified
// on FakeCommand or in the encoding parameter of FakeProcessManager.run
// or FakeProcessManager.runAsync.
expect((await process.stderr.toList()).expand((List<int> x) => x), 'stderr\u{FFFD}'.codeUnits);
expect((await process.stdout.toList()).expand((List<int> x) => x), 'stdout\u{FFFD}'.codeUnits);
});
testWithoutContext('exits after specified delay (if no completer specified)', () {
final bool done = FakeAsync().run<bool>((FakeAsync time) {
final FakeProcess process = FakeProcess(
duration: const Duration(seconds: 30),
);
bool hasExited = false;
unawaited(process.exitCode.then((int _) { hasExited = true; }));
// Verify process hasn't exited before specified delay.
time.elapse(const Duration(seconds: 15));
expect(hasExited, isFalse);
// Verify process has exited after specified delay.
time.elapse(const Duration(seconds: 20));
expect(hasExited, isTrue);
return true;
});
expect(done, isTrue);
});
testWithoutContext('exits when completer completes (if no duration specified)', () {
final bool done = FakeAsync().run<bool>((FakeAsync time) {
final Completer<void> completer = Completer<void>();
final FakeProcess process = FakeProcess(
completer: completer,
);
bool hasExited = false;
unawaited(process.exitCode.then((int _) { hasExited = true; }));
// Verify process hasn't exited when all async tasks flushed.
time.elapse(Duration.zero);
expect(hasExited, isFalse);
// Verify process has exited after completer completes.
completer.complete();
time.flushMicrotasks();
expect(hasExited, isTrue);
return true;
});
expect(done, isTrue);
});
testWithoutContext('when completer and duration are specified, does not exit until completer is completed', () {
final bool done = FakeAsync().run<bool>((FakeAsync time) {
final Completer<void> completer = Completer<void>();
final FakeProcess process = FakeProcess(
duration: const Duration(seconds: 30),
completer: completer,
);
bool hasExited = false;
unawaited(process.exitCode.then((int _) { hasExited = true; }));
// Verify process hasn't exited before specified delay.
time.elapse(const Duration(seconds: 15));
expect(hasExited, isFalse);
// Verify process hasn't exited until the completer completes.
time.elapse(const Duration(seconds: 20));
expect(hasExited, isFalse);
// Verify process exits after the completer completes.
completer.complete();
time.flushMicrotasks();
expect(hasExited, isTrue);
return true;
});
expect(done, isTrue);
});
testWithoutContext('when completer and duration are specified, does not exit until duration has elapsed', () {
final bool done = FakeAsync().run<bool>((FakeAsync time) {
final Completer<void> completer = Completer<void>();
final FakeProcess process = FakeProcess(
duration: const Duration(seconds: 30),
completer: completer,
);
bool hasExited = false;
unawaited(process.exitCode.then((int _) { hasExited = true; }));
// Verify process hasn't exited before specified delay.
time.elapse(const Duration(seconds: 15));
expect(hasExited, isFalse);
// Verify process does not exit until duration has elapsed.
completer.complete();
expect(hasExited, isFalse);
// Verify process exits after the duration elapses.
time.elapse(const Duration(seconds: 20));
expect(hasExited, isTrue);
return true;
});
expect(done, isTrue);
});
testWithoutContext('process exit is asynchronous', () async {
final FakeProcess process = FakeProcess();
bool hasExited = false;
unawaited(process.exitCode.then((int _) { hasExited = true; }));
// Verify process hasn't completed.
expect(hasExited, isFalse);
// Flush async tasks. Verify process completes.
await Future<void>.delayed(Duration.zero);
expect(hasExited, isTrue);
});
testWithoutContext('stderr, stdout stream data after exit when outputFollowsExit is true', () async {
final FakeProcess process = FakeProcess(
stderr: 'stderr'.codeUnits,
stdout: 'stdout'.codeUnits,
outputFollowsExit: true,
);
final List<int> stderr = <int>[];
final List<int> stdout = <int>[];
process.stderr.listen(stderr.addAll);
process.stdout.listen(stdout.addAll);
// Ensure that no bytes have been received at process exit.
await process.exitCode;
expect(stderr, isEmpty);
expect(stdout, isEmpty);
// Flush all remaining async work. Ensure stderr, stdout is received.
await Future<void>.delayed(Duration.zero);
expect(stderr, 'stderr'.codeUnits);
expect(stdout, 'stdout'.codeUnits);
});
});
}
......@@ -123,48 +123,63 @@ class FakeCommand {
}
}
class _FakeProcess implements io.Process {
_FakeProcess(
this._exitCode,
Duration duration,
this.pid,
this._stderr,
/// A fake process for use with [FakeProcessManager].
///
/// The process delays exit until both [duration] (if specified) has elapsed
/// and [completer] (if specified) has completed.
///
/// When [outputFollowsExit] is specified, bytes are streamed to [stderr] and
/// [stdout] after the process exits.
@visibleForTesting
class FakeProcess implements io.Process {
FakeProcess({
int exitCode = 0,
Duration duration = Duration.zero,
this.pid = 1234,
List<int> stderr = const <int>[],
IOSink? stdin,
this._stdout,
this._completer,
bool outputFollowsExit,
) : exitCode = Future<void>.delayed(duration).then((void value) {
if (_completer != null) {
return _completer.future.then((void _) => _exitCode);
}
return _exitCode;
}),
stdin = stdin ?? IOSink(StreamController<List<int>>().sink)
List<int> stdout = const <int>[],
Completer<void>? completer,
bool outputFollowsExit = false,
}) : _exitCode = exitCode,
exitCode = Future<void>.delayed(duration).then((void value) {
if (completer != null) {
return completer.future.then((void _) => exitCode);
}
return exitCode;
}),
_stderr = stderr,
stdin = stdin ?? IOSink(StreamController<List<int>>().sink),
_stdout = stdout,
_completer = completer
{
if (_stderr.isEmpty) {
stderr = const Stream<List<int>>.empty();
this.stderr = const Stream<List<int>>.empty();
} else if (outputFollowsExit) {
// Wait for the process to exit before emitting stderr.
stderr = Stream<List<int>>.fromFuture(exitCode.then((_) {
this.stderr = Stream<List<int>>.fromFuture(this.exitCode.then((_) {
return Future<List<int>>(() => _stderr);
}));
} else {
stderr = Stream<List<int>>.value(_stderr);
this.stderr = Stream<List<int>>.value(_stderr);
}
if (_stdout.isEmpty) {
stdout = const Stream<List<int>>.empty();
this.stdout = const Stream<List<int>>.empty();
} else if (outputFollowsExit) {
// Wait for the process to exit before emitting stdout.
stdout = Stream<List<int>>.fromFuture(exitCode.then((_) {
this.stdout = Stream<List<int>>.fromFuture(this.exitCode.then((_) {
return Future<List<int>>(() => _stdout);
}));
} else {
stdout = Stream<List<int>>.value(_stdout);
this.stdout = Stream<List<int>>.value(_stdout);
}
}
/// The process exit code.
final int _exitCode;
/// When specified, blocks process exit until completed.
final Completer<void>? _completer;
@override
......@@ -173,6 +188,7 @@ class _FakeProcess implements io.Process {
@override
final int pid;
/// The raw byte content of stderr.
final List<int> _stderr;
@override
......@@ -184,6 +200,7 @@ class _FakeProcess implements io.Process {
@override
late final Stream<List<int>> stdout;
/// The raw byte content of stdout.
final List<int> _stdout;
@override
......@@ -231,7 +248,7 @@ abstract class FakeProcessManager implements ProcessManager {
commands.forEach(addCommand);
}
final Map<int, _FakeProcess> _fakeRunningProcesses = <int, _FakeProcess>{};
final Map<int, FakeProcess> _fakeRunningProcesses = <int, FakeProcess>{};
/// Whether this fake has more [FakeCommand]s that are expected to run.
///
......@@ -251,7 +268,7 @@ abstract class FakeProcessManager implements ProcessManager {
int _pid = 9999;
_FakeProcess _runCommand(
FakeProcess _runCommand(
List<String> command,
String? workingDirectory,
Map<String, String>? environment,
......@@ -266,15 +283,15 @@ abstract class FakeProcessManager implements ProcessManager {
if (fakeCommand.onRun != null) {
fakeCommand.onRun!();
}
return _FakeProcess(
fakeCommand.exitCode,
fakeCommand.duration,
_pid,
encoding?.encode(fakeCommand.stderr) ?? fakeCommand.stderr.codeUnits,
fakeCommand.stdin,
encoding?.encode(fakeCommand.stdout) ?? fakeCommand.stdout.codeUnits,
fakeCommand.completer,
fakeCommand.outputFollowsExit,
return FakeProcess(
duration: fakeCommand.duration,
exitCode: fakeCommand.exitCode,
pid: _pid,
stderr: encoding?.encode(fakeCommand.stderr) ?? fakeCommand.stderr.codeUnits,
stdin: fakeCommand.stdin,
stdout: encoding?.encode(fakeCommand.stdout) ?? fakeCommand.stdout.codeUnits,
completer: fakeCommand.completer,
outputFollowsExit: fakeCommand.outputFollowsExit,
);
}
......@@ -287,7 +304,7 @@ abstract class FakeProcessManager implements ProcessManager {
bool runInShell = false, // ignored
io.ProcessStartMode mode = io.ProcessStartMode.normal, // ignored
}) {
final _FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, io.systemEncoding);
final FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, io.systemEncoding);
if (process._completer != null) {
_fakeRunningProcesses[process.pid] = process;
process.exitCode.whenComplete(() {
......@@ -307,7 +324,7 @@ abstract class FakeProcessManager implements ProcessManager {
Encoding? stdoutEncoding = io.systemEncoding,
Encoding? stderrEncoding = io.systemEncoding,
}) async {
final _FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, stdoutEncoding);
final FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, stdoutEncoding);
await process.exitCode;
return io.ProcessResult(
process.pid,
......@@ -327,7 +344,7 @@ abstract class FakeProcessManager implements ProcessManager {
Encoding? stdoutEncoding = io.systemEncoding,
Encoding? stderrEncoding = io.systemEncoding,
}) {
final _FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, stdoutEncoding);
final FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, stdoutEncoding);
return io.ProcessResult(
process.pid,
process._exitCode,
......@@ -345,7 +362,7 @@ abstract class FakeProcessManager implements ProcessManager {
@override
bool killPid(int pid, [io.ProcessSignal signal = io.ProcessSignal.sigterm]) {
// Killing a fake process has no effect unless it has an attached completer.
final _FakeProcess? fakeProcess = _fakeRunningProcesses[pid];
final FakeProcess? fakeProcess = _fakeRunningProcesses[pid];
if (fakeProcess == null) {
return false;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment