fake_process_manager_test.dart 11.3 KB
Newer Older
1 2 3 4 5
// Copyright 2014 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';
6

7
import 'package:fake_async/fake_async.dart';
8 9
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/convert.dart' show utf8;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

import '../src/common.dart';
import '../src/fake_process_manager.dart';

void main() {
  group(FakeProcess, () {
    testWithoutContext('exits with specified exit code', () async {
      final FakeProcess process = FakeProcess(exitCode: 42);
      expect(await process.exitCode, 42);
    });

    testWithoutContext('exits with specified stderr, stdout', () async {
      final FakeProcess process = FakeProcess(
        stderr: 'stderr\u{FFFD}'.codeUnits,
        stdout: 'stdout\u{FFFD}'.codeUnits,
      );
      await process.exitCode;

      // Verify that no encoding changes have been applied to output.
      //
      // In the past, we had hardcoded UTF-8 encoding for these streams in
      // FakeProcess. When a specific encoding is desired, it can be specified
      // on FakeCommand or in the encoding parameter of FakeProcessManager.run
      // or FakeProcessManager.runAsync.
      expect((await process.stderr.toList()).expand((List<int> x) => x), 'stderr\u{FFFD}'.codeUnits);
      expect((await process.stdout.toList()).expand((List<int> x) => x), 'stdout\u{FFFD}'.codeUnits);
    });

    testWithoutContext('exits after specified delay (if no completer specified)', () {
      final bool done = FakeAsync().run<bool>((FakeAsync time) {
        final FakeProcess process = FakeProcess(
          duration: const Duration(seconds: 30),
        );

        bool hasExited = false;
        unawaited(process.exitCode.then((int _) { hasExited = true; }));

        // Verify process hasn't exited before specified delay.
        time.elapse(const Duration(seconds: 15));
        expect(hasExited, isFalse);

        // Verify process has exited after specified delay.
        time.elapse(const Duration(seconds: 20));
        expect(hasExited, isTrue);

        return true;
      });
      expect(done, isTrue);
    });

    testWithoutContext('exits when completer completes (if no duration specified)', () {
      final bool done = FakeAsync().run<bool>((FakeAsync time) {
        final Completer<void> completer = Completer<void>();
        final FakeProcess process = FakeProcess(
          completer: completer,
        );

        bool hasExited = false;
        unawaited(process.exitCode.then((int _) { hasExited = true; }));

        // Verify process hasn't exited when all async tasks flushed.
        time.elapse(Duration.zero);
        expect(hasExited, isFalse);

        // Verify process has exited after completer completes.
        completer.complete();
        time.flushMicrotasks();
        expect(hasExited, isTrue);

        return true;
      });
      expect(done, isTrue);
    });

    testWithoutContext('when completer and duration are specified, does not exit until completer is completed', () {
      final bool done = FakeAsync().run<bool>((FakeAsync time) {
        final Completer<void> completer = Completer<void>();
        final FakeProcess process = FakeProcess(
          duration: const Duration(seconds: 30),
          completer: completer,
        );

        bool hasExited = false;
        unawaited(process.exitCode.then((int _) { hasExited = true; }));

        // Verify process hasn't exited before specified delay.
        time.elapse(const Duration(seconds: 15));
        expect(hasExited, isFalse);

        // Verify process hasn't exited until the completer completes.
        time.elapse(const Duration(seconds: 20));
        expect(hasExited, isFalse);

        // Verify process exits after the completer completes.
        completer.complete();
        time.flushMicrotasks();
        expect(hasExited, isTrue);

        return true;
      });
      expect(done, isTrue);
    });

    testWithoutContext('when completer and duration are specified, does not exit until duration has elapsed', () {
      final bool done = FakeAsync().run<bool>((FakeAsync time) {
        final Completer<void> completer = Completer<void>();
        final FakeProcess process = FakeProcess(
          duration: const Duration(seconds: 30),
          completer: completer,
        );

        bool hasExited = false;
        unawaited(process.exitCode.then((int _) { hasExited = true; }));

        // Verify process hasn't exited before specified delay.
        time.elapse(const Duration(seconds: 15));
        expect(hasExited, isFalse);

        // Verify process does not exit until duration has elapsed.
        completer.complete();
        expect(hasExited, isFalse);

        // Verify process exits after the duration elapses.
        time.elapse(const Duration(seconds: 20));
        expect(hasExited, isTrue);

        return true;
      });
      expect(done, isTrue);
    });

    testWithoutContext('process exit is asynchronous', () async {
      final FakeProcess process = FakeProcess();

      bool hasExited = false;
      unawaited(process.exitCode.then((int _) { hasExited = true; }));

      // Verify process hasn't completed.
      expect(hasExited, isFalse);

      // Flush async tasks. Verify process completes.
      await Future<void>.delayed(Duration.zero);
      expect(hasExited, isTrue);
    });

    testWithoutContext('stderr, stdout stream data after exit when outputFollowsExit is true', () async {
      final FakeProcess process = FakeProcess(
        stderr: 'stderr'.codeUnits,
        stdout: 'stdout'.codeUnits,
        outputFollowsExit: true,
      );

      final List<int> stderr = <int>[];
      final List<int> stdout = <int>[];
      process.stderr.listen(stderr.addAll);
      process.stdout.listen(stdout.addAll);

      // Ensure that no bytes have been received at process exit.
      await process.exitCode;
      expect(stderr, isEmpty);
      expect(stdout, isEmpty);

      // Flush all remaining async work. Ensure stderr, stdout is received.
      await Future<void>.delayed(Duration.zero);
      expect(stderr, 'stderr'.codeUnits);
      expect(stdout, 'stdout'.codeUnits);
    });
  });
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

  group(FakeProcessManager, () {
    late FakeProcessManager manager;

    setUp(() {
      manager = FakeProcessManager.empty();
    });

    group('start', () {
      testWithoutContext('can run a fake command', () async {
        manager.addCommand(const FakeCommand(command: <String>['faketool']));

        final Process process = await manager.start(<String>['faketool']);
        expect(await process.exitCode, 0);
        expect(await utf8.decodeStream(process.stdout), isEmpty);
        expect(await utf8.decodeStream(process.stderr), isEmpty);
      });

      testWithoutContext('outputFollowsExit delays stderr, stdout until after process exit', () async {
        manager.addCommand(const FakeCommand(
          command: <String>['faketool'],
          stderr: 'hello',
          stdout: 'world',
          outputFollowsExit: true,
        ));

        final List<int> stderrBytes = <int>[];
        final List<int> stdoutBytes = <int>[];

        // Start the process.
        final Process process = await manager.start(<String>['faketool']);
        final StreamSubscription<List<int>> stderrSubscription = process.stderr.listen((List<int> chunk) { stderrBytes.addAll(chunk); });
        final StreamSubscription<List<int>> stdoutSubscription = process.stdout.listen((List<int> chunk) { stdoutBytes.addAll(chunk); });

        // Immediately after exit, no output is emitted.
        await process.exitCode;
        expect(utf8.decode(stderrBytes), isEmpty);
        expect(utf8.decode(stdoutBytes), isEmpty);

        // Output is emitted asynchronously after process exit.
        await Future.wait(<Future<void>>[
          stderrSubscription.asFuture(),
          stdoutSubscription.asFuture(),
        ]);
        expect(utf8.decode(stderrBytes), 'hello');
        expect(utf8.decode(stdoutBytes), 'world');

        // Clean up stream subscriptions.
        await stderrSubscription.cancel();
        await stdoutSubscription.cancel();
      });
    });

    group('run', () {
      testWithoutContext('can run a fake command', () async {
        manager.addCommand(const FakeCommand(command: <String>['faketool']));

        final ProcessResult result = await manager.run(<String>['faketool']);
        expect(result.exitCode, 0);
        expect(result.stdout, isEmpty);
        expect(result.stderr, isEmpty);
      });

      testWithoutContext('stderr, stdout are String if encoding is unspecified', () async {
        manager.addCommand(const FakeCommand(command: <String>['faketool']));

        final ProcessResult result = await manager.run(<String>['faketool']);
        expect(result.exitCode, 0);
        expect(result.stdout, isA<String>());
        expect(result.stderr, isA<String>());
      });

      testWithoutContext('stderr, stdout are List<int> if encoding is null', () async {
        manager.addCommand(const FakeCommand(command: <String>['faketool']));

        final ProcessResult result = await manager.run(
          <String>['faketool'],
          stderrEncoding: null,
          stdoutEncoding: null,
        );
        expect(result.exitCode, 0);
        expect(result.stdout, isA<List<int>>());
        expect(result.stderr, isA<List<int>>());
      });

      testWithoutContext('stderr, stdout are String if encoding is specified', () async {
        manager.addCommand(const FakeCommand(command: <String>['faketool']));

        final ProcessResult result = await manager.run(
          <String>['faketool'],
          stderrEncoding: utf8,
          stdoutEncoding: utf8,
        );
        expect(result.exitCode, 0);
        expect(result.stdout, isA<String>());
        expect(result.stderr, isA<String>());
      });
    });

    group('runSync', () {
      testWithoutContext('can run a fake command', () {
        manager.addCommand(const FakeCommand(command: <String>['faketool']));

        final ProcessResult result = manager.runSync(<String>['faketool']);
        expect(result.exitCode, 0);
        expect(result.stdout, isEmpty);
        expect(result.stderr, isEmpty);
      });

      testWithoutContext('stderr, stdout are String if encoding is unspecified', () {
        manager.addCommand(const FakeCommand(command: <String>['faketool']));

        final ProcessResult result = manager.runSync(<String>['faketool']);
        expect(result.exitCode, 0);
        expect(result.stdout, isA<String>());
        expect(result.stderr, isA<String>());
      });

      testWithoutContext('stderr, stdout are List<int> if encoding is null', () {
        manager.addCommand(const FakeCommand(command: <String>['faketool']));

        final ProcessResult result = manager.runSync(
          <String>['faketool'],
          stderrEncoding: null,
          stdoutEncoding: null,
        );
        expect(result.exitCode, 0);
        expect(result.stdout, isA<List<int>>());
        expect(result.stderr, isA<List<int>>());
      });

      testWithoutContext('stderr, stdout are String if encoding is specified', () {
        manager.addCommand(const FakeCommand(command: <String>['faketool']));

        final ProcessResult result = manager.runSync(
          <String>['faketool'],
          stderrEncoding: utf8,
          stdoutEncoding: utf8,
        );
        expect(result.exitCode, 0);
        expect(result.stdout, isA<String>());
        expect(result.stderr, isA<String>());
      });
    });
  });
323
}