process_test.dart 16.8 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 6
import 'dart:async';

7
import 'package:flutter_tools/src/base/io.dart';
8
import 'package:flutter_tools/src/base/logger.dart';
9
import 'package:flutter_tools/src/base/platform.dart';
10
import 'package:flutter_tools/src/base/process.dart';
11
import 'package:flutter_tools/src/base/terminal.dart';
12 13
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
14
import 'package:fake_async/fake_async.dart';
15 16
import '../../src/common.dart';
import '../../src/context.dart';
17 18
import '../../src/mocks.dart' show MockProcess,
                                   MockProcessManager,
19
                                   MockStdio,
20
                                   flakyProcessFactory;
21

22
void main() {
23 24
  group('process exceptions', () {
    ProcessManager mockProcessManager;
25
    ProcessUtils processUtils;
26 27

    setUp(() {
28
      mockProcessManager = PlainMockProcessManager();
29 30
      processUtils = ProcessUtils(
        processManager: mockProcessManager,
31
        logger: BufferLogger.test(),
32
      );
33 34
    });

35
    testWithoutContext('runAsync throwOnError: exceptions should be ProcessException objects', () async {
36 37 38
      when(mockProcessManager.run(<String>['false'])).thenAnswer(
          (Invocation invocation) => Future<ProcessResult>.value(ProcessResult(0, 1, '', '')));
      expect(() async => await processUtils.run(<String>['false'], throwOnError: true),
Dan Field's avatar
Dan Field committed
39
             throwsA(isA<ProcessException>()));
40
    });
41
  });
42

43
  group('shutdownHooks', () {
44
    testWithoutContext('runInExpectedOrder', () async {
45 46 47 48 49 50
      int i = 1;
      int serializeRecording1;
      int serializeRecording2;
      int postProcessRecording;
      int cleanup;

51
      final ShutdownHooks shutdownHooks = ShutdownHooks(logger: BufferLogger.test());
52 53

      shutdownHooks.addShutdownHook(() async {
54 55 56
        serializeRecording1 = i++;
      }, ShutdownStage.SERIALIZE_RECORDING);

57
      shutdownHooks.addShutdownHook(() async {
58 59 60
        cleanup = i++;
      }, ShutdownStage.CLEANUP);

61
      shutdownHooks.addShutdownHook(() async {
62 63 64
        postProcessRecording = i++;
      }, ShutdownStage.POST_PROCESS_RECORDING);

65
      shutdownHooks.addShutdownHook(() async {
66 67 68
        serializeRecording2 = i++;
      }, ShutdownStage.SERIALIZE_RECORDING);

69
      await shutdownHooks.runShutdownHooks();
70 71 72 73 74 75 76

      expect(serializeRecording1, lessThanOrEqualTo(2));
      expect(serializeRecording2, lessThanOrEqualTo(2));
      expect(postProcessRecording, 3);
      expect(cleanup, 4);
    });
  });
77

78 79
  group('output formatting', () {
    MockProcessManager mockProcessManager;
80 81
    ProcessUtils processUtils;
    BufferLogger mockLogger;
82 83 84

    setUp(() {
      mockProcessManager = MockProcessManager();
85 86 87
      mockLogger = BufferLogger(
        terminal: AnsiTerminal(
          stdio: MockStdio(),
88
          platform: FakePlatform(stdoutSupportsAnsi: false),
89 90 91 92 93 94 95
        ),
        outputPreferences: OutputPreferences(wrapText: true, wrapColumn: 40),
      );
      processUtils = ProcessUtils(
        processManager: mockProcessManager,
        logger: mockLogger,
      );
96 97
    });

98 99 100 101 102 103 104 105 106
    MockProcess Function(List<String>) processMetaFactory(List<String> stdout, {
      List<String> stderr = const <String>[],
    }) {
      final Stream<List<int>> stdoutStream = Stream<List<int>>.fromIterable(
        stdout.map<List<int>>((String s) => s.codeUnits,
      ));
      final Stream<List<int>> stderrStream = Stream<List<int>>.fromIterable(
        stderr.map<List<int>>((String s) => s.codeUnits,
      ));
107 108 109
      return (List<String> command) => MockProcess(stdout: stdoutStream, stderr: stderrStream);
    }

110
    testWithoutContext('Command output is not wrapped.', () async {
111 112
      final List<String> testString = <String>['0123456789' * 10];
      mockProcessManager.processFactory = processMetaFactory(testString, stderr: testString);
113
      await processUtils.stream(<String>['command']);
114 115
      expect(mockLogger.statusText, equals('${testString[0]}\n'));
      expect(mockLogger.errorText, equals('${testString[0]}\n'));
116 117
    });
  });
118

119
  group('run', () {
120 121
    const Duration delay = Duration(seconds: 2);
    MockProcessManager flakyProcessManager;
122
    ProcessManager mockProcessManager;
123 124
    ProcessUtils processUtils;
    ProcessUtils flakyProcessUtils;
125 126 127 128 129

    setUp(() {
      // MockProcessManager has an implementation of start() that returns the
      // result of processFactory.
      flakyProcessManager = MockProcessManager();
130
      mockProcessManager = MockProcessManager();
131 132
      processUtils = ProcessUtils(
        processManager: mockProcessManager,
133
        logger: BufferLogger.test(),
134 135 136
      );
      flakyProcessUtils = ProcessUtils(
        processManager: flakyProcessManager,
137
        logger: BufferLogger.test(),
138
      );
139 140
    });

141
    testWithoutContext(' succeeds on success', () async {
142 143 144 145 146 147
      when(mockProcessManager.run(<String>['whoohoo'])).thenAnswer((_) {
        return Future<ProcessResult>.value(ProcessResult(0, 0, '', ''));
      });
      expect((await processUtils.run(<String>['whoohoo'])).exitCode, 0);
    });

148
    testWithoutContext(' fails on failure', () async {
149 150 151 152 153 154
      when(mockProcessManager.run(<String>['boohoo'])).thenAnswer((_) {
        return Future<ProcessResult>.value(ProcessResult(0, 1, '', ''));
      });
      expect((await processUtils.run(<String>['boohoo'])).exitCode, 1);
    });

155
    testWithoutContext(' throws on failure with throwOnError', () async {
156 157 158 159 160 161 162
      when(mockProcessManager.run(<String>['kaboom'])).thenAnswer((_) {
        return Future<ProcessResult>.value(ProcessResult(0, 1, '', ''));
      });
      expect(() => processUtils.run(<String>['kaboom'], throwOnError: true),
             throwsA(isA<ProcessException>()));
    });

163
    testWithoutContext(' does not throw on allowed Failures', () async {
164 165 166 167 168 169 170
      when(mockProcessManager.run(<String>['kaboom'])).thenAnswer((_) {
        return Future<ProcessResult>.value(ProcessResult(0, 1, '', ''));
      });
      expect(
        (await processUtils.run(
          <String>['kaboom'],
          throwOnError: true,
171
          allowedFailures: (int c) => c == 1,
172
        )).exitCode,
173 174
        1,
      );
175 176
    });

177
    testWithoutContext(' throws on disallowed failure', () async {
178 179 180 181 182 183 184
      when(mockProcessManager.run(<String>['kaboom'])).thenAnswer((_) {
        return Future<ProcessResult>.value(ProcessResult(0, 2, '', ''));
      });
      expect(
        () => processUtils.run(
          <String>['kaboom'],
          throwOnError: true,
185
          allowedFailures: (int c) => c == 1,
186
        ),
187 188
        throwsA(isA<ProcessException>()),
      );
189 190
    });

191
    testWithoutContext(' flaky process fails without retry', () async {
192 193 194 195
      flakyProcessManager.processFactory = flakyProcessFactory(
        flakes: 1,
        delay: delay,
      );
196

197
      await FakeAsync().run((FakeAsync time) async {
198 199 200 201 202 203 204 205
        final Duration timeout = delay + const Duration(seconds: 1);
        final RunResult result = await flakyProcessUtils.run(
          <String>['dummy'],
          timeout: timeout,
        );
        time.elapse(timeout);
        expect(result.exitCode, -9);
      });
206
    }, skip: true); // TODO(jonahwilliams): clean up with https://github.com/flutter/flutter/issues/60675
207

208
    testWithoutContext(' flaky process succeeds with retry', () async {
209 210 211 212
      flakyProcessManager.processFactory = flakyProcessFactory(
        flakes: 1,
        delay: delay,
      );
213
      await FakeAsync().run((FakeAsync time) async {
214 215 216 217 218 219 220 221 222
        final Duration timeout = delay - const Duration(milliseconds: 500);
        final RunResult result = await flakyProcessUtils.run(
          <String>['dummy'],
          timeout: timeout,
          timeoutRetries: 1,
        );
        time.elapse(timeout);
        expect(result.exitCode, 0);
      });
223
    }, skip: true); // TODO(jonahwilliams): clean up with https://github.com/flutter/flutter/issues/60675
224

225
    testWithoutContext(' flaky process generates ProcessException on timeout', () async {
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
      final Completer<List<int>> flakyStderr = Completer<List<int>>();
      final Completer<List<int>> flakyStdout = Completer<List<int>>();
      flakyProcessManager.processFactory = flakyProcessFactory(
        flakes: 1,
        delay: delay,
        stderr: () => Stream<List<int>>.fromFuture(flakyStderr.future),
        stdout: () => Stream<List<int>>.fromFuture(flakyStdout.future),
      );
      when(flakyProcessManager.killPid(any)).thenAnswer((_) {
        // Don't let the stderr stream stop until the process is killed. This
        // ensures that runAsync() does not delay killing the process until
        // stdout and stderr are drained (which won't happen).
        flakyStderr.complete(<int>[]);
        flakyStdout.complete(<int>[]);
        return true;
      });
242
      await FakeAsync().run((FakeAsync time) async {
243 244 245 246 247 248 249 250
        final Duration timeout = delay - const Duration(milliseconds: 500);
        expect(() => flakyProcessUtils.run(
          <String>['dummy'],
          timeout: timeout,
          timeoutRetries: 0,
        ), throwsA(isA<ProcessException>()));
        time.elapse(timeout);
      });
251
    }, skip: true); // TODO(jonahwilliams): clean up with https://github.com/flutter/flutter/issues/60675
252
  });
253 254 255

  group('runSync', () {
    ProcessManager mockProcessManager;
256 257
    ProcessUtils processUtils;
    BufferLogger testLogger;
258 259 260

    setUp(() {
      mockProcessManager = MockProcessManager();
261 262 263
      testLogger = BufferLogger(
        terminal: AnsiTerminal(
          stdio: MockStdio(),
264
          platform: FakePlatform(stdinSupportsAnsi: false),
265 266 267 268 269 270 271
        ),
        outputPreferences: OutputPreferences(wrapText: true, wrapColumn: 40),
      );
      processUtils = ProcessUtils(
        processManager: mockProcessManager,
        logger: testLogger,
      );
272 273
    });

274
    testWithoutContext(' succeeds on success', () async {
275 276 277 278 279 280
      when(mockProcessManager.runSync(<String>['whoohoo'])).thenReturn(
        ProcessResult(0, 0, '', '')
      );
      expect(processUtils.runSync(<String>['whoohoo']).exitCode, 0);
    });

281
    testWithoutContext(' fails on failure', () async {
282 283 284 285 286 287
      when(mockProcessManager.runSync(<String>['boohoo'])).thenReturn(
        ProcessResult(0, 1, '', '')
      );
      expect(processUtils.runSync(<String>['boohoo']).exitCode, 1);
    });

288 289
    testWithoutContext('throws on failure with throwOnError', () async {
      const String stderr = 'Something went wrong.';
290
      when(mockProcessManager.runSync(<String>['kaboom'])).thenReturn(
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        ProcessResult(0, 1, '', stderr),
      );
      try {
        processUtils.runSync(<String>['kaboom'], throwOnError: true);
        fail('ProcessException expected.');
      } on ProcessException catch (e) {
        expect(e, isA<ProcessException>());
        expect(e.message.contains(stderr), false);
      }
    });

    testWithoutContext('throws with stderr in exception on failure with verboseExceptions', () async {
      const String stderr = 'Something went wrong.';
      when(mockProcessManager.runSync(<String>['verybad'])).thenReturn(
        ProcessResult(0, 1, '', stderr),
      );
      expect(
        () => processUtils.runSync(
          <String>['verybad'],
          throwOnError: true,
          verboseExceptions: true,
        ),
        throwsProcessException(message: stderr),
314 315 316
      );
    });

317
    testWithoutContext(' does not throw on allowed Failures', () async {
318 319 320 321 322 323 324
      when(mockProcessManager.runSync(<String>['kaboom'])).thenReturn(
        ProcessResult(0, 1, '', '')
      );
      expect(
        processUtils.runSync(
          <String>['kaboom'],
          throwOnError: true,
325
          allowedFailures: (int c) => c == 1,
326 327 328 329
        ).exitCode,
        1);
    });

330
    testWithoutContext(' throws on disallowed failure', () async {
331 332 333 334 335 336 337
      when(mockProcessManager.runSync(<String>['kaboom'])).thenReturn(
        ProcessResult(0, 2, '', '')
      );
      expect(
        () => processUtils.runSync(
          <String>['kaboom'],
          throwOnError: true,
338
          allowedFailures: (int c) => c == 1,
339 340 341 342
        ),
        throwsA(isA<ProcessException>()));
    });

343
    testWithoutContext(' prints stdout and stderr to trace on success', () async {
344 345 346 347 348 349 350 351
      when(mockProcessManager.runSync(<String>['whoohoo'])).thenReturn(
        ProcessResult(0, 0, 'stdout', 'stderr')
      );
      expect(processUtils.runSync(<String>['whoohoo']).exitCode, 0);
      expect(testLogger.traceText, contains('stdout'));
      expect(testLogger.traceText, contains('stderr'));
    });

352
    testWithoutContext(' prints stdout to status and stderr to error on failure with throwOnError', () async {
353 354 355 356 357 358 359 360 361
      when(mockProcessManager.runSync(<String>['kaboom'])).thenReturn(
        ProcessResult(0, 1, 'stdout', 'stderr')
      );
      expect(() => processUtils.runSync(<String>['kaboom'], throwOnError: true),
             throwsA(isA<ProcessException>()));
      expect(testLogger.statusText, contains('stdout'));
      expect(testLogger.errorText, contains('stderr'));
    });

362
    testWithoutContext(' does not print stdout with hideStdout', () async {
363 364 365 366 367 368 369 370 371 372
      when(mockProcessManager.runSync(<String>['whoohoo'])).thenReturn(
        ProcessResult(0, 0, 'stdout', 'stderr')
      );
      expect(processUtils.runSync(<String>['whoohoo'], hideStdout: true).exitCode, 0);
      expect(testLogger.traceText.contains('stdout'), isFalse);
      expect(testLogger.traceText, contains('stderr'));
    });
  });

  group('exitsHappySync', () {
373
    MockProcessManager mockProcessManager;
374
    ProcessUtils processUtils;
375 376 377

    setUp(() {
      mockProcessManager = MockProcessManager();
378 379
      processUtils = ProcessUtils(
        processManager: mockProcessManager,
380
        logger: BufferLogger.test(),
381
      );
382 383
    });

384
    testWithoutContext(' succeeds on success', () async {
385 386 387 388 389 390
      when(mockProcessManager.runSync(<String>['whoohoo'])).thenReturn(
        ProcessResult(0, 0, '', '')
      );
      expect(processUtils.exitsHappySync(<String>['whoohoo']), isTrue);
    });

391
    testWithoutContext(' fails on failure', () async {
392 393 394 395 396
      when(mockProcessManager.runSync(<String>['boohoo'])).thenReturn(
        ProcessResult(0, 1, '', '')
      );
      expect(processUtils.exitsHappySync(<String>['boohoo']), isFalse);
    });
397 398 399 400 401 402 403 404

    testWithoutContext('catches Exception and returns false', () {
      when(mockProcessManager.runSync(<String>['boohoo'])).thenThrow(
        const ProcessException('Process failed', <String>[]),
      );
      expect(processUtils.exitsHappySync(<String>['boohoo']), isFalse);
    });

405 406
    testWithoutContext('does not throw Exception and returns false if binary cannot run', () {
      mockProcessManager.canRunSucceeds = false;
407
      expect(processUtils.exitsHappySync(<String>['nonesuch']), isFalse);
408 409 410 411 412 413 414 415 416 417 418 419 420
      verifyNever(
        mockProcessManager.runSync(any, environment: anyNamed('environment')),
      );
    });

    testWithoutContext('does not catch ArgumentError', () async {
      when(mockProcessManager.runSync(<String>['invalid'])).thenThrow(
        ArgumentError('Bad input'),
      );
      expect(
        () => processUtils.exitsHappySync(<String>['invalid']),
        throwsArgumentError,
      );
421
    });
422 423 424
  });

  group('exitsHappy', () {
425
    MockProcessManager mockProcessManager;
426
    ProcessUtils processUtils;
427 428 429

    setUp(() {
      mockProcessManager = MockProcessManager();
430 431
      processUtils = ProcessUtils(
        processManager: mockProcessManager,
432
        logger: BufferLogger.test(),
433
      );
434 435
    });

436
    testWithoutContext('succeeds on success', () async {
437 438 439 440 441 442
      when(mockProcessManager.run(<String>['whoohoo'])).thenAnswer((_) {
        return Future<ProcessResult>.value(ProcessResult(0, 0, '', ''));
      });
      expect(await processUtils.exitsHappy(<String>['whoohoo']), isTrue);
    });

443
    testWithoutContext('fails on failure', () async {
444 445 446 447 448
      when(mockProcessManager.run(<String>['boohoo'])).thenAnswer((_) {
        return Future<ProcessResult>.value(ProcessResult(0, 1, '', ''));
      });
      expect(await processUtils.exitsHappy(<String>['boohoo']), isFalse);
    });
449 450 451 452 453 454 455 456

    testWithoutContext('catches Exception and returns false', () async {
      when(mockProcessManager.run(<String>['boohoo'])).thenThrow(
        const ProcessException('Process failed', <String>[]),
      );
      expect(await processUtils.exitsHappy(<String>['boohoo']), isFalse);
    });

457 458
    testWithoutContext('does not throw Exception and returns false if binary cannot run', () async {
      mockProcessManager.canRunSucceeds = false;
459
      expect(await processUtils.exitsHappy(<String>['nonesuch']), isFalse);
460 461 462 463 464 465 466 467 468 469 470 471 472
      verifyNever(
        mockProcessManager.runSync(any, environment: anyNamed('environment')),
      );
    });

    testWithoutContext('does not catch ArgumentError', () async {
      when(mockProcessManager.run(<String>['invalid'])).thenThrow(
        ArgumentError('Bad input'),
      );
      expect(
        () async => await processUtils.exitsHappy(<String>['invalid']),
        throwsArgumentError,
      );
473
    });
474 475
  });

476
}
477

478
class PlainMockProcessManager extends Mock implements ProcessManager {}