runner.dart 4.82 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// 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: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 '../build_info.dart';
14
import '../dart/package_map.dart';
15
import '../globals.dart' as globals;
16
import '../project.dart';
17
import '../web/compile.dart';
18
import 'flutter_platform.dart' as loader;
19
import 'flutter_web_platform.dart';
20
import 'test_wrapper.dart';
21 22 23 24
import 'watcher.dart';

/// Runs tests using package:test and the Flutter engine.
Future<int> runTests(
25
  TestWrapper testWrapper,
26 27 28 29 30 31
  List<String> testFiles, {
  Directory workDir,
  List<String> names = const <String>[],
  List<String> plainNames = const <String>[],
  bool enableObservatory = false,
  bool startPaused = false,
32
  bool disableServiceAuthCodes = false,
33 34 35
  bool ipv6 = false,
  bool machine = false,
  String precompiledDillPath,
36
  Map<String, String> precompiledDillFiles,
37
  @required BuildMode buildMode,
38 39 40 41
  bool trackWidgetCreation = false,
  bool updateGoldens = false,
  TestWatcher watcher,
  @required int concurrency,
42
  bool buildTestAssets = false,
43
  FlutterProject flutterProject,
44
  String icudtlPath,
45
  Directory coverageDirectory,
46
  bool web = false,
47
  String randomSeed = '0',
48
}) async {
49
  // Configure package:test to use the Flutter engine for child processes.
50 51
  final String shellPath = globals.artifacts.getArtifactPath(Artifact.flutterTester);
  if (!globals.processManager.canRun(shellPath)) {
52 53 54
    throwToolExit('Cannot execute Flutter tester at $shellPath');
  }

55
  // Compute the command-line arguments for package:test.
56
  final List<String> testArgs = <String>[
57
    if (!globals.terminal.supportsColor)
58 59 60 61 62 63
      '--no-color',
    if (machine)
      ...<String>['-r', 'json']
    else
      ...<String>['-r', 'compact'],
    '--concurrency=$concurrency',
64
    for (final String name in names)
65
      ...<String>['--name', name],
66
    for (final String plainName in plainNames)
67
      ...<String>['--plain-name', plainName],
68
    '--test-randomize-ordering-seed=$randomSeed',
69
  ];
70
  if (web) {
71
    final String tempBuildDir = globals.fs.systemTempDirectory
72
      .createTempSync('flutter_test.')
73 74 75
      .absolute
      .uri
      .toFilePath();
76
    final bool result = await webCompilationProxy.initialize(
77 78
      projectDirectory: flutterProject.directory,
      testOutputDir: tempBuildDir,
79
      testFiles: testFiles,
80
      projectName: flutterProject.manifest.appName,
81
      initializePlatform: true,
82
    );
83 84 85
    if (!result) {
      throwToolExit('Failed to compile tests');
    }
86 87 88 89 90
    testArgs
      ..add('--platform=chrome')
      ..add('--precompiled=$tempBuildDir')
      ..add('--')
      ..addAll(testFiles);
91
    testWrapper.registerPlatformPlugin(
92 93
      <Runtime>[Runtime.chrome],
      () {
94 95 96 97 98 99
        return FlutterWebPlatform.start(
          flutterProject.directory.path,
          updateGoldens: updateGoldens,
          shellPath: shellPath,
          flutterProject: flutterProject,
        );
100
      },
101
    );
102
    await testWrapper.main(testArgs);
103 104
    return exitCode;
  }
105

106 107 108
  testArgs
    ..add('--')
    ..addAll(testFiles);
109 110

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

113
  final loader.FlutterPlatform platform = loader.installHook(
114
    testWrapper: testWrapper,
115 116 117
    shellPath: shellPath,
    watcher: watcher,
    enableObservatory: enableObservatory,
118
    machine: machine,
119
    startPaused: startPaused,
120
    disableServiceAuthCodes: disableServiceAuthCodes,
121
    serverType: serverType,
122
    precompiledDillPath: precompiledDillPath,
123
    precompiledDillFiles: precompiledDillFiles,
124
    buildMode: buildMode,
125
    trackWidgetCreation: trackWidgetCreation,
126
    updateGoldens: updateGoldens,
127
    buildTestAssets: buildTestAssets,
128
    projectRootDirectory: globals.fs.currentDirectory.uri,
129
    flutterProject: flutterProject,
130
    icudtlPath: icudtlPath,
131 132
  );

133 134
  // Make the global packages path absolute.
  // (Makes sure it still works after we change the current directory.)
135
  PackageMap.globalPackagesPath =
136
      globals.fs.path.normalize(globals.fs.path.absolute(PackageMap.globalPackagesPath));
137 138

  // Call package:test's main method in the appropriate directory.
139
  final Directory saved = globals.fs.currentDirectory;
140 141
  try {
    if (workDir != null) {
142 143
      globals.printTrace('switching to directory $workDir to run tests');
      globals.fs.currentDirectory = workDir;
144 145
    }

146
    globals.printTrace('running test package with arguments: $testArgs');
147
    await testWrapper.main(testArgs);
148 149

    // test.main() sets dart:io's exitCode global.
150
    globals.printTrace('test package returned with exit code $exitCode');
151 152 153

    return exitCode;
  } finally {
154
    globals.fs.currentDirectory = saved;
155
    await platform.close();
156 157
  }
}