Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alexander Aprelev | e2e2413 | 2018-09-08 17:53:40 -0700 | [diff] [blame] | 5 | import 'dart:convert' show json; |
Ian Hickson | 98c117b | 2018-08-15 12:22:30 -0700 | [diff] [blame] | 6 | import 'dart:math' as math; |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 7 | |
| 8 | import 'package:args/args.dart'; |
Alexandre Ardhuin | 0613529 | 2018-03-22 07:56:18 +0100 | [diff] [blame] | 9 | import 'package:flutter_tools/src/base/common.dart'; |
Todd Volkert | 6258f32 | 2018-07-24 16:33:49 -0700 | [diff] [blame] | 10 | import 'package:flutter_tools/src/base/context.dart'; |
Alexandre Ardhuin | 0613529 | 2018-03-22 07:56:18 +0100 | [diff] [blame] | 11 | import 'package:flutter_tools/src/base/file_system.dart'; |
| 12 | import 'package:flutter_tools/src/base/io.dart'; |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 13 | |
Ryan Macnak | b7773da | 2019-10-14 15:26:14 -0700 | [diff] [blame] | 14 | import 'package:flutter_tools/src/build_info.dart'; |
Alexandre Ardhuin | 0613529 | 2018-03-22 07:56:18 +0100 | [diff] [blame] | 15 | import 'package:flutter_tools/src/cache.dart'; |
Todd Volkert | 8d11f5c | 2018-03-28 10:58:28 -0700 | [diff] [blame] | 16 | import 'package:flutter_tools/src/context_runner.dart'; |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 17 | import 'package:flutter_tools/src/artifacts.dart'; |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 18 | import 'package:flutter_tools/src/globals.dart' as globals; |
Jonah Williams | 301eaa8 | 2019-04-15 08:59:28 -0700 | [diff] [blame] | 19 | import 'package:flutter_tools/src/project.dart'; |
Zachary Anderson | ef146f6 | 2019-07-29 07:24:02 -0700 | [diff] [blame] | 20 | import 'package:flutter_tools/src/reporting/reporting.dart'; |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 21 | import 'package:flutter_tools/src/test/coverage_collector.dart'; |
| 22 | import 'package:flutter_tools/src/test/runner.dart'; |
Dan Field | b232388 | 2019-12-18 13:58:01 -0800 | [diff] [blame] | 23 | import 'package:flutter_tools/src/test/test_wrapper.dart'; |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 24 | |
Alexandre Ardhuin | 1fce14a | 2017-10-22 18:11:36 +0200 | [diff] [blame] | 25 | const String _kOptionPackages = 'packages'; |
| 26 | const String _kOptionShell = 'shell'; |
| 27 | const String _kOptionTestDirectory = 'test-directory'; |
Todd Volkert | 5d8771d | 2018-08-10 21:18:10 -0700 | [diff] [blame] | 28 | const String _kOptionSdkRoot = 'sdk-root'; |
Todd Volkert | f8a5c86 | 2018-08-14 20:06:07 -0700 | [diff] [blame] | 29 | const String _kOptionIcudtl = 'icudtl'; |
Alexander Aprelev | e2e2413 | 2018-09-08 17:53:40 -0700 | [diff] [blame] | 30 | const String _kOptionTests = 'tests'; |
Jonah Williams | 63f2fb9 | 2018-09-26 13:17:20 -0700 | [diff] [blame] | 31 | const String _kOptionCoverageDirectory = 'coverage-directory'; |
Alexandre Ardhuin | eda03e2 | 2018-08-02 12:02:32 +0200 | [diff] [blame] | 32 | const List<String> _kRequiredOptions = <String>[ |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 33 | _kOptionPackages, |
| 34 | _kOptionShell, |
Todd Volkert | 5d8771d | 2018-08-10 21:18:10 -0700 | [diff] [blame] | 35 | _kOptionSdkRoot, |
Alexander Aprelev | e2e2413 | 2018-09-08 17:53:40 -0700 | [diff] [blame] | 36 | _kOptionIcudtl, |
| 37 | _kOptionTests, |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 38 | ]; |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 39 | const String _kOptionCoverage = 'coverage'; |
| 40 | const String _kOptionCoveragePath = 'coverage-path'; |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 41 | |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 42 | void main(List<String> args) { |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 43 | runInContext<void>(() => run(args), overrides: <Type, Generator>{ |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 44 | Usage: () => DisabledUsage(), |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 45 | }); |
| 46 | } |
| 47 | |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 48 | Future<void> run(List<String> args) async { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 49 | final ArgParser parser = ArgParser() |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 50 | ..addOption(_kOptionPackages, help: 'The .packages file') |
| 51 | ..addOption(_kOptionShell, help: 'The Flutter shell binary') |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 52 | ..addOption(_kOptionTestDirectory, help: 'Directory containing the tests') |
Todd Volkert | 5d8771d | 2018-08-10 21:18:10 -0700 | [diff] [blame] | 53 | ..addOption(_kOptionSdkRoot, help: 'Path to the SDK platform files') |
Todd Volkert | f8a5c86 | 2018-08-14 20:06:07 -0700 | [diff] [blame] | 54 | ..addOption(_kOptionIcudtl, help: 'Path to the ICU data file') |
Alexander Aprelev | e2e2413 | 2018-09-08 17:53:40 -0700 | [diff] [blame] | 55 | ..addOption(_kOptionTests, help: 'Path to json file that maps Dart test files to precompiled dill files') |
Jonah Williams | 63f2fb9 | 2018-09-26 13:17:20 -0700 | [diff] [blame] | 56 | ..addOption(_kOptionCoverageDirectory, help: 'The path to the directory that will have coverage collected') |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 57 | ..addFlag(_kOptionCoverage, |
| 58 | defaultsTo: false, |
| 59 | negatable: false, |
| 60 | help: 'Whether to collect coverage information.', |
| 61 | ) |
| 62 | ..addOption(_kOptionCoveragePath, |
| 63 | defaultsTo: 'coverage/lcov.info', |
| 64 | help: 'Where to store coverage information (if coverage is enabled).', |
| 65 | ); |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 66 | final ArgResults argResults = parser.parse(args); |
| 67 | if (_kRequiredOptions |
| 68 | .any((String option) => !argResults.options.contains(option))) { |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 69 | throwToolExit('Missing option! All options must be specified.'); |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 70 | } |
Ian Hickson | 3dec6a6 | 2018-08-17 13:17:23 -0700 | [diff] [blame] | 71 | final Directory tempDir = |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 72 | globals.fs.systemTempDirectory.createTempSync('flutter_fuchsia_tester.'); |
P.Y. Laligand | d2c6b0a | 2017-04-27 09:10:48 -0700 | [diff] [blame] | 73 | try { |
Ian Hickson | 3dec6a6 | 2018-08-17 13:17:23 -0700 | [diff] [blame] | 74 | Cache.flutterRoot = tempDir.path; |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 75 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 76 | final String shellPath = globals.fs.file(argResults[_kOptionShell]).resolveSymbolicLinksSync(); |
| 77 | if (!globals.fs.isFileSync(shellPath)) { |
P.Y. Laligand | d2c6b0a | 2017-04-27 09:10:48 -0700 | [diff] [blame] | 78 | throwToolExit('Cannot find Flutter shell at $shellPath'); |
| 79 | } |
Todd Volkert | 5d8771d | 2018-08-10 21:18:10 -0700 | [diff] [blame] | 80 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 81 | final Directory sdkRootSrc = globals.fs.directory(argResults[_kOptionSdkRoot]); |
| 82 | if (!globals.fs.isDirectorySync(sdkRootSrc.path)) { |
Todd Volkert | 5d8771d | 2018-08-10 21:18:10 -0700 | [diff] [blame] | 83 | throwToolExit('Cannot find SDK files at ${sdkRootSrc.path}'); |
| 84 | } |
Jonah Williams | 63f2fb9 | 2018-09-26 13:17:20 -0700 | [diff] [blame] | 85 | Directory coverageDirectory; |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 86 | final String coverageDirectoryPath = argResults[_kOptionCoverageDirectory] as String; |
Jonah Williams | 63f2fb9 | 2018-09-26 13:17:20 -0700 | [diff] [blame] | 87 | if (coverageDirectoryPath != null) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 88 | if (!globals.fs.isDirectorySync(coverageDirectoryPath)) { |
Jonah Williams | 63f2fb9 | 2018-09-26 13:17:20 -0700 | [diff] [blame] | 89 | throwToolExit('Cannot find coverage directory at $coverageDirectoryPath'); |
| 90 | } |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 91 | coverageDirectory = globals.fs.directory(coverageDirectoryPath); |
Jonah Williams | 63f2fb9 | 2018-09-26 13:17:20 -0700 | [diff] [blame] | 92 | } |
Todd Volkert | 5d8771d | 2018-08-10 21:18:10 -0700 | [diff] [blame] | 93 | |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 94 | // Put the tester shell where runTests expects it. |
Alexandre Ardhuin | 79b5e5b | 2018-11-19 10:37:55 +0100 | [diff] [blame] | 95 | // TODO(garymm): Switch to a Fuchsia-specific Artifacts impl. |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 96 | final Link testerDestLink = |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 97 | globals.fs.link(globals.artifacts.getArtifactPath(Artifact.flutterTester)); |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 98 | testerDestLink.parent.createSync(recursive: true); |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 99 | testerDestLink.createSync(globals.fs.path.absolute(shellPath)); |
Zachary Anderson | 61b5caf | 2019-04-01 07:48:50 -0700 | [diff] [blame] | 100 | |
Todd Volkert | 5d8771d | 2018-08-10 21:18:10 -0700 | [diff] [blame] | 101 | final Directory sdkRootDest = |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 102 | globals.fs.directory(globals.artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath)); |
Todd Volkert | 5d8771d | 2018-08-10 21:18:10 -0700 | [diff] [blame] | 103 | sdkRootDest.createSync(recursive: true); |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 104 | for (final FileSystemEntity artifact in sdkRootSrc.listSync()) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 105 | globals.fs.link(sdkRootDest.childFile(artifact.basename).path).createSync(artifact.path); |
Todd Volkert | 5d8771d | 2018-08-10 21:18:10 -0700 | [diff] [blame] | 106 | } |
| 107 | // TODO(tvolkert): Remove once flutter_tester no longer looks for this. |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 108 | globals.fs.link(sdkRootDest.childFile('platform.dill').path).createSync('platform_strong.dill'); |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 109 | |
Zachary Anderson | df1462f | 2019-02-26 11:35:28 -0800 | [diff] [blame] | 110 | Directory testDirectory; |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 111 | CoverageCollector collector; |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 112 | if (argResults['coverage'] as bool) { |
Jonah Williams | 28cb589 | 2019-04-15 12:26:43 -0700 | [diff] [blame] | 113 | collector = CoverageCollector( |
Jonah Williams | a40ee8a | 2020-11-11 13:50:41 -0800 | [diff] [blame] | 114 | packagesPath: globals.fs.path.normalize(globals.fs.path.absolute(argResults[_kOptionPackages] as String)), |
Jiahao | 4de9b44 | 2019-07-24 10:58:37 -0700 | [diff] [blame] | 115 | libraryPredicate: (String libraryName) { |
| 116 | // If we have a specified coverage directory then accept all libraries. |
| 117 | if (coverageDirectory != null) { |
| 118 | return true; |
| 119 | } |
| 120 | final String projectName = FlutterProject.current().manifest.appName; |
| 121 | return libraryName.contains(projectName); |
| 122 | }); |
Zachary Anderson | df1462f | 2019-02-26 11:35:28 -0800 | [diff] [blame] | 123 | if (!argResults.options.contains(_kOptionTestDirectory)) { |
| 124 | throwToolExit('Use of --coverage requires setting --test-directory'); |
| 125 | } |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 126 | testDirectory = globals.fs.directory(argResults[_kOptionTestDirectory]); |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Alexander Aprelev | e2e2413 | 2018-09-08 17:53:40 -0700 | [diff] [blame] | 129 | |
| 130 | final Map<String, String> tests = <String, String>{}; |
| 131 | final List<Map<String, dynamic>> jsonList = List<Map<String, dynamic>>.from( |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 132 | (json.decode(globals.fs.file(argResults[_kOptionTests]).readAsStringSync()) as List<dynamic>).cast<Map<String, dynamic>>()); |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 133 | for (final Map<String, dynamic> map in jsonList) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 134 | final String source = globals.fs.file(map['source']).resolveSymbolicLinksSync(); |
| 135 | final String dill = globals.fs.file(map['dill']).resolveSymbolicLinksSync(); |
Joshua Seaton | 1ffd5f0 | 2019-03-04 10:52:58 -0800 | [diff] [blame] | 136 | tests[source] = dill; |
Alexander Aprelev | e2e2413 | 2018-09-08 17:53:40 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Dan Field | 24f8f79 | 2020-02-12 14:52:09 -0800 | [diff] [blame] | 139 | // TODO(dnfield): This should be injected. |
| 140 | exitCode = await const FlutterTestRunner().runTests( |
Dan Field | b232388 | 2019-12-18 13:58:01 -0800 | [diff] [blame] | 141 | const TestWrapper(), |
Alexander Aprelev | e2e2413 | 2018-09-08 17:53:40 -0700 | [diff] [blame] | 142 | tests.keys.toList(), |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 143 | workDir: testDirectory, |
| 144 | watcher: collector, |
Todd Volkert | 5d8771d | 2018-08-10 21:18:10 -0700 | [diff] [blame] | 145 | ipv6: false, |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 146 | enableObservatory: collector != null, |
Jonah Williams | a40ee8a | 2020-11-11 13:50:41 -0800 | [diff] [blame] | 147 | buildInfo: BuildInfo( |
| 148 | BuildMode.debug, |
| 149 | '', |
| 150 | treeShakeIcons: false, |
| 151 | packagesPath: globals.fs.path.normalize(globals.fs.path.absolute(argResults[_kOptionPackages] as String), |
| 152 | )), |
Alexander Aprelev | e2e2413 | 2018-09-08 17:53:40 -0700 | [diff] [blame] | 153 | precompiledDillFiles: tests, |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 154 | concurrency: math.max(1, globals.platform.numberOfProcessors - 2), |
| 155 | icudtlPath: globals.fs.path.absolute(argResults[_kOptionIcudtl] as String), |
Jonah Williams | 301eaa8 | 2019-04-15 08:59:28 -0700 | [diff] [blame] | 156 | coverageDirectory: coverageDirectory, |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 157 | ); |
| 158 | |
| 159 | if (collector != null) { |
| 160 | // collector expects currentDirectory to be the root of the dart |
Jonah Williams | 63f2fb9 | 2018-09-26 13:17:20 -0700 | [diff] [blame] | 161 | // package (i.e. contains lib/ and test/ sub-dirs). In some cases, |
| 162 | // test files may appear to be in the root directory. |
| 163 | if (coverageDirectory == null) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 164 | globals.fs.currentDirectory = testDirectory.parent; |
Jonah Williams | 63f2fb9 | 2018-09-26 13:17:20 -0700 | [diff] [blame] | 165 | } else { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 166 | globals.fs.currentDirectory = testDirectory; |
Jonah Williams | 63f2fb9 | 2018-09-26 13:17:20 -0700 | [diff] [blame] | 167 | } |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 168 | if (!await collector.collectCoverageData(argResults[_kOptionCoveragePath] as String, coverageDirectory: coverageDirectory)) { |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 169 | throwToolExit('Failed to collect coverage data'); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 170 | } |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 171 | } |
P.Y. Laligand | d2c6b0a | 2017-04-27 09:10:48 -0700 | [diff] [blame] | 172 | } finally { |
Ian Hickson | 3dec6a6 | 2018-08-17 13:17:23 -0700 | [diff] [blame] | 173 | tempDir.deleteSync(recursive: true); |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 174 | } |
Ian Hickson | 3dec6a6 | 2018-08-17 13:17:23 -0700 | [diff] [blame] | 175 | // TODO(ianh): There's apparently some sort of lost async task keeping the |
| 176 | // process open. Remove the next line once that's been resolved. |
Gary Miguel | 35f3740 | 2018-07-13 10:37:44 -0700 | [diff] [blame] | 177 | exit(exitCode); |
P.Y. Laligand | 5b02aaa | 2017-04-20 14:18:24 -0700 | [diff] [blame] | 178 | } |