runner.dart 6.44 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
// @dart = 2.8

7
import 'package:meta/meta.dart';
8 9 10 11 12

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

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

28 29 30 31
  /// Runs tests using package:test and the Flutter engine.
  Future<int> runTests(
    TestWrapper testWrapper,
    List<String> testFiles, {
32
    @required DebuggingOptions debuggingOptions,
33 34
    List<String> names = const <String>[],
    List<String> plainNames = const <String>[],
35 36
    String tags,
    String excludeTags,
37 38 39 40 41 42 43 44 45 46 47 48 49
    bool enableObservatory = false,
    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,
50
    String randomSeed,
51 52
    String reporter,
    String timeout,
53 54 55 56 57 58 59 60 61 62
  });
}

class _FlutterTestRunnerImpl implements FlutterTestRunner {
  const _FlutterTestRunnerImpl();

  @override
  Future<int> runTests(
    TestWrapper testWrapper,
    List<String> testFiles, {
63
    @required DebuggingOptions debuggingOptions,
64 65
    List<String> names = const <String>[],
    List<String> plainNames = const <String>[],
66 67
    String tags,
    String excludeTags,
68 69 70 71 72 73 74 75 76 77 78 79 80
    bool enableObservatory = false,
    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,
81
    String randomSeed,
82 83
    String reporter,
    String timeout,
84 85 86 87 88 89 90 91
  }) 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',
92
      if (debuggingOptions.startPaused)
93 94 95 96
        '--pause-after-load',
      if (machine)
        ...<String>['-r', 'json']
      else
97 98 99
        ...<String>['-r', reporter ?? 'compact'],
      if (timeout != null)
        ...<String>['--timeout', timeout],
100 101 102 103 104
      '--concurrency=$concurrency',
      for (final String name in names)
        ...<String>['--name', name],
      for (final String plainName in plainNames)
        ...<String>['--plain-name', plainName],
105 106
      if (randomSeed != null)
        '--test-randomize-ordering-seed=$randomSeed',
107 108 109 110
      if (tags != null)
        ...<String>['--tags', tags],
      if (excludeTags != null)
        ...<String>['--exclude-tags', excludeTags],
111 112 113 114 115 116 117
    ];
    if (web) {
      final String tempBuildDir = globals.fs.systemTempDirectory
        .createTempSync('flutter_test.')
        .absolute
        .uri
        .toFilePath();
118 119 120 121 122 123 124 125
      final WebMemoryFS result = await WebTestCompiler(
        logger: globals.logger,
        fileSystem: globals.fs,
        platform: globals.platform,
        artifacts: globals.artifacts,
        processManager: globals.processManager,
        config: globals.config,
      ).initialize(
126 127 128
        projectDirectory: flutterProject.directory,
        testOutputDir: tempBuildDir,
        testFiles: testFiles,
129
        buildInfo: debuggingOptions.buildInfo,
130
      );
131
      if (result == null) {
132 133 134 135 136 137 138 139 140
        throwToolExit('Failed to compile tests');
      }
      testArgs
        ..add('--platform=chrome')
        ..add('--')
        ..addAll(testFiles);
      testWrapper.registerPlatformPlugin(
        <Runtime>[Runtime.chrome],
        () {
141 142
          // TODO(jonahwilliams): refactor this into a factory that handles
          // providing dependencies.
143 144 145 146 147
          return FlutterWebPlatform.start(
            flutterProject.directory.path,
            updateGoldens: updateGoldens,
            shellPath: shellPath,
            flutterProject: flutterProject,
148 149 150
            pauseAfterLoad: debuggingOptions.startPaused,
            nullAssertions: debuggingOptions.nullAssertions,
            buildInfo: debuggingOptions.buildInfo,
151
            webMemoryFS: result,
152 153 154 155 156 157 158 159 160 161 162
            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,
            ),
163 164 165 166 167
          );
        },
      );
      await testWrapper.main(testArgs);
      return exitCode;
168
    }
169

170 171 172
    testArgs
      ..add('--')
      ..addAll(testFiles);
173 174 175 176 177 178 179

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

    final loader.FlutterPlatform platform = loader.installHook(
      testWrapper: testWrapper,
      shellPath: shellPath,
180
      debuggingOptions: debuggingOptions,
181 182 183 184 185 186 187 188 189 190 191
      watcher: watcher,
      enableObservatory: enableObservatory,
      machine: machine,
      serverType: serverType,
      precompiledDillPath: precompiledDillPath,
      precompiledDillFiles: precompiledDillFiles,
      updateGoldens: updateGoldens,
      buildTestAssets: buildTestAssets,
      projectRootDirectory: globals.fs.currentDirectory.uri,
      flutterProject: flutterProject,
      icudtlPath: icudtlPath,
192
    );
193

194 195 196
    try {
      globals.printTrace('running test package with arguments: $testArgs');
      await testWrapper.main(testArgs);
197

198 199
      // test.main() sets dart:io's exitCode global.
      globals.printTrace('test package returned with exit code $exitCode');
200

201 202 203 204
      return exitCode;
    } finally {
      await platform.close();
    }
205 206
  }
}