This reverts commit 3597bae6a0.
This commit is contained in:
@@ -9,12 +9,8 @@ import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:process/process.dart';
|
||||
|
||||
import 'common.dart';
|
||||
|
||||
export 'package:process/process.dart' show ProcessManager;
|
||||
|
||||
typedef VoidCallback = void Function();
|
||||
|
||||
/// A command for [FakeProcessManager].
|
||||
@immutable
|
||||
class FakeCommand {
|
||||
@@ -23,29 +19,28 @@ class FakeCommand {
|
||||
this.workingDirectory,
|
||||
this.environment,
|
||||
this.duration = const Duration(),
|
||||
this.onRun,
|
||||
this.exitCode = 0,
|
||||
this.stdout = '',
|
||||
this.stderr = '',
|
||||
@required this.exitCode,
|
||||
@required this.stdout,
|
||||
@required this.stderr,
|
||||
}) : assert(command != null),
|
||||
assert(duration != null),
|
||||
assert(exitCode != null),
|
||||
assert(stdout != null),
|
||||
assert(stderr != null);
|
||||
|
||||
/// The exact commands that must be matched for this [FakeCommand] to be
|
||||
/// considered correct.
|
||||
/// selected from those given to the [FakeProcessManager].
|
||||
final List<String> command;
|
||||
|
||||
/// The exact working directory that must be matched for this [FakeCommand] to
|
||||
/// be considered correct.
|
||||
/// be selected from those given to the [FakeProcessManager].
|
||||
///
|
||||
/// If this is null, the working directory is ignored.
|
||||
/// If this is null, then it matches any working directory.
|
||||
final String workingDirectory;
|
||||
|
||||
/// The environment that must be matched for this [FakeCommand] to be considered correct.
|
||||
/// The environment that must be matched for this [FakeCommand] to be selected
|
||||
/// from those given to the [FakeProcessManager].
|
||||
///
|
||||
/// If this is null, then the environment is ignored.
|
||||
/// 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.
|
||||
@@ -58,10 +53,6 @@ class FakeCommand {
|
||||
/// otherwise the test will be artificially slow.
|
||||
final Duration duration;
|
||||
|
||||
/// A callback that is run after [duration] expires but before the [exitCode]
|
||||
/// (and output) are passed back.
|
||||
final VoidCallback onRun;
|
||||
|
||||
/// The process' exit code.
|
||||
///
|
||||
/// To simulate a never-ending process, set [duration] to a value greated than
|
||||
@@ -119,20 +110,13 @@ class _FakeProcess implements Process {
|
||||
_FakeProcess(
|
||||
this._exitCode,
|
||||
Duration duration,
|
||||
VoidCallback onRun,
|
||||
this.pid,
|
||||
this._stderr,
|
||||
this.stdin,
|
||||
this._stdout,
|
||||
) : exitCode = Future<void>.delayed(duration).then((void value) {
|
||||
if (onRun != null) {
|
||||
onRun();
|
||||
}
|
||||
return _exitCode;
|
||||
}),
|
||||
) : 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
|
||||
@@ -161,42 +145,44 @@ class _FakeProcess implements Process {
|
||||
}
|
||||
}
|
||||
|
||||
abstract class FakeProcessManager implements ProcessManager {
|
||||
/// A fake [ProcessManager] which responds to all commands as if they had run
|
||||
/// instantaneously with an exit code of 0 and no output.
|
||||
factory FakeProcessManager.any() = _FakeAnyProcessManager;
|
||||
/// 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);
|
||||
|
||||
/// 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 next
|
||||
/// [FakeCommand] must match (otherwise the test fails); 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.
|
||||
///
|
||||
/// There is no logic to ensure that all the listed commands are run. Use
|
||||
/// [FakeCommand.onRun] to set a flag, or specify a sentinel command as your
|
||||
/// last command and verify its execution is successful, to ensure that all
|
||||
/// the specified commands are actually called.
|
||||
factory FakeProcessManager.list(List<FakeCommand> commands) = _SequenceProcessManager;
|
||||
final List<FakeCommand> _commands;
|
||||
|
||||
FakeProcessManager._();
|
||||
|
||||
@protected
|
||||
FakeCommand findCommand(List<String> command, String workingDirectory, Map<String, String> environment);
|
||||
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);
|
||||
final FakeCommand fakeCommand = _findCommand(command, workingDirectory, environment);
|
||||
return _FakeProcess(
|
||||
fakeCommand.exitCode,
|
||||
fakeCommand.duration,
|
||||
fakeCommand.onRun,
|
||||
_pid,
|
||||
fakeCommand.stdout,
|
||||
null, // stdin
|
||||
@@ -262,41 +248,3 @@ abstract class FakeProcessManager implements ProcessManager {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class _FakeAnyProcessManager extends FakeProcessManager {
|
||||
_FakeAnyProcessManager() : super._();
|
||||
|
||||
@override
|
||||
FakeCommand findCommand(List<String> command, String workingDirectory, Map<String, String> environment) {
|
||||
return FakeCommand(
|
||||
command: command,
|
||||
workingDirectory: workingDirectory,
|
||||
environment: environment,
|
||||
duration: const Duration(),
|
||||
exitCode: 0,
|
||||
stdout: '',
|
||||
stderr: '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SequenceProcessManager extends FakeProcessManager {
|
||||
_SequenceProcessManager(this._commands) : super._();
|
||||
|
||||
final List<FakeCommand> _commands;
|
||||
|
||||
@override
|
||||
FakeCommand findCommand(List<String> command, String workingDirectory, Map<String, String> environment) {
|
||||
expect(_commands, isNotEmpty,
|
||||
reason: 'ProcessManager was told to execute $command (in $workingDirectory) '
|
||||
'but the FakeProcessManager.list expected no more processes.'
|
||||
);
|
||||
expect(_commands.first._matches(command, workingDirectory, environment), isTrue,
|
||||
reason: 'ProcessManager was told to execute $command '
|
||||
'(in $workingDirectory, with environment $environment) '
|
||||
'but the next process that was expected was ${_commands.first.command} '
|
||||
'(in ${_commands.first.workingDirectory}, with environment ${_commands.first.environment})}.'
|
||||
);
|
||||
return _commands.removeAt(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +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.any(),
|
||||
ProcessManager: () => FakeProcessManager(<FakeCommand>[]),
|
||||
Logger: () => BufferLogger(), // Allows reading logs and prevents stdout.
|
||||
OperatingSystemUtils: () => FakeOperatingSystemUtils(),
|
||||
OutputPreferences: () => OutputPreferences.test(), // configures BufferLogger to avoid color codes.
|
||||
|
||||
Reference in New Issue
Block a user