fuchsia_tester.dart 7.03 KB
Newer Older
1 2 3 4 5
// 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';
6
import 'dart:convert' show json;
7
import 'dart:math' as math;
8 9

import 'package:args/args.dart';
10
import 'package:flutter_tools/src/base/common.dart';
11
import 'package:flutter_tools/src/base/context.dart';
12 13
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
14
import 'package:flutter_tools/src/base/platform.dart';
15
import 'package:flutter_tools/src/cache.dart';
16
import 'package:flutter_tools/src/context_runner.dart';
17
import 'package:flutter_tools/src/dart/package_map.dart';
18
import 'package:flutter_tools/src/artifacts.dart';
19
import 'package:flutter_tools/src/globals.dart';
20
import 'package:flutter_tools/src/project.dart';
21
import 'package:flutter_tools/src/reporting/reporting.dart';
22 23
import 'package:flutter_tools/src/test/coverage_collector.dart';
import 'package:flutter_tools/src/test/runner.dart';
24

25
// This was largely inspired by lib/src/commands/test.dart.
26

27 28 29
const String _kOptionPackages = 'packages';
const String _kOptionShell = 'shell';
const String _kOptionTestDirectory = 'test-directory';
30
const String _kOptionSdkRoot = 'sdk-root';
31
const String _kOptionIcudtl = 'icudtl';
32
const String _kOptionTests = 'tests';
33
const String _kOptionCoverageDirectory = 'coverage-directory';
34
const List<String> _kRequiredOptions = <String>[
35 36
  _kOptionPackages,
  _kOptionShell,
37
  _kOptionSdkRoot,
38 39
  _kOptionIcudtl,
  _kOptionTests,
40
];
41 42
const String _kOptionCoverage = 'coverage';
const String _kOptionCoveragePath = 'coverage-path';
43

44
void main(List<String> args) {
45
  runInContext<void>(() => run(args), overrides: <Type, Generator>{
46
    Usage: () => DisabledUsage(),
47 48 49
  });
}

50
Future<void> run(List<String> args) async {
51
  final ArgParser parser = ArgParser()
52 53
    ..addOption(_kOptionPackages, help: 'The .packages file')
    ..addOption(_kOptionShell, help: 'The Flutter shell binary')
54
    ..addOption(_kOptionTestDirectory, help: 'Directory containing the tests')
55
    ..addOption(_kOptionSdkRoot, help: 'Path to the SDK platform files')
56
    ..addOption(_kOptionIcudtl, help: 'Path to the ICU data file')
57
    ..addOption(_kOptionTests, help: 'Path to json file that maps Dart test files to precompiled dill files')
58
    ..addOption(_kOptionCoverageDirectory, help: 'The path to the directory that will have coverage collected')
59 60 61 62 63 64 65 66 67
    ..addFlag(_kOptionCoverage,
      defaultsTo: false,
      negatable: false,
      help: 'Whether to collect coverage information.',
    )
    ..addOption(_kOptionCoveragePath,
        defaultsTo: 'coverage/lcov.info',
        help: 'Where to store coverage information (if coverage is enabled).',
    );
68 69 70
  final ArgResults argResults = parser.parse(args);
  if (_kRequiredOptions
      .any((String option) => !argResults.options.contains(option))) {
71
    throwToolExit('Missing option! All options must be specified.');
72
  }
73 74
  final Directory tempDir =
      fs.systemTempDirectory.createTempSync('flutter_fuchsia_tester.');
75
  try {
76
    Cache.flutterRoot = tempDir.path;
77

78
    final String shellPath = fs.file(argResults[_kOptionShell]).resolveSymbolicLinksSync();
79 80 81
    if (!fs.isFileSync(shellPath)) {
      throwToolExit('Cannot find Flutter shell at $shellPath');
    }
82 83 84 85 86

    final Directory sdkRootSrc = fs.directory(argResults[_kOptionSdkRoot]);
    if (!fs.isDirectorySync(sdkRootSrc.path)) {
      throwToolExit('Cannot find SDK files at ${sdkRootSrc.path}');
    }
87 88 89 90 91 92 93 94
    Directory coverageDirectory;
    final String coverageDirectoryPath = argResults[_kOptionCoverageDirectory];
    if (coverageDirectoryPath != null) {
      if (!fs.isDirectorySync(coverageDirectoryPath)) {
        throwToolExit('Cannot find coverage directory at $coverageDirectoryPath');
      }
      coverageDirectory = fs.directory(coverageDirectoryPath);
    }
95

96
    // Put the tester shell where runTests expects it.
97
    // TODO(garymm): Switch to a Fuchsia-specific Artifacts impl.
98 99 100
    final Link testerDestLink =
        fs.link(artifacts.getArtifactPath(Artifact.flutterTester));
    testerDestLink.parent.createSync(recursive: true);
101
    testerDestLink.createSync(fs.path.absolute(shellPath));
102

103 104 105 106 107 108 109 110
    final Directory sdkRootDest =
        fs.directory(artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath));
    sdkRootDest.createSync(recursive: true);
    for (FileSystemEntity artifact in sdkRootSrc.listSync()) {
      fs.link(sdkRootDest.childFile(artifact.basename).path).createSync(artifact.path);
    }
    // TODO(tvolkert): Remove once flutter_tester no longer looks for this.
    fs.link(sdkRootDest.childFile('platform.dill').path).createSync('platform_strong.dill');
111

112 113
    PackageMap.globalPackagesPath =
        fs.path.normalize(fs.path.absolute(argResults[_kOptionPackages]));
114

115
    Directory testDirectory;
116 117
    CoverageCollector collector;
    if (argResults['coverage']) {
118
      collector = CoverageCollector(
119 120 121 122 123 124 125 126
        libraryPredicate: (String libraryName) {
          // If we have a specified coverage directory then accept all libraries.
          if (coverageDirectory != null) {
            return true;
          }
          final String projectName = FlutterProject.current().manifest.appName;
          return libraryName.contains(projectName);
        });
127 128 129 130
      if (!argResults.options.contains(_kOptionTestDirectory)) {
        throwToolExit('Use of --coverage requires setting --test-directory');
      }
      testDirectory = fs.directory(argResults[_kOptionTestDirectory]);
131 132
    }

133 134 135 136 137

    final Map<String, String> tests = <String, String>{};
    final List<Map<String, dynamic>> jsonList = List<Map<String, dynamic>>.from(
      json.decode(fs.file(argResults[_kOptionTests]).readAsStringSync()));
    for (Map<String, dynamic> map in jsonList) {
138 139 140
      final String source = fs.file(map['source']).resolveSymbolicLinksSync();
      final String dill = fs.file(map['dill']).resolveSymbolicLinksSync();
      tests[source] = dill;
141 142
    }

143
    exitCode = await runTests(
144
      tests.keys.toList(),
145 146
      workDir: testDirectory,
      watcher: collector,
147
      ipv6: false,
148
      enableObservatory: collector != null,
149
      precompiledDillFiles: tests,
150
      concurrency: math.max(1, platform.numberOfProcessors - 2),
151
      icudtlPath: fs.path.absolute(argResults[_kOptionIcudtl]),
152
      coverageDirectory: coverageDirectory,
153 154 155 156
    );

    if (collector != null) {
      // collector expects currentDirectory to be the root of the dart
157 158 159 160 161 162 163 164
      // package (i.e. contains lib/ and test/ sub-dirs). In some cases,
      // test files may appear to be in the root directory.
      if (coverageDirectory == null) {
        fs.currentDirectory = testDirectory.parent;
      } else {
        fs.currentDirectory = testDirectory;
      }
      if (!await collector.collectCoverageData(argResults[_kOptionCoveragePath], coverageDirectory: coverageDirectory))
165 166
        throwToolExit('Failed to collect coverage data');
    }
167
  } finally {
168
    tempDir.deleteSync(recursive: true);
169
  }
170 171
  // TODO(ianh): There's apparently some sort of lost async task keeping the
  // process open. Remove the next line once that's been resolved.
172
  exit(exitCode);
173
}