Always fake ProcessManager when you fake Filesystem in tests (#42369)
...because otherwise, processes that think they're manipulating your filesystem will be doing crazy things the test is ignoring, leading to (at best) failures and (at worst) flakes or disk corruption.
This commit is contained in:
@@ -42,7 +42,7 @@ String getFlutterRoot() {
|
||||
return platform.environment['FLUTTER_ROOT'];
|
||||
}
|
||||
|
||||
Error invalidScript() => StateError('Invalid script: ${platform.script}');
|
||||
Error invalidScript() => StateError('Could not determine flutter_tools/ path from script URL (${platform.script}); consider setting FLUTTER_ROOT explicitly.');
|
||||
|
||||
Uri scriptUri;
|
||||
switch (platform.script.scheme) {
|
||||
|
||||
@@ -30,9 +30,11 @@ import 'package:meta/meta.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import 'common.dart';
|
||||
import 'testbed.dart';
|
||||
import 'fake_process_manager.dart';
|
||||
import 'throwing_pub.dart';
|
||||
|
||||
export 'package:flutter_tools/src/base/context.dart' show Generator;
|
||||
export 'fake_process_manager.dart' show ProcessManager, FakeProcessManager, FakeCommand;
|
||||
|
||||
/// Return the test logger. This assumes that the current Logger is a BufferLogger.
|
||||
BufferLogger get testLogger => context.get<Logger>();
|
||||
@@ -52,6 +54,14 @@ void testUsingContext(
|
||||
String testOn,
|
||||
bool skip, // should default to `false`, but https://github.com/dart-lang/test/issues/545 doesn't allow this
|
||||
}) {
|
||||
if (overrides[FileSystem] != null && overrides[ProcessManager] == null) {
|
||||
throw StateError(
|
||||
'If you override the FileSystem context you must also provide a ProcessManager, '
|
||||
'otherwise the processes you launch will not be dealing with the same file system '
|
||||
'that you are dealing with in your test.'
|
||||
);
|
||||
}
|
||||
|
||||
// Ensure we don't rely on the default [Config] constructor which will
|
||||
// leak a sticky $HOME/.flutter_settings behind!
|
||||
Directory configDir;
|
||||
|
||||
250
packages/flutter_tools/test/src/fake_process_manager.dart
Normal file
250
packages/flutter_tools/test/src/fake_process_manager.dart
Normal file
@@ -0,0 +1,250 @@
|
||||
// Copyright 2016 The Chromium 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:convert';
|
||||
import 'dart:io' as io show ProcessSignal;
|
||||
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:process/process.dart';
|
||||
|
||||
export 'package:process/process.dart' show ProcessManager;
|
||||
|
||||
/// A command for [FakeProcessManager].
|
||||
@immutable
|
||||
class FakeCommand {
|
||||
const FakeCommand({
|
||||
@required this.command,
|
||||
this.workingDirectory,
|
||||
this.environment,
|
||||
this.duration = const Duration(),
|
||||
@required this.exitCode,
|
||||
@required this.stdout,
|
||||
@required this.stderr,
|
||||
}) : assert(command != null),
|
||||
assert(duration != null),
|
||||
assert(stdout != null),
|
||||
assert(stderr != null);
|
||||
|
||||
/// The exact commands that must be matched for this [FakeCommand] to be
|
||||
/// selected from those given to the [FakeProcessManager].
|
||||
final List<String> command;
|
||||
|
||||
/// The exact working directory that must be matched for this [FakeCommand] to
|
||||
/// be selected from those given to the [FakeProcessManager].
|
||||
///
|
||||
/// If this is null, then it matches any working directory.
|
||||
final String workingDirectory;
|
||||
|
||||
/// The environment that must be matched for this [FakeCommand] to be selected
|
||||
/// from those given to the [FakeProcessManager].
|
||||
///
|
||||
/// If this is null, then it matches any environment.
|
||||
///
|
||||
/// Otherwise, each key in this environment must be present and must have a
|
||||
/// value that matches the one given here for the [FakeCommand] to match.
|
||||
final Map<String, String> environment;
|
||||
|
||||
/// The time to allow to elapse before returning the [exitCode], if this command
|
||||
/// is "executed".
|
||||
///
|
||||
/// If you set this to a non-zero time, you should use a [FakeAsync] zone,
|
||||
/// otherwise the test will be artificially slow.
|
||||
final Duration duration;
|
||||
|
||||
/// The process' exit code.
|
||||
///
|
||||
/// To simulate a never-ending process, set [duration] to a value greated than
|
||||
/// 15 minutes (the timeout for our tests).
|
||||
///
|
||||
/// To simulate a crash, subtract the crash signal number from 256. For example,
|
||||
/// SIGPIPE (-13) is 243.
|
||||
final int exitCode;
|
||||
|
||||
/// The output to simulate on stdout. This will be encoded as UTF-8 and
|
||||
/// returned in one go.
|
||||
final String stdout;
|
||||
|
||||
/// The output to simulate on stderr. This will be encoded as UTF-8 and
|
||||
/// returned in one go.
|
||||
final String stderr;
|
||||
|
||||
static bool _listEquals<T>(List<T> a, List<T> b) {
|
||||
if (a == null) {
|
||||
return b == null;
|
||||
}
|
||||
if (b == null || a.length != b.length) {
|
||||
return false;
|
||||
}
|
||||
for (int index = 0; index < a.length; index += 1) {
|
||||
if (a[index] != b[index]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _matches(List<String> command, String workingDirectory, Map<String, String> environment) {
|
||||
if (!_listEquals(command, this.command)) {
|
||||
return false;
|
||||
}
|
||||
if (this.workingDirectory != null && workingDirectory != this.workingDirectory) {
|
||||
return false;
|
||||
}
|
||||
if (this.environment != null) {
|
||||
if (environment == null) {
|
||||
return false;
|
||||
}
|
||||
for (String key in environment.keys) {
|
||||
if (environment[key] != this.environment[key]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class _FakeProcess implements Process {
|
||||
_FakeProcess(
|
||||
this._exitCode,
|
||||
Duration duration,
|
||||
this.pid,
|
||||
this._stderr,
|
||||
this.stdin,
|
||||
this._stdout,
|
||||
) : exitCode = Future<void>.delayed(duration).then((void value) => _exitCode),
|
||||
stderr = Stream<List<int>>.value(utf8.encode(_stderr)),
|
||||
stdout = Stream<List<int>>.value(utf8.encode(_stdout));
|
||||
final int _exitCode;
|
||||
|
||||
@override
|
||||
final Future<int> exitCode;
|
||||
|
||||
@override
|
||||
final int pid;
|
||||
|
||||
final String _stderr;
|
||||
|
||||
@override
|
||||
final Stream<List<int>> stderr;
|
||||
|
||||
@override
|
||||
final IOSink stdin;
|
||||
|
||||
@override
|
||||
final Stream<List<int>> stdout;
|
||||
|
||||
final String _stdout;
|
||||
|
||||
@override
|
||||
bool kill([io.ProcessSignal signal = io.ProcessSignal.sigterm]) {
|
||||
assert(false, 'Process.kill() should not be used directly in flutter_tools.');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// A fake [ProcessManager] which responds to particular commands with particular results.
|
||||
///
|
||||
/// On creation, pass in a list of [FakeCommand] objects. When the [ProcessManager] methods
|
||||
/// such as [start] are invoked, the first matching [FakeCommand] is found and its settings
|
||||
/// are used to simulate the result of running that command.
|
||||
///
|
||||
/// If no command is found, then one is implied which immediately returns exit
|
||||
/// code 0 with no output.
|
||||
class FakeProcessManager implements ProcessManager {
|
||||
FakeProcessManager(this._commands);
|
||||
|
||||
final List<FakeCommand> _commands;
|
||||
|
||||
FakeCommand _findCommand(List<String> command, String workingDirectory, Map<String, String> environment) {
|
||||
for (FakeCommand candidate in _commands) {
|
||||
if (candidate._matches(command, workingDirectory, environment)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return FakeCommand(
|
||||
command: command,
|
||||
workingDirectory: workingDirectory,
|
||||
environment: environment,
|
||||
duration: const Duration(),
|
||||
exitCode: 0,
|
||||
stdout: '',
|
||||
stderr: '',
|
||||
);
|
||||
}
|
||||
|
||||
int _pid = 9999;
|
||||
|
||||
_FakeProcess _runCommand(List<String> command, String workingDirectory, Map<String, String> environment) {
|
||||
_pid += 1;
|
||||
final FakeCommand fakeCommand = _findCommand(command, workingDirectory, environment);
|
||||
return _FakeProcess(
|
||||
fakeCommand.exitCode,
|
||||
fakeCommand.duration,
|
||||
_pid,
|
||||
fakeCommand.stdout,
|
||||
null, // stdin
|
||||
fakeCommand.stderr,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Process> start(
|
||||
List<dynamic> command, {
|
||||
String workingDirectory,
|
||||
Map<String, String> environment,
|
||||
bool includeParentEnvironment = true, // ignored
|
||||
bool runInShell = false, // ignored
|
||||
ProcessStartMode mode = ProcessStartMode.normal, // ignored
|
||||
}) async => _runCommand(command, workingDirectory, environment);
|
||||
|
||||
@override
|
||||
Future<ProcessResult> run(
|
||||
List<dynamic> command, {
|
||||
String workingDirectory,
|
||||
Map<String, String> environment,
|
||||
bool includeParentEnvironment = true, // ignored
|
||||
bool runInShell = false, // ignored
|
||||
Encoding stdoutEncoding = systemEncoding,
|
||||
Encoding stderrEncoding = systemEncoding,
|
||||
}) async {
|
||||
final _FakeProcess process = _runCommand(command, workingDirectory, environment);
|
||||
await process.exitCode;
|
||||
return ProcessResult(
|
||||
process.pid,
|
||||
process._exitCode,
|
||||
stdoutEncoding == null ? process.stdout : await stdoutEncoding.decodeStream(process.stdout),
|
||||
stderrEncoding == null ? process.stderr : await stderrEncoding.decodeStream(process.stderr),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
ProcessResult runSync(
|
||||
List<dynamic> command, {
|
||||
String workingDirectory,
|
||||
Map<String, String> environment,
|
||||
bool includeParentEnvironment = true, // ignored
|
||||
bool runInShell = false, // ignored
|
||||
Encoding stdoutEncoding = systemEncoding, // actual encoder is ignored
|
||||
Encoding stderrEncoding = systemEncoding, // actual encoder is ignored
|
||||
}) {
|
||||
final _FakeProcess process = _runCommand(command, workingDirectory, environment);
|
||||
return ProcessResult(
|
||||
process.pid,
|
||||
process._exitCode,
|
||||
stdoutEncoding == null ? utf8.encode(process._stdout) : process._stdout,
|
||||
stderrEncoding == null ? utf8.encode(process._stderr) : process._stderr,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool canRun(dynamic executable, {String workingDirectory}) => true;
|
||||
|
||||
@override
|
||||
bool killPid(int pid, [io.ProcessSignal signal = io.ProcessSignal.sigterm]) {
|
||||
assert(false, 'ProcessManager.killPid() should not be used directly in flutter_tools.');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -22,8 +22,11 @@ import 'package:flutter_tools/src/dart/pub.dart';
|
||||
import 'package:flutter_tools/src/features.dart';
|
||||
import 'package:flutter_tools/src/reporting/reporting.dart';
|
||||
import 'package:flutter_tools/src/version.dart';
|
||||
import 'package:process/process.dart';
|
||||
|
||||
import 'context.dart';
|
||||
import 'fake_process_manager.dart';
|
||||
import 'throwing_pub.dart';
|
||||
|
||||
export 'package:flutter_tools/src/base/context.dart' show Generator;
|
||||
|
||||
@@ -32,6 +35,7 @@ export 'package:flutter_tools/src/base/context.dart' show Generator;
|
||||
final Map<Type, Generator> _testbedDefaults = <Type, Generator>{
|
||||
// Keeps tests fast by avoiding the actual file system.
|
||||
FileSystem: () => MemoryFileSystem(style: platform.isWindows ? FileSystemStyle.windows : FileSystemStyle.posix),
|
||||
ProcessManager: () => FakeProcessManager(<FakeCommand>[]),
|
||||
Logger: () => BufferLogger(), // Allows reading logs and prevents stdout.
|
||||
OperatingSystemUtils: () => FakeOperatingSystemUtils(),
|
||||
OutputPreferences: () => OutputPreferences.test(), // configures BufferLogger to avoid color codes.
|
||||
@@ -730,35 +734,3 @@ class TestFeatureFlags implements FeatureFlags {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class ThrowingPub implements Pub {
|
||||
@override
|
||||
Future<void> batch(List<String> arguments, {
|
||||
PubContext context,
|
||||
String directory,
|
||||
MessageFilter filter,
|
||||
String failureMessage = 'pub failed',
|
||||
bool retry,
|
||||
bool showTraceForErrors,
|
||||
}) {
|
||||
throw UnsupportedError('Attempted to inovke pub during test.');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> get({
|
||||
PubContext context,
|
||||
String directory,
|
||||
bool skipIfAbsent = false,
|
||||
bool upgrade = false,
|
||||
bool offline = false,
|
||||
bool checkLastModified = true,
|
||||
bool skipPubspecYamlCheck = false,
|
||||
}) {
|
||||
throw UnsupportedError('Attempted to inovke pub during test.');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> interactively(List<String> arguments, {String directory}) {
|
||||
throw UnsupportedError('Attempted to inovke pub during test.');
|
||||
}
|
||||
}
|
||||
|
||||
39
packages/flutter_tools/test/src/throwing_pub.dart
Normal file
39
packages/flutter_tools/test/src/throwing_pub.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright 2019 The Chromium 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:flutter_tools/src/dart/pub.dart';
|
||||
|
||||
class ThrowingPub implements Pub {
|
||||
@override
|
||||
Future<void> batch(List<String> arguments, {
|
||||
PubContext context,
|
||||
String directory,
|
||||
MessageFilter filter,
|
||||
String failureMessage = 'pub failed',
|
||||
bool retry,
|
||||
bool showTraceForErrors,
|
||||
}) {
|
||||
throw UnsupportedError('Attempted to inovke pub during test.');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> get({
|
||||
PubContext context,
|
||||
String directory,
|
||||
bool skipIfAbsent = false,
|
||||
bool upgrade = false,
|
||||
bool offline = false,
|
||||
bool checkLastModified = true,
|
||||
bool skipPubspecYamlCheck = false,
|
||||
}) {
|
||||
throw UnsupportedError('Attempted to inovke pub during test.');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> interactively(List<String> arguments, {String directory}) {
|
||||
throw UnsupportedError('Attempted to inovke pub during test.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user