Files
packages/script/tool/test/mocks.dart
stuartmorgan 74cf0287f9 [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.
2021-08-24 11:42:21 -07:00

87 lines
2.2 KiB
Dart

// Copyright 2013 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 'dart:convert';
import 'dart:io' as io;
import 'package:file/file.dart';
import 'package:mockito/mockito.dart';
import 'package:platform/platform.dart';
class MockPlatform extends Mock implements Platform {
MockPlatform({
this.isLinux = false,
this.isMacOS = false,
this.isWindows = false,
});
@override
bool isLinux;
@override
bool isMacOS;
@override
bool isWindows;
@override
Uri get script => isWindows
? Uri.file(r'C:\foo\bar', windows: true)
: Uri.file('/foo/bar', windows: false);
}
class MockProcess extends Mock implements io.Process {
/// Creates a mock process with the given results.
///
/// The default encodings match the ProcessRunner defaults; mocks for
/// processes run with a different encoding will need to be created with
/// the matching encoding.
MockProcess({
int exitCode = 0,
String? stdout,
String? stderr,
Encoding stdoutEncoding = io.systemEncoding,
Encoding stderrEncoding = io.systemEncoding,
}) : _exitCode = exitCode {
if (stdout != null) {
_stdoutController.add(stdoutEncoding.encoder.convert(stdout));
}
if (stderr != null) {
_stderrController.add(stderrEncoding.encoder.convert(stderr));
}
_stdoutController.close();
_stderrController.close();
}
final int _exitCode;
final StreamController<List<int>> _stdoutController =
StreamController<List<int>>();
final StreamController<List<int>> _stderrController =
StreamController<List<int>>();
final MockIOSink stdinMock = MockIOSink();
@override
int get pid => 99;
@override
Future<int> get exitCode async => _exitCode;
@override
Stream<List<int>> get stdout => _stdoutController.stream;
@override
Stream<List<int>> get stderr => _stderrController.stream;
@override
IOSink get stdin => stdinMock;
}
class MockIOSink extends Mock implements IOSink {
List<String> lines = <String>[];
@override
void writeln([Object? obj = '']) => lines.add(obj.toString());
}