runner.dart 7.21 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 'package:meta/meta.dart';
6 7 8 9 10

import '../artifacts.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/io.dart';
11
import '../build_info.dart';
12
import '../globals.dart' as globals;
13
import '../project.dart';
14
import '../web/chrome.dart';
15
import '../web/memory_fs.dart';
16
import 'flutter_platform.dart' as loader;
17
import 'flutter_web_platform.dart';
18
import 'test_wrapper.dart';
19
import 'watcher.dart';
20
import 'web_test_compiler.dart';
21

22 23 24
/// A class that abstracts launching the test process from the test runner.
abstract class FlutterTestRunner {
  const factory FlutterTestRunner() = _FlutterTestRunnerImpl;
25

26 27 28 29 30 31 32
  /// Runs tests using package:test and the Flutter engine.
  Future<int> runTests(
    TestWrapper testWrapper,
    List<String> testFiles, {
    Directory workDir,
    List<String> names = const <String>[],
    List<String> plainNames = const <String>[],
33 34
    String tags,
    String excludeTags,
35 36 37
    bool enableObservatory = false,
    bool startPaused = false,
    bool disableServiceAuthCodes = false,
38
    bool disableDds = false,
39 40 41 42 43 44 45 46 47 48 49 50
    bool ipv6 = false,
    bool machine = false,
    String precompiledDillPath,
    Map<String, String> precompiledDillFiles,
    bool updateGoldens = false,
    TestWatcher watcher,
    @required int concurrency,
    bool buildTestAssets = false,
    FlutterProject flutterProject,
    String icudtlPath,
    Directory coverageDirectory,
    bool web = false,
51
    String randomSeed,
52
    bool nullAssertions = false,
53
    @required BuildInfo buildInfo,
54 55
    String reporter,
    String timeout,
56
    List<String> additionalArguments,
57 58 59 60 61 62 63 64 65 66 67 68 69
  });
}

class _FlutterTestRunnerImpl implements FlutterTestRunner {
  const _FlutterTestRunnerImpl();

  @override
  Future<int> runTests(
    TestWrapper testWrapper,
    List<String> testFiles, {
    Directory workDir,
    List<String> names = const <String>[],
    List<String> plainNames = const <String>[],
70 71
    String tags,
    String excludeTags,
72 73 74
    bool enableObservatory = false,
    bool startPaused = false,
    bool disableServiceAuthCodes = false,
75
    bool disableDds = false,
76 77 78 79 80 81 82 83 84 85 86 87
    bool ipv6 = false,
    bool machine = false,
    String precompiledDillPath,
    Map<String, String> precompiledDillFiles,
    bool updateGoldens = false,
    TestWatcher watcher,
    @required int concurrency,
    bool buildTestAssets = false,
    FlutterProject flutterProject,
    String icudtlPath,
    Directory coverageDirectory,
    bool web = false,
88
    String randomSeed,
89
    bool nullAssertions = false,
90
    @required BuildInfo buildInfo,
91 92
    String reporter,
    String timeout,
93
    List<String> additionalArguments,
94 95 96 97 98 99 100 101 102 103 104 105 106
  }) async {
    // Configure package:test to use the Flutter engine for child processes.
    final String shellPath = globals.artifacts.getArtifactPath(Artifact.flutterTester);

    // Compute the command-line arguments for package:test.
    final List<String> testArgs = <String>[
      if (!globals.terminal.supportsColor)
        '--no-color',
      if (startPaused)
        '--pause-after-load',
      if (machine)
        ...<String>['-r', 'json']
      else
107 108 109
        ...<String>['-r', reporter ?? 'compact'],
      if (timeout != null)
        ...<String>['--timeout', timeout],
110 111 112 113 114
      '--concurrency=$concurrency',
      for (final String name in names)
        ...<String>['--name', name],
      for (final String plainName in plainNames)
        ...<String>['--plain-name', plainName],
115 116
      if (randomSeed != null)
        '--test-randomize-ordering-seed=$randomSeed',
117 118 119 120
      if (tags != null)
        ...<String>['--tags', tags],
      if (excludeTags != null)
        ...<String>['--exclude-tags', excludeTags],
121 122 123 124 125 126 127
    ];
    if (web) {
      final String tempBuildDir = globals.fs.systemTempDirectory
        .createTempSync('flutter_test.')
        .absolute
        .uri
        .toFilePath();
128 129 130 131 132 133 134 135
      final WebMemoryFS result = await WebTestCompiler(
        logger: globals.logger,
        fileSystem: globals.fs,
        platform: globals.platform,
        artifacts: globals.artifacts,
        processManager: globals.processManager,
        config: globals.config,
      ).initialize(
136 137 138
        projectDirectory: flutterProject.directory,
        testOutputDir: tempBuildDir,
        testFiles: testFiles,
139
        buildInfo: buildInfo,
140
      );
141
      if (result == null) {
142 143 144 145 146 147 148 149 150
        throwToolExit('Failed to compile tests');
      }
      testArgs
        ..add('--platform=chrome')
        ..add('--')
        ..addAll(testFiles);
      testWrapper.registerPlatformPlugin(
        <Runtime>[Runtime.chrome],
        () {
151 152
          // TODO(jonahwilliams): refactor this into a factory that handles
          // providing dependencies.
153 154 155 156 157 158
          return FlutterWebPlatform.start(
            flutterProject.directory.path,
            updateGoldens: updateGoldens,
            shellPath: shellPath,
            flutterProject: flutterProject,
            pauseAfterLoad: startPaused,
159
            nullAssertions: nullAssertions,
160
            buildInfo: buildInfo,
161
            webMemoryFS: result,
162 163 164 165 166 167 168 169 170 171 172
            logger: globals.logger,
            fileSystem: globals.fs,
            artifacts: globals.artifacts,
            chromiumLauncher: ChromiumLauncher(
              fileSystem: globals.fs,
              platform: globals.platform,
              processManager: globals.processManager,
              operatingSystemUtils: globals.os,
              browserFinder: findChromeExecutable,
              logger: globals.logger,
            ),
173 174 175 176 177
          );
        },
      );
      await testWrapper.main(testArgs);
      return exitCode;
178
    }
179

180 181 182
    testArgs
      ..add('--')
      ..addAll(testFiles);
183 184 185 186 187 188 189 190 191 192 193 194

    final InternetAddressType serverType =
        ipv6 ? InternetAddressType.IPv6 : InternetAddressType.IPv4;

    final loader.FlutterPlatform platform = loader.installHook(
      testWrapper: testWrapper,
      shellPath: shellPath,
      watcher: watcher,
      enableObservatory: enableObservatory,
      machine: machine,
      startPaused: startPaused,
      disableServiceAuthCodes: disableServiceAuthCodes,
195
      disableDds: disableDds,
196 197 198 199 200 201 202 203
      serverType: serverType,
      precompiledDillPath: precompiledDillPath,
      precompiledDillFiles: precompiledDillFiles,
      updateGoldens: updateGoldens,
      buildTestAssets: buildTestAssets,
      projectRootDirectory: globals.fs.currentDirectory.uri,
      flutterProject: flutterProject,
      icudtlPath: icudtlPath,
204
      nullAssertions: nullAssertions,
205
      buildInfo: buildInfo,
206
      additionalArguments: additionalArguments,
207
    );
208

209 210 211 212 213 214 215
    // Call package:test's main method in the appropriate directory.
    final Directory saved = globals.fs.currentDirectory;
    try {
      if (workDir != null) {
        globals.printTrace('switching to directory $workDir to run tests');
        globals.fs.currentDirectory = workDir;
      }
216

217 218
      globals.printTrace('running test package with arguments: $testArgs');
      await testWrapper.main(testArgs);
219

220 221
      // test.main() sets dart:io's exitCode global.
      globals.printTrace('test package returned with exit code $exitCode');
222

223 224
      return exitCode;
    } finally {
225
      globals.fs.currentDirectory = saved.path;
226 227
      await platform.close();
    }
228 229
  }
}