fuchsia_tester.dart 7.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
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/dart/package_map.dart';
18
import 'package:flutter_tools/src/artifacts.dart';
19
import 'package:flutter_tools/src/globals.dart' as globals;
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
import 'package:flutter_tools/src/test/test_wrapper.dart';
25

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

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

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

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

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

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

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

111
    globalPackagesPath =
112
        globals.fs.path.normalize(globals.fs.path.absolute(argResults[_kOptionPackages] as String));
113

114
    Directory testDirectory;
115
    CoverageCollector collector;
116
    if (argResults['coverage'] as bool) {
117
      collector = CoverageCollector(
118 119 120 121 122 123 124 125
        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);
        });
126 127 128
      if (!argResults.options.contains(_kOptionTestDirectory)) {
        throwToolExit('Use of --coverage requires setting --test-directory');
      }
129
      testDirectory = globals.fs.directory(argResults[_kOptionTestDirectory]);
130 131
    }

132 133 134

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

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

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