common.dart 2.35 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 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';

7
import 'package:file/local.dart';
8
import 'package:flutter_tools/executable.dart' as tools;
9
import 'package:flutter_tools/src/cache.dart';
10
import 'package:flutter_tools/src/base/context.dart';
11
import 'package:flutter_tools/src/base/io.dart' as io;
12
import 'package:flutter_tools/src/base/port_scanner.dart';
13
import 'package:flutter_tools/src/runner/flutter_command.dart';
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import 'package:test/test.dart';

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

/// Runs the specified [testMethod] in a minimal `AppContext` that is set up
/// to redirect log output to a `BufferLogger` to avoid spamming `stdout`.
///
/// Test methods will generally want to use [expectProcessExits] in their method
/// bodies.
void testReplay(
  String description,
  dynamic testMethod(), {
  Timeout timeout,
  Map<Type, Generator> overrides: const <Type, Generator>{},
  bool skip,
}) {
  setUp(() {
32
    io.setExitFunctionForTests();
33 34 35
  });

  tearDown(() {
36
    io.restoreExitFunction();
37 38 39 40 41 42 43 44
  });

  testUsingContext(
    description,
    testMethod,
    timeout: timeout,
    overrides: overrides,
    skip: skip,
45 46 47
    initializeContext: (AppContext testContext) {
      testContext.putIfAbsent(PortScanner, () => new MockPortScanner());
    },
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  );
}

/// Expects that the specified [command] to Flutter tools exits with the
/// specified [exitCode] (defaults to zero). It is expected that callers will
/// be running in a test via [testReplay].
///
/// [command] should be the list of arguments that are passed to the `flutter`
/// command-line tool.  For example:
///
/// ```
///   <String>[
///     'run',
///     '--no-hot',
///     '--no-resident',
///   ]
/// ```
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
void expectProcessExits(
  FlutterCommand command, {
  List<String> args: const <String>[],
  dynamic exitCode: 0,
}) {
  final Future<Null> runFuture = tools.run(
    <String>[command.name]..addAll(args),
    <FlutterCommand>[command],
    reportCrashes: false,
    flutterVersion: 'test',
  );
  expect(runFuture, throwsProcessExit(exitCode));
}

/// The base path of the replay tests.
String get replayBase {
  return const LocalFileSystem().path.joinAll(<String>[
    Cache.flutterRoot,
    'packages',
    'flutter_tools',
    'test',
    'replay',
  ]);
88
}