[flutter_plugin_tools] Improve process mocking (#4254)

The mock process runner used in most of the tests had poor handling of
stdout/stderr:
- By default it would return the `List<int>` output from the mock
  process, which was never correct since the parent process runner
  interface always sets encodings, thus the `dynamic` should always be
  of type `String`
- The process for setting output on a `MockProcess` was awkward, since
  it was based on a `Stream<Lint<int>>`, even though in every case what
  we actually want to do is just set the full output to a string.
- A hack was at some point added (presumably due to the above issues)
  to bypass that flow at the process runner level, and instead return a
  `String` set there. That meant there were two ways of setting output
  (one of which that only worked for one of the ways of running
  processes)
  - That hack wasn't updated when the ability to return multiple mock
    processes instead of a single global mock process was added, so the
    API was even more confusing, and there was no way to set different
    output for different processes.

This changes the test APIs so that:
- `MockProcess` takes stdout and stderr as strings, and internally
  manages converting them to a `Stream<List<int>>`.
- `RecordingProcessRunner` correctly decodes and returns the output
  streams when constructing a process result.

It also removes the resultStdout and resultStderr hacks, as well as the
legacy `processToReturn` API, and converts all uses to the new
structure, which is both simpler to use, and clearly associates output
with specific processes.
This commit is contained in:
stuartmorgan
2021-08-24 14:42:21 -04:00
committed by GitHub
parent 5bbc1791cc
commit 74cf0287f9
16 changed files with 199 additions and 193 deletions

View File

@ -60,12 +60,10 @@ void main() {
final String output =
'''${includeBanner ? updateBanner : ''}[${devices.join(',')}]''';
final MockProcess mockDevicesProcess = MockProcess.succeeding();
mockDevicesProcess.stdoutController.close(); // ignore: unawaited_futures
final MockProcess mockDevicesProcess = MockProcess(stdout: output);
processRunner
.mockProcessesForExecutable[getFlutterCommand(mockPlatform)] =
<io.Process>[mockDevicesProcess];
processRunner.resultStdout = output;
}
test('fails if no platforms are provided', () async {
@ -151,7 +149,7 @@ void main() {
// Simulate failure from `flutter devices`.
processRunner
.mockProcessesForExecutable[getFlutterCommand(mockPlatform)] =
<io.Process>[MockProcess.failing()];
<io.Process>[MockProcess(exitCode: 1)];
Error? commandError;
final List<String> output = await runCapturingPrint(
@ -954,8 +952,8 @@ void main() {
.mockProcessesForExecutable[getFlutterCommand(mockPlatform)] =
<io.Process>[
// No mock for 'devices', since it's running for macOS.
MockProcess.failing(), // 'drive' #1
MockProcess.failing(), // 'drive' #2
MockProcess(exitCode: 1), // 'drive' #1
MockProcess(exitCode: 1), // 'drive' #2
];
Error? commandError;