runner.dart 3.36 KB
Newer Older
1 2 3 4 5 6
// Copyright 2017 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:args/command_runner.dart';
8 9
import 'package:meta/meta.dart';
import 'package:test/src/executable.dart' as test; // ignore: implementation_imports
10 11 12 13 14

import '../artifacts.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/io.dart';
15
import '../base/process_manager.dart';
16 17 18
import '../base/terminal.dart';
import '../dart/package_map.dart';
import '../globals.dart';
19
import 'flutter_platform.dart' as loader;
20 21 22 23
import 'watcher.dart';

/// Runs tests using package:test and the Flutter engine.
Future<int> runTests(
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  List<String> testFiles, {
  Directory workDir,
  List<String> names = const <String>[],
  List<String> plainNames = const <String>[],
  bool enableObservatory = false,
  bool startPaused = false,
  bool ipv6 = false,
  bool machine = false,
  bool previewDart2 = false,
  String precompiledDillPath,
  bool trackWidgetCreation = false,
  bool updateGoldens = false,
  TestWatcher watcher,
  @required int concurrency,
}) async {
39 40 41 42 43 44 45
  if (trackWidgetCreation && !previewDart2) {
    throw new UsageException(
      '--track-widget-creation is valid only when --preview-dart-2 is specified.',
      null,
    );
  }

46 47
  // Compute the command-line arguments for package:test.
  final List<String> testArgs = <String>[];
48 49 50
  if (!terminal.supportsColor) {
    testArgs.addAll(<String>['--no-color']);
  }
51

52
  if (machine) {
53
    testArgs.addAll(<String>['-r', 'json']);
54 55
  } else {
    testArgs.addAll(<String>['-r', 'compact']);
56 57
  }

58
  testArgs.add('--concurrency=$concurrency');
59

60
  for (String name in names) {
61
    testArgs..add('--name')..add(name);
62 63 64
  }

  for (String plainName in plainNames) {
65
    testArgs..add('--plain-name')..add(plainName);
66 67
  }

68 69 70 71 72
  testArgs.add('--');
  testArgs.addAll(testFiles);

  // Configure package:test to use the Flutter engine for child processes.
  final String shellPath = artifacts.getArtifactPath(Artifact.flutterTester);
73
  if (!processManager.canRun(shellPath))
74 75 76
    throwToolExit('Cannot find Flutter shell at $shellPath');

  final InternetAddressType serverType =
77
      ipv6 ? InternetAddressType.IPv6 : InternetAddressType.IPv4;
78 79 80 81 82

  loader.installHook(
    shellPath: shellPath,
    watcher: watcher,
    enableObservatory: enableObservatory,
83
    machine: machine,
84 85
    startPaused: startPaused,
    serverType: serverType,
86
    previewDart2: previewDart2,
87
    precompiledDillPath: precompiledDillPath,
88
    trackWidgetCreation: trackWidgetCreation,
89
    updateGoldens: updateGoldens,
90 91
  );

92 93
  // Make the global packages path absolute.
  // (Makes sure it still works after we change the current directory.)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  PackageMap.globalPackagesPath =
      fs.path.normalize(fs.path.absolute(PackageMap.globalPackagesPath));

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

    printTrace('running test package with arguments: $testArgs');
    await test.main(testArgs);

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

    return exitCode;
  } finally {
    fs.currentDirectory = saved;
  }
}