fake_process_manager.dart 10.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:async';
6 7 8 9 10 11
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';
12 13
import 'common.dart';

14 15
export 'package:process/process.dart' show ProcessManager;

16 17
typedef VoidCallback = void Function();

18 19 20 21 22 23 24
/// A command for [FakeProcessManager].
@immutable
class FakeCommand {
  const FakeCommand({
    @required this.command,
    this.workingDirectory,
    this.environment,
25
    this.encoding,
26
    this.duration = const Duration(),
27 28 29 30
    this.onRun,
    this.exitCode = 0,
    this.stdout = '',
    this.stderr = '',
31
    this.completer,
32
    this.stdin,
33 34
  }) : assert(command != null),
       assert(duration != null),
35
       assert(exitCode != null);
36 37

  /// The exact commands that must be matched for this [FakeCommand] to be
38
  /// considered correct.
39 40 41
  final List<String> command;

  /// The exact working directory that must be matched for this [FakeCommand] to
42
  /// be considered correct.
43
  ///
44
  /// If this is null, the working directory is ignored.
45 46
  final String workingDirectory;

47
  /// The environment that must be matched for this [FakeCommand] to be considered correct.
48
  ///
49
  /// If this is null, then the environment is ignored.
50 51 52 53 54
  ///
  /// 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;

55 56 57 58 59 60
  /// The stdout and stderr encoding that must be matched for this [FakeCommand]
  /// to be considered correct.
  ///
  /// If this is null, then the encodings are ignored.
  final Encoding encoding;

61 62 63 64 65 66 67
  /// 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;

68 69 70 71
  /// A callback that is run after [duration] expires but before the [exitCode]
  /// (and output) are passed back.
  final VoidCallback onRun;

72 73
  /// The process' exit code.
  ///
74
  /// To simulate a never-ending process, set [duration] to a value greater than
75 76 77 78 79 80 81 82 83 84 85 86 87 88
  /// 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;

89 90 91 92
  /// If provided, allows the command completion to be blocked until the future
  /// resolves.
  final Completer<void> completer;

93 94 95 96
  /// An optional stdin sink that will be exposed through the resulting
  /// [FakeProcess].
  final IOSink stdin;

97 98 99 100 101 102
  void _matches(
    List<String> command,
    String workingDirectory,
    Map<String, String> environment,
    Encoding encoding,
  ) {
103 104 105
    expect(command, equals(this.command));
    if (this.workingDirectory != null) {
      expect(this.workingDirectory, workingDirectory);
106 107
    }
    if (this.environment != null) {
108
      expect(this.environment, environment);
109
    }
110 111 112
    if (this.encoding != null) {
      expect(this.encoding, encoding);
    }
113 114 115 116 117 118 119
  }
}

class _FakeProcess implements Process {
  _FakeProcess(
    this._exitCode,
    Duration duration,
120
    VoidCallback onRun,
121 122 123 124
    this.pid,
    this._stderr,
    this.stdin,
    this._stdout,
125
    Completer<void> completer,
126 127 128 129
  ) : exitCode = Future<void>.delayed(duration).then((void value) {
        if (onRun != null) {
          onRun();
        }
130 131 132
        if (completer != null) {
          return completer.future.then((void _) => _exitCode);
        }
133 134
        return _exitCode;
      }),
135 136 137 138 139 140
      stderr = _stderr == null
        ? const Stream<List<int>>.empty()
        : Stream<List<int>>.value(utf8.encode(_stderr)),
      stdout = _stdout == null
        ? const Stream<List<int>>.empty()
        : Stream<List<int>>.value(utf8.encode(_stdout));
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  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]) {
165
    // Killing a fake process has no effect.
166 167 168 169
    return false;
  }
}

170 171 172 173
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;
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  /// 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;
191

192 193
  FakeProcessManager._();

194 195 196 197 198 199 200 201
  /// Adds a new [FakeCommand] to the current process manager.
  ///
  /// This can be used to configure test expectations after the [ProcessManager] has been
  /// provided to another interface.
  ///
  /// This is a no-op on [FakeProcessManager.any].
  void addCommand(FakeCommand command);

202 203 204 205 206
  /// Whether this fake has more [FakeCommand]s that are expected to run.
  ///
  /// This is always `true` for [FakeProcessManager.any].
  bool get hasRemainingExpectations;

207
  @protected
208 209 210 211 212 213
  FakeCommand findCommand(
    List<String> command,
    String workingDirectory,
    Map<String, String> environment,
    Encoding encoding,
  );
214 215 216

  int _pid = 9999;

217 218 219 220 221 222
  _FakeProcess _runCommand(
    List<String> command,
    String workingDirectory,
    Map<String, String> environment,
    Encoding encoding,
  ) {
223
    _pid += 1;
224
    final FakeCommand fakeCommand = findCommand(command, workingDirectory, environment, encoding);
225 226 227
    return _FakeProcess(
      fakeCommand.exitCode,
      fakeCommand.duration,
228
      fakeCommand.onRun,
229 230
      _pid,
      fakeCommand.stderr,
231
      fakeCommand.stdin,
232
      fakeCommand.stdout,
233
      fakeCommand.completer,
234 235 236 237 238 239 240 241 242 243 244
    );
  }

  @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
245
  }) async => _runCommand(command.cast<String>(), workingDirectory, environment, systemEncoding);
246 247 248 249 250 251 252 253 254 255 256

  @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 {
257
    final _FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, stdoutEncoding);
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    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
  }) {
277
    final _FakeProcess process = _runCommand(command.cast<String>(), workingDirectory, environment, stdoutEncoding);
278 279 280 281 282 283 284 285 286 287 288 289 290
    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]) {
291
    // Killing a fake process has no effect.
292 293 294
    return false;
  }
}
295 296 297 298 299

class _FakeAnyProcessManager extends FakeProcessManager {
  _FakeAnyProcessManager() : super._();

  @override
300 301 302 303 304 305
  FakeCommand findCommand(
    List<String> command,
    String workingDirectory,
    Map<String, String> environment,
    Encoding encoding,
  ) {
306 307 308 309
    return FakeCommand(
      command: command,
      workingDirectory: workingDirectory,
      environment: environment,
310
      encoding: encoding,
311 312 313 314 315 316
      duration: const Duration(),
      exitCode: 0,
      stdout: '',
      stderr: '',
    );
  }
317 318 319

  @override
  void addCommand(FakeCommand command) { }
320 321 322

  @override
  bool get hasRemainingExpectations => true;
323 324 325 326 327 328 329 330
}

class _SequenceProcessManager extends FakeProcessManager {
  _SequenceProcessManager(this._commands) : super._();

  final List<FakeCommand> _commands;

  @override
331 332 333 334 335 336
  FakeCommand findCommand(
    List<String> command,
    String workingDirectory,
    Map<String, String> environment,
    Encoding encoding,
  ) {
337 338 339 340
    expect(_commands, isNotEmpty,
      reason: 'ProcessManager was told to execute $command (in $workingDirectory) '
              'but the FakeProcessManager.list expected no more processes.'
    );
341
    _commands.first._matches(command, workingDirectory, environment, encoding);
342 343
    return _commands.removeAt(0);
  }
344 345 346 347 348

  @override
  void addCommand(FakeCommand command) {
    _commands.add(command);
  }
349 350 351

  @override
  bool get hasRemainingExpectations => _commands.isNotEmpty;
352
}