fuchsia_tester.dart 7.52 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 'dart:convert' show json;
6
import 'dart:math' as math;
7 8

import 'package:args/args.dart';
9
import 'package:flutter_tools/src/base/common.dart';
10
import 'package:flutter_tools/src/base/context.dart';
11 12
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
13

14
import 'package:flutter_tools/src/build_info.dart';
15
import 'package:flutter_tools/src/cache.dart';
16
import 'package:flutter_tools/src/context_runner.dart';
17
import 'package:flutter_tools/src/artifacts.dart';
18
import 'package:flutter_tools/src/globals.dart' as globals;
19
import 'package:flutter_tools/src/project.dart';
20
import 'package:flutter_tools/src/reporting/reporting.dart';
21 22
import 'package:flutter_tools/src/test/coverage_collector.dart';
import 'package:flutter_tools/src/test/runner.dart';
23
import 'package:flutter_tools/src/test/test_wrapper.dart';
24

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

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

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

76 77
    final String shellPath = globals.fs.file(argResults[_kOptionShell]).resolveSymbolicLinksSync();
    if (!globals.fs.isFileSync(shellPath)) {
78 79
      throwToolExit('Cannot find Flutter shell at $shellPath');
    }
80

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

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

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

110
    Directory testDirectory;
111
    CoverageCollector collector;
112
    if (argResults['coverage'] as bool) {
113
      collector = CoverageCollector(
114
        packagesPath: globals.fs.path.normalize(globals.fs.path.absolute(argResults[_kOptionPackages] as String)),
115 116 117 118 119 120 121 122
        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);
        });
123 124 125
      if (!argResults.options.contains(_kOptionTestDirectory)) {
        throwToolExit('Use of --coverage requires setting --test-directory');
      }
126
      testDirectory = globals.fs.directory(argResults[_kOptionTestDirectory]);
127 128
    }

129 130 131

    final Map<String, String> tests = <String, String>{};
    final List<Map<String, dynamic>> jsonList = List<Map<String, dynamic>>.from(
132
      (json.decode(globals.fs.file(argResults[_kOptionTests]).readAsStringSync()) as List<dynamic>).cast<Map<String, dynamic>>());
133
    for (final Map<String, dynamic> map in jsonList) {
134 135
      final String source = globals.fs.file(map['source']).resolveSymbolicLinksSync();
      final String dill = globals.fs.file(map['dill']).resolveSymbolicLinksSync();
136
      tests[source] = dill;
137 138
    }

139 140
    // TODO(dnfield): This should be injected.
    exitCode = await const FlutterTestRunner().runTests(
141
      const TestWrapper(),
142
      tests.keys.toList(),
143
      watcher: collector,
144
      ipv6: false,
145
      enableObservatory: collector != null,
146 147 148 149 150 151
      buildInfo: BuildInfo(
        BuildMode.debug,
        '',
        treeShakeIcons: false,
        packagesPath: globals.fs.path.normalize(globals.fs.path.absolute(argResults[_kOptionPackages] as String),
      )),
152
      precompiledDillFiles: tests,
153 154
      concurrency: math.max(1, globals.platform.numberOfProcessors - 2),
      icudtlPath: globals.fs.path.absolute(argResults[_kOptionIcudtl] as String),
155
      coverageDirectory: coverageDirectory,
156 157 158 159
    );

    if (collector != null) {
      // collector expects currentDirectory to be the root of the dart
160 161 162
      // 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) {
163
        globals.fs.currentDirectory = testDirectory.parent;
164
      } else {
165
        globals.fs.currentDirectory = testDirectory;
166
      }
167
      if (!await collector.collectCoverageData(argResults[_kOptionCoveragePath] as String, coverageDirectory: coverageDirectory)) {
168
        throwToolExit('Failed to collect coverage data');
169
      }
170
    }
171
  } finally {
172
    tempDir.deleteSync(recursive: true);
173
  }
174 175
  // TODO(ianh): There's apparently some sort of lost async task keeping the
  // process open. Remove the next line once that's been resolved.
176
  exit(exitCode);
177
}