• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:async';
6import 'dart:math' as math;
7
8import '../asset.dart';
9import '../base/common.dart';
10import '../base/file_system.dart';
11import '../base/platform.dart';
12import '../bundle.dart';
13import '../cache.dart';
14import '../codegen.dart';
15import '../dart/pub.dart';
16import '../devfs.dart';
17import '../globals.dart';
18import '../project.dart';
19import '../runner/flutter_command.dart';
20import '../test/coverage_collector.dart';
21import '../test/event_printer.dart';
22import '../test/runner.dart';
23import '../test/watcher.dart';
24
25class TestCommand extends FastFlutterCommand {
26  TestCommand({ bool verboseHelp = false }) {
27    requiresPubspecYaml();
28    usesPubOption();
29    argParser
30      ..addMultiOption('name',
31        help: 'A regular expression matching substrings of the names of tests to run.',
32        valueHelp: 'regexp',
33        splitCommas: false,
34      )
35      ..addMultiOption('plain-name',
36        help: 'A plain-text substring of the names of tests to run.',
37        valueHelp: 'substring',
38        splitCommas: false,
39      )
40      ..addFlag('start-paused',
41        defaultsTo: false,
42        negatable: false,
43        help: 'Start in a paused mode and wait for a debugger to connect.\n'
44              'You must specify a single test file to run, explicitly.\n'
45              'Instructions for connecting with a debugger are printed to the '
46              'console once the test has started.',
47      )
48      ..addFlag('disable-service-auth-codes',
49        hide: !verboseHelp,
50        defaultsTo: false,
51        negatable: false,
52        help: 'No longer require an authentication code to connect to the VM '
53              'service (not recommended).'
54      )
55      ..addFlag('coverage',
56        defaultsTo: false,
57        negatable: false,
58        help: 'Whether to collect coverage information.',
59      )
60      ..addFlag('merge-coverage',
61        defaultsTo: false,
62        negatable: false,
63        help: 'Whether to merge coverage data with "coverage/lcov.base.info".\n'
64              'Implies collecting coverage data. (Requires lcov)',
65      )
66      ..addFlag('ipv6',
67        negatable: false,
68        hide: true,
69        help: 'Whether to use IPv6 for the test harness server socket.',
70      )
71      ..addOption('coverage-path',
72        defaultsTo: 'coverage/lcov.info',
73        help: 'Where to store coverage information (if coverage is enabled).',
74      )
75      ..addFlag('machine',
76        hide: !verboseHelp,
77        negatable: false,
78        help: 'Handle machine structured JSON command input\n'
79              'and provide output and progress in machine friendly format.',
80      )
81      ..addFlag('update-goldens',
82        negatable: false,
83        help: 'Whether matchesGoldenFile() calls within your test methods should '
84              'update the golden files rather than test for an existing match.',
85      )
86      ..addOption('concurrency',
87        abbr: 'j',
88        defaultsTo: math.max<int>(1, platform.numberOfProcessors - 2).toString(),
89        help: 'The number of concurrent test processes to run.',
90        valueHelp: 'jobs'
91      )
92      ..addFlag('test-assets',
93        defaultsTo: true,
94        negatable: true,
95        help: 'Whether to build the assets bundle for testing.\n'
96              'Consider using --no-test-assets if assets are not required.',
97      )
98      ..addOption('platform',
99        allowed: const <String>['tester', 'chrome'],
100        defaultsTo: 'tester',
101        help: 'The platform to run the unit tests on. Defaults to "tester".'
102      );
103    usesTrackWidgetCreation(verboseHelp: verboseHelp);
104  }
105
106  @override
107  Future<Set<DevelopmentArtifact>> get requiredArtifacts async {
108    final Set<DevelopmentArtifact> results = <DevelopmentArtifact>{
109      DevelopmentArtifact.universal,
110    };
111    if (argResults['platform'] == 'chrome') {
112      results.add(DevelopmentArtifact.web);
113    }
114    return results;
115  }
116
117  @override
118  String get name => 'test';
119
120  @override
121  String get description => 'Run Flutter unit tests for the current project.';
122
123  @override
124  Future<FlutterCommandResult> runCommand() async {
125    await cache.updateAll(await requiredArtifacts);
126    if (!fs.isFileSync('pubspec.yaml')) {
127      throwToolExit(
128        'Error: No pubspec.yaml file found in the current working directory.\n'
129        'Run this command from the root of your project. Test files must be '
130        'called *_test.dart and must reside in the package\'s \'test\' '
131        'directory (or one of its subdirectories).');
132    }
133    if (shouldRunPub) {
134      await pubGet(context: PubContext.getVerifyContext(name), skipPubspecYamlCheck: true);
135    }
136    final bool buildTestAssets = argResults['test-assets'];
137    final List<String> names = argResults['name'];
138    final List<String> plainNames = argResults['plain-name'];
139    final FlutterProject flutterProject = FlutterProject.current();
140
141    if (buildTestAssets && flutterProject.manifest.assets.isNotEmpty) {
142      await _buildTestAsset();
143    }
144
145    Iterable<String> files = argResults.rest.map<String>((String testPath) => fs.path.absolute(testPath)).toList();
146
147    final bool startPaused = argResults['start-paused'];
148    if (startPaused && files.length != 1) {
149      throwToolExit(
150        'When using --start-paused, you must specify a single test file to run.',
151        exitCode: 1,
152      );
153    }
154
155    final int jobs = int.tryParse(argResults['concurrency']);
156    if (jobs == null || jobs <= 0 || !jobs.isFinite) {
157      throwToolExit(
158        'Could not parse -j/--concurrency argument. It must be an integer greater than zero.'
159      );
160    }
161
162    Directory workDir;
163    if (files.isEmpty) {
164      // We don't scan the entire package, only the test/ subdirectory, so that
165      // files with names like like "hit_test.dart" don't get run.
166      workDir = fs.directory('test');
167      if (!workDir.existsSync())
168        throwToolExit('Test directory "${workDir.path}" not found.');
169      files = _findTests(workDir).toList();
170      if (files.isEmpty) {
171        throwToolExit(
172            'Test directory "${workDir.path}" does not appear to contain any test files.\n'
173            'Test files must be in that directory and end with the pattern "_test.dart".'
174        );
175      }
176    } else {
177      files = <String>[
178        for (String path in files)
179          if (fs.isDirectorySync(path))
180            ..._findTests(fs.directory(path))
181          else
182            path
183      ];
184    }
185
186    CoverageCollector collector;
187    if (argResults['coverage'] || argResults['merge-coverage']) {
188      final String projectName = FlutterProject.current().manifest.appName;
189      collector = CoverageCollector(
190        libraryPredicate: (String libraryName) => libraryName.contains(projectName),
191      );
192    }
193
194    final bool machine = argResults['machine'];
195    if (collector != null && machine) {
196      throwToolExit("The test command doesn't support --machine and coverage together");
197    }
198
199    TestWatcher watcher;
200    if (collector != null) {
201      watcher = collector;
202    } else if (machine) {
203      watcher = EventPrinter();
204    }
205
206    Cache.releaseLockEarly();
207
208    // Run builders once before all tests.
209    if (flutterProject.hasBuilders) {
210      final CodegenDaemon codegenDaemon = await codeGenerator.daemon(flutterProject);
211      codegenDaemon.startBuild();
212      await for (CodegenStatus status in codegenDaemon.buildResults) {
213        if (status == CodegenStatus.Succeeded) {
214          break;
215        }
216        if (status == CodegenStatus.Failed) {
217          throwToolExit('Code generation failed.');
218        }
219      }
220    }
221
222    final bool disableServiceAuthCodes =
223      argResults['disable-service-auth-codes'];
224
225    final int result = await runTests(
226      files,
227      workDir: workDir,
228      names: names,
229      plainNames: plainNames,
230      watcher: watcher,
231      enableObservatory: collector != null || startPaused,
232      startPaused: startPaused,
233      disableServiceAuthCodes: disableServiceAuthCodes,
234      ipv6: argResults['ipv6'],
235      machine: machine,
236      trackWidgetCreation: argResults['track-widget-creation'],
237      updateGoldens: argResults['update-goldens'],
238      concurrency: jobs,
239      buildTestAssets: buildTestAssets,
240      flutterProject: flutterProject,
241      web: argResults['platform'] == 'chrome',
242    );
243
244    if (collector != null) {
245      if (!await collector.collectCoverageData(
246          argResults['coverage-path'], mergeCoverageData: argResults['merge-coverage']))
247        throwToolExit(null);
248    }
249
250    if (result != 0)
251      throwToolExit(null);
252    return const FlutterCommandResult(ExitStatus.success);
253  }
254
255  Future<void> _buildTestAsset() async {
256    final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();
257    final int build = await assetBundle.build();
258    if (build != 0) {
259      throwToolExit('Error: Failed to build asset bundle');
260    }
261    if (_needRebuild(assetBundle.entries)) {
262      await writeBundle(fs.directory(fs.path.join('build', 'unit_test_assets')),
263          assetBundle.entries);
264    }
265  }
266
267  bool _needRebuild(Map<String, DevFSContent> entries) {
268    final File manifest = fs.file(fs.path.join('build', 'unit_test_assets', 'AssetManifest.json'));
269    if (!manifest.existsSync()) {
270      return true;
271    }
272    final DateTime lastModified = manifest.lastModifiedSync();
273    final File pub = fs.file('pubspec.yaml');
274    if (pub.lastModifiedSync().isAfter(lastModified)) {
275      return true;
276    }
277
278    for (DevFSFileContent entry in entries.values.whereType<DevFSFileContent>()) {
279      // Calling isModified to access file stats first in order for isModifiedAfter
280      // to work.
281      if (entry.isModified && entry.isModifiedAfter(lastModified)) {
282        return true;
283      }
284    }
285    return false;
286  }
287}
288
289Iterable<String> _findTests(Directory directory) {
290  return directory.listSync(recursive: true, followLinks: false)
291      .where((FileSystemEntity entity) => entity.path.endsWith('_test.dart') &&
292      fs.isFileSync(entity.path))
293      .map((FileSystemEntity entity) => fs.path.absolute(entity.path));
294}
295