fuchsia_tester.dart 7.68 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/artifacts.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/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/device.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
    ..addFlag(_kOptionCoverage,
      negatable: false,
      help: 'Whether to collect coverage information.',
    )
    ..addOption(_kOptionCoveragePath,
        defaultsTo: 'coverage/lcov.info',
        help: 'Where to store coverage information (if coverage is enabled).',
    );
65 66 67
  final ArgResults argResults = parser.parse(args);
  if (_kRequiredOptions
      .any((String option) => !argResults.options.contains(option))) {
68
    throwToolExit('Missing option! All options must be specified.');
69
  }
70
  final Directory tempDir =
71
      globals.fs.systemTempDirectory.createTempSync('flutter_fuchsia_tester.');
72
  try {
73
    Cache.flutterRoot = tempDir.path;
74

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

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

93
    // Put the tester shell where runTests expects it.
94
    // TODO(garymm): Switch to a Fuchsia-specific Artifacts impl.
95
    final Artifacts artifacts = globals.artifacts!;
96
    final Link testerDestLink =
97
        globals.fs.link(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(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 111
    Directory? testDirectory;
    CoverageCollector? collector;
112
    if (argResults['coverage'] as bool) {
113 114
      // If we have a specified coverage directory then accept all libraries by
      // setting libraryNames to null.
115
      final Set<String>? libraryNames = coverageDirectory != null ? null :
116
          <String>{FlutterProject.current().manifest.appName};
117
      final String packagesPath = globals.fs.path.normalize(globals.fs.path.absolute(argResults[_kOptionPackages] as String));
118
      collector = CoverageCollector(
119 120 121
        packagesPath: packagesPath,
        libraryNames: libraryNames,
        resolver: await CoverageCollector.getResolver(packagesPath));
122 123 124
      if (!argResults.options.contains(_kOptionTestDirectory)) {
        throwToolExit('Use of --coverage requires setting --test-directory');
      }
125
      testDirectory = globals.fs.directory(argResults[_kOptionTestDirectory]);
126 127
    }

128 129 130

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

138 139
    // TODO(dnfield): This should be injected.
    exitCode = await const FlutterTestRunner().runTests(
140
      const TestWrapper(),
141
      tests.keys.toList(),
142 143 144 145 146 147 148 149
      debuggingOptions: DebuggingOptions.enabled(
        BuildInfo(
          BuildMode.debug,
          '',
          treeShakeIcons: false,
          packagesPath: globals.fs.path.normalize(globals.fs.path.absolute(argResults[_kOptionPackages] as String)),
        ),
      ),
150 151
      watcher: collector,
      enableObservatory: collector != null,
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
}